Introduction to Ontology -- print out the simplified triplet after removing the URI prefix in the Ontology

Source: Internet
Author: User

 

 

If you study the ontology file (. OWL), you will find that all of them are composed of three tuples. However, both subject, predicte, and object are described as resources, and the resource identifier is Uri, therefore, there will always be a large string of prefixes. What we are interested in is often the last word, which is called localname. I don't know why. How can we get the triple represented by localname? Subject and predicte can be obtained directly through getlocalname, while object is annoying, without the getlocalname Function ~ Check the statement structure (resource arg0, property arg1, rdfnode arg2). The problem is that the rdfnode type does not have the getlocalname function, but you can convert it to a resource by calling the asresource function. Code:

 

Fileinputstream file = new
Fileinputstream (owl_path); // Of course, owl_path is the path of the owl file.
Inputstreamreader in = new
Inputstreamreader (file, "UTF-8"); // solves Chinese problems
Model =
Modelfactory. createdefamodel model ();
Model. Read (in, null );

 

Required titerator stmiter = model. liststatements ();

While (stmiter. hasnext ()){

Statement STM = stmiter. nextstatement ();

Rdfnode object = STM. GetObject ();

If (Object instanceof Resource) {// mainly to determine whether rdfnode is a resource or a literal

System. out. println (STM. getsubject (). getlocalname () + "**" + STM. getpredicate (). getlocalname () + "**" + object. asresource (). getlocalname ());

}

Else

System. out. println (STM. getsubject (). getlocalname () + "**" + STM. getpredicate (). getlocalname () + "**" + object. tostring ());

}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.