Skip to content

Commit

Permalink
Unify :outputs in queries
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbgn committed Jul 26, 2023
1 parent 65c2467 commit 52a85f1
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions transforge/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,16 @@ def __init__(self, lang: Language, graph: Graph,
None if self.unfold_tree else dict()

# Assigned variables to the outputs and inputs of the transformation
self.inputs: list[Variable] = []
self.outputs: list[Variable] = []
self.inputs: dict[Variable, Node] = dict()
self.outputs: dict[Variable, Node] = dict()

# Connections between the variables
self.before: dict[Variable, list[Variable]] = defaultdict(list)
self.after: dict[Variable, list[Variable]] = defaultdict(list)

# Traverse the graph
for node in self.graph.objects(self.root, TF.output):
self.outputs.append(self.assign_variables(node))
self.assign_variables(node)

@staticmethod
def from_list(lang: Language, aspects: list[Operator | Type | list],
Expand Down Expand Up @@ -203,6 +203,12 @@ def assign_variables(self, node: Node, path: list[Node] = []) -> Variable:
self.type[var] = list(self.graph.objects(node, TF.type))
self.operator[var] = list(self.graph.objects(node, TF.via))

if (self.root, TF.output, node) in self.graph:
self.outputs[var] = node

if (self.root, TF.input, node) in self.graph:
self.inputs[var] = node

for next_node in self.graph.objects(node, TF["from"]):
next_var = self.assign_variables(next_node, path + [node])
self.before[var].append(next_var)
Expand Down Expand Up @@ -281,12 +287,14 @@ def io(self) -> Iterator[str]:
"""
Conditions for matching on input and outputs of the query.
"""
for output in self.graph.objects(self.root, TF.output):
type_var = self.fresh()
for outputv, output in self.outputs.items():
if self.by_penultimate:
yield f"?workflow :output/:from?/:type {type_var.n3()}."
yield f"?workflow :output/:from? {outputv.n3()}."
else:
yield f"?workflow :output/:type {type_var.n3()}."
yield f"?workflow :output {outputv.n3()}."

type_var = self.fresh()
yield f"{outputv.n3()} :type {type_var.n3()}."

# TODO general method for this
type_set = TypeUnion((self.lang.parse_type_uri(t)
Expand All @@ -299,14 +307,15 @@ def io(self) -> Iterator[str]:
(self.lang.uri(t) for t in type_set))
yield "}"

for input in self.graph.objects(self.root, TF.input):
for inputv, input in self.inputs.items():
yield f"?workflow :input {inputv.n3()}."
type_var = self.fresh()
type_set = TypeUnion((self.lang.parse_type_uri(t)
for t in self.graph.objects(input, TF.type)
if isinstance(t, URIRef)),
specific=False)

yield f"?workflow :input/:type {type_var.n3()}."
yield f"{inputv.n3()} :type {type_var.n3()}."
yield f"GRAPH {self.lang.vocab.n3()} {{"
yield from union(f"{type_var.n3()} rdfs:subClassOf",
(self.lang.uri(t) for t in type_set))
Expand Down Expand Up @@ -341,6 +350,12 @@ def chronology(self) -> Iterator[str]:
if current in visited:
continue

# Prepare for next round
visited.add(current)
for b in self.before[current]:
if b not in visited:
waiting.append(b)

if self.skip_same_branch_matches:
yield f"\n{{SELECT DISTINCT {current.n3()} WHERE {{"

Expand Down Expand Up @@ -402,10 +417,3 @@ def chronology(self) -> Iterator[str]:

if self.skip_same_branch_matches:
yield "}}"

visited.add(current)

# Add successors to queue
for b in self.before[current]:
if b not in visited:
waiting.append(b)

0 comments on commit 52a85f1

Please sign in to comment.