buildingmotif.template_matcher#

Using the VF2 algorithm to compute subgraph isomorphisms between a template T and a graph G. If the found isomorphism is a subgraph of T, then T is not fully matched and additional input is required to fully populate the template.

Functions

digraph_to_rdflib(digraph)

Turns a nx.DiGraph into an rdflib.Graph.

generate_all_subgraphs(T)

Generates all node-induced subgraphs of T in order of decreasing size.

get_semantic_feasibility(G1, G2, ontology, ...)

Returns a function that checks if two nodes are semantically feasible to be matched given the information in the provided ontology.

Classes

TemplateMatcher(building, template, ontology)

Computes the set of subgraphs of G that are monomorphic to T; these are organized by how "complete" the monomorphism is.

get_semantic_feasibility(G1: Graph, G2: Graph, ontology: Graph, _cache: _ontology_lookup_cache) Callable[[Node, Node], bool][source]#

Returns a function that checks if two nodes are semantically feasible to be matched given the information in the provided ontology.

The function returns true if the two nodes are semantically feasible to be matched. We use the following checks: 1. If the two nodes are both classes, one must be a subclass of the other. 2. If the two nodes are instances, they must be of the same class.

Parameters:
  • G1 (Graph) – graph 1

  • G2 (Graph) – graph 2

  • ontology (Graph) – ontology graph

  • _cache (_ontology_lookup_cache) – ontology lookup cache

Returns:

function that checks if two nodes are semantically feasible

Return type:

Callable[[Node, Node], bool]

generate_all_subgraphs(T: Graph) Generator[Graph, None, None][source]#

Generates all node-induced subgraphs of T in order of decreasing size.

We generate subgraphs in decreasing order of size because we want to find the largest subgraph as part of the monomorphism search process.

Parameters:

T (Graph) – template graph

Yield:

subgraphs

Return type:

Generator[Graph, None, None]

digraph_to_rdflib(digraph: DiGraph) Graph[source]#

Turns a nx.DiGraph into an rdflib.Graph.

Expects the nx.DiGraph to have been produced by rdflib_to_networkx_digraph().

Parameters:

digraph (nx.DiGraph) – directed graph

Returns:

RDF graph

Return type:

Graph

class TemplateMatcher(building: Graph, template: Template, ontology: Graph, graph_target: Optional[Node] = None)[source]#

Computes the set of subgraphs of G that are monomorphic to T; these are organized by how “complete” the monomorphism is.

mappings: Dict[int, List[Dict[Node, Node]]]#
template_bindings: Dict[str, Node]#
template: Template#
building: Graph#
template_graph: Graph#
add_mapping(mapping: Dict[Node, Node])[source]#

Adds a mapping to the set of mappings.

Parameters:

mapping (Mapping) – mapping

property largest_mapping_size: int#

Returns the size of the largest mapping.

Returns:

size of largest mapping

Return type:

int

building_subgraph_from_mapping(mapping: Dict[Node, Node]) Graph[source]#

Returns the subgraph of the building graph that corresponds to the given mapping.

Parameters:

mapping (Mapping) – mapping

Returns:

subgraph

Return type:

Graph

template_subgraph_from_mapping(mapping: Dict[Node, Node]) Graph[source]#

Returns the subgraph of the template graph that corresponds to the given mapping.

Parameters:

mapping (Mapping) – mapping

Returns:

subgraph

Return type:

Graph

remaining_template_graph(mapping: Dict[Node, Node]) Graph[source]#

Returns the remaining template graph to be filled out given a mapping.

Parameters:

mapping (Mapping) – mapping

Returns:

remaining template graph to be filled out

Return type:

Graph

remaining_template(mapping: Dict[Node, Node]) Optional[Template][source]#

Returns the remaining template to be filled out given a mapping.

Parameters:

mapping (Mapping) – mapping

Returns:

remaining template to be filled out

Return type:

Optional[Template]

mappings_iter(size=None) Generator[Dict[Node, Node], None, None][source]#

Returns an iterator over all of the mappings of the given size.

If size is None, then all mappings are returned in descending order of the size of the mapping. This means the most complete mappings will be returned first.

Parameters:

size (int, optional) – size, defaults to None

Yield:

mapping iterator

Return type:

Generator[Mapping, None, None]

building_mapping_subgraphs_iter(size=None) Generator[Tuple[Dict[Node, Node], Graph], None, None][source]#

Returns an iterator over all of the subgraphs with a mapping of the given size.

If size is None, then all mappings are returned in descending order of the size of the mapping. This means the most complete subgraphs will be returned first.

Parameters:

size (int, optional) – size, defaults to None

Yield:

mapping and subgraph iterator

Return type:

Generator[Tuple[Mapping, Graph], None, None]