首先,你不能使用「供應商2與郵政編碼334「,然後」供應商2與郵政編碼335「,因爲它是相同的個人,並且您在兩次申請中將看到」供應商2與郵政編碼334和郵政編碼334「。
有一些變體的實現。
帶滑動耶拿API:
Model model; // your model
Resource supplierClass = model.getResource(YOUR_NS + "Supplier");
Resource buyerClass = model.getResource(YOUR_NS + "Buyer");
Resource procClass = model.getResource(YOUR_NS + "Procedure");
Property zipCodeProp = model.getProperty(YOUR_NS + "zipCode");
Property hasBuyerProp = model.getProperty(YOUR_NS + "hasBuyer");
Property hasSupplierProp = model.getProperty(YOUR_NS + "hasSupplier");
StmtIterator iter =
model.listStatements(new SimpleSelector(null, zipCodeProp, "333"));
while (iter.hasNext()) {
Resource subject = iter.next().getSubject();
if (!subject.hasProperty(RDF.type))
continue;
Resource subjectClass = subject.getPropertyResourceValue(RDF.type);
SimpleSelector sel;
if (subjectClass.equals(supplierClass))
sel = new SimpleSelector(null, hasSupplierProp, subject);
else if (subjectClass.equals(buyerClass))
sel = new SimpleSelector(null, hasBuyerProp, subject);
else
continue;
StmtIterator innerIter = model.listStatements(sel);
while (innerIter.hasNext()) {
Resource proc = innerIter.next().getSubject();
if (!proc.hasProperty(RDF.type) ||
!proc.getPropertyResourceValue(RDF.type).equals(procClass))
continue;
// now you can retrieve linked entities from this procedure
}
}
和SPARQL查詢:
PREFIX yourns: <YOUR_NS>
SELECT DISTINCT ?proc
{
?proc a yourns:Procedure;
yourns:hasBuyer ?buyer;
yourns:hasSupplier ?supplier.
?supplier zipCode ?supplierZip.
?buyer zipCode ?buyerZip.
FILTER (?supplierZip = '333' || ?buyerZip = '333')
}
進一步使用ARQ的:
Query query = QueryFactory.create(queryString);
QueryExecution qe = QueryExecutionFactory.create(query, model);
ResultSet results = qe.execSelect();
while (results.hasNext()) {
QuerySolution qs = results.next();
Resource proc = qs.getResource("proc");
// now again you can retrieve linked entities
}
列弗您好,感謝您的回覆。本週我不能嘗試你的解決方案。下週我會的。 NG PS:我忘記更改更新號碼了) –