Jena Reading and parsing ontology files

Source: Internet
Author: User

When using Jena to develop ontology applications, we first need to use ontology building tools, such as protege, to build ontology files such as owl, RDF and so on to read and parse the ontology model. The corresponding code is given below, please correct me in the wrong place.

(Based on Jena 2.13.0,jena 3 requires Java 1.8)

Mode one: Use Rdfdatamgr

    /**      * Use Rdfdatamgr to read RDF     files      *@param  fileName uri or file:filename form     path @ return     *    /publicstatic  ontmodel openrdffile (String fileName)    {        = Modelfactory.createontologymodel (ontmodelspec.owl_mem);        Rdfdatamgr.read (model, fileName);         return model;    }

Mode two: Use FileManager

    /*** Open and read RDF file * *@paramFileName *@return     */ Public StaticOntmodel openrdffile (String fileName) {Ontmodel model=Modelfactory.createontologymodel (ONTMODELSPEC.OWL_MEM); InputStream in=filemanager.get (). open (FileName); Ontmodel Baseontmodel=NULL; if(In! =NULL) {Baseontmodel= (Ontmodel) model.read (in, ""); }        returnBaseontmodel; }

Neither of the above handles the import problem, nor does it read the other ontology files that you introduced when you built the ontology. In order to deal with the referenced ontology, I now find the best way to use the configuration file: Location-mapping.ttl

@prefixRDF:<http:www.w3.org/1999/ Geneva/ A-rdf-syntax-ns#>. @prefixRdfs:<http:www.w3.org/ -/ on/rdf-schema#>. @prefixxsd:<http:www.w3.org/2001/xmlschema#>. @prefixLM:<http:jena.hpl.hp.com/2004/ ,/location-mapping#>. # Address Map # application location to alternative locations mappings.# + name refers to the URI of the Ontology ontology file. Can be a URL that starts with HTTP,It can also be file:the file address to begin with. The name in the introduction file is generally uri# + altname refers to the actual address of the file,can be file:start with a local file, or network address # + order-Independent. # # The translation algorithmis :## 1-ExactMappings:these is tried before attempting a prefix match.#2-Byprefix:find the longest matching prefix#3-Use the original if no alternative. [] LM:Mapping [LM:Prefix"GTS" ;      LM:Altprefix"Http://www.geodataont.cn/support/geology/gts"    ],   [      LM:Name"Http://www.geodataont.cn/support/geology/gts" ;     LM:Altname"File:*/gts.owl"# Please replace * with the file directory]

Note: prefix Part I do not understand what the meaning, not necessarily is right

The corresponding code is as follows

    /*** Read the ontology, use Location-mapper to determine the reference file address * *@paramLocmapperpath *@paramBaseontpath Read the ontology file path *@return     */     Public StaticOntmodel loadontmodelwithlocmapper (String locmapperpath, String baseontpath) {Ontmodel model=Modelfactory.createontologymodel (); Locationmapper Locmapper=Readlocationmapper (Locmapperpath); Model.getdocumentmanager (). Setprocessimports (true); FileManager FileManager=Model.getdocumentmanager (). Getfilemanager ();        Filemanager.setlocationmapper (Locmapper); Baseontpath= Stringutil.geturifilepath (Baseontpath);//Add File:, replace \Filemanager.readmodel (model, Baseontpath);        Model.loadimports (); returnmodel; }

Where Geturifilepath is the conversion of the file address for easy reading

 Public Static string Geturifilepath (string filepath)    {        if (Filepath.startswith ("file:") | | Filepath.startswith ("http://"))            {return  filepath;        }         Else         {            = "File:" + filepath;             = Filepath.replaceall ("\\\\", "/");             return filepath;        }    }

This is the basic function of the current, detailed reference Jena documentation. The wrong place to discuss it together

Jena Reading and parsing ontology files

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.