Question

First of all I must apologize since I am very new when it comes to XML, RDF and SPARQL queries. All my knowledge is based on 3 days of much reading so it has not fully sunk in. Nevertheless, a problem I encountered when trying to query a large RDF file using Jena was how to get all instances that are part of a superclass?

Sorry if this is badly worded so here is an example. In my RDF file, there is always a main node:

The Main Node

and then there are objects that have this node as a resource like:

<cim:Connection rdf:ID="connection_1">
    <cim:Object.name>Connection Point 1</cim:Object.name>
    <cim:Connection.node rdf:resource="#main_node_1"/>
</cim:Connection>

or

<cim:Potential rdf:ID="potential_1">
    <cim:Object.name>Equal Potential 1</cim:Object.name>
    <cim:Potential.node rdf:resource="#main_node_1"/>
</cim:Connection>

Now I know how to query for any rdf:type and (a bit) how to graph query using SPARQL. What I want to do is 1st find the MainNode (which I already can) and then find all elements/objetcs/classes/instances that reference to this MainNode.

With my current attempts I can query for a Connection or Potential directly, but not for both. So the query returning all Connections would be:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX cim: <http://iec.ch/TC57/2010/CIM-schema-cim15#>
SELECT ?object
WHERE {
    ?object   cim:Connection.node ?mainNode . 
    ?mainNode cim:Object.name     "The Main Node" . 
}

and for all Potentials the query would be:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX cim: <http://iec.ch/TC57/2010/CIM-schema-cim15#>
SELECT ?object
WHERE {
    ?object   cim:potential.node  ?mainNode . 
    ?mainNode cim:Object.name     "The Main Node" . 
}

Yet I need to extract both and do not want to use UNION since there may be more classes that I have not included. My attempt was to change the ?object line to:

?mainNode cim:*.name "The Main Node" . 

yet the asterisk * does not allow me to make any class available here. In fact, both Connection and Potential could be seen as children of a superclass that contains the variable node.

Was it helpful?

Solution

What I think you're asking for is any node that is one triple-connection away from the main node:

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX cim: <http://iec.ch/TC57/2010/CIM-schema-cim15#>
SELECT ?relationship ?object
WHERE {
  ?object   ?relationship  ?mainNode . 
  ?mainNode cim:Object.name     "The Main Node" . 
}

That query will tell you all of the nodes directly connected to the main node, and the URI of the connecting predicate.

If you only want some of the connections from related nodes to the main node, then you either need to enumerate them in a union, or add a FILTER clause to remove the ones that you don't want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top