Extending the Solution

We’re extending Stardog’s knowledge graph capabilities to include arbitrary graph algorithms that aren’t easily expressed in SPARQL. But first we have to fix SPARQL solutions.


This is a companion discussion topic for the original entry at https://blog.stardog.com/extending-the-solution/

This is fantastic. I’ve been trying to think of a way to do something like that but haven’t found a very elegant solution. I’ve never been a big fan of JSON-LD framing and this sounds like a great alternative. What might a recursive query look like? How would the bottom up evaluation of SPARQL queries work with recursion? It always feels like the recursion wants to be top down but SPARQL is bottom up. I’m probably missing something and it’s just not that complicated or doesn’t matter.

SELECT * {
?person :date ?bdate .
{ SELECT ?person (array(?child, ?date, {SELECT
(array(?favoriteIceCream) AS ?favoriteIceCream) {
?child :favoriteIceCream
}
GROUP BY ?child }) AS ?children) {
?person :hasChild ?child .
?child :birthDate ?date
}
GROUP BY ?person
}
}

Quick question about the following example

Query:

SELECT * {
   ?person :date ?bdate .
   { SELECT ?person (array(?child, ?date) AS ?children) {
       ?person :hasChild ?child .
       ?child :birthDate ?date 
     }
     GROUP BY ?person
   }
}

Result:

[
  {"person": ":John" ,
   "date": "1974-02-17" ,
   "children": [
        {"child": ":Alice", date: "1995-05-02"},
        {"child": ":Bob", date: "1999-09-22"},
        {"child": ":Charlie", date: "1999-09-22"}
   ]
  },     
  {"person": ":Jane" ,
   "bdate": "1963-11-03" ,
   "children": [
        {"child": ":David", date: "1992-08-12"},
        {"child": ":Eve", date: "1996-06-02"}
   ]
  }
]

If you allowed that it seems like it would “auto promote” to an array if multiple bindings were returned and an explicit query would be something like

SELECT array(?person, ?date, ?children) {
   ?person :date ?bdate .
   { SELECT ?person (array(?child, ?date) AS ?children) {
       ?person :hasChild ?child .
       ?child :birthDate ?date 
     }
     GROUP BY ?person
   }
}

but you’d have to leave off the AS or require it and ignore it?

Hi Zachary,

In this extension, SPARQL queries still returns a solution sequence so that is the array we show in the example. That array comes for free even in regular SPARQL/JSON results format (right after the “bindings” keyword). In that syntax the example array we show would be wrapped inside other things as in:

{
  "head" : {
    "vars" : [
      "person",
      "bdate",
      "children"
    ]
  },
  "results" : {
    "bindings" : [
      ....
    ]
  }
}

We have not yet finalized how the results with extended solutions would be serialized in SPARQL/XML or SPARQL/JSON but we’d probably have a compatibility mode that reuses those results formats as much as possible.

Best,
Evren

How about using negation for finding the oldest child?

select ?person ?child where {
	
	?person :hasChild ?child .
    ?child :birthDate ?date .

    MINUS {

    	# subtract all bindings for ?person, ?child, ?date where there exists another child that is older.

    	?person :hasChild ?child, ?otherChild.

    	?child :birthDate ?date .
    	?otherChild :birthDate ?otherChildDate .

    	FILTER(?date < ?otherChildDate)

    }

}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.