Django - 对转换函数建模

时间:2017-01-22 00:22:11

标签: python django automaton

我正在使用Django并尝试为有限自动机创建一个模型,这是我到目前为止所提出的:

class Alphabet(models.Model):
    alphabet = models.CharField(max_length = 10, null = True, blank = True)
    automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

class States(models.Model):
    state = models.CharField(max_length = 10, null = True, blank = True)
    final = models.BooleanField(default = False)
    initial = models.BooleanField(default = False)
    automata = models.ForeignKey(Automata, on_delete = models.CASCADE)

class Automata(models.Model):
    pass

现在我想对转换函数进行建模,每个自动机都有一个:

示例:我们可以使用symbol1

从state1转到state2

我不知道该怎么做,任何帮助都会受到赞赏!

1 个答案:

答案 0 :(得分:1)

以下可以为有限自动机的转换函数的单个转换建模。然而值得注意的是,确定性有限自动机和非确定性有限自动机转换看起来不同。 DFA过渡映射到单个状态,而NFA过渡映射到一组状态。在添加关系时,您必须牢记这一点。

In [201]: df
Out[201]:
                  date           crime
0  2015-05-13 08:55:00   VEHICLE THEFT
1  2015-05-13 08:41:00  OTHER OFFENSES
2  2015-05-13 08:36:00  OTHER OFFENSES
3  2015-05-13 08:30:00    NON-CRIMINAL
4  2015-05-13 08:17:00  OTHER OFFENSES
5  2015-05-13 08:16:00  OTHER OFFENSES
6  2015-05-13 08:10:00   LARCENY/THEFT
7  2015-05-13 08:00:00        BURGLARY
8  2015-05-13 08:00:00  MISSING PERSON
9  2015-05-13 08:00:00  OTHER OFFENSES
10 2015-05-13 08:00:00         ASSAULT

In [202]: df.groupby([pd.Grouper(key='date', freq='D'), 'crime']).size()
Out[202]:
date        crime
2015-05-13  ASSAULT           1
            BURGLARY          1
            LARCENY/THEFT     1
            MISSING PERSON    1
            NON-CRIMINAL      1
            OTHER OFFENSES    5
            VEHICLE THEFT     1
dtype: int64