public class Student {
//here call from another class where name is student name and score is
GPA
static void use(String name, int score)
{
int userscore=score;
OntModel model=ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
InputStream in =FileManager.get().open("D://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");
// OntClass good=model.getOntClass(ns+ "GoodStudent");
//name is entered by user in text field
Individual indiv = user1.createIndividual(ns + name);
Property prop= model.getProperty(ns,"GPA");
indiv.addLiteral(prop, userscore);
String rule="[rule1:(?x http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://www.semanticweb.org#Student) " +
"( ?x http://www.semanticweb.org#GPA ?score + )" +
"greaterThan(?score, userscore) "+
" -> (?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);
for (Iterator i = inf.listResourcesWithProperty(RDF.type,
"GoodStudent"); i.hasNext();) {
System.out.println("Good student: " + i.next());
}
//saving to file
try (FileOutputStream writer = new
FileOutputStream("D://std.owl")) {
model.write(writer, "RDF/XML");
} catch (IOException ex) {
Logger.getLogger(mystdclass.class.getName()).log(Level.SEVERE,
null, ex);
}
model.write(System.out, "N3");
}
On Tue, Sep 20, 2016 at 12:35 AM, Lorenz B. <
[email protected]> wrote:
> @Javed:
>
> It would be good if you show us the WHOLE CURRENT part of the code and
> some sample data, otherwise it's useless to continue the discussion here.
>
> > On 19/09/16 17:14, javed khan wrote:
> >> Hello Dave, though I have Student class in my owl and GoodStudent as the
> >> subclass of Student but this statement gives me error and does not
> >> recognize GoodStudent.
> >>
> >> for (Iterator i = inf.listResourcesWithProperty(RDF.type,
> >> *GoodStudent*);
> >> i.hasNext();) {
> >> System.out.println("Good student: " + i.next());
> >
> > The code sample I provided worked. I suspect you have omitted some
> > lines such as the one that assigned a value for GoodStudent.
> >
> > Dave
> >
> >>
> >> On Sun, Sep 18, 2016 at 1:36 PM, javed khan <[email protected]>
> >> wrote:
> >>
> >>> Thanks a lot Dave, let me try it. I hope it will help.
> >>>
> >>> Regards
> >>>
> >>> On Sun, Sep 18, 2016 at 1:28 PM, Dave Reynolds
> >>> <[email protected]>
> >>> wrote:
> >>>
> >>>> 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 <
> >>>>> [email protected]> 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");
> >>>>>>>
> >>>>>>> }
> >>>>>>>
> >>>>>>>
> >>>>>>
> >>>>>>
> >>>>>
> >>>>
> >>>
> >>
> >
> >
> --
> Lorenz Bühmann
> AKSW group, University of Leipzig
> Group: http://aksw.org - semantic web research center
>
>