Advertisement
Guest User

Code

a guest
Apr 6th, 2021
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import owlready2
  2. owlready2.JAVA_EXE = r"my-path-to-java-exe"
  3.  
  4. # new.owl is a non-existing file and therefore onto has no pre-defined classes
  5. # if you know of any nicer way to define an ontology, I'd appreciate it
  6.  
  7. onto = get_ontology("new.owl")
  8.  
  9. with onto:
  10. class Drug(Thing): pass
  11. class number_of_tablets(Drug >> int, FunctionalProperty): pass # Creating some properties
  12. class price(Drug >> float, FunctionalProperty): pass
  13. class price_per_tablet(Drug >> float, FunctionalProperty): pass
  14.  
  15. rule = Imp()
  16.  
  17. # Rule: "Drug instance ?d AND price of ?d is ?p AND drug ?d has number_of_tablets = ?n
  18. # AND ?r = ?p/?n -> Drug ?d has price_per_tablet = ?r"
  19.  
  20. rule.set_as_rule("""Drug(?d), price(?d,?p), number_of_tablets(?d,?n), divide(?r, ?p, ?n) -> price_per_tablet(?d, ?r)""")
  21.  
  22. # Create an instance "drug" with properties defined in brackets
  23. drug = Drug(number_of_tablets = 10, price = 25.0)
  24. #print(drug.iri)
  25.  
  26. # Syncing the reasoner infers new info
  27. sync_reasoner_pellet(infer_property_values = True, infer_data_property_values = True)
  28.  
  29. # New property price_per_tablet is now added to drug and we can use it normally:
  30. print(drug.price_per_tablet)
  31.  
  32. # Save this ontology with rules in the same folder, filename: test
  33. onto.save(file = "test", format = "rdfxml")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement