我很容易设法用简单属性(例如String类型)反序列化对象。但是当存在指向另一个类型的链接时,它不会自动解析,而是会出现异常。似乎链接未解析,但处理为简单的URI属性。
如何自动反序列化整个对象图?
从调试我知道,通过查询特定谓词的三元组来解析元素,因此在读取文件之前手动添加这些行时,可以将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的引用。
根据元素的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();