Problems when creating new instances

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Problems when creating new instances

Fory123
Here is a problem when creating new instances with python and pycharm: The ontology is created with protege in the format of rdfxml, which can be successfully loaded. >>onto_path = '../static/ontology/Assembly_Workshop_DT.owl' >>onto = get_ontology(onto_path).load() The classes in the ontology can be printed. >>print(onto.Assembler) >>print(onto.classes()) >>print(list(onto.classes())) Assembly_Workshop_DT.Assembler [Assembly_Workshop_DT.Staff, Assembly_Workshop_DT.Assembler, Assembly_Workshop_DT.Assembly_Resource, Assembly_Workshop_DT.Assembly_Workshop_DT, Assembly_Workshop_DT.Assembly_Task, Assembly_Workshop_DT.Inspector, Assembly_Workshop_DT.Personnel, Assembly_Workshop_DT.Process_designer, Assembly_Workshop_DT.Team] However, instances can not be created with the error of "NameError: name 'Assembler' is not defined" when running code >>Assembler0205 = Assembler("Assembler0205") Seems like if the classes is created with Owlready, the instances can be added without a hitch. I wonder what to do when creating instances of a existed class?
Reply | Threaded
Open this post in threaded view
|

Re: Problems when creating new instances

Jiba
Administrator
Hi,

If you load the ontology as "onto", you need to use "onto.Assembler" to access the class inside the ontology.

If you create the class directly in Python, it is available as "onto.Assembler" but also as "Assembler", because Python creates variables when creating a class.

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Problems when creating new instances

Fory123
Thanks a lot for your help!

In addition, I also want to consult the method of adding data or object properties which are pre-defined in protege instead of in python directly.

For example, when running the code ">>Assembler0205.Staff_has_Name = "Fory"", error appears "ValueError: Property 'Staff_has_Name' is not functional, cannot assign directly (use .append() or assign a list)."

I wonder the appropriate way to use the properties predefined in the ontology when coding with python.

Sorry to bother you with such simple problems. I just started with the Owlready.
Reply | Threaded
Open this post in threaded view
|

Re: Problems when creating new instances

Jiba
Administrator
If the property is not functional, it is represented by a list in Python.

You can do one of the folowwing:

Assembler0205.Staff_has_Name.append("name")

Assembler0205.Staff_has_Name = ["name"]

Jiba
Reply | Threaded
Open this post in threaded view
|

Re: Problems when creating new instances

Fory123
Thanks a lot!

The problems have been perfectly worked out!