The second condition of your rule doesn't make any sense as the subject
is Student and it should be ?x.
On 18.09.2016 17:41, javed khan wrote:
> This code does not work. I want to save student marks/GPA in the file and
> based on GPA assign students to GoodStudent or WorstStudents sub classes of
> Student via Jena rules.
>
>
>
>
> OntModel model=ModelFactory.createOntologyModel();
>
> InputStream in =FileManager.get().open("C://std.owl");
> if (in==null) {
> throw new IllegalArgumentException( "File: " + " not
> found");
> } model.read(in,"");
>
> String ns="http://www.semanticweb.org#";
>
> OntClass user1 = model.getOntClass(ns + "Student");
>
> Individual indiv = user1.createIndividual(ns + name); //name is
> variable
>
> Property prop= model.getProperty(ns,"GPA");
>
> indiv.addLiteral(prop, marks); //marks also variable having
> some value i-e 3.0
>
>
>
> String rule="[rule1:(?x http://www.w3.org/1999/02/22-rdf-syntax-ns#type
> http://www.semanticweb.org#Student) " +
> "( http://www.semanticweb.org#Student
> http://www.semanticweb.org#GPA ?marks + )" + "greaterThan(?marks, 2) "+
> " -> (?x http://www.w3.org/1999/02/22-rdf-syntax-ns#type
> http://www.semanticweb.org#GoodStudent )]";
>
> String queryString= "PREFIX std:<http://www.semanticweb.org#>
> "+
> "PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#> " +
> "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#> "+
> "SELECT * " +
> " WHERE { ?x rdf:type std:GoodStudent}";
>
> Reasoner reasoner2 = new
> GenericRuleReasoner(Rule.parseRules(rule));
> InfModel inf = ModelFactory.createInfModel(reasoner2, model);
> Query query = QueryFactory.create(queryString);
> QueryExecution qe = QueryExecutionFactory.create(query, inf);
> ResultSet results = qe.execSelect();
> ResultSetFormatter.out(System.out, results, query);
> qe.close();
>
>
>
> try (FileOutputStream writer = new
> FileOutputStream("C://std.owl")) {
> model.write(writer, "RDF/XML");
> } catch (IOException ex) {
> Logger.getLogger(stdinfo.class.getName()).log(Level.SEVERE,
> null, ex);
> }
> model.write(System.out, "N3");
>
> }
>