0

I created the class dynamically as shown in the Owlready2 documentation

from owlready2 import *
import types

onto_1 = get_ontology("http://test_1.org/onto/")

ObjectProperty_list = ['on', 'of']
Thing_list = ['car', 'tree']
obj_list = ['car_1','car_2', 'tree_1', 'tree_2']

with onto_1:
    for class_name in Thing_list:
        globals()[class_name] = types.new_class(class_name, (Thing, ))

    class color(DataProperty):
        pass

ObjectProperty is also dynamically created as shown in the URL.

Creating Owlready2 properties dynamically

    for class_name in ObjectProperty_list:
        globals()[class_name] = types.new_class(class_name,(ObjectProperty, FunctionalProperty), {})

However, the relationship between object and object could not be set dynamically.

a_node = globals()['car']("car_1", namespace=onto_1, color=['red', 'white'])
b_node = globals()['car']("car_2", namespace=onto_1, color=['red', 'white'])

for relation_name in ObjectProperty_list:
    a_node.globals()[class_name] = b_node

error is AttributeError: 'globals' property is not defined.

please help me

this is a full code

from owlready2 import *
import types

onto_1 = get_ontology("http://test_1.org/onto/")

ObjectProperty_list = ['on', 'of']
Thing_list = ['car', 'tree']
obj_list = ['car_1','car_2', 'tree_1', 'tree_2']

with onto_1:

    for class_name in Thing_list:
        globals()[class_name] = types.new_class(class_name, (Thing, ))

    class color(DataProperty):
        pass

    for class_name in ObjectProperty_list:
        print(class_name)
        globals()[class_name] = types.new_class(class_name, (ObjectProperty, FunctionalProperty), {})

a_node = globals()['car']("car_1", namespace=onto_1, color=['red', 'white'])
b_node = globals()['car']("car_2", namespace=onto_1, color=['red', 'white'])

for relation_name in ObjectProperty_list:
    a_node.globals()[class_name] = b_node

onto_1.save("./test_back_back.xml")

0 Answers0