Example #1
0
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
Example #2
0
 def next_nodes(self, states: Mapping, automate: Automate):
     res = defaultdict(lambda: 0)
     for v1 in states:
         for _, a, v2, n in automate.transitions(v1):
             res[v2] += states[v1] * n
             if self.use_mod:
                 res[v2] %= MOD
     return dict(res)
Example #3
0
def build_data(states: int, edges: int):
    S = list(map(str, range(states)))
    A = list(map(str, range(5)))
    Q = Automate.builder()

    for i in range(edges):
        s1 = random.choice(S)
        a = random.choice(A)
        s2 = random.choice(S)
        Q[s1][a][s2] += 1

    return {
        "A": list(sorted(A)),
        "S": list(sorted(S)),
        "Q": Automate.builder_clean(Q),
        "I": random.choice(S),
        "T": list(sorted(random.sample(S, 10)))
    }
Example #4
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
Example #5
0
    def from_automat(g: Automate):
        g0 = {k: [_[1] for _ in v] for k, v in g.graph.items()}

        mat = EmptyMatrix()
        for i in g0:
            for j in g0[i]:
                mat.graph[i][j] += 1

        aa = mat.to_graph()
        xx = aa.graph

        assert xx.keys() == g0.keys()
        for i in xx.keys():
            if sorted(xx[i]) != sorted(g0[i]):
                print(i, xx[i])
                print(i, g0[i])

        assert g.size() == mat.size(), '{} {}'.format(g.size(), mat.size())
        return mat
Example #6
0
def split(aut: Automate, Z: Set[str], a: str, X: Set[str]):
    A = set()
    B = set()
    for s1 in X:
        img = aut.find(s1, a)
        if len(img) == 0:
            B.add(s1)
        else:
            for s2 in img:
                if s2 in Z:
                    A.add(s1)
                else:
                    B.add(s1)
    assert len(A) + len(B) == len(X)
    if len(A) == 0 or len(B) == 0:
        return [X]
    return [freeze(A), freeze(B)]
Example #7
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))
Example #8
0
def test_automat():
    data = build_data(20, 30)
    a = Automate.from_json(data)
    data2 = a.to_json()
    assert data == data2