def test_repeat(verbose=False, show=False): setDFAType(DFAType.Normal) props = ['A', 'B', 'C'] dfa = hold(props, prop='A', duration=2, negation=False) dfa = repeat(dfa, low=2, high=4) if verbose: print dfa for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d if show: dfa.visualize() plt.show() # Test with low zero dfa = hold(props, prop='A', duration=2, negation=False) dfa = repeat(dfa, low=0, high=4) if verbose: print dfa for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d if show: dfa.visualize() plt.show() # Test with trap state dfa = hold(props, prop='A', duration=2, negation=False) dfa.add_trap_state() dfa = repeat(dfa, low=0, high=4) if verbose: print dfa for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d if show: dfa.visualize() plt.show() # Test with truncated dfa dfa1 = hold(props, prop='A', duration=2, negation=False) dfa2 = hold(props, prop='B', duration=3, negation=False) dfa = union(dfa1, dfa2) if verbose: print '[test_within] Preprocessing:' print dfa1 print dfa2 print 'Union' print dfa for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d dfa = repeat(dfa, low=0, high=4) if verbose: print dfa for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d if show: dfa.visualize() plt.show()
def test_eventually(verbose=False, show=False): setDFAType(DFAType.Infinity) props = ['A', 'B', 'C'] dfa = hold(props, prop='A', duration=2, negation=False) dfa = eventually(dfa, low=2, high=5) if verbose: print dfa print dfa.tree print dfa.counters print for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d if show: dfa.visualize(draw='matplotlib') plt.show() # Test with low zero dfa = hold(props, prop='A', duration=2, negation=False) dfa = eventually(dfa, low=0, high=5) if verbose: print dfa print dfa.tree print dfa.counters print for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d if show: dfa.visualize(draw='matplotlib') plt.show() # Test with trap state dfa = hold(props, prop='A', duration=2, negation=False) dfa.add_trap_state() dfa = eventually(dfa, low=0, high=5) if verbose: print dfa print dfa.tree print dfa.counters print for u, v, d in dfa.g.edges_iter(data=True): print (u, v), d if show: dfa.visualize(draw='matplotlib') plt.show()
def translate(formula, kind='both', norm=False, optimize=True): '''Converts a TWTL formula into an FSA. It can returns both a normal FSA or the automaton corresponding to the relaxed infinity version of the specification. If kind is: (a) DFAType.Normal it returns only the normal version; (b) DFAType.Infinity it returns only the relaxed version; and (c) 'both' it returns both automata versions. If norm is True then the bounds of the TWTL formula are computed as well. The functions returns a tuple containing in order: (a) the alphabet; (b) the normal automaton (if requested); (c) the infinity version automaton (if requested); and (d) the bounds of the TWTL formula (if requested). The ``optimize'' flag is used to specify that the annotation data should be optimized. Note that the synthesis algorithm assumes an optimized automaton, while computing temporal relaxations is performed using an unoptimized automaton. ''' if kind == 'both': kind = [DFAType.Normal, DFAType.Infinity] elif kind in [DFAType.Normal, DFAType.Infinity]: kind = [kind] else: raise ValueError('DFA type must be either DFAType.Normal, ' + 'DFAType.Infinity or "both"! {} was given!'.format(kind)) lexer = twtlLexer(ANTLRStringStream(formula)) lexer.setAlphabet(set()) tokens = CommonTokenStream(lexer) parser = twtlParser(tokens) phi = parser.formula() # CommonTree t = phi.tree alphabet = lexer.getAlphabet() result= [alphabet] if DFAType.Normal in kind: setDFAType(DFAType.Normal) nodes = CommonTreeNodeStream(t) nodes.setTokenStream(tokens) translator = twtl2dfa(nodes) translator.props = alphabet translator.eval() dfa = translator.getDFA() dfa.kind = DFAType.Normal result.append(dfa) if DFAType.Infinity in kind: setDFAType(DFAType.Infinity) setOptimizationFlag(optimize) nodes = CommonTreeNodeStream(t) nodes.setTokenStream(tokens) translator = twtl2dfa(nodes) translator.props = alphabet translator.eval() dfa_inf = translator.getDFA() dfa_inf.kind = DFAType.Infinity result.append(dfa_inf) if norm: # compute TWTL bound nodes = CommonTreeNodeStream(t) nodes.setTokenStream(tokens) boundEvaluator = bound(nodes) boundEvaluator.eval() result.append(boundEvaluator.getBound()) if logging.getLogger().isEnabledFor(logging.DEBUG): for mode, name in [(DFAType.Normal, 'Normal'), (DFAType.Infinity, 'Infinity')]: if mode not in kind: continue elif mode == DFAType.Normal: pdfa = dfa else: pdfa = dfa_inf logging.debug('[spec] spec: {}'.format(formula)) logging.debug('[spec] mode: {} DFA: {}'.format(name, pdfa)) if mode == DFAType.Infinity: logging.debug('[spec] tree:\n{}'.format(pdfa.tree.pprint())) logging.debug('[spec] No of nodes: {}'.format(pdfa.g.number_of_nodes())) logging.debug('[spec] No of edges: {}'.format(pdfa.g.number_of_edges())) return tuple(result)