def test_pltlf_dfa(): parser = PLTLfParser() f = parser("a") dfa = f.to_dfa(mona_dfa_out=False) expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 2; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 1 [label="~a"]; 1 -> 2 [label="a"]; 2 -> 1 [label="~a"]; 2 -> 2 [label="a"]; }""" assert dfa == expected f = parser("true") dfa = f.to_dfa(mona_dfa_out=False) expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 1; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 1 [label="true"]; }""" assert dfa == expected f = parser("O(a) -> O(b)") dfa = f.to_dfa(mona_dfa_out=False) expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 1; 2; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 1 [label="~a & ~b"]; 1 -> 2 [label="b"]; 1 -> 3 [label="a & ~b"]; 2 -> 2 [label="true"]; 3 -> 3 [label="~b"]; 3 -> 2 [label="b"]; }""" assert dfa == expected
def test_pltlf_example_readme(): from ltlf2dfa.parser.pltlf import PLTLfParser parser = PLTLfParser() formula = "H(a -> Y b)" parsed_formula = parser(formula) assert str(parsed_formula) == "H((a -> Y(b)))" assert parsed_formula.find_labels() == [c for c in "ab"]
def test_mona(): parser = PLTLfParser() a, b, c = [PLTLfAtomic(c) for c in "abc"] tt = PLTLfTrue() ff = PLTLfFalse() assert a.to_mona(v="max($)") == "(max($) in A)" assert b.to_mona(v="max($)") == "(max($) in B)" assert c.to_mona(v="max($)") == "(max($) in C)" assert tt.to_mona(v="max($)") == "true" assert ff.to_mona(v="max($)") == "false" f = parser("!(a & !b)") assert f.to_mona(v="max($)") == "~(((max($) in A) & ~((max($) in B))))" f = parser("!(!a | b)") assert f.to_mona(v="max($)") == "~((~((max($) in A)) | (max($) in B)))" f = parser("!(a <-> b)") assert ( f.to_nnf().to_mona(v="max($)") == "((~((max($) in A)) | ~((max($) in B))) & ((max($) in A) | (max($) in B)))" ) # Before f = parser("Y(a & b)") assert ( f.to_mona(v="max($)") == "(ex1 v_1: v_1 in $ & v_1=max($)-1 & max($)>0 & ((v_1 in A) & (v_1 in B)))" ) # Since f = parser("a S b") assert ( f.to_mona(v="max($)") == "(ex1 v_1: v_1 in $ & 0<=v_1&v_1<=max($) & (v_1 in B) & (all1 v_2: v_2 in $ & v_1<v_2&v_2<=max($)" " => (v_2 in A)))") # Once and Historically f = parser("O(a & b)") assert ( f.to_mona(v="max($)") == "(ex1 v_1: v_1 in $ & 0<=v_1&v_1<=max($) & ((v_1 in A) & (v_1 in B)) & (all1 v_2: " "v_2 in $ & v_1<v_2&v_2<=max($) => true))") f = parser("a & O(b)") assert ( f.to_mona(v="max($)") == "((max($) in A) & (ex1 v_1: v_1 in $ & 0<=v_1&v_1<=max($) & (v_1 in B) & (all1 v_2: v_2 in $ & v_1<v_2&v_2<=max($) => " "true)))") f = parser("H(a | b)") assert ( f.to_mona(v="max($)") == "~((ex1 v_1: v_1 in $ & 0<=v_1&v_1<=max($) & ~(((v_1 in A) | (v_1 in B))) & (all1 v_2: " "v_2 in $ & v_1<v_2&v_2<=max($) => true)))")
def dfa(): formula_string = request.form["inputFormula"] control_set = request.form["controllables"] uncontrol_set = request.form["uncontrollables"] assert formula_string automa_name = "dfa_" + str(datetime.datetime.now()).replace( " ", "_") + "_" + str(uuid.uuid4()) isLtlf = True if all(c in FUTURE_OPS for c in formula_string if c.isupper()): f_parser = LTLfParser() try: formula = f_parser(formula_string) except Exception as e: if request.form.get("exampleCheck1"): return render_template("dfa.html", error=str(e).encode("utf-8")) return render_template("index.html", error=str(e).encode("utf-8")) else: assert all(c in PAST_OPS for c in formula_string if c.isupper()) isLtlf = False p_parser = PLTLfParser() try: formula = p_parser(formula_string) except Exception as e: if request.form.get("exampleCheck1"): return render_template("dfa.html", error=str(e).encode("utf-8")) return render_template("index.html", error=str(e).encode("utf-8")) dfa = ltl2dfagame(formula_string, control_set, uncontrol_set, isLtlf) write_dot_file(str(dfa), automa_name) subprocess.call('dot -Tsvg {} -o {}'.format( "{}/static/dot/{}.dot".format(PACKAGE_DIR, automa_name), "{}/static/tmp/{}.svg".format(PACKAGE_DIR, automa_name)), shell=True) encoding = encode_svg("{}/static/tmp/{}.svg".format( PACKAGE_DIR, automa_name)).decode("utf-8") piero_encoding = encode_svg( "{}/static/tmp/piero.svg".format(PACKAGE_DIR)).decode("utf-8") os.unlink("{}/static/dot/{}.dot".format(PACKAGE_DIR, automa_name)) os.unlink("{}/static/tmp/{}.svg".format(PACKAGE_DIR, automa_name)) os.unlink("{}/static/tmp/piero.svg".format(PACKAGE_DIR)) return render_template("dfa.html", formula=formula, output=piero_encoding, output2=encoding)
def test_nnf(): parser = PLTLfParser() a, b, c = [PLTLfAtomic(c) for c in "abc"] f = parser("!(a & !b)") assert f.to_nnf() == PLTLfOr([PLTLfNot(a), b]) f = parser("!(!a | b)") assert f.to_nnf() == PLTLfAnd([a, PLTLfNot(b)]) f = parser("!(a <-> b)") assert f.to_nnf() == PLTLfAnd( [PLTLfOr([PLTLfNot(a), PLTLfNot(b)]), PLTLfOr([a, b])])
def test_parser(): parser = PLTLfParser() a, b, c = [PLTLfAtomic(c) for c in "abc"] assert parser("!a | b <-> !(a & !b) <-> a->b") == PLTLfEquivalence([ PLTLfOr([PLTLfNot(a), b]), PLTLfNot(PLTLfAnd([a, PLTLfNot(b)])), PLTLfImplies([a, b]), ]) assert parser("(Y a)") == PLTLfBefore(a) assert parser("(O (a&b)) <-> !(H (!a | !b) )") == PLTLfEquivalence([ PLTLfOnce(PLTLfAnd([a, b])), PLTLfNot(PLTLfHistorically(PLTLfOr([PLTLfNot(a), PLTLfNot(b)]))), ]) assert parser("(a S b S !c)") == PLTLfSince([a, b, PLTLfNot(c)])
def ltl2dfagame(ltl, controllables, uncontrollables, isLtlf): controllables = controllables.split(' ') uncontrollables = uncontrollables.split(' ') print("controllables :") print( controllables) print("uncontrollables :") print( uncontrollables) controllables = set(controllables) uncontrollables = set(uncontrollables) ### trasformazione in automata (LTL2DFA) if(isLtlf): parser = LTLfParser() else: parser = PLTLfParser() formula = parser(ltl) print("converting to automaton....") dfa = formula.to_dfa() print(dfa) # prints the DFA in DOT format ### trasformazione in automata (PYTHOMATA) dfa = converter(dfa) ### stampo automa normale graph = dfa.to_graphviz() #graph.render(outputFileName+"_normal") ### calcolo winning dict print("\n\n############# winning_dict ################") win_dict = winning_dict(dfa, controllables, uncontrollables) #print(win_dict) ### stampo winning map graph = to_graphviz_winning_map(dfa, win_dict) #graph.render(outputFileName) return graph
def execute(planning_domain, planning_problem, goal_formula): """Execute the compilation.""" pddl_parser = PDDLParser() parsed_domain = pddl_parser(planning_domain) parsed_problem = pddl_parser(planning_problem) # parsed_domain = planning_domain # parsed_problem = planning_problem symbols = compute_symb_vars(goal_formula) if not check_symbols( symbols, parsed_domain): # TODO: it checks symbols but not objects.... raise ValueError("[ERROR]: Formula symbols not in the domain.") if all(c in FUTURE_OPS for c in goal_formula if c.isupper()): f_parser = LTLfParser() try: formula = f_parser(goal_formula) except Exception: raise ParsingError() else: assert all(c in PAST_OPS for c in goal_formula if c.isupper()) p_parser = PLTLfParser() try: formula = p_parser(goal_formula) except Exception: raise ParsingError() mona_output = formula.to_dfa(mona_dfa_out=True) dfa = parse_dfa(mona_output) operators_trans, parameters = dfa.create_operators_trans( parsed_domain.predicates, symbols) new_domain = parsed_domain.get_new_domain(parameters, dfa.states, operators_trans) new_problem = parsed_problem.get_new_problem(list(dfa.accepting_states), symbols) return new_domain, new_problem
def test_nnf(): parser = PLTLfParser() a, b, c = [PLTLfAtomic(c) for c in "abc"] f = parser("!(a & !b)") assert f.to_nnf() == PLTLfOr([PLTLfNot(a), b]) f = parser("!(!a | b)") assert f.to_nnf() == PLTLfAnd([a, PLTLfNot(b)]) f = parser("!(a <-> b)") assert f.to_nnf() == PLTLfAnd( [PLTLfOr([PLTLfNot(a), PLTLfNot(b)]), PLTLfOr([a, b])]) # Yesterday and Weak Yesterday f = parser("!(Y (a & b))") assert f.to_nnf() == PLTLfWeakBefore(PLTLfOr([PLTLfNot(a), PLTLfNot(b)])) f = parser("!(WY (a & b))") assert f.to_nnf() == PLTLfBefore(PLTLfOr([PLTLfNot(a), PLTLfNot(b)])) # Once and Historically f = parser("!(O (a | b))") assert (f.to_nnf() == PLTLfHistorically( PLTLfAnd([PLTLfNot(a), PLTLfNot(b)])).to_nnf()) # Since f = parser("!(a S b)") assert f.to_nnf() == PLTLfPastRelease([PLTLfNot(a), PLTLfNot(b)]) f = parser("!(a P b)") assert f.to_nnf() == PLTLfSince([PLTLfNot(a), PLTLfNot(b)]) f = parser("!(O (a | b))") assert (f.to_nnf() == PLTLfHistorically( PLTLfAnd([PLTLfNot(a), PLTLfNot(b)])).to_nnf()) f = parser("!(H (a | b))") assert f.to_nnf() == PLTLfOnce(PLTLfAnd([PLTLfNot(a), PLTLfNot(b)])).to_nnf()
def test_negate(): parser = PLTLfParser() sa, sb, sc = "a", "b", "c" a, b, c = PLTLfAtomic(sa), PLTLfAtomic(sb), PLTLfAtomic(sc) a_and_b = PLTLfAnd([a, b]) not_a_or_not_b = PLTLfOr([PLTLfNot(a), PLTLfNot(b)]) assert a_and_b.negate() == not_a_or_not_b a_and_b_and_c = PLTLfAnd([a, b, c]) not_a_or_not_b_or_not_c = PLTLfOr([PLTLfNot(a), PLTLfNot(b), PLTLfNot(c)]) assert a_and_b_and_c.negate() == not_a_or_not_b_or_not_c before_a = PLTLfBefore(a) weakBefore_not_a = PLTLfWeakBefore(PLTLfNot(a)) assert before_a.negate() == weakBefore_not_a once_a = PLTLfOnce(a) false_pastRelease_not_a = PLTLfPastRelease([PLTLfFalse(), PLTLfNot(a)]) assert once_a.negate() == false_pastRelease_not_a historically_a = PLTLfHistorically(a) true_since_not_a = PLTLfSince([PLTLfTrue(), PLTLfNot(a)]) assert historically_a.negate() == true_since_not_a
def ltl2dfagame(ltl, controllables, uncontrollables, isLtlf): controllables = controllables.split(' ') uncontrollables = uncontrollables.split(' ') print("controllables :") print(controllables) print("uncontrollables :") print(uncontrollables) controllables = set(controllables) uncontrollables = set(uncontrollables) ### trasformazione in automata (LTL2DFA) if (isLtlf): parser = LTLfParser() else: parser = PLTLfParser() formula = parser(ltl) print("converting to automaton....") dfa = formula.to_dfa() print(dfa) # prints the DFA in DOT format ### trasformazione in automata (PYTHOMATA) dfa = converter(dfa) ### stampo automa normale graph = dfa.to_graphviz() #graph.render(outputFileName+"_normal") ### calcolo winning dict print("\n\n############# winning_dict ################") win_dict, strat_list = winning_dict(dfa, controllables, uncontrollables) #print(win_dict) ### stampo winning map graph, color_map = to_graphviz_winning_map(dfa, win_dict) #graph.render(outputFileName) ### stampo se e' realizzabile realizable = realizzabile(dfa, win_dict) #realizable = False ### stampo la strategy ''' if realizable: strat, strat_list = computeStrategy(dfa, win_dict, controllables) else: strat_list = [] ''' if not realizable: strat_list = [] ### plot print(color_map) plot(ltl, strat_list, realizable, controllables, uncontrollables, color_map) return graph
def test_pltlf_dfa(): parser = PLTLfParser() f = parser("a") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 3; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 2 [label="~a"]; 1 -> 3 [label="a"]; 2 -> 2 [label="true"]; 3 -> 3 [label="true"]; }""" assert dfa == expected f = parser("true") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 1; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 1 [label="true"]; }""" assert dfa == expected f = parser("false") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 1 [label="true"]; }""" assert dfa == expected f = parser("H a") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 3; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 2 [label="~a"]; 1 -> 3 [label="a"]; 2 -> 2 [label="true"]; 3 -> 2 [label="~a"]; 3 -> 3 [label="a"]; }""" assert dfa == expected f = parser("O(a & b)") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 3; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 2 [label="~a | ~b"]; 1 -> 3 [label="a & b"]; 2 -> 2 [label="~a | ~b"]; 2 -> 3 [label="a & b"]; 3 -> 3 [label="true"]; }""" assert dfa == expected f = parser("Y(a)") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 4; 5; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 2 [label="~a"]; 1 -> 3 [label="a"]; 2 -> 2 [label="~a"]; 2 -> 3 [label="a"]; 3 -> 4 [label="~a"]; 3 -> 5 [label="a"]; 4 -> 2 [label="~a"]; 4 -> 3 [label="a"]; 5 -> 4 [label="~a"]; 5 -> 5 [label="a"]; }""" assert dfa == expected f = parser("a S b") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 3; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 2 [label="~b"]; 1 -> 3 [label="b"]; 2 -> 2 [label="~b"]; 2 -> 3 [label="b"]; 3 -> 2 [label="~a & ~b"]; 3 -> 3 [label="a | b"]; }""" assert dfa == expected f = parser("H(a) & O(b)") dfa = f.to_dfa() expected = """digraph MONA_DFA { rankdir = LR; center = true; size = "7.5,10.5"; edge [fontname = Courier]; node [height = .5, width = .5]; node [shape = doublecircle]; 4; node [shape = circle]; 1; init [shape = plaintext, label = ""]; init -> 1; 1 -> 2 [label="~a"]; 1 -> 3 [label="a & ~b"]; 1 -> 4 [label="a & b"]; 2 -> 2 [label="true"]; 3 -> 2 [label="~a"]; 3 -> 3 [label="a & ~b"]; 3 -> 4 [label="a & b"]; 4 -> 2 [label="~a"]; 4 -> 4 [label="a"]; }""" assert dfa == expected