我正在尝试将OpenRDF Alibaba(与Sesame相关联)作为将 Java 对象映射到 RDF 三元组并再次映射回来的工具。目前,我正在研究它如何处理对象图。
我有两个对象,内部和外部。外部引用了内部。当我坚持一个外部实例时,似乎内部实例被表示为 b 节点,即使我之前已经用分配的 IRI 坚持了内部实例。
我必须做些什么才能自己成功分配内部实例的 IRI,而不是创建 b 节点?
额外的信用问题:我怎样才能使资源 IRI 成为 Java 对象的属性,而不是让它与它识别的对象平行但断开连接?
代码:
内:
package alibabaeval.domain;
import org.openrdf.annotations.Iri;
@Iri("http://example.com/innerType")
public class Inner {
@Iri("http://example.com/innerType/data")
private String data;
public Inner(String data) {
this.data = data;
}
// if this is missing, an unhelpful ClassCastException will be thrown on retrieval
public Inner() {
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
外:
package alibabaeval.domain;
import org.openrdf.annotations.Iri;
@Iri("http://example.com/outerType")
public class Outer {
@Iri("http://example.com/outerType/data")
private String outerData;
@Iri("http://example.com/outerType/innerObject")
private Inner innerObject;
public Outer(String outerData) {
this.outerData = outerData;
}
// if this is missing, an unhelpful ClassCastException will be thrown on retrieval
public Outer() {
}
public String getOuterData() {
return outerData;
}
public void setOuterData(String outerData) {
this.outerData = outerData;
}
public Inner getInnerObject() {
return innerObject;
}
public void setInnerObject(Inner innerObject) {
this.innerObject = innerObject;
}
}
测试程序:
package alibabaeval;
import org.junit.Test;
import org.openrdf.model.URI;
import org.openrdf.model.ValueFactory;
import org.openrdf.query.QueryLanguage;
import org.openrdf.repository.Repository;
import org.openrdf.repository.object.ObjectConnection;
import org.openrdf.repository.object.ObjectRepository;
import org.openrdf.repository.object.config.ObjectRepositoryFactory;
import org.openrdf.repository.sail.SailRepository;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFWriter;
import org.openrdf.rio.Rio;
import org.openrdf.sail.memory.MemoryStore;
import alibabaeval.domain.Inner;
import alibabaeval.domain.Outer;
public class AlibabaEval {
public static void main(String[] args) throws Exception {
Repository store = new SailRepository(new MemoryStore());
store.initialize();
// wrap in an object repository
ObjectRepositoryFactory factory = new ObjectRepositoryFactory();
ObjectRepository repository = factory.createRepository(store);
// add a stuff to the repository
ObjectConnection con = repository.getConnection();
ValueFactory vf = con.getValueFactory();
Inner inner = new Inner("some inner data");
URI innerId = vf.createURI("http://example.com/inners/inner1");
con.addObject(innerId, inner);
URI outerId = vf.createURI("http://example.com/outers/outer1");
Outer outer = new Outer("some outer data");
outer.setInnerObject(inner);
con.addObject(outerId, outer);
// look at the triples that were created
System.out.println("\n\n\nGenerated triples:");
RDFWriter writer = Rio.createWriter(RDFFormat.NTRIPLES, System.out);
con.prepareGraphQuery(QueryLanguage.SPARQL, "CONSTRUCT { ?s ?p ?o } WHERE {?s ?p ?o }").evaluate(writer);
// close everything down
con.close();
repository.shutDown();
}
}
输出:
我只创建了两个对象实例,并将它们分别持久化。阿里巴巴似乎忽略了这一点,并创建了 Inner-instance 的第二个副本作为 b 节点,以供 Outer-instance 引用。
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-jdk14/1.7.7/25d160723ea37a6cb84e87cd70773ff02997e857/slf4j-jdk14-1.7.7.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/me/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-log4j12/1.7.12/485f77901840cf4e8bf852f2abb9b723eb8ec29/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.JDK14LoggerFactory]
Jan 08, 2016 6:00:21 PM org.openrdf.repository.object.managers.helpers.Scanner scan
INFO: Scanning C:\workspace\AlibabaTest\bin for concepts
Jan 08, 2016 6:00:22 PM org.openrdf.repository.object.ObjectRepository compileSchema
INFO: Compiling schema
Jan 08, 2016 6:00:22 PM org.openrdf.repository.object.composition.ClassResolver setBaseClassRoles
WARNING: Concept will only be mergable: class alibabaeval.domain.Inner
Jan 08, 2016 6:00:22 PM org.openrdf.repository.object.composition.ClassResolver setBaseClassRoles
WARNING: Concept will only be mergable: class alibabaeval.domain.Outer
Generated triples:
<http://example.com/inners/inner1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/innerType> .
<http://example.com/inners/inner1> <http://example.com/innerType/data> "some inner data" .
<http://example.com/outers/outer1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/outerType> .
_:node1a8hqu4aqx1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/innerType> .
_:node1a8hqu4aqx1 <http://example.com/innerType/data> "some inner data" .
<http://example.com/outers/outer1> <http://example.com/outerType/innerObject> _:node1a8hqu4aqx1 .
<http://example.com/outers/outer1> <http://example.com/outerType/data> "some outer data" .