From b635990580aed4c47448701830d4e19c45044d9b Mon Sep 17 00:00:00 2001 From: Paco Nathan Date: Mon, 11 Oct 2021 22:13:00 -0700 Subject: [PATCH 1/3] get predicates for query engine --- goedwig/query.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/goedwig/query.py b/goedwig/query.py index ec5feec..34c7ec5 100644 --- a/goedwig/query.py +++ b/goedwig/query.py @@ -416,3 +416,12 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 self.projections.append(ProjectionElement(bind_lit, prop_lit, alias_lit)) return None + + + def get_paths ( + self, + ) -> list: + """ +JSON representation. + """ + return self.paths From 79716c9fa77f1419288bbba9ccc9a27480f97825 Mon Sep 17 00:00:00 2001 From: Paco Nathan Date: Mon, 11 Oct 2021 23:06:06 -0700 Subject: [PATCH 2/3] JSON serializable --- goedwig/query.py | 97 ++++++++++++++++++++++++++++++++++++++++-------- sample.py | 4 ++ 2 files changed, 85 insertions(+), 16 deletions(-) diff --git a/goedwig/query.py b/goedwig/query.py index 34c7ec5..50a997e 100644 --- a/goedwig/query.py +++ b/goedwig/query.py @@ -13,7 +13,7 @@ from .cypher import CypherItem -class Variable: # pylint: disable=R0903 +class Variable: """ Represent a node or edge variable within a query. """ @@ -45,6 +45,19 @@ def __repr__ ( return str(_repr) + def toJSON ( # pylint: disable=C0103 + self, + ) -> dict: + """ +JSON-serializable representation + """ + return { + "name": self.name, + "item": self.item, + "predicates": [ p.toJSON() for p in self.predicates ], + } + + class Predicate: # pylint: disable=R0903 """ Represent a simple predicate. @@ -52,11 +65,24 @@ class Predicate: # pylint: disable=R0903 def __init__ ( self, var: Variable, + elem: str, ) -> None: """ Constructor. """ self.var: Variable = var + self.elem: str = elem + + + def toJSON ( # pylint: disable=C0103 + self, + ) -> dict: + """ +JSON-serializable representation + """ + return { + "elem": self.elem + } class PredicateMap (Predicate): # pylint: disable=R0903 @@ -66,13 +92,14 @@ class PredicateMap (Predicate): # pylint: disable=R0903 def __init__ ( self, var: Variable, + elem: str, key: str, val: str, ) -> None: """ Constructor. """ - super().__init__(var) + super().__init__(var, elem) self.key: str = key self.val: str = val @@ -92,6 +119,20 @@ def __repr__ ( return str(_repr) + def toJSON ( # pylint: disable=C0103 + self, + ) -> dict: + """ +JSON-serializable representation + """ + j = super().toJSON() + j["kind"] = "map" + j["key"] = self.key + j["val"] = self.val + + return j + + class PredicateLabel (Predicate): # pylint: disable=R0903 """ Represent a predicate that specifies a label. @@ -99,12 +140,13 @@ class PredicateLabel (Predicate): # pylint: disable=R0903 def __init__ ( self, var: Variable, + elem: str, label: str, ) -> None: """ Constructor. """ - super().__init__(var) + super().__init__(var, elem) self.label: str = label @@ -122,6 +164,19 @@ def __repr__ ( return str(_repr) + def toJSON ( # pylint: disable=C0103 + self, + ) -> dict: + """ +JSON-serializable representation + """ + j = super().toJSON() + j["kind"] = "label" + j["label"] = self.label + + return j + + class PredicateDirection (Predicate): # pylint: disable=R0903 """ Represent a predicate that specifies a direction. @@ -129,12 +184,13 @@ class PredicateDirection (Predicate): # pylint: disable=R0903 def __init__ ( self, var: Variable, + elem: str, direction: str, ) -> None: """ Constructor. """ - super().__init__(var) + super().__init__(var, elem) self.direction: str = direction @@ -152,6 +208,19 @@ def __repr__ ( return str(_repr) + def toJSON ( # pylint: disable=C0103 + self, + ) -> dict: + """ +JSON-serializable representation + """ + j = super().toJSON() + j["kind"] = "direction" + j["direction"] = self.direction + + return j + + class Path: # pylint: disable=R0903 """ Represent a path pattern, with multiple node/edge/regex elements. @@ -162,7 +231,7 @@ def __init__ ( """ Constructor. """ - self.elem: list = [] + self.elem: typing.List[Variable] = [] def __repr__ ( @@ -320,10 +389,10 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 child_type = self.items[j].ast_typestr if child_type == "node pattern": - path.elem.append(self.parse_items(i = j, debug=debug)) + path.elem.append(self.parse_items(i = j, debug=debug)) # type: ignore elif child_type == "rel pattern": - path.elem.append(self.parse_items(i = j, debug=debug)) + path.elem.append(self.parse_items(i = j, debug=debug)) # type: ignore return path @@ -343,6 +412,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 if child_type == "label": node_var.predicates.append(PredicateLabel( node_var, + "node", self.items[j].literal.strip(":"), )) @@ -351,6 +421,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 node_var.predicates.append(PredicateMap( node_var, + "node", self.items[key_i].literal, # pylint: disable=W0621 self.items[val_i].literal.strip("'"), # pylint: disable=W0621 )) @@ -372,6 +443,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 if child_type == "label": edge_var.predicates.append(PredicateLabel( edge_var, + "edge", self.items[j].literal.strip(":"), )) @@ -380,6 +452,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 edge_var.predicates.append(PredicateMap( edge_var, + "edge", self.items[key_i].literal, # pylint: disable=W0621 self.items[val_i].literal.strip("'"), # pylint: disable=W0621 )) @@ -387,6 +460,7 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 elif child_type == "rel type": edge_var.predicates.append(PredicateDirection( edge_var, + "edge", self.items[j].literal.strip(":"), )) @@ -416,12 +490,3 @@ def parse_items ( # pylint: disable=R0912,R0914,R0915 self.projections.append(ProjectionElement(bind_lit, prop_lit, alias_lit)) return None - - - def get_paths ( - self, - ) -> list: - """ -JSON representation. - """ - return self.paths diff --git a/sample.py b/sample.py index 3ba00b3..37e0a2c 100644 --- a/sample.py +++ b/sample.py @@ -37,6 +37,10 @@ plan = eval(str(q)) # pylint: disable=W0123 print(json.dumps(plan, indent=1, sort_keys=False)) + path = q.paths[0] + for elem in path.elem: + print(json.dumps(elem.toJSON())) + # testing assertions if testing: assert len(q.items) == 14 From e9fe5b40b399184c2f825428369031d9b4e0ae25 Mon Sep 17 00:00:00 2001 From: Paco Nathan Date: Mon, 11 Oct 2021 23:43:29 -0700 Subject: [PATCH 3/3] prep release --- CHANGELOG.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f702e9e..b941ac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # `goedwig` changelog +## 0.2.0 + +2021-10-11 + + * recursive descent traversal of AST items to develop a query plan + * can now handle arbitrary length path patterns + * toJSON() calls to pass into partitions at runtime + + ## 0.1.4 2021-10-10