def test_quant2_quant1(self): formula1 = lexpr( r'forall x. forall y. exists z. (P(x) & Q(x, y) & R(z))') formula2 = lexpr(r'forall x y. exists z. (P(x) & Q(x, y) & R(z))') formula3 = lexpr( r'forall x. exists z. forall y. (P(x) & Q(x, y) & R(z))') graph1 = formula_to_graph(formula1, normalize=True) graph2 = formula_to_graph(formula2, normalize=True) graph3 = formula_to_graph(formula3, normalize=True) self.assert_graphs_are_equal(graph1, graph2) self.assert_graphs_are_equal(graph1, graph3)
def test_and_pred_var_pred_var_rename(self): formula1 = lexpr(r'all x. (P(x) & Q(x))') formula2 = lexpr(r'all y. (Q(y) & P(y))') eG = nx.DiGraph() eG.add_nodes_from([(i, { 'label': s }) for i, s in enumerate(['all', '<var_en>', '&', 'P', 'Q'])]) eG.add_edges_from([(0, 1), (0, 2), (2, 3), (2, 4), (3, 1), (4, 1)]) G1 = formula_to_graph(formula1) G2 = formula_to_graph(formula2) self.assert_graphs_are_equal(eG, G1) self.assert_graphs_are_equal(eG, G2)
def test_and_pred_var_pred_var(self): formula = lexpr(r'P(x) & Q(x)') eG = nx.DiGraph() eG.add_nodes_from([(i, {'label': s}) for i, s in enumerate('&PxQx')]) eG.add_edges_from([(0, 1), (0, 3), (1, 2), (3, 4)]) G = formula_to_graph(formula) self.assert_graphs_are_equal(eG, G)
def test_lamdba_pred_var(self): formula = lexpr(r'\x. P(x)') eG = nx.DiGraph() eG.add_nodes_from([(i, { 'label': s }) for i, s in enumerate(['lambda', 'x', 'P', 'x'])]) eG.add_edges_from([(0, 1), (0, 2), (2, 3)]) G = formula_to_graph(formula) self.assert_graphs_are_equal(eG, G)
def test_quantf_pred_var(self): formula = lexpr(r'exists P. P(x)') eG = nx.DiGraph() eG.add_nodes_from([(i, { 'label': s }) for i, s in enumerate(['exists', '<var_func>', '<var_func>', 'x'])]) eG.add_edges_from([(0, 1), (0, 2), (2, 3)]) G = formula_to_graph(formula) self.assert_graphs_are_equal(eG, G)
def from_formulas(formulas, max_nodes=None, max_bi_relations=None, max_tri_relations=None, emb_dim=128): graphs = [] for formula in formulas: try: graph = formula_to_graph(formula, normalize=True) except Exception as e: graph = make_empty_graph() graphs.append(graph) graph_structs = [GraphStructures(g) for g in graphs] graph_data = GraphData(graph_structs, max_bi_relations, max_tri_relations) graph_data.emb_dim = emb_dim return graph_data
def test_quant_inner(self): formula1 = lexpr(r'forall x. (P(x) | exists y. Q(x, y))') formula2 = lexpr(r'forall x. exists y. (P(x) | Q(x, y))') graph1 = formula_to_graph(formula1, normalize=True) graph2 = formula_to_graph(formula2, normalize=True) self.assert_graphs_are_equal(graph1, graph2)
def test_quant_swap(self): formula1 = lexpr(r'forall x. exists y. P(x, y)') formula2 = lexpr(r'exists y. forall x. P(x, y)') graph1 = formula_to_graph(formula1, normalize=True) graph2 = formula_to_graph(formula2, normalize=True) self.assert_graphs_are_equal(graph1, graph2)
def test_constant(self): formula = lexpr(r'a') eG = nx.DiGraph() eG.add_nodes_from([(i, {'label': s}) for i, s in enumerate('a')]) G = formula_to_graph(formula) self.assert_graphs_are_equal(eG, G)
import logging import subprocess import sys from logic_parser import lexpr from nltk2graph import formula_to_graph import networkx as nx logging.basicConfig(level=logging.INFO) for i, line in enumerate(fileinput.input()): line = line.strip() try: expr = lexpr(line) except Exception as e: print('Failed to parse formula: {0}'.format(line), file=sys.stderr) print('Error: {0}'.format(e), file=sys.stderr) continue expr = lexpr(line) graph = formula_to_graph(expr, normalize=True) graph_basename = 'graph_' + str(i) nx.drawing.nx_pydot.write_dot(graph, './{0}.dot'.format(graph_basename)) process = subprocess.Popen( 'cat ./{0}.dot | dot -Tpng -o {0}.png'.format(graph_basename), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) logging.info('{0}.png : {1}'.format(graph_basename, expr))
def from_formulas(formulas): graphs = [ formula_to_graph(formula, normalize=True) for formula in formulas ] graph_structs = [GraphStructures(g) for g in graphs] return GraphData(graph_structs)