how to read an existing ontolog and add some individuals and rewrite it using rdflib?

762 views
Skip to first unread message

Ehsan Majidi

unread,
Jul 19, 2015, 1:35:15 PM7/19/15
to rdfli...@googlegroups.com
I have an ontology that I want to instantiate it in a python code based on another file. here is the code:

from roundabout import *
from random import *
import numpy as np  
import arcpy
from Class_v2 import loading_layer
import time
import math
import re
from decimal import Decimal
from scipy import spatial
from roundabout import *
from Preprocessing import *
#from rdflib.store import Store, NO_STORE, VALID_STORE
from NVDB_Matching_v18_H_4_1.Segment_Node_extraction_v18_H_4_4 import *

arcpy
.env.overwriteOutput = True


def instantiating_ontology(layer, rdf_address):
   
from rdflib import *
    g
= Graph()
    input_RDF
= g.parse(rdf_address)
   
#input_RDF = g.open(rdf_address, create=False)

    myNamespace
= rdf_address.split(".owl", 1)[0]
   
print myNamespace
   
print rdf_address
    rno
= Namespace(myNamespace+"#")
    nodeClass
= URIRef(rno+"Node")
    arcClass
= URIRef(rno+"Arc")
    namedIndividual
= URIRef('http://www.w3.org/2002/07/owl#NamedIndividual')
    rdftype
= URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
   
for arc in layer.new_features_dict:
        arcID
= arc[u'FID']
        individualName
= rno + "arc_"+str(arcID)
        arc_individual
= BNode(individualName)
        g
.add((arc_individual,rdftype, namedIndividual))
       
#g.commit()
    output_address
="RNO_V5042_RDF.owl"
    g
.serialize(destination = output_address)

the argument layer comes from another part of code where it has a list of individuals with some info that I want to add to my ontology as well. as I open the file in Protege there is no instances in the ontology however checking the file in Notepad++ shows the instances as follows:

  <rdf:Description rdf:nodeID="http://www.semanticweb.org/ehsan.abdolmajidi/ontologies/2015/3/RNO_V5042#arc_42">
   
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#NamedIndividual"/>
 
</rdf:Description>

rdf/xml from Protege on the other hand is like:

     <NamedIndividual rdf:about="&RNO_V5042;street_001">
       
<rdf:type rdf:resource="&RNO_V5042;Primary"/>
       
<RNO_V5042:has_name rdf:datatype="&xsd;string">Ällingävagen 16</RNO_V5042:has_name>
       
<RNO_V5042:is_built_of rdf:resource="&RNO_V5042;Generalized_part_001"/>
       
<RNO_V5042:is_built_of rdf:resource="&RNO_V5042;detailed_part_001"/>
   
</NamedIndividual>

Why they are different? aren't they in a standard format rdf/xml?
I need to open the instantiated ontology in protege for further process. please help me to get the proper results that I can see the individuals in protege!!!!

moreover, I can read a file outside my workspace but can't rewrite it. I get the following error:

IOError: [Errno 2] No such file or directory: '/Ehsan/Skane/Input/Skane_data/Under_processing/Ontology/test/RNO_V5042_RDF.owl'

It would make my output tidier if I can define a proper destination.

I appreciate if you can help me with the above issues, and am looking forward to hearing from you soon.

Best regards,
Ehsan

Gunnar Aastrand Grimnes

unread,
Jul 19, 2015, 1:44:56 PM7/19/15
to rdfli...@googlegroups.com
On 19 July 2015 at 19:35, Ehsan Majidi <ehsan...@gmail.com> wrote:
>
> Why they are different? aren't they in a standard format rdf/xml?
> I need to open the instantiated ontology in protege for further process.
> please help me to get the proper results that I can see the individuals in
> protege!!!!

RDF/XML has several ways to represent the same triples, the type
triple expressed here is the same. (The protege examples has some more
triples though).

Protege is very picky in what RDF it will display, I haven't touched
it for many years, so I forget the details - but back then it expected
URIs of certain patterns etc.

> IOError: [Errno 2] No such file or directory: '/Ehsan/Skane/Input/Skane_data/Under_processing/Ontology/test/RNO_V5042_RDF.owl'

this error is from python, the problem is probably exactly what it
says: Some directory in this path does not exist.


- Gunnar




--
http://gromgull.net

Gunnar Aastrand Grimnes

unread,
Jul 19, 2015, 1:47:59 PM7/19/15
to rdfli...@googlegroups.com
btw - I know this isn't what you asked, but both rdf/xml and protege
are (in my opinion) not really the "good parts" of the linked
data/semantic web stack.

You may find your life much easier if you find tools that work with
Turtle as a serialisation format, and I've found Topbraid Composed to
be a far better alternative to protege.

- Gunnar

On 19 July 2015 at 19:35, Ehsan Majidi <ehsan...@gmail.com> wrote:
> moreover, as I can read a file outside my workspace but can't rewrite. I get
> the error:
>
> IOError: [Errno 2] No such file or directory:
> '/Ehsan/Skane/Input/Skane_data/Under_processing/Ontology/test/RNO_V5042_RDF.owl'
>
> It would make my output tidier if I can define a proper destination.
>
> I appreciate if you can help me the above issues, and am looking forward to
> hearing from you soon.
>
> Best regards,
> Ehsan
>
> --
> http://github.com/RDFLib
> ---
> You received this message because you are subscribed to the Google Groups
> "rdflib-dev" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rdflib-dev+...@googlegroups.com.
> To post to this group, send email to rdfli...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rdflib-dev/45ba55f2-90fa-49c0-a784-cae37daf6c1a%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://gromgull.net

Ehsan Majidi

unread,
Jul 19, 2015, 2:05:19 PM7/19/15
to rdfli...@googlegroups.com
Thanks for your reply.
the extra triples are not important as I myself added them to my ontology in Protege but did not add in the python code.
the fact is, after rewriting the ontology, protege can open the file and all the classes and properties are available but the added triples (individuals) are not there.

I really need to fix it or have to find another python package to work with my ontology. I would appreciate if you can help me in any way.

about the IOError I must say that the path is correct as I read the ontology from there and then when I want to rewrite on the same file I get the error.

Ehsan Majidi

unread,
Jul 20, 2015, 9:21:39 AM7/20/15
to rdfli...@googlegroups.com
It has a simple answer that I just realized by manually checking the difference between the rdf/xml produced by rdflib from an already instantiated ontology.
the trick is to use  the following line:

nodeIndividualName = URIRef(rno + "node_"+str(nodeID))

instead of :

nodeIndividualName = BNode(rno + "node_"+str(nodeID))

I hope this can help other newbies in rdflib :D

/Ehsan
Reply all
Reply to author
Forward
0 new messages