Advertisement
CodeFerret

call sparql query on model

Feb 11th, 2018
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1.  
  2.     protected static final String QUERY_PROLOG =
  3.             StrUtils.strjoinNL(
  4.                     "prefix : <http://purl.bdrc.io/ontology/core/>",
  5.                     "prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
  6.                     "prefix bdr: <http://purl.bdrc.io/resource/>",
  7.                     "prefix apf: <http://jena.apache.org/ARQ/property#>"
  8.                     );
  9.  
  10.     protected static final String PERSON_DOUBLE_QUERY = StrUtils.strjoinNL(
  11.             QUERY_PROLOG,
  12.             "select ?diff",
  13.             "where {",
  14.             "  {",
  15.             "    select (count(?id) as ?c) (count(distinct ?id) as ?cd)",
  16.             "    where {",
  17.             "      ?R :personName ?b .",
  18.             "      ?b rdfs:label ?nm .",
  19.             "      ?b a ?type .",
  20.             "      bind (lang(?nm) as ?lang) ",
  21.             "      ?id apf:concat(?type '+' ?nm '@' ?lang) .",
  22.             "    }",
  23.             "  } .",
  24.             "  bind (?c - ?cd as ?diff)",
  25.             "}"
  26.             );
  27.  
  28.    
  29.     /*
  30.      * return true if there are doubled triples in the model; otherwise, false
  31.      * currently only tests DocType.PERSON and returns false otherwise
  32.      */
  33.     public static boolean resourceDoubled(String resource, Model m, DocType type) {
  34.         if (type == DocType.PERSON) {
  35.             ParameterizedSparqlString qStr = new ParameterizedSparqlString(PERSON_DOUBLE_QUERY);
  36.             qStr.setIri("?R", resource);
  37.             Query query = QueryFactory.create(qStr.asQuery());
  38.             try (QueryExecution qexec = QueryExecutionFactory.create(query, m)) {
  39.                 ResultSet results = qexec.execSelect() ;
  40.                 if (results.hasNext()) {
  41.                     QuerySolution soln = results.nextSolution();
  42.                     Literal diffLit = soln.getLiteral("diff");
  43.                     int diff = diffLit.getInt();
  44.                     if (diff > 0) {
  45.                         return true;
  46.                     }
  47.                 }
  48.             } catch (Exception ex) {
  49.                 TransferHelpers.logger.error("checkForDoubling failed: " + ex.getMessage(), ex);
  50.             }
  51.         }
  52.            
  53.         return false;
  54.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement