You could use the CONSTRUCT query form as rules and augment your model with the constructed triples. Something like this (untested):
PREFIX covidepid: <> PREFIX foaf: <http://xmlns.com/foaf/0.1/> CONSTRUCT { ?person a covidepid:YoungestPerson . } WHERE { SELECT ?house ?person ?lowestAge WHERE { ?person foaf:age ?lowestAge ; covidepid:livesIn ?house { SELECT ?house (MIN(?age) AS ?lowestAge) WHERE { ?person foaf:age ?age ; covidepid:livesIn ?house } GROUP BY ?house } } } Fix the covidepid: namespaces before use. Execute using QueryExecution::execConstruct: https://jena.apache.org/documentation/javadoc/arq/org/apache/jena/query/QueryExecution.html#execConstruct(org.apache.jena.rdf.model.Model) On Sun, Dec 5, 2021 at 2:24 PM Jakub Jałowiec <j.jalow...@student.uw.edu.pl> wrote: > > Thanks, that solves the problem and I'll stick to it for now. > Nonetheless, is it possible to automatically infer being an instance of the > hypothetical class "YoungestPerson" ("the person with the lowest foaf:age > aggregate by house") in Apache Jena as described above? Ideally, I would > prefer to separate my conceptual/declarative model from raw data > manipulation using SPARQL. I am new to RDF & ontologies and I am not sure > to what extent keeping those two is possible and if it is worth to actually > invest a lot of time into that. > > Best regards, > Jakub > > niedz., 5 gru 2021 o 10:12 Lorenz Buehmann < > buehm...@informatik.uni-leipzig.de> napisał(a): > > > Hi, > > > > > > the common pattern in SPARQL is to get the aggregated value in an inner > > query first, then in the outer query get the entity with the aggregated > > value: > > > > SELECT ?house ?person ?lowestAge { > > ?person foaf:age ?lowestAge . > > ?person covidepid:livesIn ?house . > > > > > > {SELECT ?house (min(?age) as ?lowestAge) > > WHERE { > > ?person foaf:age ?age . > > ?person covidepid:livesIn ?house . > > } > > GROUP BY ?house} > > } > > > > > > > > On 03.12.21 02:16, Jakub Jałowiec wrote: > > > Hi, > > > I would appreciate any help with the following problem. I have a bunch of > > > (foaf:Persons, myOntology:livesIn, myOntology:Place) triples. I am trying > > > to find the youngest person in each myOntology:Place (i.e. the person > > with > > > the earliest value of foaf:age for each myOntology:Place). > > > What I've tried so far were: > > > - OWL complex classes (Class Expression Syntax (protegeproject.github.io > > ) > > > <http://protegeproject.github.io/protege/class-expression-syntax/>) - > > per > > > my understanding they have too weak expressivity to express aggregates > > > among other individuals associated with them > > > - SPARQL query - something along those lines would work fine but I do not > > > know how to retrieve the IRI of the youngest person: > > > > > >> SELECT ?house (min(?age) as ?lowestAge) > > >> WHERE { > > >> ?person foaf:age ?age . > > >> ?person covidepid:livesIn ?house . > > >> } > > >> GROUP BY ?house > > > > > > I am curious if extraction of the lowest foaf:age value among a group of > > > people could be achieved using Apache Jena rules. From the documentation > > ( > > > https://jena.apache.org/documentation/inference/#rules) it seems to me > > that > > > the closest it gets to it is to write my custom built-in function that > > > would do exactly that. Is that correct? > > > > > > Best regards, > > > Jakub > > > > >