CodeFerret icon

call sparql query on model

CodeFerret | PRO | 02/11/18 02:44:38 PM UTC | 0 ⭐ | 637 👁️ | Never ⏰ | []
Java |

2.17 KB

|

None

|

0 👍

/

0 👎

 
    protected static final String QUERY_PROLOG = 
            StrUtils.strjoinNL(
                    "prefix : <http://purl.bdrc.io/ontology/core/>",
                    "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
                    "prefix bdr: <http://purl.bdrc.io/resource/>",
                    "prefix apf: <http://jena.apache.org/ARQ/property#>"
                    );
 
    protected static final String PERSON_DOUBLE_QUERY = StrUtils.strjoinNL(
            QUERY_PROLOG,
            "select ?diff",
            "where {",
            "  {",
            "    select (count(?id) as ?c) (count(distinct ?id) as ?cd)",
            "    where {",
            "      ?R :personName ?b .",
            "      ?b rdfs:label ?nm .",
            "      ?b a ?type .",
            "      bind (lang(?nm) as ?lang) ",
            "      ?id apf:concat(?type '+' ?nm '@' ?lang) .",
            "    }",
            "  } .",
            "  bind (?c - ?cd as ?diff)",
            "}"
            );
 
    
    /*
     * return true if there are doubled triples in the model; otherwise, false
     * currently only tests DocType.PERSON and returns false otherwise
     */
    public static boolean resourceDoubled(String resource, Model m, DocType type) {
        if (type == DocType.PERSON) {
            ParameterizedSparqlString qStr = new ParameterizedSparqlString(PERSON_DOUBLE_QUERY);
            qStr.setIri("?R", resource);
            Query query = QueryFactory.create(qStr.asQuery());
            try (QueryExecution qexec = QueryExecutionFactory.create(query, m)) {
                ResultSet results = qexec.execSelect() ;
                if (results.hasNext()) {
                    QuerySolution soln = results.nextSolution();
                    Literal diffLit = soln.getLiteral("diff");
                    int diff = diffLit.getInt();
                    if (diff > 0) {
                        return true;
                    }
                }
            } catch (Exception ex) {
                TransferHelpers.logger.error("checkForDoubling failed: " + ex.getMessage(), ex);
            } 
        }
            
        return false;
    }

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎