1、,语义网开发工具:Jena,1,Jena,Jena由 HP Labs(http:/)开发的Java开发工具包用于开发语义网的应用系统Jena的功能解析RDF模型提供对RDFS、OWL、DAML+OIL等本体的操作支持基于本体的规则的推理和验证,2,Jena,Jena includes:A RDF APIReading and writing RDF in RDF/XML,N3 and N-TriplesAn OWL APIIn-memory and persistent storageSPARQL query enginea rule-based inference engine,3,一、解析
2、RDF模型,Jena定义了许多接口来访问和处理RDF陈述RDF图称为模型,用Model接口表示陈述用Statement接口表示资源用Resource接口表示属性用Property接口表示常量用Literal接口表示提供了一个RDFNode接口作为Resource、Property、Literal的超 接口通过接口操作RDF创建资源、属性等RDF元素添加、删除模型中的陈述查询RDF模型以XML/RDF、三元组格式读写RDF文档,4,创建简单 RDF 模型,创建描述一组虚构家庭成员之间关系的模型,使用来自“关系”词汇表(http:/purl.org/vocab/relationship)的属性si
3、blingOf、spouseOf、parentOf 和 childOf 来描述家庭成员之间关 系。家庭成员用来自虚构名称空间的 URI(http:/family/)进行标识,5,1、创建模型,模型是RDF图在Jena中的表示形式模型由一组RDF陈述组成通过模型可以创建资源、属性、常量增加、删除陈述可进行查询模型创建模型ModelFactory 类用于创建不同类型的模型ModelFactory.createDefaultModel():创建一个空的、内存模型,返回 Model 实例例:Model model=ModelFactory.createDefaultModel();,6,2、创建属性,
4、使用 Model.createProperty创建属性Property createProperty(String nameSpace,String localName)nameSpace:命名空间localName:属性局部名称/Namespace declarationsString familyUri=http:/family/;String relationshipUri=http:/purl.org/vocab/relationship/;/Create the types of Property to describe relationships in the modelPrope
5、rty childOf=model.createProperty(relationshipUri,childOf);Property parentOf=model.createProperty(relationshipUri,parentOf);Property siblingOf=model.createProperty(relationshipUri,siblingOf);Property spouseOf=model.createProperty(relationshipUri,spouseOf);,7,3、创建资源,使用Model.createResource创建资源 Resource
6、 createResource(String uri)uri:资源的URI/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);
7、Resource edward=model.createResource(familyUri+edward);Resource fran=model.createResource(familyUri+fran);Resource greg=model.createResource(familyUri+greg);Resource harriet=model.createResource(familyUri+harriet);,8,4、添加陈述,使用Resource.addProperty添加关于资源的陈述到模型Resource addProperty(Property p,RDFNode o)
8、p:陈述的谓词,o:陈述的客体,本资源为主体/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(s
9、pouseOf,beth);dotty.addProperty(spouseOf,adam);dotty.addProperty(parentOf,edward);dotty.addProperty(parentOf,fran);,9,4、添加陈述(续),也可使用Model.createStatement创建陈述Statement createStatement(Resource s,Property p,RDFNode o)Model add(Statement stmt)/Statements can also be directly created.Statement statement
10、1=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);,10,4、添加陈述(续),/Arrays of Stateme
11、nts can also be added to a Model:Statement statements=new Statement5;statements0=model.createStatement(fran,childOf,adam);statements1=model.createStatement(fran,childOf,dotty);statements2=model.createStatement(fran,siblingOf,edward);statements3=model.createStatement(fran,spouseOf,greg);statements4=m
12、odel.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(m
13、odel.createStatement(harriet,childOf,greg);model.add(list);,11,5、查询RDF模型,Model接口提供对模型中的资源、属性、陈述等的查询listStatements():List all statementslistStatements(Resource s,Property p,RDFNode o):Find all the statements matching a pattern.listSubjects():List all resources which are subjects of statements.listSub
14、jectsWithProperty(Property p):List all subjects with a given propertylistObjects():List all objects in a model.listObjectsOfProperty(Property p):List all objects of a given property,12,5、查询RDF模型(续),列出陈述StmtIterator listStatements()列出所有陈述结果存放在陈述迭代器中StmtIterator listStatements(Resource s,Property p,RD
15、FNode o)查找满足给定模式的陈述s:查找的资源,p:查找的谓词,o:查找的客体参数可为null,表示匹配任何值例如:/Find the exact statement adam is a spouse of dotty model.listStatements(adam,spouseOf,dotty);,13,5、查询RDF模型(续),例:/Find any statements made about adamStmtIterator iter=model.listStatements(adam,null,(RDFNode)null);/print out the predicate,s
16、ubject and object of each statementwhile(iter.hasNext(),14,/get next statement/get the subject/get the predicate/get the object,Statement stmt=iter.nextStatement();Resource subject=stmt.getSubject();Property predicate=stmt.getPredicate();RDFNodeobject=stmt.getObject();System.out.print(subject.toString();,System.out.print(+predicate.toString()+);if(object instanceof Resource)System.out.print(object.toString();else/object is a literalSystem.out.print(+object.toString()+);System.out.println(.);,5、查
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1