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()
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)
def learn_policy(self, identifier): ''' Learn a policy file. Args: identifier: a string, either a URL to a policy file or the text of the policy itself. Keep in mind a policy can be comprised of more than one policy file (a file containing valid policy DSL) or string containing policy DSL. This way you break your rule set, imports, and policy attributes across any number of files. See reason-method for more. Returns: The resulting File Node. Raises: ValueError: if the policy already exists in knowledge. TypeError: if parameter 'identifier' is a NoneType, or is not a string representing either a file path to a policy or the text of the policy itself. ''' is_file = False if identifier: if isinstance(identifier, basestring): if urlparse(identifier).scheme: # Treat 'identifier' as an URL self.log("Learning policy from URL: {0}".format(identifier)) stream = ANTLRStringStream(Intellect.policy_from(identifier)) is_file = True else: #Treat 'identifier' as policy string self.log("Learning policy from string") stream = ANTLRStringStream(identifier) lexer = PolicyLexer(stream) tokens = CommonTokenStream(lexer) tokens.discardOffChannelTokens = True indented_source = PolicyTokenSource(tokens) tokens = CommonTokenStream(indented_source) parser = PolicyParser(tokens) with RedirectStdError() as stderr: try: # ANTL3 may raise an exception, and doing so the stderror # will not be printed hiding the underlying problem. GRRR!!!! file_node = parser.file() except Exception as e: if stderr.getvalue().rstrip() != "": trace = sys.exc_info()[2] raise Exception(stderr.getvalue().rstrip()), None, trace else: raise e # The ANTLR3 Recognizer class prints a number of ANTLR3 Exceptions to # stderr vice throwing an exception, because it will try to recover and # continue parsing. # # In the case of NoViableAltException, I've chosen to raise an # exception. # # Otherwise, all the other error message that Recognizer writes to # stderr will be returned for the benefit of the policy author. if stderr.getvalue().rstrip() != "": # check for stderror msg indicating an NoViableAltException occured. # if did raise an exception with the stderror message. if "no viable alternative at input" in stderr.getvalue().rstrip(): raise Exception("Error parsing policy: {0}\n{1}".format(identifier, stderr.getvalue().rstrip())) else: print >> sys.stderr, stderr.getvalue().rstrip() # set path attribute file_node.path = identifier if is_file else None # associate the path to all descendants file_node.set_file_on_descendants(file_node, file_node) try: # determine if the policy already exists in knowledge self.policy.files.index(file_node) raise ValueError("Policy already exists in knowledge: {0}".format(identifier)) except: pass # store add the policy file to the policy self.policy.append_child(file_node) self.log("learned a policy file") return file_node else: raise TypeError("parameter 'identifier' must be a string, either a file path to a policy or the text of the policy itself") else: raise TypeError("parameter 'identifier' cannot be a NoneType.")