#!/usr/bin/python3 from owlready2 import * onto_path.append("repo") onto = get_ontology("http://www.lesfleursdunormal.fr/static/_downloads/pizza_onto.owl") onto.load() # onto = get_ontology("file:///home/jiba/onto/pizza_onto.owl").load() class NonVegetarianPizza(onto.Pizza): equivalent_to = [ onto.Pizza & ( onto.has_topping.some(onto.MeatTopping) | onto.has_topping.some(onto.FishTopping) ) ] def eat(self): print("Beurk! I'm vegetarian!") print(onto.Pizza) ### pizza_onto.Pizza test_pizza = onto.Pizza("jjtest_pizza_owl_identifier") test_pizza.has_topping = [ onto.CheeseTopping(), onto.TomatoTopping() ] test_pizza.has_topping.append(onto.MeatTopping()) print(test_pizza.__class__) sync_reasoner() print( test_pizza.__class__) print(test_pizza.eat()) onto.save() #test_pizza.save() #=== ''' default_world.set_backend(filename = "my.sqlite3") default_world.save() onto = my_world.get_ontology("http://test.org/onto/").load() sync_reasoner(my_world) #=== graph = default_world.as_rdflib_graph() r = list(graph.query("""SELECT ?p WHERE { ?p . }""")) r = list(graph.query_owlready("""SELECT ?p WHERE { ?p . }""")) ''' ''' .base_iri : base IRI for the ontology .imported_ontologies : the list of imported ontologies (see below) .classes() : returns a generator for the Classes defined in the ontology (see Classes and Individuals (Instances)) .individuals() : returns a generator for the individuals (or instances) defined in the ontology (see Classes and Individuals (Instances)) .object_properties() : returns a generator for ObjectProperties defined in the ontology (see Properties) .data_properties() : returns a generator for DataProperties defined in the ontology (see Properties) .annotation_properties() : returns a generator for AnnotationProperties defined in the ontology (see Annotations) .properties() : returns a generator for all Properties (object-, data- and annotation-) defined in the ontology .disjoint_classes() : returns a generator for AllDisjoint constructs for Classes defined in the ontology (see Disjointness, open and local closed world reasoning) .disjoint_properties() : returns a generator for AllDisjoint constructs for Properties defined in the ontology (see Disjointness, open and local closed world reasoning) .disjoints() : returns a generator for AllDisjoint constructs (for Classes and Properties) defined in the ontology .different_individuals() : returns a generator for AllDifferent constructs for individuals defined in the ontology (see Disjointness, open and local closed world reasoning) .get_namepace(base_iri) : returns a namespace for the ontology and the given base IRI (see namespaces below, in the next section) ''' ''' list(onto.classes()) >>> onto.search(iri = "*Topping") [pizza_onto.CheeseTopping, pizza_onto.FishTopping, pizza_onto.MeatTopping, pizza_onto.TomatoTopping, pizza_onto.Topping] In addition, the special value “*” can be used as a wildcard for any object. For example, the following line searches for all individuals that are related to another one with the ‘has_topping’ relation: >>> onto.search(has_topping = "*") When a single return value is expected, the .search_one() method can be used. It works similarly: >>> onto.search_one(label = "my label") Owlready classes and individuals can be used as values within search(), as follows: >>> onto.search_one(is_a = onto.Pizza) onto.search(is_a = onto.Pizza, has_topping = onto.search(is_a = onto.Tomato)) ''' """ onto.save(file = "filename or fileobj", format = "rdfxml") ==== onto = get_ontology("http://test.org/onto.owl") class Drug(Thing): namespace = onto for i in Drug.instances(): print(i) my_drug = Drug("my_drug") print(my_drug.name) >>> print(my_drug.iri) http://test.org/onto.owl#my_drug >>> unamed_drug = Drug() >>> print(unamed_drug.name) drug_1 my_drug = Drug("my_drug2", namespace = onto, has_for_active_principle = [],...) for prop in onto.drug_1.get_properties(): for value in prop[onto.drug_1]: print(".%s == %s" % (prop.python_name, value)) with onto: class is_ingredient_of(ObjectProperty): domain = [Ingredient] range = [Drug] inverse_property = has_for_ingredient with onto: ... class has_for_cost(DataProperty, FunctionalProperty): # Each drug has a single cost ... domain = [Drug] ... range = [float] my_drug.has_for_cost = 4.2 print(my_drug.has_for_cost) 4.2 === with onto: class BodyPart(Thing): pass class part_of(BodyPart >> BodyPart, TransitiveProperty): pass abdomen = BodyPart("abdomen") heart = BodyPart("heart" , part_of = [abdomen]) left_ventricular = BodyPart("left_ventricular", part_of = [heart]) kidney = BodyPart("kidney" , part_of = [abdomen]) print(left_ventricular.part_of) [heart] print(list(left_ventricular.part_of.indirect())) [heart, abdomen] === from owlready2 import * onto = get_ontology("http://test.org/onto.owl") with onto: class Drug(Thing): pass class ActivePrinciple(Thing): pass class has_for_active_principle(Drug >> ActivePrinciple): pass class Placebo(Drug): equivalent_to = [Drug & Not(has_for_active_principle.some(ActivePrinciple))] class NonPlaceboDrug(Drug): equivalent_to = [Drug & has_for_active_principle.some(ActivePrinciple)] == >>> onto2 = get_ontology("http://test.org/onto2.owl") with onto2: class Drug(Thing): pass class ActivePrinciple(Thing): pass class has_for_active_principle(Drug >> ActivePrinciple): pass class HeathCondition(Thing): pass class Pain(HeathCondition): pass class ModeratePain(Pain): pass class CardiacDisorder(HeathCondition): pass class Hypertension(CardiacDisorder): pass class Pregnancy(HeathCondition): pass class Child(HeathCondition): pass class Bleeding(HeathCondition): pass class has_for_indications (Drug >> HeathCondition): class_property_type = ["some"] class has_for_contraindications(Drug >> HeathCondition): class_property_type = ["only"] class Antalgic(Drug): defined_class = True has_for_indications = [Pain] has_for_contraindications = [Pregnancy, Child, Bleeding] class Aspirin(Antalgic): defined_class = True has_for_indications = [ModeratePain] has_for_contraindications = [Pregnancy, Bleeding] ... class is_ingredient_of(ObjectProperty): domain = [Ingredient] range = [Drug] inverse_property = has_for_ingredient """