On 18/09/16 21:24, javed khan wrote:
Thanks Lorenz and Dave, I have corrected the  http://www.semanticweb.org#
<http://www.semanticweb.org/#GPA>Student to ?x but it does not
work.Actually it does not save the updated marks/GPA of student and when I
remove the Jena rules part, it then updates and save the GPA.
I also try the SPARQL query inside Protege Query tab but it does not give
me any instance of the GoodStudent class.


The example I showed does work. If you write inf to file or std out you can see the class, if you run your sparql query it lists s1.

Dave

On Sun, Sep 18, 2016 at 12:40 PM, Lorenz Buehmann <
buehm...@informatik.uni-leipzig.de> wrote:

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");

    }





Reply via email to