Exploring SPARQL Queries in Gruff - mdebellis/SemanticKG-Design GitHub Wiki
🧠 Exploring SPARQL Queries in Gruff
When you launch Gruff, it opens with a Welcome Screen. Your first step is to switch to the Query View, where you can enter SPARQL queries and interact with your ontology.
➤ Step 1: Open the Query View
Go to:
View > Query View
This opens the area where SPARQL queries can be written and executed.
➤ Step 2: Query Connections Between Einstein and Gödel
Delete the default query shown and paste the following:
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX : <http://michaeldebellis.com/people/>
SELECT *
WHERE {:Albert_Einstein ?p :Kurt_Godel}
✅ Click Run Query (the large button above the query box).
📄 This lists all predicates that connect Einstein to Gödel.
📉 The graph may not be visually interesting at this point, so let’s go further.
➤ Step 3: View All Connections from Einstein
Replace the previous SELECT query with:
SELECT *
WHERE {:Albert_Einstein ?p ?o}
(Keep the PREFIX statements from above.)
✅ Run the query again.
📈 This gives you every node connected to Einstein—much more interesting!
🖼️ To visualize it, click Create Visual Graph (first button above the query results).
💡 Tip: Many anon nodes are Anonymous Classes created by OWL axioms.
➤ Step 4: Filter Out rdf:type Relationships
To avoid clutter from subclass/type links and focus on other relationships, try this:
-
Return to the Query View
View > Query View -
Replace the query with:
SELECT *
WHERE {:Albert_Einstein !rdf:type ?o}
(Again, leave the PREFIX statements as-is.)
📌 This will show all connections except rdf:type, giving a cleaner result for visualization.
🧩 Why this matters:
Even in a small ontology like the People Ontology, the class hierarchy (e.g., Person → Adult → Man and Mammal → Human → Employee) generates many type relationships. Excluding these gives you a clearer view of meaningful links. However, because of the way path operators work, while you can see the results in the results window below the query, you don't have anything that Gruff can create a graph for.
➤ Step 5: List all of Einstein's Social Relations
Now let's examine Einstein's social relations. Remember that :has_Social_Relationship is a super property so it is a great way to see all social relations such as spouse, friend, colleague, etc.
-
Return to the Query View
View > Query View -
Replace the query with:
SELECT *
WHERE {:Albert_Einstein :has_Social_Relationship ?person}
🖼️ Again, to visualize it, click Create Visual Graph
Still not a very interesting graph. Now let's get all of Einstein's social relations and all of their social relations:
-
Return to the Query View
View > Query View -
Replace the query with:
SELECT *
WHERE {:Albert_Einstein :has_Social_Relationship ?person.
?person :has_Social_Relationship ?person1.}
🖼️ Click Create Visual Graph That's a more interesting graph. You can also use Gruff in countless ways to expand this graph. Here is one way:
- Click right on Alan Turing.
- Select the first of the many pop-up menus: Display Linked Nodes from Menu
- Select As Subject (we want to see more triples that have Turing as their subject)
- Select
has social relationship - Select All That should add Alonzo Church and Claude Shannon to the graph.
➤ Step 6: Nodes Connected to Einstein's Social Relations
Now let's look at all the connections to all of Einstein's social relations:
- Go back to:
View > Query View - Replace the query with:
SELECT *
WHERE {:Albert_Einstein :has_Social_Relationship ?person.
?person :has_Social_Relationship ?person1;
?p ?o.}
✅ Run the query again.
🖼️ Click Create Visual Graph
What you get is probably a big mess because there are lots of blank (anon) nodes and also literals (simple data types like strings). To get a somewhat less messy (but still pretty busy) graph:
3. Go back to:
View > Query View
4. Replace the query with:
SELECT *
WHERE {:Albert_Einstein :has_Social_Relationship ?person.
?person :has_Social_Relationship ?person1;
?p ?o.
FILTER (isIRI(?o))}
✅ Run the query again.
🖼️ Click Create Visual Graph
That's still a mess but there is a lot less uninteresting info. The reason is that the isIRI Boolean will remove blanks nodes. Remember FILTER means that only nodes that satisfy the FILTER condition will be shown. Blank nodes are often created when you have axioms for a class and other nested RDF structures. Weeding them out gives a more readable view. SPARQL provides several built-in functions such as isIRI, isLiteral, and isNumeric to test what kind of datatype a SPARQL entity is. These built-in functions are abstract and can usually match many actual datatypes. E.g., isNumeric is true if the datatype is xsd:decimal, xsd:integer, and many others. If you want to test for a specific datatype such as xsd:decimal you use: FILTER(DATATYPE(?value) = xsd:decimal) Now that you are filtering out things that don't add meaning to the graph, select the following from the top level Gruff menu:
Display> Display a recently selected node
That should bring up a big menu with Einstein somewhere near the top.
🖼️ Click on Albert Einstein from the menu.
That makes Einstein the currently selected node, again from the top level menu do:
Layout> Do tree layout from selected node
You should now see a big (but shallow) tree showing all the connections from Einstein that looks something like:
If you wish continue with more SPARQL queries: Exercise 4. SPARQL Queries that Show Interesting Gruff Graphs