如何使用 RDFBeans 和 Apache Jena 反序列化 Java 个对象
How to deserialize Java Objects with RDFBeans and Apache Jena
问题/难题
我很容易反序列化具有简单属性(例如字符串类型)的对象。但是,当另一个类型有 link 时,它不会自动解析,我得到一个异常。 link 似乎没有解析,而是作为简单的 URI-属性.
处理
如何自动反序列化整个对象图?
更新 1
从调试中我知道,通过查询特定谓词的三重存储来解析元素,因此在读取文件之前手动添加这些行时,SubGeographicalRegion
可以解组为 JavaBean。
Class<SubGeographicalRegion> class1 = SubGeographicalRegion.class;
URI class1RdfType = RDFBeanInfo.get(class1).getRDFType();
PlainLiteral class1Literal = model.createPlainLiteral(class1.getName());
model.addStatement(class1RdfType, RDFBeanManager.BINDINGCLASS_PROPERTY, class1Literal);
但是,似乎创建了一个新对象,即使仅引用了同一个 SubGeographicalRegion。
更新 2
根据元素的ID实现hashCode()
和equals()
解决了多实例的问题!
代码
RDF/XML 文件(摘录):
<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/2010/CIM-schema-cim15#">
<cim:SubGeographicalRegion rdf:ID="_93ed4cbc90fe424caa7f572e9652997">
<cim:IdentifiedObject.name>SubRegion</cim:IdentifiedObject.name>
<cim:SubGeographicalRegion.Region rdf:resource="#_a56f739020054bcd826f675918ab2df"/>
</cim:SubGeographicalRegion>
<cim:Substation rdf:ID="_da9f289336dd46bdac22c961b7b525f3">
<cim:IdentifiedObject.name>73109E0009</cim:IdentifiedObject.name>
<cim:Substation.Region rdf:resource="#_93ed4cbc90fe424caa7f572e9652997"/>
</cim:Substation>
<cim:Substation rdf:ID="_e66d0514110841c285b7956c98e52b32">
<cim:IdentifiedObject.name>73019J0003</cim:IdentifiedObject.name>
<cim:Substation.Region rdf:resource="#_93ed4cbc90fe424caa7f572e9652997"/>
</cim:Substation>
</rdf:RDF>
Substation.java:
@RDFBean("http://iec.ch/TC57/2010/CIM-schema-cim15#Substation")
public class Substation {
private String id;
private String name;
private SubGeographicalRegion subRegion;
@RDFSubject
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@RDF("http://iec.ch/TC57/2010/CIM-schema-cim15#IdentifiedObject.name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@RDF("http://iec.ch/TC57/2010/CIM-schema-cim15#Substation.Region")
public SubGeographicalRegion getSubRegion() {
return subRegion;
}
public void setSubRegion(SubGeographicalRegion subRegion) {
this.subRegion = subRegion;
}
}
SubGeographicalRegion.java:
@RDFBean("http://iec.ch/TC57/2010/CIM-schema-cim15#SubGeographicalRegion")
public class SubGeographicalRegion {
private String id;
private String name;
private Collection<Substation> substations;
@RDF("http://iec.ch/TC57/2010/CIM-schema-cim15#IdentifiedObject.name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@RDFSubject(prefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
// this works, but returns a HashSet<URI>
@RDF(inverseOf = "http://iec.ch/TC57/2010/CIM-schema-cim15#Substation.Region")
public Collection<Substation> getSubstations() {
return substations;
}
public void setSubstations(Collection<Substation> substations) {
this.substations = substations;
}
}
提示:方法SubGeographicalRegion.getSubstations()
returns a HashSet<URI>
(尽管它声明为Collection<Substation>
)
这是我启动引擎的方式:
org.ontoware.rdf2go.model.Model model = RDF2Go.getModelFactory().createModel();
model.open();
// try-catch omitted for readability
model.readFrom(getClass().getResourceAsStream(RDF_XML_INPUT_FILENAME), Syntax.RdfXml);
RDFBeanManager rdfBeanManager = new RDFBeanManager(model);
ClosableIterator<Substation> substationIterator = rdfBeanManager.getAll(Substation.class);
while (substationIterator.hasNext()) {
logger.info("substation.name: ", substationIterator.next().getName());
}
model.close();
问题/难题
我很容易反序列化具有简单属性(例如字符串类型)的对象。但是,当另一个类型有 link 时,它不会自动解析,我得到一个异常。 link 似乎没有解析,而是作为简单的 URI-属性.
处理如何自动反序列化整个对象图?
更新 1
从调试中我知道,通过查询特定谓词的三重存储来解析元素,因此在读取文件之前手动添加这些行时,SubGeographicalRegion
可以解组为 JavaBean。
Class<SubGeographicalRegion> class1 = SubGeographicalRegion.class;
URI class1RdfType = RDFBeanInfo.get(class1).getRDFType();
PlainLiteral class1Literal = model.createPlainLiteral(class1.getName());
model.addStatement(class1RdfType, RDFBeanManager.BINDINGCLASS_PROPERTY, class1Literal);
但是,似乎创建了一个新对象,即使仅引用了同一个 SubGeographicalRegion。
更新 2
根据元素的ID实现hashCode()
和equals()
解决了多实例的问题!
代码
RDF/XML 文件(摘录):
<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/2010/CIM-schema-cim15#">
<cim:SubGeographicalRegion rdf:ID="_93ed4cbc90fe424caa7f572e9652997">
<cim:IdentifiedObject.name>SubRegion</cim:IdentifiedObject.name>
<cim:SubGeographicalRegion.Region rdf:resource="#_a56f739020054bcd826f675918ab2df"/>
</cim:SubGeographicalRegion>
<cim:Substation rdf:ID="_da9f289336dd46bdac22c961b7b525f3">
<cim:IdentifiedObject.name>73109E0009</cim:IdentifiedObject.name>
<cim:Substation.Region rdf:resource="#_93ed4cbc90fe424caa7f572e9652997"/>
</cim:Substation>
<cim:Substation rdf:ID="_e66d0514110841c285b7956c98e52b32">
<cim:IdentifiedObject.name>73019J0003</cim:IdentifiedObject.name>
<cim:Substation.Region rdf:resource="#_93ed4cbc90fe424caa7f572e9652997"/>
</cim:Substation>
</rdf:RDF>
Substation.java:
@RDFBean("http://iec.ch/TC57/2010/CIM-schema-cim15#Substation")
public class Substation {
private String id;
private String name;
private SubGeographicalRegion subRegion;
@RDFSubject
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@RDF("http://iec.ch/TC57/2010/CIM-schema-cim15#IdentifiedObject.name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@RDF("http://iec.ch/TC57/2010/CIM-schema-cim15#Substation.Region")
public SubGeographicalRegion getSubRegion() {
return subRegion;
}
public void setSubRegion(SubGeographicalRegion subRegion) {
this.subRegion = subRegion;
}
}
SubGeographicalRegion.java:
@RDFBean("http://iec.ch/TC57/2010/CIM-schema-cim15#SubGeographicalRegion")
public class SubGeographicalRegion {
private String id;
private String name;
private Collection<Substation> substations;
@RDF("http://iec.ch/TC57/2010/CIM-schema-cim15#IdentifiedObject.name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@RDFSubject(prefix = "http://www.w3.org/1999/02/22-rdf-syntax-ns#ID")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
// this works, but returns a HashSet<URI>
@RDF(inverseOf = "http://iec.ch/TC57/2010/CIM-schema-cim15#Substation.Region")
public Collection<Substation> getSubstations() {
return substations;
}
public void setSubstations(Collection<Substation> substations) {
this.substations = substations;
}
}
提示:方法SubGeographicalRegion.getSubstations()
returns a HashSet<URI>
(尽管它声明为Collection<Substation>
)
这是我启动引擎的方式:
org.ontoware.rdf2go.model.Model model = RDF2Go.getModelFactory().createModel();
model.open();
// try-catch omitted for readability
model.readFrom(getClass().getResourceAsStream(RDF_XML_INPUT_FILENAME), Syntax.RdfXml);
RDFBeanManager rdfBeanManager = new RDFBeanManager(model);
ClosableIterator<Substation> substationIterator = rdfBeanManager.getAll(Substation.class);
while (substationIterator.hasNext()) {
logger.info("substation.name: ", substationIterator.next().getName());
}
model.close();