I'm not sure what you are doing:
1) SPARQL is the query language for RDF
2) (Jena) Rules do not pull or "filter" anything, they infer new
statements based on existing statements
3) your rule does literally nothing, there is not any variable in the
rule but only existing RDF terms of your ontology. That is equivalent to
just add the triple in the conclusion of the rule to your dataset ...
4) if you only want to have the inferred statements shown, then simply
filter by those that are not in the original data.
But, please adapt your rule first, you need variables for subject and
object that can be bound. Your rule is nothing more that denoting the
symmetry of the hasConcept relation - not that I understand if this
makes sense from your example
Rule syntax is online:
https://jena.apache.org/documentation/inference/index.html#RULEsyntax
Please have a look at the example rules and especially at how variables
are declared.
On 23.07.21 01:05, Brian Le wrote:
Please see below I corrected it I am just trying to write rules to have each
concept come out with an exercise. The one I wrote just pulls everything any
assistance would be greatly appreciated.
Thanks,
Brian
On Jul 22, 2021, at 3:13 PM, Brian Le <[email protected]> wrote:
Hello All,
Apologies If I don't do this right but I am new to java and jena so please bear
with me if I am doing this wrong completely. I just started to get back into
coding after being away for some time. I've made an ontology but I was
wondering If It is possible to use SPARQL to pull in everything and then use a
rule to help filter the data and make it unique. Any advice or help would be
really
String prefixString=
"@prefix uo:
<http://www.semanticweb.org/szhang10/ontologies/2021/5/untitled-ontology-2#>";
String inputFile =
"C:\\Users\\brian\\IdeaProjects\\hello-semantic-web\\src\\model.rdf";
Model model = ModelFactory.createDefaultModel();
InputStream in = FileManager.get().open(inputFile);
if (in == null) {
throw new IllegalArgumentException("Filenot found");
}
model.read(in, "");
String rules = prefixString + "[rule: (uo:DiscreteStructures uo:HasConcept
uo:Recursion)->" +
"(uo:Recursion uo:HasConcept uo:DiscreteStructures)";
Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
InfModel inf = ModelFactory.createInfModel(reasoner, model);
inf.write(System.out,"TURTLE");
The example would come out to be something like |s
|o |
| untitled-ontology-2:Recursion | untitled-ontology-2:Exercise5-2 |
| untitled-ontology-2:Proposition | untitled-ontology-2:Exercise1-1 |
| untitled-ontology-2:Congruence | untitled-ontology-2:Exercise4-2 |
| untitled-ontology-2:Sequence | untitled-ontology-2:Exercise2-2 |
| untitled-ontology-2:Function | untitled-ontology-2:Exercise2-2 |
| untitled-ontology-2:Predicate | untitled-ontology-2:Exercise1-1 |
| untitled-ontology-2:Set | untitled-ontology-2:Exercise2-1 |
| untitled-ontology-2:Induction | untitled-ontology-2:Exercise5-1 |
Some snippet of the ont is <owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Predicate">
<rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Concept"/>
</owl:NamedIndividual>
<owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Exercise1-1">
<rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Exercise"/>
<untitled-ontology-2:isRelatedToConcept
rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Predicate"/>
<untitled-ontology-2:isRelatedToConcept
rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Proposition"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise1-2">
<rdf:type rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise"/>
<untitled-ontology-2:isRelatedToConcept rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Predicate"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise2-1">
<rdf:type rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise"/>
<untitled-ontology-2:isRelatedToConcept rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Set"/>
</owl:NamedIndividual>
in TTL format if needed
http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Structures:Structures
rdf:type owl:NamedIndividual ,:Course ;
:hasConcept :Congruence ,
:Function ,
:Induction ,
:Predicate ,
:Proposition ,
:Recursion ,
:Sequence ,
:Set ;
:hasTopic :BaseBExpansion ,
:TimeComplexity.
Any advice or help would be really appreciated needed any help tips
anything really would be greatly appreciated!
Thanks for everything,
On Thu, Jul 22, 2021 at 12:37 PM Brian Le <[email protected]> wrote:
Hello All,
Apologies If I don't do this right but I am new to java and jena so please bear
with me if I am doing this wrong completely. I just started to get back into
coding after being away for some time. I've made an ontology but I was
wondering If It is possible to use SPARQL to pull in everything and then use a
rule to help filter the data and make it unique. Any advice or help would be
really appreciated
public static String reasoningTest() {
Model model = ModelFactory.createDefaultModel();
String rules = "[rule1:(if possible?)]";
Instant start = Instant.now();
Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
InfModel inf = ModelFactory.createInfModel(reasoner, model);
String queryString = "select ?o (min(?s)) " +
"where { ?s untitled-ontology-2:isRelatedToConcept ?o }" +
" GROUP BY ?o";;
Query query = QueryFactory.create(queryString);
try (QueryExecution qexec = QueryExecutionFactory.create(query, inf)) {
ResultSet results = qexec.execSelect();
for ( ; results.hasNext() ; ) {
QuerySolution soln = results.nextSolution();
System.out.println(soln.toString());
}
}
Instant end = Instant.now();
System.out.println("Sparql query " + queryString + " evaluated in " +
Duration.between(start, end));
}
The example would come out to be something like |s
|o |
| untitled-ontology-2:Recursion | untitled-ontology-2:Exercise5-2 |
| untitled-ontology-2:Proposition | untitled-ontology-2:Exercise1-1 |
| untitled-ontology-2:Congruence | untitled-ontology-2:Exercise4-2 |
| untitled-ontology-2:Sequence | untitled-ontology-2:Exercise2-2 |
| untitled-ontology-2:Function | untitled-ontology-2:Exercise2-2 |
| untitled-ontology-2:Predicate | untitled-ontology-2:Exercise1-1 |
| untitled-ontology-2:Set | untitled-ontology-2:Exercise2-1 |
| untitled-ontology-2:Induction | untitled-ontology-2:Exercise5-1 |
Some snippet of the ont is <owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Predicate">
<rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Concept"/>
</owl:NamedIndividual>
<owl:NamedIndividual
rdf:about="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Exercise1-1">
<rdf:type
rdf:resource="http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Exercise"/>
<untitled-ontology-2:isRelatedToConcept
rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Predicate"/>
<untitled-ontology-2:isRelatedToConcept
rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Proposition"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise1-2">
<rdf:type rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise"/>
<untitled-ontology-2:isRelatedToConcept rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Predicate"/>
</owl:NamedIndividual>
<owl:NamedIndividual rdf:about="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise2-1">
<rdf:type rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Exercise"/>
<untitled-ontology-2:isRelatedToConcept rdf:resource="http://www.semanticweb.org
/ontologies/2021/5/untitled-ontology-2#Set"/>
</owl:NamedIndividual>
in TTL format if needed
http://www.semanticweb.org/ontologies/2021/5/untitled-ontology-2#Structures:Structures
rdf:type owl:NamedIndividual ,:Course ;
:hasConcept :Congruence ,
:Function ,
:Induction ,
:Predicate ,
:Proposition ,
:Recursion ,
:Sequence ,
:Set ;
:hasTopic :BaseBExpansion ,
:TimeComplexity.
Any advice or help would be really appreciated needed any help tips
anything really would be greatly appreciated!
Thanks for everything,