Sparql 如何将类表达式(从unionOf)转换为字符串?

Sparql 如何将类表达式(从unionOf)转换为字符串?,sparql,jena,protege,Sparql,Jena,Protege,SPARQL查询返回带有allValuesFrom和unionOf限制的结果。我需要计算这些值,但是,当我使用bind或str函数时,结果是空的 我尝试了bind、str和group_concat函数,但都失败了。Group_concat返回一个空节点 SELECT DISTINCT ?source ?is_succeeded_by WHERE { ?source rdfs:subClassOf ?restriction . ?restriction owl:onProper

SPARQL查询返回带有allValuesFrom和unionOf限制的结果。我需要计算这些值,但是,当我使用bind或str函数时,结果是空的

我尝试了bind、str和group_concat函数,但都失败了。Group_concat返回一个空节点

SELECT DISTINCT ?source  ?is_succeeded_by
WHERE {
    ?source rdfs:subClassOf ?restriction . 
    ?restriction owl:onProperty j.0:isSucceededBy . 
    ?restriction owl:allValuesFrom  ?is_succeeded_by .
    FILTER (REGEX(STR(?source), 'gatw-Invoice_match'))
}
Protegé中SPARQL查询的结果:


在Jena中,很难通过编程方式获得类似“xxx或yyy”的字符串, 因为它是Manchester语法,一种OWL-API本机格式,Jena不支持它。 任何类表达式实际上都是b节点,在原始RDF中没有像“或”这样的内置符号

要将任何匿名类表达式表示为字符串,可以使用, 这是一个基于jena的OWL-API,因此,SPARQL和Manchester语法都受支持

以下是一个基于pizza本体的示例:

    // use pizza, since no example data provided in the question:
    IRI pizza = IRI.create("https://raw.githubusercontent.com/owlcs/ont-api/master/src/test/resources/ontapi/pizza.ttl");
    // get OWLOntologyManager instance from ONT-API
    OntologyManager manager = OntManagers.createONT();
    // as extended Jena model:
    OntModel model = manager.loadOntology(pizza).asGraphModel();

    // prepare query that looks like the original, but for pizza
    String txt = "SELECT DISTINCT ?source ?is_succeeded_by\n" +
            "WHERE {\n" +
            "    ?source rdfs:subClassOf ?restriction . \n" +
            "    ?restriction owl:onProperty :hasTopping . \n" +
            "    ?restriction owl:allValuesFrom  ?is_succeeded_by .\n" +
            "    FILTER (REGEX(STR(?source), 'Am'))\n" +
            "}";
    Query q = new Query();
    q.setPrefixMapping(model);
    q = QueryFactory.parse(q, txt, null, Syntax.defaultQuerySyntax);

    // from owlapi-parsers package:
    OWLObjectRenderer renderer = new ManchesterOWLSyntaxOWLObjectRendererImpl();
    // from ont-api (although it is a part of internal API, it is public):
    InternalObjectFactory iof = new SimpleObjectFactory(manager.getOWLDataFactory());

    // exec SPARQL query:
    try (QueryExecution exec = QueryExecutionFactory.create(q, model)) {
        ResultSet res = exec.execSelect();
        while (res.hasNext()) {
            QuerySolution qs = res.next();
            List<Resource> vars = Iter.asStream(qs.varNames()).map(qs::getResource).collect(Collectors.toList());
            if (vars.size() != 2)
                throw new IllegalStateException("For the specified query and valid OWL must not happen");
            // Resource (Jena) -> OntCE (ONT-API) -> ONTObject (ONT-API) -> OWLClassExpression (OWL-API)
            OWLClassExpression ex = iof.getClass(vars.get(1).inModel(model).as(OntClass.class)).getOWLObject();
            // format: 'class local name' ||| 'superclass string in ManSyn'
            System.out.println(vars.get(0).getLocalName() + " ||| " + renderer.render(ex));
        }
    }

Used env:ont api:2.0.0、owl api:5.1.11、jena arq:3.13.1

您是否使用SPARQL选项卡或Snap SPARQL插件?您能否展示您如何尝试计算这些值?或许还可以告诉我预期的输出是什么?
American ||| MozzarellaTopping or PeperoniSausageTopping or TomatoTopping
AmericanHot ||| HotGreenPepperTopping or JalapenoPepperTopping or MozzarellaTopping or PeperoniSausageTopping or TomatoTopping