def update_classes(automate: Automate, classes: List[List[int]]):
    # init mapping function :
    mapping = dict()
    cc = set()
    for c in classes:
        c = list(sorted(c, key=lambda x: (len(x), x)))
        x = c[0]
        cc.add(x)
        for _ in c[1:]:
            mapping[_] = x

    def f(s):
        return mapping.get(s, s)

    # check all
    for s1, a, s2, n in automate.edges():
        items = automate.find(f(s1), a)

        if f(s2) not in set(map(f, items)):
            print(
                '-----------------------------------------------------------')
            print('error on:', f(s1), a, f(s2), n)
            print('found   :', items)
            raise ValueError

    # update edges (s1,a,s2,n) --> (s1,a,f(s2),n)
    Q = Automate.from_scratch()
    for s1, a, s2, n in automate.edges():
        if not Q.find(f(s1), a):
            Q.add(f(s1), a, f(s2), n)
    Q.I = f(automate.I)
    Q.T = set(map(f, automate.T))
    Q.validate()
    return Q
Exemple #2
0
    def build_automate(self, filename='automat.json'):
        if Path(filename).exists():
            return Automate.from_path(filename)

        g = Automate.from_scratch()

        to_visit = {self.item(0)}
        while len(to_visit) > 0:
            s1 = to_visit.pop()
            for a, s2 in s1.adjacent:
                if str(s2) not in g.S:
                    to_visit.add(s2)
                g.add(str(s1), str(a), str(s2))

        g.I = '0'
        g.compute_terminals(lambda x: self.item(x).full_check())
        g.save(filename)
        return g
Exemple #3
0
def test_from_scratch():
    states = 1000
    edges = 12
    S = list(map(str, range(states)))
    A = list(map(str, range(5)))

    Q = Automate.from_scratch()
    Q.T = set(sorted(random.sample(S, 5)))
    Q.I = S[0]

    for s in Q.T:
        Q.add(Q.I, A[0], s, 1)
    for i in range(edges):
        s1 = random.choice(S)
        a = random.choice(A)
        s2 = random.choice(S)
        Q.add(s1, a, s2, 1)

    Q.validate()
    print(json.dumps(Q.to_json(), indent=4, sort_keys=True))