Installation¶
The connection needs the rdflib extra, which installs rdflib and the oxrdflib Oxigraph store:
Without it, from cimgraph.databases import RDFlibConnection raises an ImportError naming the extra to install.
Loading a model¶
Select the CIM profile, then pass the path of a local RDF/XML file. The connection parses the file when it is created.
Parameters
filename(str): a local CIM RDF/XML file. A path that is not a local file raisesFileNotFoundError.use_oxigraph(bool, defaultTrue): use the Oxigraph store fromoxrdflib;Falseuses rdflib's default in-memory store.
import os
os.environ['CIMG_CIM_PROFILE'] = 'cimhub_2023'
import cimprofiles.cim17.cimhub_2023 as cim
from cimgraph.databases import RDFlibConnection
database = RDFlibConnection(filename='../../tests/test_models/ieee13.xml')
print(f'{len(database.connection_obj)} triples loaded')
3392 triples loaded
The connection is used like any other: pass it to a graph model with the feeder to load.
from cimgraph.models import FeederModel
feeder = cim.Feeder(mRID='49AD8E07-3BF9-A4E2-CB8F-C3722F837B62')
network = FeederModel(container=feeder, connection=database, distributed=False)
for cim_class, objects in network.graph.items():
print(f'{cim_class.__name__}: {len(objects)}')
Feeder: 1 TransformerTank: 4 EnergyConsumer: 16 ConnectivityNode: 22 Terminal: 70 ACLineSegment: 11 PowerTransformer: 4 PowerElectronicsConnection: 5 LoadBreakSwitch: 2 Recloser: 1 LinearShuntCompensator: 2 Breaker: 1 EnergySource: 1 Fuse: 1
Expanding the graph¶
get_all_edges queries the attributes and associations of every object of a class. Unit values are CIMUnits in base SI units; convert them with .to().
network.get_all_edges(cim.ACLineSegment)
for line in network.list_by_class(cim.ACLineSegment)[:3]:
print(f'{line.name}: {line.length.to("ft"):.0f} ft')
645646: 300 ft 632645: 500 ft 670671: 1333 ft
Running SPARQL queries¶
execute runs a SPARQL query on the in-memory graph. The result has the SPARQL JSON layout {'results': {'bindings': [...]}}, where each binding is an rdflib result row.
query = """
PREFIX cim: <http://iec.ch/TC57/CIM100#>
SELECT ?name WHERE {
?switch a cim:LoadBreakSwitch ;
cim:IdentifiedObject.name ?name .
}
ORDER BY ?name
"""
result = database.execute(query)
print([str(row.name) for row in result['results']['bindings']])
['671692', 'sect1']
Limitations¶
- Only local RDF/XML files are read, not JSON-LD or Turtle, and URLs are not fetched.
- Files with
rdf:about="urn:uuid:..."identifiers (CIMG_IEC61970_552='552-NEW', the default) work. For552-LEGACYandCGMESfiles (rdf:ID="_...") the queries build object IRIs fromCIMG_URL, while rdflib resolvesrdf:IDagainst the file's location, so objects other than the container are not found. get_all_edgesruns its batch queries one after another, not in parallel as on Blazegraph and GraphDB.
See the Databases Overview for the methods shared by all SPARQL connections.
<style>
['pre { line-height: 125%; }\ntd.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }\nspan.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }\ntd.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }\nspan.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }\n.highlight-ipynb .hll { background-color: var(--jp-cell-editor-active-background) }\n.highlight-ipynb { background: var(--jp-cell-editor-background); color: var(--jp-mirror-editor-variable-color) }\n.highlight-ipynb .c { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment */\n.highlight-ipynb .err { color: var(--jp-mirror-editor-error-color) } /* Error */\n.highlight-ipynb .k { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword */\n.highlight-ipynb .o { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator */\n.highlight-ipynb .p { color: var(--jp-mirror-editor-punctuation-color) } /* Punctuation */\n.highlight-ipynb .ch { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Hashbang */\n.highlight-ipynb .cm { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Multiline */\n.highlight-ipynb .cp { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Preproc */\n.highlight-ipynb .cpf { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.PreprocFile */\n.highlight-ipynb .c1 { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Single */\n.highlight-ipynb .cs { color: var(--jp-mirror-editor-comment-color); font-style: italic } /* Comment.Special */\n.highlight-ipynb .kc { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Constant */\n.highlight-ipynb .kd { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Declaration */\n.highlight-ipynb .kn { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Namespace */\n.highlight-ipynb .kp { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Pseudo */\n.highlight-ipynb .kr { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Reserved */\n.highlight-ipynb .kt { color: var(--jp-mirror-editor-keyword-color); font-weight: bold } /* Keyword.Type */\n.highlight-ipynb .m { color: var(--jp-mirror-editor-number-color) } /* Literal.Number */\n.highlight-ipynb .s { color: var(--jp-mirror-editor-string-color) } /* Literal.String */\n.highlight-ipynb .ow { color: var(--jp-mirror-editor-operator-color); font-weight: bold } /* Operator.Word */\n.highlight-ipynb .pm { color: var(--jp-mirror-editor-punctuation-color) } /* Punctuation.Marker */\n.highlight-ipynb .w { color: var(--jp-mirror-editor-variable-color) } /* Text.Whitespace */\n.highlight-ipynb .mb { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Bin */\n.highlight-ipynb .mf { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Float */\n.highlight-ipynb .mh { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Hex */\n.highlight-ipynb .mi { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer */\n.highlight-ipynb .mo { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Oct */\n.highlight-ipynb .sa { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Affix */\n.highlight-ipynb .sb { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Backtick */\n.highlight-ipynb .sc { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Char */\n.highlight-ipynb .dl { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Delimiter */\n.highlight-ipynb .sd { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Doc */\n.highlight-ipynb .s2 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Double */\n.highlight-ipynb .se { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Escape */\n.highlight-ipynb .sh { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Heredoc */\n.highlight-ipynb .si { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Interpol */\n.highlight-ipynb .sx { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Other */\n.highlight-ipynb .sr { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Regex */\n.highlight-ipynb .s1 { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Single */\n.highlight-ipynb .ss { color: var(--jp-mirror-editor-string-color) } /* Literal.String.Symbol */\n.highlight-ipynb .il { color: var(--jp-mirror-editor-number-color) } /* Literal.Number.Integer.Long */']
</style>