问题/问题
我已经很容易地通过简单的属性(例如String类型)反序列化对象。但是,当存在到另一个类型的链接时,它不会自动解决,并且会出现异常。似乎该链接未解析,而是作为简单的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()
返回一个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();
最佳答案
您可以考虑直接使用Jena或Sesame代替RDF2Go,后者似乎不再维护。
如果您不介意使用(某些)芝麻API,则可以尝试Pinto。