Dear Mr. Jean-Baptiste Lamy
First of all, thanks for the great software. I've been using owlready2 for my master's thesis for a few weeks now and so far it has been great as I need to create almost everything dynamically. Speaking of dynamic creation - I have no issues creating Classes and ObjectProperties with ranges and domains: with onto: # for classes sub_class = types.new_class(name, (super_class, )) sub_class.isDefinedBy = p.definition() # for ObjectProperties type(predicate, (ObjectProperty,), {"domain": [class_dict[subject]], "range": [class_dict[object]]}) This works nicely. However, due to the nature of my ontology I have multiple domains and ranges for a single ObjectProperty (they get added sequentially). Which is why I would like to introduce Restrictions to ensure that certain domains are only valid for certain ranges as described here [1]. I managed to create it within Protégé as well, as can be seen here (example simplified): <owl:Class rdf:about="file:/xyz.owl#Stove"> <rdfs:subClassOf rdf:resource="file:/xyz.owl#KitchenAppliances"/> <rdfs:subClassOf> <owl:Restriction> <owl:onProperty rdf:resource="file:/xyz.owl#provide"/> <owl:allValuesFrom rdf:resource="file:/xyz.owl#Heat"/> </owl:Restriction> </rdfs:subClassOf> </owl:Class> How can I achieve this and add Restrictions _after_ dynamically creating the classes and ObjectProperties. I am not quite sure what functions one can use. I know there is a Restriction class in owlready2 but not sure how to access and use it with pre-existing objects. Your help is greatly appreciated! [1] https://stackoverflow.com/a/34820480 |
Administrator
|
Hello,
You can create and assert restrictions dynamically, using the following syntax : Class.is_a.append(Prop.only(Range)) Where Class is the class, Prop the property and Range the desired range. Class, Prop and Range can be obtained from the ontologie (e.g. my_onto.MyClass), but you can also use the object created using types.new_class(...) (they are actually the same objects). Range can also be a datatype for data property. Best regards, Jean-Baptiste Lamy MCF, LIMICS, Université Paris 13 > Dear Mr. Jean-Baptiste Lamy > > First of all, thanks for the great software. I've been using owlready2 for > my master's thesis for a few weeks now and so far it has been great as I > need to create almost everything dynamically. > > Speaking of dynamic creation - I have no issues creating Classes and > ObjectProperties with ranges and domains: > > > with onto: > # for classes > sub_class = types.new_class(name, (super_class, )) > sub_class.isDefinedBy = p.definition() > > # for ObjectProperties > type(predicate, > (ObjectProperty,), > {"domain": [class_dict[subject]], > "range": [class_dict[object]]}) > > This works nicely. > However, due to the nature of my ontology I have multiple domains and ranges > for a single ObjectProperty (they get added sequentially). > > Which is why I would like to introduce Restrictions to ensure that certain > domains are only valid for certain ranges as described here [1]. I managed > to create it within Protégé as well, as can be seen here (example > simplified): > > <owl:Class rdf:about="file:/xyz.owl#Stove"> > <rdfs:subClassOf rdf:resource="file:/xyz.owl#KitchenAppliances"/> > <rdfs:subClassOf> > <owl:Restriction> > <owl:onProperty rdf:resource="file:/xyz.owl#provide"/> > <owl:allValuesFrom rdf:resource="file:/xyz.owl#Heat"/> > </owl:Restriction> > </rdfs:subClassOf> > </owl:Class> > > How can I achieve this and add Restrictions _after_ dynamically creating the > classes and ObjectProperties. I am not quite sure what functions one can > use. I know there is a Restriction class in owlready2 but not sure how to > access and use it with pre-existing objects. > > Your help is greatly appreciated! > > [1] https://stackoverflow.com/a/34820480 > > > > _______________________________________________ > If you reply to this email, your message will be added to the discussion below: > http://owlready.8326.n8.nabble.com/Dynamically-add-restrictions-after-creation-of-class-tp267.html > To start a new topic under Owlready, email [hidden email] > To unsubscribe from Owlready, visit |
Thank you very much! Worked like a charm :)
|
This post was updated on .
Hello,
I tried the given script, Class.is_a.append(Prop.only(Range)) in order to add restrictions dynamically. It worked for me as well. But the same restriction will be added to the class if we run the script several times. How to avoid this? (Simply, if a given restriction is already available, I don't want to add them again.) Therefore would like to know is there any mechanism, that I can follow to check whether a given restriction is already available under a given class? Any suggestion, Highly appreciated. |
Administrator
|
Hello,
I think you have to verify if it already exists: for x in Class.is_a: if isinstance(x, Restriction) and (x.property is Prop) and (x.type == ONLY): break else: Class.is_a.append(Prop.only(Range)) In the development version, I've added == support on Restriction, so you can also write : new_restriction = Prop.only(Range) if not new_restriction in Class.is_a: Class.is_a.append(new_restriction) Best regards, Jean-Baptiste Lamy MCF, LIMICS, Université Paris 13 |
Free forum by Nabble | Edit this page |