设为首页 收藏本站
查看: 1468|回复: 0

[经验分享] jena 操作RDF的一个例子(参考IBM的一篇文章)

[复制链接]

尚未签到

发表于 2017-5-27 09:23:15 | 显示全部楼层 |阅读模式
  jena 操作RDF的一个例子
RDF 越来越被认为是表示和处理半结构化数据的一种极好选择。本文中,Web 开发人员 Philip McCarthy 向您展示了如何使用 Jena Semantic Web Toolkit,以便在 Java 应用程序中使用 RDF 数据模型。
  “资源描述框架(Resource Description Framework,RDF)”最近成为 W3C 推荐标准,与 XML 和 SOAP 等 Web 标准并排。
  创建简单的 RDF 模型
  我们从基本操作开始:从头创建模型并向其添加 RDF 语句。本节,我将说明如何创建描述一组虚构家庭成员之间关系的模型,如图 1 中所示:
  
图 1. 虚拟家庭树
DSC0000.gif
  将使用来自“关系”词汇表(请参阅 参考资料)的属性 siblingOfspouseOfparentOfchildOf来描述不同的关系类型。为简单起见,家庭成员用来自虚构名称空间的 URI( http://family/)进行标识。词汇表 URI 通常以 Jena 代码形式使用,所以将它们声明为 Java 常量会非常有用,减少了错误输入。
  Jena 的 ModelFactory类是创建不同类型模型的首选方式。在这种情况下,您想要空的、内存模型,所以要调用的方法是 ModelFactory.createDefaultModel()。这种方法返回 Model实例,您将使用它创建表示家庭中每个成员的 Resource。创建了资源后,可以编写关于这些资源的语句并添加到模型中。
  在 Jena 中,语句的主题永远是 Resource,谓词由 Property表示,对象是另一个 Resource或常量值。常量在 Jena 中通过 Literal类型表示。所有这些类型共享公共接口 RDFNode。将需要四个不同的 Property实例表示家庭树中的关系。这些实例使用 Model.createProperty()创建。
  将语句添加到模型中的最简单方法是通过调用 Resource.addProperty()。此方法以 Resource作为主题在模型中创建语句。该方法使用两个参数,表示语句谓词的 Property和语句的对象。 addProperty()方法被过载:一个过载使用 RDFNode作为对象,所以可以使用 ResourceLiteral。还有有益过载,它们使用由Java 原语或 String表示的常量。在示例中,语句的对象是表示其他家庭成员的 Resource
  通过使用三元组的主题、谓词和对象调用 Model.createStatement(),还可以直接在模型上创建语句。注意以此种方式创建 Statement不将其添加到模型中。如果想将其添加到模型中,请使用创建的 Statement调用 Model.add()
  下面给出一些源代码:
  package com.jena.ibm;
import java.util.*;
import com.hp.hpl.jena.rdf.model.*;
public class FamilyModel {
// Namespace declarations
static final String familyUri = "http://family/";
static final String relationshipUri = "http://family.org/relationship/";
// Jena model representing the family
private Model model;
public FamilyModel() {
     // Create an empty Model
     model = ModelFactory.createDefaultModel();
}
public void test()
{
    // Create the types of Property we need to describe relationships
      // in the model
    Property childOf = model.createProperty(relationshipUri,"childOf");
      Property parentOf = model.createProperty(relationshipUri,"parentOf");
      Property siblingOf = model.createProperty(relationshipUri,"siblingOf");
      Property spouseOf = model.createProperty(relationshipUri,"spouseOf");
      
      // Create resources representing the people in our model
      Resource adam = model.createResource(familyUri+"adam");
      Resource beth = model.createResource(familyUri+"beth");
      Resource chuck = model.createResource(familyUri+"chuck");
      Resource dotty = model.createResource(familyUri+"dotty");
      Resource edward = model.createResource(familyUri+"edward");
      Resource fran = model.createResource(familyUri+"fran");
      Resource greg = model.createResource(familyUri+"greg");
      Resource harriet = model.createResource(familyUri+"harriet");
  // Add properties to describing the relationships between them
      adam.addProperty(siblingOf,beth);
      adam.addProperty(spouseOf,dotty);
      adam.addProperty(parentOf,edward);
      adam.addProperty(parentOf,fran);
  beth.addProperty(siblingOf,adam);
      beth.addProperty(spouseOf,chuck);
      chuck.addProperty(spouseOf,beth);
     
      dotty.addProperty(spouseOf,adam);
      dotty.addProperty(parentOf,edward);
      dotty.addProperty(parentOf,fran);
      // Statements can also be directly created ...
      Statement statement1 = model.createStatement(edward,childOf,adam);
      Statement statement2 = model.createStatement(edward,childOf,dotty);
      Statement statement3 = model.createStatement(edward,siblingOf,fran);
      // ... then added to the model:
      model.add(statement1);
      model.add(statement2);
      model.add(statement3);
      // Arrays of Statements can also be added to a Model:
      Statement statements[] = new Statement[5];
      statements[0] = model.createStatement(fran,childOf,adam);
      statements[1] = model.createStatement(fran,childOf,dotty);
      statements[2] = model.createStatement(fran,siblingOf,edward);
      statements[3] = model.createStatement(fran,spouseOf,greg);
      statements[4] = model.createStatement(fran,parentOf,harriet);
      model.add(statements);
      // A List of Statements can also be added
      List list = new ArrayList();
      list.add(model.createStatement(greg,spouseOf,fran));
      list.add(model.createStatement(greg,parentOf,harriet));
      list.add(model.createStatement(harriet,childOf,fran));
      list.add(model.createStatement(harriet,childOf,greg));
      model.add(list);
       System.out.println("------------------");
// List everyone in the model who has a child:
      ResIterator parents = model.listSubjectsWithProperty(parentOf);
      // Because subjects of statements are Resources, the method returned a ResIterator
      while (parents.hasNext()) {
      // ResIterator has a typed nextResource() method
      Resource person = parents.nextResource();
      // Print the URI of the resource
      System.out.println(person.getURI()); }
      // Can also find all the parents by getting the objects of all "childOf" statements
      // Objects of statements could be Resources or literals, so the Iterator returned
      // contains RDFNodes
      NodeIterator moreParents = model.listObjectsOfProperty(childOf);
      while(moreParents.hasNext())
      {
       RDFNode r=moreParents.nextNode();
       if(r instanceof Resource){
         System.out.println(r);
       }
       //literal
       else System.out.println(r);
      }
      // To find all the siblings of a specific person, the model itselfcan be queried
      NodeIterator siblings = model.listObjectsOfProperty(edward, siblingOf);
      // But it's more elegant to ask the Resource directly
      // This method yields an iterator over Statements
      StmtIterator moreSiblings = edward.listProperties(siblingOf);
    System.out.println("------------------");
    model.listStatements(adam,spouseOf,dotty);
// Find all statements with adam as the subject and dotty as the object
model.listStatements(adam,null,dotty);
  
// Find any statements made about adam
model.listStatements(adam,null,(RDFNode)null);
// Find any statement with the siblingOf property
StmtIterator stmtIter= model.listStatements(null,siblingOf,(RDFNode)null);
while(stmtIter.hasNext())
{
   Statement stmt=stmtIter.nextStatement();
   System.out.println(stmt);
}
}
public static void main(String args[]) {
     // Create a model representing the family
     FamilyModel theFamily = new FamilyModel();
     theFamily.test();
     //System.out.println(theFamily.model);
}
}
参考:http://www.ibm.com/developerworks/cn/java/j-jena/#resources

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-381577-1-1.html 上篇帖子: IBM FileNet BPM 流程优化系列产品简介及应用实例 下篇帖子: 在IBM Portal6.1上自动发布主题、外观和Portlet
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表