Skip to content

Commit

Permalink
Add cypher syntax generation for dynamic edge constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
j6k4m8 committed Jun 6, 2022
1 parent e42f48a commit db4154a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
22 changes: 22 additions & 0 deletions dotmotif/executors/Neo4jExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,28 @@ def motif_to_cypher(
)
)

# Dynamic edge constraints:
# Constraints are of the form:
# {('A', 'B'): {'weight': {'==': ['A', 'C', 'weight']}}}
for (u, v), constraints in motif.list_dynamic_edge_constraints().items():
for this_attr, ops in constraints.items():
for op, (that_u, that_v, that_attr) in ops.items():
this_edge_name = edge_mapping[(u, v)]
that_edge_name = edge_mapping[(that_u, that_v)]
cypher_edge_constraints.append(
(
"NOT ({}[{}] {} {}[{}])"
if _operator_negation_infix(op)
else "{}[{}] {} {}[{}]"
).format(
this_edge_name,
_quoted_if_necessary(this_attr),
_remapped_operator(op),
that_edge_name,
_quoted_if_necessary(that_attr),
)
)

conditions.extend([*cypher_node_constraints, *cypher_edge_constraints])

if count_only:
Expand Down
16 changes: 16 additions & 0 deletions dotmotif/executors/test_dm_cypher.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ def test_dynamic_constraints_in_cypher(self):
)


class TestDynamicEdgeConstraints(unittest.TestCase):
def test_dynamic_constraints_in_cypher(self):
dm = dotmotif.Motif(enforce_inequality=True)
dm.from_motif(
"""
A -> B as AB
A -> C as AC
AB.weight >= AC.weight
"""
)
self.assertIn(
"""WHERE A_B["weight"] >= A_C["weight"]""",
Neo4jExecutor.motif_to_cypher(dm).strip(),
)


class BugReports(unittest.TestCase):
def test_fix_where_clause__github_35(self):
dm = dotmotif.Motif(enforce_inequality=True)
Expand Down
29 changes: 29 additions & 0 deletions dotmotif/executors/test_grandisoexecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,32 @@ def test_aliased_edge_comparisons_with_different_edge_attributes(self):
host.add_edge("C", "D", length=1, weight=1)
res = GrandIsoExecutor(graph=host).find(dm)
self.assertEqual(len(res), 2)


# class TestEdgeConstraintsInMacros(unittest.TestCase):
# def test_edge_comparison_in_macro(self):
# host = nx.DiGraph()
# host.add_edge("A", "B", foo=1)
# host.add_edge("A", "C", foo=2)
# host.add_edge("B", "C", foo=0.5)
# host.add_edge("C", "D", foo=0.25)
# host.add_edge("D", "C", foo=1)
# host.add_edge("C", "B", foo=2)
# host.add_edge("B", "A", foo=2)
# E = GrandIsoExecutor(graph=host)

# M = Motif(
# """

# descending(a, b, c) {
# a -> b as Edge1
# b -> c as Edge2
# Edge1.foo > Edge2.foo
# }

# descending(a, b, c)
# descending(b, c, d)

# """
# )
# assert E.count(M) == 1

0 comments on commit db4154a

Please sign in to comment.