GraphDB Database¶
GraphDB is an RDF graph database (triplestore) developed by Ontotext, with a SPARQL 1.1 endpoint for each repository. The Free edition runs without a license.
CIMantic Graphs provides the GraphDBConnection class for reading and updating CIM models in a GraphDB repository. Like Blazegraph, it is a SPARQLEndpointConnection, so it has the same methods and queries.
Installation and Setup¶
Python Dependencies¶
GraphDBConnection needs the SPARQLWrapper library, from the sparql extra:
Local Instance (docker compose)¶
The repository's docker-compose.yml runs GraphDB 10.8 Free. From a CIM-Graph checkout:
docker compose up -d --wait graphdb
uv run python tests/seed_databases.py graphdb # loads tests/test_models/ieee13.xml
The seed script creates the repository powergrid_models with the empty ruleset (no inference), so queries see only the triples in the file. Its SPARQL endpoint is http://localhost:7200/repositories/powergrid_models, and the GraphDB Workbench is at http://localhost:7200. The container has no volume, so docker compose down discards the data.
Environment Configuration¶
A connection reads its settings from CIMG_* environment variables when it is created (see Environment Variables). CIMG_URL is the repository endpoint, http://<host>:7200/repositories/<repository>. For the docker compose instance:
import os
os.environ['CIMG_CIM_PROFILE'] = 'cimhub_2023'
os.environ['CIMG_URL'] = 'http://localhost:7200/repositories/powergrid_models'
os.environ['CIMG_IEC61970_552'] = '552-NEW'
import cimprofiles.cim17.cimhub_2023 as cim
Creating a Connection¶
from cimgraph.databases import GraphDBConnection
db = GraphDBConnection()
The SPARQLWrapper client is created on the first query. Queries are sent by POST to CIMG_URL, and SPARQL updates to CIMG_URL/statements.
Usage Examples¶
Example 1: Retrieving an Object by mRID¶
feeder = db.get_object(mRID="49AD8E07-3BF9-A4E2-CB8F-C3722F837B62")
feeder.pprint()
{
"@id": "49ad8e07-3bf9-a4e2-cb8f-c3722f837b62",
"@type": "Feeder"
}
Example 2: Loading a Feeder Model¶
from cimgraph.models import FeederModel
network = FeederModel(container=feeder, connection=db)
print(f"Loaded {len(network.graph[cim.ACLineSegment])} line segments")
Loaded 11 line segments
Example 3: Expanding the Graph¶
get_all_edges() adds the attributes and associated objects of every object of a class. Unit values are CIMUnit objects in SI base units; convert them with .to():
network.get_all_edges(cim.ACLineSegment)
line = network.first(cim.ACLineSegment)
print(f"Line {line.name}: length {line.length.to('m')} m ({line.length.to('ft'):.0f} ft)")
Line 645646: length 91.44 m (300 ft)
Example 4: Querying Object Attributes¶
name = db.get_from_triple(subject=feeder, predicate='IdentifiedObject.name')
print(f"Feeder name: {name}")
substation = db.get_from_triple(subject=feeder, predicate='Feeder.NormalEnergizingSubstation')
print(f"Substation: {substation}")
Feeder name: ['ieee13nodeckt']
Substation: [{"@id": "6c62c905-6fc7-653d-9f1e-1340f974a587", "@type": "Substation"}]
Example 5: Executing a Custom SPARQL Query¶
execute() returns the SPARQL JSON results as a dict:
query_text = '''
PREFIX r: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX cim: <http://iec.ch/TC57/CIM100#>
SELECT ?name (COUNT(?line) AS ?lines)
WHERE {
?line r:type cim:ACLineSegment ;
cim:Equipment.EquipmentContainer ?feeder .
?feeder cim:IdentifiedObject.name ?name .
}
GROUP BY ?name
'''
for result in db.execute(query_text)['results']['bindings']:
print(f"{result['name']['value']}: {result['lines']['value']} line segments")
ieee13nodeckt: 11 line segments
The Blazegraph page describes the rest of the SPARQLEndpointConnection methods, the query batching and the sequence diagrams, which apply to GraphDB as well.
<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>