Skip to content

Commit

Permalink
add debug on sgs
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed Nov 22, 2024
1 parent d638016 commit 2990b71
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions buildingmotif/ingresses/semantic_graph_synthesis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List
import logging

from rdflib import Graph, Namespace

Expand All @@ -15,6 +16,9 @@
from buildingmotif.ingresses.naming_convention import NamingConventionIngress


logger = logging.getLogger(__name__)


class SemanticGraphSynthesizerIngress(GraphIngressHandler):
def __init__(
self,
Expand All @@ -29,27 +33,33 @@ def __init__(
self.ontology = ontology
# Add templates from libraries; the synthesizer will look at these to determine the "best fit"
# for each group of input tokens
logger.info(f"Adding templates from {len(libraries)} libraries")
for library in libraries:
self.sgs.add_templates_from_library(library)

def graph(self, ns: Namespace) -> Graph:
g = Graph()
# converts the input records to TokenizedLabels
labels = [TokenizedLabel.from_dict(x.fields) for x in self.upstream.records]
logger.debug(f"Got {len(labels)} labels from upstream")
# this groups labels into LabelSets based on shared sets of token classes.
# This is used to speed up the matching process as we can figure out which labels are compatible
bindings_list = self.sgs.find_bindings_for_labels(self.ontology, labels)
logger.debug(f"Got {len(bindings_list)} bindings from the synthesizer")
unified_bindings_list = unify_bindings(bindings_list)
logger.debug(f"Unified bindings into {len(unified_bindings_list)} groups")
# at this point, the unified_bindings_list contains the best fit for each group of input tokens

# for each group of labels, evaluate the bindings and add the resulting graph to the output
for ub in unified_bindings_list:
ev = evaluate_bindings(ns, ub)
logger.debug(f"Evaluated bindings to {ev}")
# if the evaluation returns a Template, we need to mint new URIs in the given namespace
# for any unbound parameters. If it returns a Graph, we can just add it to the output.
if isinstance(ev, Template):
_, graph = ev.fill(ns)
else:
graph = ev
logger.debug(f"Adding graph with {len(graph)} triples to output")
g += graph
return g

0 comments on commit 2990b71

Please sign in to comment.