列出包含字符串的域

发布于 2024-11-28 11:18:09 字数 556 浏览 4 评论 0原文

首先,我是耶拿的新手。我创建了一个本体,有 4 个类,程序、买家、供应商和邮政编码。我有以下属性:

  1. 程序 hasBuyer 买家
  2. 程序 hasSupplier 供应商
  3. 供应商 hasZipCode 邮政编码
  4. 买家 hasZipCode 邮政编码

我想知道什么是耶拿返回包含字符串“3333”的所有域的最佳方法。

例如:

  • 程序 1 的买家 1 的邮政编码为 333,供应商 1 的邮政编码为 333,供应商 2 的邮政编码
  • 为 334。程序 2 的买家 2 的邮政编码为 331,供应商 2 的邮政编码为 334,供应商 2 的邮政编码为 335。
  • 程序 2 3 有买家 3(邮政编码为 333)、供应商 1(邮政编码为 333)和供应商 3(邮政编码为 333) 335.

结果必须是:

  • 程序 - 程序 1 和程序 3
  • 买家 - 买家 1 和买家 3
  • 供应商 - 供应商 1

NG

first off all I´m new to Jena. I´ve created an ontology and i´ve 4 classes, procedure, buyer, supplier and Zip Code.And i´ve the following properties:

  1. Procedure hasBuyer Buyers
  2. Procedure hasSupplier Supplier
  3. Supplier hasZipCode Zip Code
  4. Buyer hasZipCode Zip Code

What i want to know it´s the best approach in Jena to return all domains that contain a string "3333".

For example:

  • Procedure 1 have buyer 1 with zip code 333, supplier 1 with zip code 333 and supplier2 with zip code 334.
  • Procedure 2 have buyer 2 with zip code 331, supplier 2 with zip code 334 and supplier2 with zip code 335.
  • Procedure 3 have buyer 3 with zip code 333, supplier 1 with zip code 333 and supplier3 with zip code 335.

The results must be:

  • Procedures - Procedure1 and Procedure3
  • Buyer - Buyer1 and Buyer 3
  • Supplier - supplier1

NG

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

国粹 2024-12-05 11:18:09

首先,你不能使用“邮政编码334的供应商2”,然后“邮政编码335的供应商2”,因为它是同一个人,你会看到“邮政编码334的供应商2和邮政编码335” 334" 两次都在应用中。

有一些实现的变体。

使用普通 Jena 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
}

First of all, you can't use "supplier 2 with zip code 334", and then "supplier 2 with zip code 335", because it's the same individual, and you'll see "supplier 2 with zip code 334 and zip code 334" in application both times.

There is some variants of realisation.

With plain Jena 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
    }
}

And SPARQL query:

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')
}

with further use of 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文