buildingmotif.utils#

Functions

combine_graphs(*graphs)

Combine all of the graphs into a new graph.

copy_graph(g[, preserve_blank_nodes])

Copy a graph.

get_ontology_files(directory[, recursive])

Returns a list of all ontology files in the given directory.

get_parameters(graph)

Returns the set of parameter names in the given graph.

get_template_parts_from_shape(shape_name, ...)

Turn a SHACL shape into a template.

graph_hash(graph)

Returns a cryptographic hash of the graph contents.

graph_size(g)

Returns the number of triples in a graph.

inline_sh_nodes(g)

Recursively inlines all sh:node properties and objects on the graph.

new_temporary_graph([more_namespaces])

Creates a new in-memory RDF graph with common and additional namespace bindings.

remove_triples_with_node(g, node)

Remove all triples that include the given node.

replace_nodes(g, replace)

Replace nodes in a graph.

rewrite_shape_graph(g)

Rewrites the input graph to make the resulting validation report more useful.

shacl_inference(data_graph[, shape_graph, ...])

Infer new triples in the data graph using the shape graph.

shacl_validate(data_graph[, shape_graph, engine])

Validate the data graph against the shape graph.

skip_uri(uri)

Returns True if the URI should be skipped during processing because it is axiomatic or not expected to be imported.

skolemize_shapes(g)

Skolemize the shapes in the graph.

template_to_shape(template)

Turn this template into a SHACL shape.

copy_graph(g: Graph, preserve_blank_nodes: bool = True) Graph[source]#

Copy a graph. Creates new blank nodes so that these remain unique to each Graph

Parameters:
  • g (Graph) – the graph to copy

  • preserve_blank_nodes (boolean, defaults to True) – if true, keep blank nodes the same when copying the graph

Returns:

a copy of the input graph

Return type:

Graph

inline_sh_nodes(g: Graph)[source]#

Recursively inlines all sh:node properties and objects on the graph.

Parameters:

g (Graph) – graph to be edited

combine_graphs(*graphs: Graph) Graph[source]#

Combine all of the graphs into a new graph.

Returns:

combined graph

Return type:

Graph

graph_size(g: Graph) int[source]#

Returns the number of triples in a graph.

Parameters:

g (Graph) – graph to be measured

Returns:

number of triples in the graph

Return type:

int

remove_triples_with_node(g: Graph, node: URIRef) None[source]#

Remove all triples that include the given node. Edits the graph in-place.

Parameters:
  • g (Graph) – the graph to remove triples from

  • node (URIRef) – the node to remove

replace_nodes(g: Graph, replace: Dict[Node, Node]) None[source]#

Replace nodes in a graph.

Parameters:
  • g (Graph) – graph to replace nodes in

  • replace (Dict[Node, Node]) – dict mapping old nodes to new nodes

get_ontology_files(directory: Path, recursive: bool = True) List[Path][source]#

Returns a list of all ontology files in the given directory.

If recursive is true, traverses the directory structure to find ontology files not just in the given directory.

Parameters:
  • directory (Path) – the directory to begin the search

  • recursive (bool, optional) – if true, find ontology files in nested directories, defaults to true

Returns:

a list of filenames

Return type:

List[Path]

get_template_parts_from_shape(shape_name: URIRef, shape_graph: Graph) Tuple[Graph, List[Dict]][source]#

Turn a SHACL shape into a template. The following attributes of NodeShapes will be incorporated into the resulting template: - sh:property with sh:minCount - sh:property with sh:qualifiedMinCount - sh:class - sh:node

Parameters:
  • shape_name (URIRef) – name of shape

  • shape_graph (Graph) – shape graph

Raises:
  • Exception – if more than one object type detected on shape

  • Exception – if more than one min count detected on shape

Returns:

template parts

Return type:

Tuple[Graph, List[Dict]]

template_to_shape(template: Template) Graph[source]#

Turn this template into a SHACL shape.

Parameters:

template (template) – template to convert

Returns:

graph of template

Return type:

Graph

new_temporary_graph(more_namespaces: Optional[dict] = None) Graph[source]#

Creates a new in-memory RDF graph with common and additional namespace bindings.

Parameters:

more_namespaces (Optional[dict], optional) – namespaces, defaults to None

Returns:

graph with namespaces

Return type:

Graph

get_parameters(graph: Graph) Set[str][source]#

Returns the set of parameter names in the given graph.

Parameters are identified by the PARAM namespace, urn:___param___#. This method returns the names of the parameters, not the full URIs. For example, the parameter urn:___param___#abc in a graph would be returned as abc.

Parameters:

graph (Graph) – a graph containing parameters

Returns:

a set of the parameter names in the graph

Return type:

Set[str]

rewrite_shape_graph(g: Graph) Graph[source]#

Rewrites the input graph to make the resulting validation report more useful.

Parameters:

g (Graph) – the shape graph to rewrite

Returns:

a copy of the original shape graph w/ rewritten shapes

Return type:

Graph

skip_uri(uri: URIRef) bool[source]#

Returns True if the URI should be skipped during processing because it is axiomatic or not expected to be imported.

Skips URIs in the XSD and SHACL namespaces.

Returns:

True if the URI should be skipped; False otherwise

Return type:

bool

shacl_validate(data_graph: Graph, shape_graph: Optional[Graph] = None, engine: Optional[str] = 'topquadrant') Tuple[bool, Graph, str][source]#

Validate the data graph against the shape graph. Uses the fastest validation method available. Use the ‘topquadrant’ feature to use TopQuadrant’s SHACL engine. Defaults to using PySHACL.

Parameters:
  • data_graph (Graph) – the graph to validate

  • shape_graph (Graph, optional) – the shape graph to validate against

  • engine (str, optional) – the SHACL engine to use, defaults to “topquadrant”

Returns:

a tuple containing the validation result, the validation report, and the validation report string

Return type:

Tuple[bool, Graph, str]

shacl_inference(data_graph: Graph, shape_graph: Optional[Graph] = None, engine: Optional[str] = 'topquadrant') Graph[source]#

Infer new triples in the data graph using the shape graph. Edits the data graph in place. Uses the fastest inference method available. Use the ‘topquadrant’ feature to use TopQuadrant’s SHACL engine. Defaults to using PySHACL.

Parameters:
  • data_graph (Graph) – the graph to infer new triples in

  • shape_graph (Optional[Graph]) – the shape graph to use for inference

  • engine (str, optional) – the SHACL engine to use, defaults to “topquadrant”

Returns:

the data graph with inferred triples

Return type:

Graph

skolemize_shapes(g: Graph) Graph[source]#

Skolemize the shapes in the graph.

Parameters:

g (Graph) – the graph to skolemize

Returns:

the skolemized graph

Return type:

Graph

graph_hash(graph: Graph) int[source]#

Returns a cryptographic hash of the graph contents. This uses the same method as rdflib’s isomorphic function to generate a cryptographic hash of a given graph. This method calculates a consistent hash of the canonicalized form of the graph. If the hashes of two graphs are equal, this means that the graphs are isomorphic. Generating the hashes (using this method) and caching them allows graph isomorphism to be determined without having to recalculate the canonical form of the graph, which can be expensive.

Parameters:

graph (graph) – graph to hash

Returns:

integer hash

Return type:

int