本站首页    管理页面    写新日志    退出


«September 2025»
123456
78910111213
14151617181920
21222324252627
282930


公告
暂无公告...

我的分类(专题)

日志更新

最新评论

留言板

链接


Blog信息
blog名称:
日志总数:8
评论数量:19
留言数量:0
访问次数:69927
建立时间:2006年8月1日




[Semantic Web]Jena Introduction 
读书笔记,  软件技术

爱睡觉的猫 发表于 2006/8/20 10:45:24

1. Introduction创建一个模型,并用StmtInterator循环取出Statement// some definitionsString personURI    = "http://somewhere/JohnSmith";String givenName    = "John";String familyName   = "Smith";String fullName     = givenName + " " + familyName; // create an empty ModelModel model = ModelFactory.createDefaultModel(); // create the resource//   and add the properties cascading styleResource johnSmith  = model.createResource(personURI)         .addProperty(VCARD.FN, fullName)         .addProperty(VCARD.N,                      model.createResource()                           .addProperty(VCARD.Given, givenName)                           .addProperty(VCARD.Family, familyName));// list the statements in the ModelStmtIterator iter = model.listStatements();// print out the predicate, subject and object of each statementwhile (iter.hasNext()) {    Statement stmt      = iter.nextStatement();  // get next statement    Resource  subject   = stmt.getSubject();     // get the subject    Property  predicate = stmt.getPredicate();   // get the predicate    RDFNode   object    = stmt.getObject();      // get the object     System.out.print(subject.toString());    System.out.print(" " + predicate.toString() + " ");    if (object instanceof Resource) {       System.out.print(object.toString());    } else {        // object is a literal        System.out.print(" \"" + object.toString() + "\"");    }     System.out.println(" .");} 2.Writing RDF// now write the model in XML form to a file model.write(System.out);The output should look something like this:<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/JohnSmith'>    <vcard:FN>John Smith</vcard:FN>    <vcard:N rdf:nodeID="A0"/>  </rdf:Description>  <rdf:Description rdf:nodeID="A0">    <vcard:Given>John</vcard:Given>    <vcard:Family>Smith</vcard:Family>  </rdf:Description></rdf:RDF>以更紧凑的方法书写RDF模型,保留空节点,但不适合大型的模型输出// now write the model in XML form to a filemodel.write(System.out, "RDF/XML-ABBREV");To write large files and preserve blank nodes, write in N-Triples format:// now write the model in XML form to a filemodel.write(System.out, "N-TRIPLE"); 3. Reading RDFpackage jena.rdf.example; import com.hp.hpl.jena.rdf.model.*;import com.hp.hpl.jena.util.FileManager;import java.io.*; public class Tutorial05{ static String fileName = D:/../data/camera.owl"; public static void main(String[] args){ Model model = ModelFactory.createDefaultModel(); InputStream in = FileManager.get().open(fileName);if(in == null){ throw new IllegalArgumentException("File "+fileName+" Not Found!!"); }else{ model.read(new InputStreamReader(in),""); model.write(System.out,"RDF/XML-ABBREV"); } }}4. Controlling Prefixes4.1explicit prefix definitionsModel m = ModelFactory.createDefaultModel(); String nsA = "http://somewhere/else#"; String nsB = "http://nowhere/else#"; Resource root = m.createResource( nsA + "root" ); Property P = m.createProperty( nsA + "P" ); Property Q = m.createProperty( nsB + "Q" ); Resource x = m.createResource( nsA + "x" ); Resource y = m.createResource( nsA + "y" ); Resource z = m.createResource( nsA + "z" ); m.add( root, P, x ).add( root, P, y ).add( y, Q, z ); System.out.println( "# -- no special prefixes defined" ); m.write( System.out ); System.out.println( "# -- nsA defined" ); m.setNsPrefix( "nsA", nsA ); m.write( System.out ); System.out.println( "# -- nsA and cat defined" ); m.setNsPrefix( "cat", nsB ); m.write( System.out ); # -- no special prefixes defined<rdf:RDF    xmlns:j.0="http://nowhere/else#"    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"    xmlns:j.1="http://somewhere/else#" >  <rdf:Description rdf:about="http://somewhere/else#root">    <j.1:P rdf:resource="http://somewhere/else#x"/>    <j.1:P rdf:resource="http://somewhere/else#y"/>  </rdf:Description>  <rdf:Description rdf:about="http://somewhere/else#y">    <j.0:Q rdf:resource="http://somewhere/else#z"/>  </rdf:Description></rdf:RDF>没有引进名称空间,Jena就用默认的方式对名称空间命名j.0 and j.1.可以用方法setNsPrefix(String prefix, String URI)引进名称空间 4.2 implicit prefix definitions(?????)As well as prefix declarations provided by calls to setNsPrefix, Jena will remember the prefixes that were used in input to model.read(). Take the output produced by the previous fragment, and paste it into some file, with URL file:/tmp/fragment.rdf say. Then run the code:  Model m2 = ModelFactory.createDefaultModel();m2.read( "file:/tmp/fragment.rdf" );m2.write( System.out ); You'll see that the prefixes from the input are preserved in the output. All the prefixes are written, even if they're not used anywhere. You can remove a prefix with removeNsPrefix(String prefix) if you don't want it in the output. Since NTriples doesn't have any short way of writing URIs, it takes no notice of prefixes on output and doesn't provide any on input. The notation N3, also supported by Jena, does have short prefixed names, and records them on input and uses them on output. Jena has further operations on the prefix mappings that a model holds, such as extracting a Java Map of the exiting mappings, or adding a whole group of mappings at once; see the documentation for PrefixMapping for details. 5. Jena RDF 包 Jena 是一个为语义网应用设计的一个Java API. 对应用开发者而言, 主要可用的RDF 包是com.hp.hpl.je na.rdf.model. 因为API 是以接口的方式定义的, 所以应用代码可以使用不同的实现机制而不用改变代码 本身. 这个包包含了可以表示模型, 资源, 属性, 文本, 陈述和其他RDF 关键概念的接口, 还有一个用来 创建模型的ModelFactory. 所以如果要应用代码与实现类保持独立, 最好尽可能地使用接口, 而不要使用 特定的实现类. com.hp.hpl.jena.impl 这些包包含了许多执行时所常用的执行类. 比如, 它们定义了诸如ResourseImp l, PropertyImpl 和LiteralImpl 的类, 这些类可以被不同的应用直接使用也可以被继承使用. 应用程序 应该尽可能少地直接使用这些类. 例如, 与其使用ResouceImpl 来创建一个新的实例, 更好的办法是使用 任何正在使用的模型的createResource 方法来完成. 那样的话, 如果模型的执行采用了一个优化的Resou ce 执行, 那么在这两种类型中不需要有任何的转换工作.  


阅读全文(2656) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.204 second(s), page refreshed 144773186 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号