Esempio n. 1
0
def norm(formula):
    '''Computes the bounds of the given TWTL formula and returns a 2-tuple
    containing the lower and upper bounds, respectively.
    '''
    lexer = twtlLexer(ANTLRStringStream(formula))
    tokens = CommonTokenStream(lexer)
    parser = twtlParser(tokens)
    phi = parser.formula()

    # CommonTree
    t = phi.tree

    # compute TWTL bound
    nodes = CommonTreeNodeStream(t)
    nodes.setTokenStream(tokens)
    boundEvaluator = bound(nodes)
    boundEvaluator.eval()
    return boundEvaluator.getBound()
Esempio n. 2
0
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)