JavaAPI~jena(OWL、RDF、ARQ、XML)

1.OWL

OWL用于表示丰富和复杂的知识,包括事物、事物组和事物之间的关系。它的关键部分是能够以更有趣和更复杂的方式来描述类。OWL核心是围绕三元组的,即我们在很多资料中看到的陈述(Statement),它的组成为:Statement=(Subject,Property,Object),其中Subject我们熟成为主体,Property成为属性,Object成为客体。
举个栗子:Statement=(餐具,装,食物)
餐具:主体,可以为碗也可以为盘子
食物:客体,可以为馒头也可以为米饭
从这个角度来说,主体和客体是类
OntPropety用来创建属性(关系),Individual用来创建个体

2.RDF

例:《XXX》的作者是XX:在这个陈述句中,《XXX》是主体,作者是谓语,XX是客体。
RDF就是这些一个个的陈述语句构成的,RDF模型可以转换成图结构,其中主体和客体代表图的节点,谓语代表边。
JavaAPI~jena(OWL、RDF、ARQ、XML)_第1张图片

java代码

pom.xml中引入jar包

<dependency>
      <groupId>com.hp.hpl.jenagroupId>
      <artifactId>jenaartifactId>
      <version>2.6.0version>
      <type>pomtype>
dependency>

<dependency>
        <groupId>org.apache.jenagroupId>
        <artifactId>apache-jena-libsartifactId>
        <type>pomtype>
        <version>3.5.0version>
dependency>

创建如下关系
JavaAPI~jena(OWL、RDF、ARQ、XML)_第2张图片

package com.testJenaRDF.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.vocabulary.VCARD;

public class StatementDemo {
     public static void main(String[] args) throws FileNotFoundException{  

            //Introduction  
            String personURI = "http://somewhere/JohnSmith";  
            String givenName = "John";  
            String familyName = "Smith";  
            String fullName = givenName + " " + familyName;  

            //整体模型用Model 类来表示
            Model model = ModelFactory.createDefaultModel();  

            //资源用 Resource 类来表示、johnSmith为主体
            Resource johnSmith = model.createResource(personURI);

            //属性用 Property 类来表示,,添加属性
            //给johnSmith这个人,加入名字为FN的关系
            johnSmith.addProperty(VCARD.FN, fullName);
            johnSmith.addProperty(VCARD.N,   
                        model.createResource()  
                            .addProperty(VCARD.Given, givenName)  
                            .addProperty(VCARD.Family, familyName));  

            //每个箭头都是一个Statement,每个箭头都由实体 谓语 客体三个部分组成  
            StmtIterator iter = model.listStatements();  

            while(iter.hasNext()){  
                Statement stmt = iter.nextStatement();  
                Resource subject = stmt.getSubject();  
                Property predicate = stmt.getPredicate();  
                RDFNode object = stmt.getObject();  

                System.out.print(subject.toString());  
                System.out.print(" "+predicate.toString());  
                if(object instanceof Resource){  
                    System.out.print(object.toString());  
                }else{  
                    System.out.print("\"" + object.toString() + "\"");  
                }  

                System.out.println(" .");  
            } 


          //Model write输出  
            model.write(System.out);  
            System.out.println();  
            model.write(System.out, "RDF/XML-ABBREV");  
            System.out.println();  
            System.out.println("------------------------------------");
            //在桌面创建a.rdf文件
            File outfile = new File("C:/Users/Administrator/Desktop/a.rdf");
            FileOutputStream outputStream = new FileOutputStream(outfile);
            model.write(System.out, "N-TRIPLE");
            //将model存入该文件中
            model.write(outputStream, "RDF/XML-ABBREV");
        }  
}

3.ARQ

ARQ是Jena的一个查询引擎,支持SPARQL的RDF查询语言。
数据源,一个RDF文件sparql.rdf,文档描述一些简单的人名信息,下面是类似三元组形式的数据表示



<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"

  xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#">

  <rdf:Description rdf:about="http://somewhere/MattJones/">

    <vCard:FN>Matt JonesvCard:FN>

      <vCard:N rdf:parseType="Resource">

        <vCard:Family>JonesvCard:Family>

        <vCard:Given>MatthewvCard:Given>

      vCard:N>

  rdf:Description>

  <rdf:Description rdf:about="http://somewhere/RebeccaSmith/">

    <vCard:FN>Becky SmithvCard:FN>

    <vCard:N rdf:parseType="Resource">

      <vCard:Family>SmithvCard:Family>

      <vCard:Given>RebeccavCard:Given>

    vCard:N>

  rdf:Description>

  <rdf:Description rdf:about="http://somewhere/JohnSmith/">

    <vCard:FN>John SmithvCard:FN>

    <vCard:N rdf:parseType="Resource">

      <vCard:Family>SmithvCard:Family>

      <vCard:Given>JonesvCard:Given>

    vCard:N>

  rdf:Description>

  <rdf:Description rdf:about="http://somewhere/SarahJones/">

    <vCard:FN>Sarah JonesvCard:FN>

    <vCard:N rdf:parseType="Resource">

      <vCard:Family>JonesvCard:Family>

      <vCard:Given>SarahvCard:Given>

    vCard:N>

  rdf:Description>

rdf:RDF>

基本查询语句

1.查询FN为John Smith的主体

package com.testJenaRDF.test;

import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

public class sparqlTest {
    public static String inputFileName = "C:/Users/Administrator/Desktop/sparql.rdf"; 
    public static void main(String[] args) {
        Model model = ModelFactory.createMemModelMaker().createDefaultModel();
        model.read(inputFileName);
        String queryString = "SELECT ?x  WHERE { ?x   'John Smith' }";
        Query query = QueryFactory.create(queryString);
        QueryExecution qe = QueryExecutionFactory.create(query, model);
        ResultSet results = qe.execSelect();
        ResultSetFormatter.out(System.out, results, query);
        qe.close();
    }
}

JavaAPI~jena(OWL、RDF、ARQ、XML)_第3张图片

2.查询谓语为FN的主体,返回值为谓语和主体

String queryString = "SELECT ?x ?fname WHERE {?x    ?fname}";

JavaAPI~jena(OWL、RDF、ARQ、XML)_第4张图片

3.查询姓Smith的 名字

String queryString = "SELECT ?givenName WHERE  { ?y    'Smith' . ?y    ?givenName .}";

JavaAPI~jena(OWL、RDF、ARQ、XML)_第5张图片

根据命名空间查询

//命名空间的简写替代 vcard 代替
 "PREFIX vcard:"
 "SELECT ?y ?givenName WHERE { ?y vcard:Family 'Smith' . ?y vcard:Given  ?givenName . }";

JavaAPI~jena(OWL、RDF、ARQ、XML)_第6张图片

4.过滤查询结果 ”r”表示,名字中必须出现的字母”r”或”R” ”i”表示,对签名的字母限制,默认不加对大小写敏感,加上则对大小写不敏感。

"PREFIX vcard: "+
"SELECT ?g "+//?g 后面加空格很重要
"WHERE { ?y vcard:Given ?g . FILTER regex(?g, 'r', 'i')}";       // FILTER (?age >= 24)}

JavaAPI~jena(OWL、RDF、ARQ、XML)_第7张图片

你可能感兴趣的:(java框架)