You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by javed khan <ja...@gmail.com> on 2016/09/18 15:41:20 UTC
Jena rules not working
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");
}
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
Hello Andy, I used this:
?x URI#GPA ?score + "greaterThan(?score, 3)
?score will return the scores/GPA of students and if it is greater than 3,
it will assign user to GoodStudent class.
On Wed, Sep 21, 2016 at 2:18 AM, Andy Seaborne <an...@apache.org> wrote:
> The rules have some syntax problems:
>
> "( ?x http://www.semanticweb.org#GPA ?score + )"
>
> as mentioned earlier in the thread then
>
> "greaterThan(?score, userscore) "
>
> but userscore is a java variable.
>
> Also
>
> inf.listResourcesWithProperty(RDF.type,
> "GoodStudent")
>
> uses a string not a URI so if not going to find anything.
>
> Andy
>
>
> On 21/09/16 09:21, Lorenz B. wrote:
>
>> Are you sure that this code compiles?
>>
>> Property prop= model.getProperty(ns,"GPA");
>>>
>>
>> ns is a String in your code, the method expects a Resource object is
>> first argument. You totally use the wrong method here, as it returns a
>> Statement.
>>
>> Model::createProperty(String nameSpace, String localName)
>>
>> would be the method to call, or even better as you use an OntModel
>>
>> OntModel::getDatatypeProperty( String uri )
>>
>>
>>
Re: Jena rules not working
Posted by Lorenz Buehmann <bu...@informatik.uni-leipzig.de>.
As Andy said, in the example code of Dave GoodStudent is an object of
type OntClass, not a String object. The problem is mostly your code
quality, I kindly suggest you to use proper Jena objects if possible to
avoid those problems...
On 21.09.2016 12:46, javed khan wrote:
> Hello Andy, shouldn't there be the class name to which we assign new
> individuals.?
> inf.listResourcesWithProperty(RDF.type,
> "GoodStudent")
>
> Dave run this code and it works for him, but I do not know why, it does not
> work for me.
>
> On Wed, Sep 21, 2016 at 2:18 AM, Andy Seaborne <an...@apache.org> wrote:
>
>> The rules have some syntax problems:
>>
>> "( ?x http://www.semanticweb.org#GPA ?score + )"
>>
>> as mentioned earlier in the thread then
>>
>> "greaterThan(?score, userscore) "
>>
>> but userscore is a java variable.
>>
>> Also
>>
>> inf.listResourcesWithProperty(RDF.type,
>> "GoodStudent")
>>
>> uses a string not a URI so if not going to find anything.
>>
>> Andy
>>
>>
>> On 21/09/16 09:21, Lorenz B. wrote:
>>
>>> Are you sure that this code compiles?
>>>
>>> Property prop= model.getProperty(ns,"GPA");
>>> ns is a String in your code, the method expects a Resource object is
>>> first argument. You totally use the wrong method here, as it returns a
>>> Statement.
>>>
>>> Model::createProperty(String nameSpace, String localName)
>>>
>>> would be the method to call, or even better as you use an OntModel
>>>
>>> OntModel::getDatatypeProperty( String uri )
>>>
>>>
>>>
Re: Jena rules not working
Posted by Andy Seaborne <an...@apache.org>.
Classes are identified by URIs, not by strings.
On 21/09/16 11:46, javed khan wrote:
> Hello Andy, shouldn't there be the class name to which we assign new
> individuals.?
> inf.listResourcesWithProperty(RDF.type,
> "GoodStudent")
>
> Dave run this code and it works for him, but I do not know why, it does not
> work for me.
>
> On Wed, Sep 21, 2016 at 2:18 AM, Andy Seaborne <an...@apache.org> wrote:
>
>> The rules have some syntax problems:
>>
>> "( ?x http://www.semanticweb.org#GPA ?score + )"
>>
>> as mentioned earlier in the thread then
>>
>> "greaterThan(?score, userscore) "
>>
>> but userscore is a java variable.
>>
>> Also
>>
>> inf.listResourcesWithProperty(RDF.type,
>> "GoodStudent")
>>
>> uses a string not a URI so if not going to find anything.
>>
>> Andy
>>
>>
>> On 21/09/16 09:21, Lorenz B. wrote:
>>
>>> Are you sure that this code compiles?
>>>
>>> Property prop= model.getProperty(ns,"GPA");
>>>>
>>>
>>> ns is a String in your code, the method expects a Resource object is
>>> first argument. You totally use the wrong method here, as it returns a
>>> Statement.
>>>
>>> Model::createProperty(String nameSpace, String localName)
>>>
>>> would be the method to call, or even better as you use an OntModel
>>>
>>> OntModel::getDatatypeProperty( String uri )
>>>
>>>
>>>
>
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
Hello Andy, shouldn't there be the class name to which we assign new
individuals.?
inf.listResourcesWithProperty(RDF.type,
"GoodStudent")
Dave run this code and it works for him, but I do not know why, it does not
work for me.
On Wed, Sep 21, 2016 at 2:18 AM, Andy Seaborne <an...@apache.org> wrote:
> The rules have some syntax problems:
>
> "( ?x http://www.semanticweb.org#GPA ?score + )"
>
> as mentioned earlier in the thread then
>
> "greaterThan(?score, userscore) "
>
> but userscore is a java variable.
>
> Also
>
> inf.listResourcesWithProperty(RDF.type,
> "GoodStudent")
>
> uses a string not a URI so if not going to find anything.
>
> Andy
>
>
> On 21/09/16 09:21, Lorenz B. wrote:
>
>> Are you sure that this code compiles?
>>
>> Property prop= model.getProperty(ns,"GPA");
>>>
>>
>> ns is a String in your code, the method expects a Resource object is
>> first argument. You totally use the wrong method here, as it returns a
>> Statement.
>>
>> Model::createProperty(String nameSpace, String localName)
>>
>> would be the method to call, or even better as you use an OntModel
>>
>> OntModel::getDatatypeProperty( String uri )
>>
>>
>>
Re: Jena rules not working
Posted by Andy Seaborne <an...@apache.org>.
The rules have some syntax problems:
"( ?x http://www.semanticweb.org#GPA ?score + )"
as mentioned earlier in the thread then
"greaterThan(?score, userscore) "
but userscore is a java variable.
Also
inf.listResourcesWithProperty(RDF.type,
"GoodStudent")
uses a string not a URI so if not going to find anything.
Andy
On 21/09/16 09:21, Lorenz B. wrote:
> Are you sure that this code compiles?
>
>> Property prop= model.getProperty(ns,"GPA");
>
> ns is a String in your code, the method expects a Resource object is
> first argument. You totally use the wrong method here, as it returns a
> Statement.
>
> Model::createProperty(String nameSpace, String localName)
>
> would be the method to call, or even better as you use an OntModel
>
> OntModel::getDatatypeProperty( String uri )
>
>
Re: Jena rules not working
Posted by "Lorenz B." <bu...@informatik.uni-leipzig.de>.
> model.getProperty(ns,"GPA"); I think this works for me and store the data
> property GPA in the file.
>
> Actually I do not need this: Model::createProperty(String nameSpace, String
> localName)
> as I have already created the property in my owl file and just needs
> getProperty() method.
>
> If I use this : OntModel::getDatatypeProperty( String uri ) which property
> (data property) will be returned ? Because the argument only contains the
> namespace and not the actual property.
The argument is the full URI, this is the same as you do to get the OntClass
>
> I tried this but it gives me error:
>
> DataProperty property=ontModel::getDatatypeProperty( String uri, "Score )
This is not Java syntax...
OntModel::getDatatypeProperty( String ur)
means to use the method on the an OntModel object, but indeed with a
dot, not two colons.
And as I said, it has only one argument.
As Andy Seaborne mentioned, your rule is still with illegal syntax,
that's what Dave also mentioned at least twice in previous answers...
>
> On Wed, Sep 21, 2016 at 1:21 AM, Lorenz B. <
> buehmann@informatik.uni-leipzig.de> wrote:
>
>> Are you sure that this code compiles?
>>
>>> Property prop= model.getProperty(ns,"GPA");
>> ns is a String in your code, the method expects a Resource object is
>> first argument. You totally use the wrong method here, as it returns a
>> Statement.
>>
>> Model::createProperty(String nameSpace, String localName)
>>
>> would be the method to call, or even better as you use an OntModel
>>
>> OntModel::getDatatypeProperty( String uri )
>>
>>
>> --
>> Lorenz Bühmann
>> AKSW group, University of Leipzig
>> Group: http://aksw.org - semantic web research center
>>
--
Lorenz Bühmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
model.getProperty(ns,"GPA"); I think this works for me and store the data
property GPA in the file.
Actually I do not need this: Model::createProperty(String nameSpace, String
localName)
as I have already created the property in my owl file and just needs
getProperty() method.
If I use this : OntModel::getDatatypeProperty( String uri ) which property
(data property) will be returned ? Because the argument only contains the
namespace and not the actual property.
I tried this but it gives me error:
DataProperty property=ontModel::getDatatypeProperty( String uri, "Score )
On Wed, Sep 21, 2016 at 1:21 AM, Lorenz B. <
buehmann@informatik.uni-leipzig.de> wrote:
> Are you sure that this code compiles?
>
> > Property prop= model.getProperty(ns,"GPA");
>
> ns is a String in your code, the method expects a Resource object is
> first argument. You totally use the wrong method here, as it returns a
> Statement.
>
> Model::createProperty(String nameSpace, String localName)
>
> would be the method to call, or even better as you use an OntModel
>
> OntModel::getDatatypeProperty( String uri )
>
>
> --
> Lorenz Bühmann
> AKSW group, University of Leipzig
> Group: http://aksw.org - semantic web research center
>
Re: Jena rules not working
Posted by "Lorenz B." <bu...@informatik.uni-leipzig.de>.
Are you sure that this code compiles?
> Property prop= model.getProperty(ns,"GPA");
ns is a String in your code, the method expects a Resource object is
first argument. You totally use the wrong method here, as it returns a
Statement.
Model::createProperty(String nameSpace, String localName)
would be the method to call, or even better as you use an OntModel
OntModel::getDatatypeProperty( String uri )
--
Lorenz B�hmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
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. <
buehmann@informatik.uni-leipzig.de> 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 <ja...@gmail.com>
> >> 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
> >>> <da...@gmail.com>
> >>> 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 <
> >>>>> buehmann@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");
> >>>>>>>
> >>>>>>> }
> >>>>>>>
> >>>>>>>
> >>>>>>
> >>>>>>
> >>>>>
> >>>>
> >>>
> >>
> >
> >
> --
> Lorenz Bühmann
> AKSW group, University of Leipzig
> Group: http://aksw.org - semantic web research center
>
>
Re: Jena rules not working
Posted by "Lorenz B." <bu...@informatik.uni-leipzig.de>.
@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 <ja...@gmail.com>
>> 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
>>> <da...@gmail.com>
>>> 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 <
>>>>> buehmann@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");
>>>>>>>
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>
>>
>
>
--
Lorenz B�hmann
AKSW group, University of Leipzig
Group: http://aksw.org - semantic web research center
Re: Jena rules not working
Posted by Dave Reynolds <da...@gmail.com>.
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 <ja...@gmail.com> 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 <da...@gmail.com>
>> 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 <
>>>> buehmann@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");
>>>>>>
>>>>>> }
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
It does not recognize the class name but only property names.
On Mon, Sep 19, 2016 at 9:14 AM, javed khan <ja...@gmail.com> 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());
>
> On Sun, Sep 18, 2016 at 1:36 PM, javed khan <ja...@gmail.com> 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 <dave.e.reynolds@gmail.com
>> > 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 <
>>>> buehmann@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");
>>>>>>
>>>>>> }
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
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());
On Sun, Sep 18, 2016 at 1:36 PM, javed khan <ja...@gmail.com> 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 <da...@gmail.com>
> 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 <
>>> buehmann@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");
>>>>>
>>>>> }
>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
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 <da...@gmail.com>
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 <
>> buehmann@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");
>>>>
>>>> }
>>>>
>>>>
>>>
>>>
>>
>
Re: Jena rules not working
Posted by Dave Reynolds <da...@gmail.com>.
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 <
> buehmann@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");
>>>
>>> }
>>>
>>
>>
>
Re: Jena rules not working
Posted by javed khan <ja...@gmail.com>.
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.
On Sun, Sep 18, 2016 at 12:40 PM, Lorenz Buehmann <
buehmann@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");
> >
> > }
> >
>
>
Re: Jena rules not working
Posted by Lorenz Buehmann <bu...@informatik.uni-leipzig.de>.
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");
>
> }
>
Re: Jena rules not working
Posted by Dave Reynolds <da...@gmail.com>.
On 18/09/16 16:41, javed khan wrote:
> This code does not work.
You haven't shown us the data and haven't said in what way it doesn't 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.
[snip]
> 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 + )"
That *still" has the spurious + in from your last question.
Furthermore it is highly unlikely you mean the subject of that pattern
to be the Student class, it should be ?x.
A simplified version of your code, which doesn't require any input data is:
public void temp() throws Exception {
OntModel model = ModelFactory.createOntologyModel(
OntModelSpec.OWL_MEM );
String ns = "http://www.semanticweb.org#";
OntClass Student = model.createClass(ns + "Student");
OntClass GoodStudent = model.createClass(ns + "GoodStudent");
Individual indiv = Student.createIndividual(ns + "s1");
Property prop = model.getProperty(ns, "GPA");
indiv.addLiteral(prop, 3);
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 ?marks )"
+ "greaterThan(?marks, 2) "
+ " -> (?x
http://www.w3.org/1999/02/22-rdf-syntax-ns#type
http://www.semanticweb.org#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());
}
}
Which produces the output:
Good student: http://www.semanticweb.org#s1
Dave