{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Blazegraph Database\n", "\n", "[Blazegraph DB](https://blazegraph.com/) is an open-source triplestore database supporting Blueprints and RDF/SPARQL APIs. It supports up to 50 Billion edges on a single machine and has since been commercialized as [AWS Neptune](https://aws.amazon.com/neptune/)\n", "\n", "Docker images preloaded with IEEE test feeders are available from https://hub.docker.com/r/gridappsd/blazegraph/tags.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## About Triplestore Databases" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The triple-store database offers a semantic solution for data management. Unlike the relational database solution (which requires a DDL database schema), the triple-store database structure is comprised of resource descriptive framework (RDF) statements. These RDF statements take the form of subject (node), predicate (relation), object (node) that can be dynamically generated to form inter-related complex class structures. RDF Schema (RDFS) supports polymorphism concepts, provides a means to create graph constructs, and supports the concept of subgraphs so that partitioning of the graph space is possible. RDF can be specified in a variety of ways including XML, TTL, and JSON-LD. Other languages, such as the Web Ontology Language (OWL), can be used to offer more sophisticated constraints and additional constructs for reasoning and inferencing.\n", "\n", "The RDF statement structure intuitively corresponds to the structure of object-attribute specifications used in CIM (e.g. `ACLineSegment` (subject) has an attribute `length` (predicate) with value `105 meters` (object)). The CIM supports translation of data structures directly into RDF with the ability to automate correlation of data. The CIMTool software supports translation of a CIM profile into RDFS and OWL. After ingesting the power system network model data and the RDFS / OWL structures from the data profile, the user can use a triple-store databased to correlate well-defined CIM structures with newly generated data automatically. \n", "\n", "The main advantage of using a triple-store database is that it directly supports CIM development. It is highly agile and supports dynamic data structures generated by multiple developers. It is also supported by a standardized, mature language for specifying directed graph and hierarchical class structures as well as a standardized way to manage RDF data. RDF supports both type-checking and the Shapes Constraints Language (SHACL) for structure validation. The main disadvantage of triple-store is the risk for garbage-in-garbage-out (GIGO), meaning that without a well-thought-out strategy to support data management activities, maintenance can be difficult. Likewise, without a certain rigor for data contributors, dangling references or conflicts can appear in the database." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Blazegraph Environment Variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If using in conjunction with GridAPPS-D or if using one of the docker images from the GridAPPS-D dockerhub, the following default variables are recommended." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For GridAPPS-D / Blazegraph tags between `v2021.01.0` and `v2024.09.0`, the following environment variables are recommended:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['CIMG_CIM_PROFILE'] = 'rc4_2021'\n", "os.environ['CIMG_URL'] = 'http://localhost:8889/bigdata/namespace/kb/sparql'\n", "os.environ['CIMG_IEC61970_301'] = '7'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For GridAPPS-D / Blazegraph tags `v2025.01.0` and later, the following environment variables are recommended:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ['CIMG_CIM_PROFILE'] = 'cimhub_2023'\n", "os.environ['CIMG_URL'] = 'http://localhost:8889/bigdata/namespace/kb/sparql'\n", "os.environ['CIMG_IEC61970_301'] = '8'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The BlazegraphConnection class then uses the .get_[var_name] methods to retrieve the environment variables" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from cimgraph.databases import get_cim_profile\n", "cim_profile, cim = get_cim_profile()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Blazegraph Database Connection" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `BlazegraphConnection` class provides the interface to the database and all core methods needed to query and update the database. It is a specialization of the abstract ConnectionInterface class in CIM-Graph.\n", "\n", "The class can be imported from the `cimgraph.databases` module as shown below" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "# Import class from cimgraph databases module\n", "from cimgraph.databases import BlazegraphConnection\n", "# Create a new connection to the database\n", "database = BlazegraphConnection()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## API Reference\n", "\n", "The `BlazegraphConnection` class offers the following methods:\n", "\n", "### Public Methods for Users\n", "\n", "The following methods are intended for users who wish to access and retrieve objects prior to creating a GraphModel representation of the full power system.\n", "\n", "* `.get_object()` -- Retrieves a CIM object based on its mRID\n", "\n", "* `.get_from_triple` -- Retrieves a list of objects/strings based on an RDF triple\n", "\n", "\n", "\n", "Internal methods for database access and queries:\n", "\n", "* `.connect()` -- Connects to database endpoint\n", "\n", "* `.execute()` -- Executes SPARQL query\n", "\n", "* `.update()` -- Runs update statement such as drop all\n", "\n", "* `.disconnect()` -- Disconnects from database endpoint\n", "\n", "* `.create_new_graph()` -- Creates network graph from an EquipmentContainer object\n", "\n", "* `.create_distributed_graph()` -- Creates graph of a switch-delimited topological area\n", "\n", "* `.get_all_edges()` -- Parallel execution of get_all_edges queries\n", "\n", "* `.get_all_attributes()` -- Runs get_all_edges without creating new objects\n", "\n", "* `.parse_node_query()` -- Processes query response for .create_new_graph\n", "\n", "* `.edge_query_parser()` -- Processes query response for .get_all_edges" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---- " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_object()\n", "\n", "The `get_object()` method is used to retrieve an object from the Blazegraph database using its mRID. \n", "\n", "The arguments are\n", "\n", "* `mRID` (str): The mRID of the object to be retrieved.\n", "\n", "* `graph` (dict[type, uuid]): Optional -- An existing graph to which the object should be added\n", "\n", "The return values are\n", "\n", "* `object`: A CIM object of the correct type with the requested mRID.\n", "\n", "* `graph`: A graph dictionary with the object added" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Example 1\n", "\n", "The example below shows how to retrieve a Feeder object using its mRID:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\n", " \"@id\": \"49ad8e07-3bf9-a4e2-cb8f-c3722f837b62\",\n", " \"@type\": \"Feeder\"\n", "}\n" ] } ], "source": [ "feeder = database.get_object(mRID=\"49AD8E07-3BF9-A4E2-CB8F-C3722F837B62\") #ieee 13 bus\n", "feeder.pprint()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_from_triple()\n", "\n", "The `.get_from_triple()` method is used to retrieve the object completing a subject-predict-object triple based on a source object and the full CIM property string. \n", "\n", "The arguments are\n", "\n", "* `subject` (object): A CIM object instance created using CIM-Graph\n", "\n", "* `predicate` (str): A CIM RDF property string, such as `IdentifiedObject.name`\n", "\n", "The return value is a list of the retrieved object(s) or property value(s).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Example 1\n", "\n", "The first example below shows how to retrieve the name of the feeder retrieved in the previous example. As `name` is a string attribute, the return value is a list of string." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['ieee13nodeckt']\n" ] } ], "source": [ "name = database.get_from_triple(subject=feeder, predicate='IdentifiedObject.name')\n", "print(name)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Example 2\n", "\n", "The second example below shows how to retrieve the substation associated with the same feeder. In this case, the return value is a list of the dataclass instance of type `cim.Substation`." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[{\"@id\": \"6c62c905-6fc7-653d-9f1e-1340f974a587\", \"@type\": \"Substation\"}]\n" ] } ], "source": [ "substation = database.get_from_triple(subject=feeder, predicate='Feeder.NormalEnergizingSubstation')\n", "print(substation)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### connect()\n", "\n", "When the `BlazegraphConnection` object is instantiated, it will automatically attempt to connect to the database. The `.connect()` creates a new SPARQL wrapper for executing queries using the specified database url.\n", "\n", "It does not take any input arguments or return any values:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "database.connect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### execute()\n", "\n", "The `.execute()` method is used to execute all SPARQL queries developed by the user or auto-generated by CIM-Graph. The input argument is a text string with the full query. Addition single `\\` or double `\\\\` escape characters are typically needed for all special characters (e.g. quotation marks) in the query text.\n", "\n", "The input argument is \n", "\n", "* `query_message`(str): A string with the SPARQL query text\n", "\n", "The output argument is a python dictionary with the column headers contained in `query_output['head']['vars']` and the row values in `query_output['results']['bindings']`.\n", "\n", "\n", "##### Example 1:\n", "\n", "The example below shows a simple SPARQL query and the query response" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'head': {'vars': ['identifier', 'obj_class']},\n", " 'results': {'bindings': [{'identifier': {'type': 'literal',\n", " 'value': '49AD8E07-3BF9-A4E2-CB8F-C3722F837B62'},\n", " 'obj_class': {'type': 'literal', 'value': 'Feeder'}}]}}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "query_text = '''\n", "PREFIX r: \n", "PREFIX cim: \n", "SELECT DISTINCT ?identifier ?obj_class\n", "WHERE {\n", "VALUES ?identifier {\"49AD8E07-3BF9-A4E2-CB8F-C3722F837B62\"}\n", "bind(iri(concat(\"urn:uuid:\", ?identifier)) as ?eq)\n", "?eq a ?classraw.\n", "bind(strafter(str(?classraw),\"http://iec.ch/TC57/CIM100#\") as ?obj_class)\n", "}\n", "ORDER by ?identifier\n", "'''\n", "\n", "query_response = database.execute(query_text)\n", "query_response" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A simple example of how to parse the query results is shown below" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['identifier', 'obj_class']\n", "49AD8E07-3BF9-A4E2-CB8F-C3722F837B62 Feeder\n" ] } ], "source": [ "headers = query_response['head']['vars']\n", "print(headers)\n", "for row in query_response['results']['bindings']:\n", " value0 = row[headers[0]]['value']\n", " value1 = row[headers[1]]['value']\n", " print(value0, value1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### update()\n", "\n", "The `.update()` method executes an blazegraph update message, such as `drop all`. It is also invoked by the `.upload()` method for adding new CIM objects to the database.\n", "\n", "The arguments of the method are\n", "\n", "* `update_message` (string): The SPARQL message or database routine\n", "\n", "The method returns a string with the status message from the database\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### create_new_graph()\n", "\n", "The `.create_new_graph()` method creates a new graph structure for a CIM EquipmentContainer object. The method uses a SPARQL query to obtain all terminals in the graph, along with all nodes and conducting equipment associated with each terminal. This forms the baseline knowledge graph for the GraphModel. If a graph is specified, the new objects will be added to the existing graph. Otherwise, a new graph will be created from scratch.\n", "\n", "The arguments of the method are\n", "\n", "* container (object): The container object for which the graph is created.\n", "\n", "* graph (dict, optional): Graph of CIM objects, grouped by class and UUID.\n", "\n", "The method returns a Graph dict consisting of types and UUID mapped to object instances.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### create_distributed_graph()\n", "\n", "The `.create_distributed_graph()` method creates a new graph structure for a CIM EquipmentContainer object. The method uses a SPARQL query to obtain all terminals in the graph, along with all nodes and conducting equipment associated with each terminal. This forms the baseline knowledge graph for the GraphModel. If a graph is specified, the new objects will be added to the existing graph. Otherwise, a new graph will be created from scratch.\n", "\n", "The arguments of the method are\n", "\n", "* container (object): The container object for which the graph is created.\n", "\n", "* graph (dict, optional): Graph of CIM objects, grouped by class and UUID.\n", "\n", "The method returns a Graph dict consisting of types and UUID mapped to object instances.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_all_edges()\n", "\n", "The `.get_all_edges()` method is the core library method that enables the flexibility of CIM-Graph to query for CIM objects of any class and build the knowledge graph without custom queries.\n", "\n", "The arguments of the method are\n", "\n", "* graph (dict): Graph of CIM objects, grouped by class and UUID.\n", "\n", "* cim_class (type): The CIM class for which to retrieve edges (e.g. `cim.ACLineSegment`)\n", "\n", "The method does not return any values.\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "database.get_all_edges(graph, cim.Feeder)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_edges_query()\n", "\n", "This is a debugging method that can be used to obtain the query text passed to the database endpoint. The query text can then be copied and pasted into the database GUI for error-checking.\n", "\n", "The arguments of the method are\n", "\n", "* graph (dict): Graph of CIM objects, grouped by class and UUID.\n", "\n", "* cim_class (type): The CIM class for which to retrieve edges (e.g. `cim.ACLineSegment`)\n", "\n", "The method returns a string with the SPARQL query text." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Example 1\n", "\n", "The example below shows how the SPARQL string can be retrieved for debugging. The text can then be copied and pasted into the Blazegraph GUI query window." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " PREFIX r: \n", " PREFIX cim: \n", " SELECT DISTINCT ?identifier ?attribute ?value ?edge\n", " WHERE {\n", " \n", " VALUES ?identifier { \"49AD8E07-3BF9-A4E2-CB8F-C3722F837B62\" \n", " }\n", " bind(iri(concat(\"urn:uuid:\", ?identifier)) as ?eq)\n", "\n", " ?eq r:type cim:Feeder.\n", "\n", " {?eq (cim:|!cim:) ?val.\n", " ?eq ?attr ?val.}\n", " UNION\n", " {?val (cim:|!cim:) ?eq.\n", " ?val ?attr ?eq.}\n", "\n", " {bind(strafter(str(?attr),\"#\") as ?attribute)}\n", " {bind(strafter(str(?val),\"urn:uuid:\") as ?uri)}\n", " {bind(if(?uri = \"\", ?val, ?uri) as ?value)}\n", "\n", " OPTIONAL {?val a ?classraw.\n", " bind(strafter(str(?classraw),\"http://iec.ch/TC57/CIM100#\") as ?edge_class)\n", " {bind(strafter(str(?val),\"urn:uuid:\") as ?uri)}\n", "\n", " bind(concat(\"{\\\"@id\\\":\\\"\", ?uri,\"\\\",\\\"@type\\\":\\\"\", ?edge_class, \"\\\"}\") as ?edge)}\n", " }\n", "\n", " ORDER by ?identifier ?attribute\n", " \n" ] } ], "source": [ "query_message = database.get_edges_query(graph, cim.Feeder)\n", "print(query_message)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### edge_query_parser()\n", "\n", "The `.edge_query_parser()` method is used to convert JSON-LD formatted query responses into a set of Equipment, ConnectivityNode, Terminal, and Measurement objects.\n", "\n", "The arguments of the method are\n", "\n", "* `graph` (dict, optional): Graph of CIM objects, grouped by class and UUID.\n", "\n", "* `query_output` (dict): JSON-LD formatted query output to be parsed\n", "\n", "* cim_class (type): The CIM class for which edges are to be created (e.g. `cim.ACLineSegment`)\n", "\n", "* `expand_graph` (bool): Used by get_all_attributes so as not to expand graph with more nodes, defaults to True\n", "\n", "The method returns an updated graph dictionary\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## UML Sequence Diagrams\n", "\n", "This section contains UML sequence diagrams explaining how the CIMantic Graphs library executes database queries and API calls. The UML diagrams on this page are rendered from flat-text using mermaid.js, which can be imported using" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "from mermaid import Mermaid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_object()\n", "\n", "A UML sequence diagram depicting how the BlazegraphConnection `get_object()` method retrieves an object using its mRID is shown below" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_get_object.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_triple()\n", "\n", "A UML sequence diagram depicting how the BlazegraphConnection `get_from_triple()` method retrieves an object using the RDF triple is shown below" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_get_from_triple.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### connect()\n", "\n", "A UML sequence diagram for how the BlazegraphConnection class is initialized and connects to the database is shown below" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_connect.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### execute()\n", "\n", "The diagram below illustrates how a block of SPARQL query text is executed:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from mermaid import Mermaid\n", "with open('./images/3_3_execute.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### update()\n", "\n", "The diagram below illustrates how a SPARQL update statement is executed " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_update.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### create_new_graph()\n", "\n", "The `.create_new_graph()` method is invoked when the user creates a new graph model, as shown in the UML sequence diagram below" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_create_new_graph.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### create_distributed_graph()\n", "\n", "The `.create_distributed_graph()` method is invoked when the user creates a new graph model with the `distributed` flag set to True, as shown in the UML sequence diagram below" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_create_distributed_graph.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_all_edges()\n", "\n", "The UML sequence diagram below shows how the `.get_all_edges()` method is invoked by the higher-level GraphModel method of the same name by a user.\n", "\n", "For improved performance, CIM-Graph uses parallel processing with sets of 100 objects queried for in each batch. The execution workflow is shown below" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_get_all_edges.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### get_edges_query()\n", "\n", "The diagram below illustrates how a SPARQL query is retrieved and returned to the user for debugging " ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_get_edges_query.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "-----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### parse_node_query()\n", "\n", "The `.parse_node_query()` method is invoked as part of initialization of a new GraphModel as shown below" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_parse_node_query.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### edge_query_parser()\n", "\n", "The `.edge_query_parser()` method is used to convert JSON-LD formatted query responses into a set of Equipment, ConnectivityNode, Terminal, and Measurement objects. The method is invoked as part of the get_all_edges() and get_all_attributes() methods" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "with open('./images/3_3_parse_edges_query.txt', 'r') as diagram:\n", " diagram_text = diagram.read()\n", "Mermaid(diagram_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Blazegraph Connection Parameters (Deprecated)\n", "\n", "Older versions of CIM-Graph used the `ConnectionParameters` class to authenticate with database as shown below. This method is now deprecated and will throw an error." ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "ConnectionParameters class is deprecated and will be deleted in a future release\n", "Set environment variables for required authentication\n" ] }, { "ename": "TypeError", "evalue": "BlazegraphConnection.__init__() takes 1 positional argument but 2 were given", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[25], line 6\u001b[0m\n\u001b[1;32m 3\u001b[0m params \u001b[38;5;241m=\u001b[39m ConnectionParameters(url \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mhttp://localhost:8889/bigdata/namespace/kb/sparql\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 4\u001b[0m cim_profile\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mrc4_2021\u001b[39m\u001b[38;5;124m'\u001b[39m, iec61970_301\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m8\u001b[39m)\n\u001b[1;32m 5\u001b[0m \u001b[38;5;66;03m# Create database connection object\u001b[39;00m\n\u001b[0;32m----> 6\u001b[0m blazegraph \u001b[38;5;241m=\u001b[39m \u001b[43mBlazegraphConnection\u001b[49m\u001b[43m(\u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mTypeError\u001b[0m: BlazegraphConnection.__init__() takes 1 positional argument but 2 were given" ] } ], "source": [ "from cimgraph.databases import ConnectionParameters\n", "# Create connection parameters\n", "params = ConnectionParameters(url = \"http://localhost:8889/bigdata/namespace/kb/sparql\",\n", " cim_profile='rc4_2021', iec61970_301=8)\n", "# Create database connection object\n", "blazegraph = BlazegraphConnection(params)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "----" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 2 }