Some More Interesting Gruff SPARQL Queries - mdebellis/SemanticKG-Design GitHub Wiki

🧠 SPARQL Queries that result in interesting Gruff graphs

The following queries are designed to give some interesting graphic visualizations. To start, load the People Ontology, run the materializer, and open Gruff. If you need a refresher on how to do that revisit: Exercise 2. All the SPARQL queries can be found in: SPARQL Graph Queries Although, you should also be able to copy and paste the queries from this page.

➤ 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 displaying colleagues and their employers

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 :  <https://michaeldebellis.com/people/>
SELECT  *
WHERE {?person1 :has_Colleague ?person2.
OPTIONAL {?person1 :has_Employer ?employer1}
OPTIONAL {?person2 :has_Employer ?employer2}}

✅ Click Run Query (the large button above the query box).

📉 This query will find all the people who are colleagues and the organizations that they work at (when known).

🖼️ To visualize it, click Create Visual Graph (first button above the query results). If you move some nodes around to bring them closer together your graph should look like: ColleaguesAndEmployers


➤ Step 3: View Containment Relations

Replace the previous SELECT query (as always, be sure to keep the PREFIX statements) with:

SELECT *
WHERE {?place1 :contains ?place2.}

✅ Run the query again.

📈 This gives you what in math is called the "transitive closure" of the contains relation. E.g., the United Kingdom contains London, and Europe (the continent) contains England so because contains is a transitive property the reasoner infers that Europe contains London.

🖼️ To visualize it, click Create Visual Graph (first button above the query results).

💡 Tip: Don't try to make this graph look pretty. Just note that the reasoner has inferred all the containment relations, because containment is a transitive relation. However, it makes for a messy graph and sometimes we don't want to see all the transitive inferences, just the direct relations. We can use the + operator for that.


➤ Step 4: Let's use the + operator and a Filter to get a more useful graph

  1. Return to the Query View
    View > Query View

  2. Replace the query with:

CONSTRUCT {?x :contains ?y .}
WHERE {
  ?x :contains ?y .
  # throw away non-minimal (indirect) edges
  FILTER NOT EXISTS {
    ?x :contains ?m .
    ?m :contains+ ?y .}}

✅ Run the query again.

📌 Now instead of all the inferred relations, you just see the direct containment relations. The contains property is transitive, so when you display it directly in Gruff you see both the explicit links you asserted and all the inferred links the reasoner adds (e.g., if USA contains California, and California contains San_Francisco, Gruff will also show that USA contains San_Francisco). This query extracts only the direct containment edges by removing any ?x contains ?y pair that can be reached through a longer path.

This works using the SPARQL property-path operator +. A path like :contains+ means “one or more steps along contains,” so it matches any multi-step chain such as x → m → y or x → m1 → m2 → y. The query keeps each asserted or inferred pair ?x contains ?y, but filters it out if there exists some ?m such that ?x contains ?m and ?m contains+ ?y. If such an intermediate node exists, the edge is indirect and removed.

The result is a clean set of immediate containment relations, producing a Gruff visualization that matches the intuitive hierarchy—like the one in your screenshot—without turning off reasoning.

✅ Left click on the node for Earth to select it.

📈 With the Earth node selected, do: Layout>Do Tree Layout from Selected Graph

📈 Your graph should look like:

ContainsTreeLayout


There are a couple more queries in that file that are left for the reader. They illustrate how ontology definitions (in this case properties and their domains) are also part of the graph.