Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import owlready2
- owlready2.JAVA_EXE = r"my-path-to-java-exe"
- # new.owl is a non-existing file and therefore onto has no pre-defined classes
- # if you know of any nicer way to define an ontology, I'd appreciate it
- onto = get_ontology("new.owl")
- with onto:
- class Drug(Thing): pass
- class number_of_tablets(Drug >> int, FunctionalProperty): pass # Creating some properties
- class price(Drug >> float, FunctionalProperty): pass
- class price_per_tablet(Drug >> float, FunctionalProperty): pass
- rule = Imp()
- # Rule: "Drug instance ?d AND price of ?d is ?p AND drug ?d has number_of_tablets = ?n
- # AND ?r = ?p/?n -> Drug ?d has price_per_tablet = ?r"
- rule.set_as_rule("""Drug(?d), price(?d,?p), number_of_tablets(?d,?n), divide(?r, ?p, ?n) -> price_per_tablet(?d, ?r)""")
- # Create an instance "drug" with properties defined in brackets
- drug = Drug(number_of_tablets = 10, price = 25.0)
- #print(drug.iri)
- # Syncing the reasoner infers new info
- sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True)
- # New property price_per_tablet is now added to drug and we can use it normally:
- print(drug.price_per_tablet)
- # Save this ontology with rules in the same folder, filename: test
- onto.save(file = "test", format = "rdfxml")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement