1. 首页 > 常识

Jena解析RDF数据 rdf数据

本文为大家介绍了 Jena解析RDF数据 ,还有的小伙伴在问rdf数据,下面小编给大家细致的讲述一下。

←奉贤菜花节

岁月神偷→

Jena解析RDF数据

April 16, 2010ZenUncategorizedGo to comment

RDF在电力系统有着很广泛的应用,之前产品的一个功能需要解析别的系统中导出的RDF数据,后来选用了惠普的Jena来辅助完成,趁今天有空,把内容整理下。不过现在好像HP已经停止对Jena的更新了。对RDF不了解的同学,推荐这个入门。

Jena有着无比强大的功能,我只是用到了其中一小块而已。这里举例的数据是实例数据,非模型。但是Jena两种都支持。

这个是生产环境中RDF文件的一部分,其实你可以理解为XML。模型文件就是那个传说中的CIM模型,这篇文章只是一个简单的介绍,需要补课的自己Google之。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 <?xml version="1.0" encoding="GB2312"?> <rdf:RDF xml:base="tsinghua" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/2003/CIM-schema-cim10#"> <cim:TransformerWinding rdf:ID="_ADB84FDEE5244889B31EC1739F812754"> <cim:TransformerWinding.b>0.000000</cim:TransformerWinding.b> <cim:TransformerWinding.r>5.766110</cim:TransformerWinding.r> <cim:TransformerWinding.ratedMVA>123.000000</cim:TransformerWinding.ratedMVA> <cim:TransformerWinding.windingType rdf:resource="http://iec.ch/TC57/2003/CIM-schema-cim10#WindingType.primary"/> <cim:TransformerWinding.x>50.572449</cim:TransformerWinding.x> <cim:Naming.name>High Winding</cim:Naming.name> <cim:TransformerWinding.MemberOf_PowerTransformer rdf:resource="#_41BB4445675643FA9E5A48B6CD71790E"/> </cim:TransformerWinding> <cim:TransformerWinding rdf:ID="_7AEF968E56DC455280804D6BADB94E31"> <cim:TransformerWinding.b>0.000000</cim:TransformerWinding.b> <cim:TransformerWinding.r>0.000000</cim:TransformerWinding.r> <cim:TransformerWinding.ratedMVA>123.000000</cim:TransformerWinding.ratedMVA> <cim:TransformerWinding.windingType rdf:resource="http://iec.ch/TC57/2003/CIM-schema-cim10#WindingType.secondary"/> <cim:TransformerWinding.x>0.000000</cim:TransformerWinding.x> <cim:Naming.name>Low Winding</cim:Naming.name> <cim:TransformerWinding.MemberOf_PowerTransformer rdf:resource="#_41BB4445675643FA9E5A48B6CD71790E"/> </cim:TransformerWinding> </rdf:RDF>

首先需要定义一些常量

1 2 3 4 final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns"; final String CIM = "http://iec.ch/TC57/2003/CIM-schema-cim10"; final String RDF_TYPE = RDF + "#" + "type"; final String CIM_PRIX = "cim:";

从RDF读取数据

1 2 3 4 5 6 7 8 9 10 11 12 public void read(String path) { this.model = ModelFactory.createDefaultModel(); // use the FileManager to find the input file java.io.InputStream in = FileManager.get().open(path); if (in == null) { throw new IllegalArgumentException("File: " + path + " not found"); } // read the RDF/XML file this.model.read(in, com.hp.hpl.jena.vocabulary.RDF.getURI(), "RDF/XML-ABBREV"); }

获取实例数据的所有类

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public List<String> getInstanceClasses() { ResIterator subjects = this.model.listSubjects(); List<String> classNames = new ArrayList<String>(); while (subjects.hasNext()) { // 类 Resource subject = subjects.next(); // type属性 Property property = this.model.createProperty(RDF_TYPE); StmtIterator properties = subject.listProperties(property); while (properties.hasNext()) { RDFNode object = properties.nextStatement().getObject(); String type = object.asNode().getLocalName(); if (type.equals("")) continue; type = CIM_PRIX + type; if (!classNames.contains(type)) { classNames.add(type); } } } subjects.close(); return classNames; }

找出制定类型的所有实例数据

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public HashMap<String, HashMap<String, Object>> getCollectionByClass(String className) { HashMap<String, HashMap<String, Object>> values = new HashMap<String, HashMap<String, Object>>(); Property predicate = this.model.createProperty(RDF_TYPE); if (className.startsWith(CIM_PRIX)) className = className.substring(4); Resource object = this.model.createResource(CIM + "#" + className); Selector selector = new SimpleSelector(null, predicate, object); StmtIterator stmtIterator = this.model.listStatements(selector); while (stmtIterator.hasNext()) { Statement nextStatement = stmtIterator.nextStatement(); // 类 Resource subject = nextStatement.getSubject(); String rdfId = subject.getLocalName(); HashMap<String, Object> propertiesmMap = new HashMap<String, Object>(); StmtIterator properties = subject.listProperties(); while (properties.hasNext()) { Statement statement = properties.nextStatement(); String property = statement.getPredicate().getLocalName(); property = CIM_PRIX + property; RDFNode rdfNode = statement.getObject(); if (rdfNode.isURIResource()) {// Resource String uri = rdfNode.asNode().getLocalName(); if (property.equals("cim:type")) {// 类名 rdfId = String.format("%s/%s", uri, rdfId); } else {// 关联属性 if (propertiesmMap.containsKey(property)) { Object o = propertiesmMap.get(property); List<String> temp = new ArrayList<String>(); if (o.getClass().isArray()) { String[] ids = (String[]) o; temp.addAll(Arrays.asList(ids)); } else { temp.add(uri); } temp.add(uri); propertiesmMap.put(property, temp.toArray(new String[] {})); } else { propertiesmMap.put(property, uri); } } } else {// 普通属性 propertiesmMap.put(property, rdfNode.toString()); } } values.put(rdfId, propertiesmMap); } stmtIterator.close(); return values; }

上面的代码片段只是示例性质的,在生产环境中还有比较复杂的业务逻辑需要处理。

 Jena解析RDF数据  rdf数据

rdf数据

rdf是资源描述框架的简称,在java中可以用jena来解析,在python中可以用rdflib等解析!这里有简明教程:http://www.w3school.com.cn/rdf/index.asp

Jena框架是一个RDF,RDFS以及OWL(Web Ontology Language)的开源实现, 它包含着一个基于规则的推论引擎.它提供了建立和操纵RDF模型的java API.本文介绍的SemanticRDFUtils.bat是一种使用Jena开发的脚本.使用这种脚本,我们可以维护存储在关系数据库和文本文件中的元数据模型.

由于之前系统学过SPARQL的理论和实践,但都不够系统,为此重新进行总结一下:1 工具环境采用:Sesame-workbench来直接测试SPARQL语句的正确性.2 编码环境采用:jena来测试相关代码应用.3 测试数据:如下内容,并保存为UTF-8的文件test.rdf文件<?xml version="1.0" encoding="UTF-8"?>