Yes you are right. I think the rule is Ok, Jena code is Ok but the problem is in the ontology part i-e data property/object property.
Let me choose BestCategory as object property and will make its domain User class and range as class Category. Category class have three members, that is catPhysics, catChemistry and catGeography. I will let you know the outcome. Thanks again Dave, Kind regards On Sat, Jan 14, 2017 at 9:29 PM, Dave Reynolds <[email protected]> wrote: > On 14/01/17 18:22, Sidra shah wrote: > >> Thank you Dave for such a detailed reply. Actually you got my problem, but >> I have taken BestCategory as data property and the problem which bothered >> me is I want the user BestCategory as a data or object property. >> The main idea behind this is I want the value of this property for further >> processing. >> For instance, I want if BestCategory==Physics >> Then do something. >> >> Should I take BestCategory as Object property? Do you suggest it.? >> > > In OWL a property can only be a DatatypeProperty or an ObjectProperty but > not both. So you have to choose. > > In general in OWL modelling it is best to use symbols (i.e. URIs) for > concepts like "Physics" and so BestCategory (or bestCategory) should be an > ObjectProperty. > > You are completely free choose a different style and decide you will use > strings and so make bestCategory an DatatypeProperty. > > Just be consistent in how you declare it and how you use it. > > And none of this has anything to do with Jena or with Jena rules! > > Dave > > >> Kind regards >> >> On Sat, Jan 14, 2017 at 9:08 PM, Dave Reynolds <[email protected] >> > >> wrote: >> >> Hi Sidra, >>> >>> You have tried to explain your problem but the example is not minimal (it >>> has elements in that aren't used) and isn't complete (no data, no minimal >>> ontology, code doesn't generate any output or test any assertions) and >>> it's >>> not clear what your problem is. >>> >>> I'm not clear what you mean by "property not in the data property >>> assertion but in the Annotations". I assume are referring to the Protege >>> UI >>> which in turn suggests that your ontology doesn't match your example >>> code. >>> If that's the case then you just need to fix your ontology - nothing to >>> do >>> with jena or jena rules or this list. >>> >>> In the interests of trying to explain for you and Neha what's needed then >>> here's your code with some of the unnecessary bits removed and some >>> output >>> code added. I've also changed the name >>> #BestCategory#Physics to #BestCategory_Physics because having a # >>> character in your local name is really confusing. But I've not fixed any >>> of >>> the other style issues with the code or ontology names. >>> >>> >>>>>> OntModel model >>> = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); >>> InputStream in = FileManager.get().open("data/test-ont.ttl"); >>> model.read(in, "", "Turtle"); >>> String ns="http://www.semanticweb.org/t/ontologies/2016/7/myOWL#"; >>> OntProperty bestcat=model.getOntProperty(ns+ "BestCategory"); >>> >>> //Then rule >>> String rule ="[rule1: ( ?x http://www.semanticweb.org/t/o >>> ntologies/2016/7/myOWL#Physics_Preferred_Category ?cat1 )" + >>> "( ?x http://www.semanticweb.org/t/o >>> ntologies/2016/7/myOWL#Chem_Pr >>> eferred_Category ?cat2 )" + >>> "greaterThan(?cat1,?cat2)" + >>> " -> (?x http://www.semanticweb.org/t/o >>> ntologies/2016/7/myOWL#BestCategory http://www.semanticweb.org/t/o >>> ntologies/2016/7/myOWL#BestCategory_Physics )]"; >>> >>> Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule)); >>> InfModel infModel = ModelFactory.createInfModel(reasoner2, model); >>> for (StmtIterator i = infModel.listStatements(null,b >>> estcat,(RDFNode)null); >>> i.hasNext();) { >>> Statement s = i.next(); >>> System.out.println( String.format("%s has best category %s", >>> s.getSubject().getLocalName(), >>> s.getObject().asResource().getLocalName()) ); >>> } >>> infModel.write(System.out, "Turtle"); >>> <<< >>> >>> Now to run this we need an ontology but we only need to include those >>> declarations used in the sample code (stripping out the bits your don't >>> need is part of creating a minimal example). We also need test data to >>> trigger the rule but for a minimal example, that might as well go in the >>> ontology. So an example minimal "test-ont.ttl" file might be: >>> >>> >>>>>> @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . >>> @prefix owl: <http://www.w3.org/2002/07/owl#> . >>> @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . >>> @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . >>> @prefix ex: <http://www.semanticweb.org/t/ontologies/2016/7/myOWL#> . >>> >>> ex:User a owl:Class . >>> ex:BestCategory a owl:ObjectProperty . >>> >>> ex:Category a owl:Class . >>> ex:BestCategory_Physics a ex:Category . >>> >>> ex:Physics_Preferred_Category a owl:DatatypeProperty . >>> ex:Chem_Preferred_Category a owl:DatatypeProperty . >>> >>> ex:fred a ex:User; >>> ex:Physics_Preferred_Category 3 ;minimal >>> ex:Chem_Preferred_Category 1 . >>> <<< >>> >>> That breaks lots of OWL naming conventions but at least it matches your >>> code. >>> >>> If we run the sample code now then we see that rule fires correctly and >>> outputs: >>> >>> fred has best category BestCategory_Physics >>> >>> and that the resulting complete model is displayed as >>> >>> >>>>>> @prefix ex: <http://www.semanticweb.org/t/ontologies/2016/7/myOWL#> >>> . >>> @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . >>> @prefix owl: <http://www.w3.org/2002/07/owl#> . >>> @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . >>> @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . >>> >>> ex:fred a ex:User ; >>> ex:BestCategory ex:BestCategory_Physics ; >>> ex:Chem_Preferred_Category 1 ; >>> ex:Physics_Preferred_Category 3 . >>> >>> ex:BestCategory a owl:ObjectProperty . >>> >>> ex:BestCategory_Physics >>> a ex:Category . >>> >>> ex:Chem_Preferred_Category >>> a owl:DatatypeProperty . >>> >>> ex:User a owl:Class . >>> >>> ex:Physics_Preferred_Category >>> a owl:DatatypeProperty . >>> >>> ex:Category a owl:Class . >>> >>>> >>>>>> >>> That looks correct (or at least looks like it does what you say you are >>> trying to do). If I load that into protege then I can see the individual >>> fred and the ex:BestCategory property is shown as being a property >>> assertion not an axiom. >>> >>> So on the face of it there is nothing wrong. >>> >>> Now if that's not enough to help you solve your problem then create a >>> similarly complete minimal example which does show your problem. I expect >>> your ontology will be a lot different from my example. However, for the >>> sake of debugging your rule most of it won't be relevant so you should be >>> able to cut it down to something of comparable size to the above, in >>> which >>> case it is then possible to post it inline in an email message for people >>> to look at. If you do this please make sure it really is minimal and >>> complete. Also for preference use Turtle (not RDF/XML) syntax because >>> that >>> is so much easier to read and more compact. >>> >>> Dave >>> >>> >>> On 14/01/17 16:07, Sidra shah wrote: >>> >>> Hello All, I have a problem, very same to Neha. I was waiting some answer >>>> will come and I will get help but I think it did not solve. >>>> I have quiz game for three categories(subjects) Physics, Chemistry and >>>> Geography. User have to select one category and questions of that >>>> category >>>> will come. When user select a category Physics, for instance, 1 will be >>>> saved in a property Physics_Preferred_Category. When user select second >>>> time Physics when login to the system, 2 will be saved. >>>> I have designed a rule, shown below, to see which category user has >>>> selected most of the time and will consider that BestCategory for that >>>> user. The problem which I have currently is it saved the BestCategory >>>> property not in the data property assertion but in the Annotations. >>>> >>>> I have tried to include possible code but not ontology. If it is >>>> allowed I >>>> will paste the screen shot of my properties. >>>> >>>> >>>> OntModel model=ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_ >>>> MEM); >>>> InputStream in =FileManager.get().open("F://final.owl"); >>>> if (in==null) { >>>> throw new IllegalArgumentException( "File: " + " not >>>> found"); >>>> } model.read(in,""); >>>> String ns="http://www.semanticweb.org >>>> /t/ontologies/2016/7/myOWL#"; >>>> OntClass dil = model.getOntClass(ns + "DiligentUser"); >>>> OntClass avgr = model.getOntClass(ns + "AverageUser"); >>>> OntProperty bestcat=model.getOntProperty(ns+ "BestCategory"); >>>> Individual indiv = user1.createIndividual(ns + name); //name comes >>>> from a >>>> text field >>>> //Then rule >>>> >>>> String rule ="[rule1: ( ?x http://www.semanticweb.org/t/ >>>> ontologies/2016/7/myOWL#Physics_Preferred_Category ?cat1 )" + >>>> "( ?x http://www.semanticweb.org/t/ontologies/2016/7/myOWL#Chem_ >>>> Preferred_Category ?cat2 )" + >>>> // "( ?x http://www.semanticweb.org/t/o >>>> ntologies/2016/7/myOWL#Geo_ >>>> Preferred_Category ?cat3 )" + >>>> // "greaterThan(?cat1,?cat2), greaterThan(?cat1,?cat3)" >>>> + " -> (?x http://www.semanticweb.org/t/o >>>> ntologies/2016/7/myOWL# >>>> BestCategory http://www.semanticweb.org/t/ontologies/2016/7/myOWL# >>>> BestCategory#Physics )]"; >>>> >>>> //in ?cat1, ?cat2 ?cat3 there will be numbers like 1 2 3 >>>> >>>> Reasoner reasoner2 = new GenericRuleReasoner(Rule.parseRules(rule)); >>>> InfModel infModel = ModelFactory.createInfModel(reasoner2, >>>> model); >>>> >>>> infModel.listStatements(null,bestcat,(RDFNode)null); >>>> } >>>> >>>> >>>> >>> >> >
