Knowledge Graph model¶
The Knowledge Graph (KG) sublanguage represents an RDF-style graph: typed nodes joined by directed, labelled edges. It is the model behind the Web Modeling Editor’s Knowledge Graph diagram, and the input to the deterministic OWL 2 / SHACL to B-UML transformation.
Unlike the other sublanguages, KG identifiers are IRIs, blank-node ids and
literal values rather than Python names. KGNode therefore extends
Element instead of NamedElement: IRIs and labels are preserved exactly
as written, and safe variable names are derived separately when the model is
exported to Python.
Nodes¶
Seven node kinds, matching what an OWL/RDF graph can contain:
Kind |
Represents |
|---|---|
|
A class or concept ( |
|
An instance ( |
|
A property that is itself the subject or object of assertions |
|
A literal value, with an optional datatype IRI |
|
An anonymous resource (RDF blank node) |
|
A constraint on a class: an OWL restriction set or a |
|
A constraint on a property: an |
The two constraint kinds reify constraints as first-class, visible nodes so
they can be inspected and edited on the canvas. Their payload lives in
metadata['constraintSpecs'] as a list of vocabulary-agnostic
constraint-spec dicts, each naming a kind (minCardinality,
allValuesFrom, pattern, shaclOr, …) and a value.
Edges and axioms¶
KGEdge is a directed relation carrying an optional predicate IRI. Some OWL
constructs relate sets or ordered lists of elements and so fit on neither a
single node nor a single edge — owl:equivalentClass, owl:disjointUnionOf,
owl:propertyChainAxiom, owl:hasKey and friends. Those are recorded as
KGAxiom records on KnowledgeGraph.axioms, referencing node ids
rather than node objects so they stay serialisable. Like the rest of the
metamodel they validate on assignment: an id must be a str and a list of ids
a list of str. Each class lists its fields in FIELDS, and
to_dict() gives the plain-dict form the converters serialise.
The OWL importer stores each constraint parameter once, in the
constraintSpecs of its constraint node, which is what the editor edits; no
raw edge repeats it. owl:hasKey in particular becomes only the hasKey
spec of a node constraint on the class, not a HasKeyAxiom.
Example¶
from besser.BUML.metamodel.kg import (
KnowledgeGraph, KGClass, KGProperty, KGEdge,
)
person = KGClass(id="ex:Person", label="Person", iri="http://ex.org/Person")
knows = KGProperty(id="ex:knows", label="knows", iri="http://ex.org/knows")
kg = KnowledgeGraph(name="social")
kg.add_node(person)
kg.add_node(knows)
kg.add_edge(KGEdge(
id="e1", source=knows, target=person,
iri="http://www.w3.org/2000/01/rdf-schema#domain",
))
Building and consuming a KG¶
Import an ontology with
besser.BUML.notations.kg_rdf.importer.owl_file_to_knowledge_graph().Export one with
besser.BUML.notations.kg_rdf.exporter.serialize_knowledge_graph()(Turtle or RDF/XML, with OWL restrictions, SHACL shapes, or both).Transform one into a structural model with OCL constraints — see From a Knowledge Graph to B-UML.