public Map<String, String> getProperties(String query) throws AutoSPARQLException {
    property2LabelMap = new TreeMap<String, String>();

    String queryTriples = query.substring(18, query.length() - 1);

    String newQuery =
        "SELECT DISTINCT ?p ?label WHERE {"
            + queryTriples
            + "?x0 ?p ?o. "
            + "?p <"
            + RDFS.label
            + "> ?label. FILTER(LANGMATCHES(LANG(?label), 'en'))} "
            + "LIMIT 1000";

    ResultSet rs =
        SparqlQuery.convertJSONtoResultSet(selectCache.executeSelectQuery(endpoint, newQuery));
    QuerySolution qs;
    while (rs.hasNext()) {
      qs = rs.next();
      property2LabelMap.put(qs.getResource("p").getURI(), qs.getLiteral("label").getLexicalForm());
    }

    Iterator<String> it = property2LabelMap.keySet().iterator();
    while (it.hasNext()) {
      String uri = it.next();
      if (!uri.startsWith("http://dbpedia.org/ontology")) {
        it.remove();
      }
    }
    property2LabelMap.put(RDFS.label.getURI(), "label");

    return property2LabelMap;
  }
  @Override
  public List<Literal> getDataPropertyValuesForIndividualByProperty(
      String subjectUri, String propertyUri) {
    log.debug("Data property value query string:\n" + DATA_PROPERTY_VALUE_QUERY_STRING);
    log.debug("Data property value:\n" + dataPropertyValueQuery);

    QuerySolutionMap initialBindings = new QuerySolutionMap();
    initialBindings.add("subject", ResourceFactory.createResource(subjectUri));
    initialBindings.add("property", ResourceFactory.createResource(propertyUri));

    // Run the SPARQL query to get the properties
    List<Literal> values = new ArrayList<Literal>();
    DatasetWrapper w = dwf.getDatasetWrapper();
    Dataset dataset = w.getDataset();
    dataset.getLock().enterCriticalSection(Lock.READ);
    try {
      QueryExecution qexec =
          QueryExecutionFactory.create(dataPropertyValueQuery, dataset, initialBindings);
      ResultSet results = qexec.execSelect();

      while (results.hasNext()) {
        QuerySolution soln = results.next();
        RDFNode node = soln.get("value");
        if (!node.isLiteral()) {
          continue;
        }
        Literal value = soln.getLiteral("value");
        values.add(value);
      }
    } finally {
      dataset.getLock().leaveCriticalSection();
      w.close();
    }
    return values;
  }
Example #3
0
  @Test
  public void testNextWithLiteral() throws Exception {
    when(mockValue.getString()).thenReturn("x");
    final QuerySolution solution = testObj.next();

    assertTrue(solution.contains("a"));
    assertEquals("x", solution.get("a").asLiteral().getLexicalForm());
    assertEquals(solution.get("a"), solution.getLiteral("a"));
  }
Example #4
0
  private boolean checkAlreadyAgreed() {
    //		System.err.println(license);
    if (license.exists() && user != null && !user.equals("")) {
      try {
        model.read(new FileInputStream(license), null);

        QueryExecution qexec = QueryExecutionFactory.create(queryString, model);
        try {
          ResultSet results = qexec.execSelect();
          while (results.hasNext()) {
            QuerySolution soln = results.nextSolution();
            try {
              Literal u = soln.getLiteral("user");
              //							System.err.println("user: "******"software");
              //							System.err.println("software: "+s);
              Calendar d = ((XSDDateTime) soln.getLiteral("date").getValue()).asCalendar();
              //							System.err.println("date: "+d);

              if (u.getString().equals(user)
                  && s.getString().equals(software)
                  && d.before(Calendar.getInstance())) {
                return true;
              }
            } catch (JenaException e) {
              // ignore
            } catch (ClassCastException e) {
              System.err.println(e.getMessage());
            }
          }
        } finally {
          qexec.close();
        }

      } catch (FileNotFoundException e) {
      }
    }
    return false;
  }
  private void runSPARQL1_1_Mode() {
    // get subjects with types
    int limit = 1000;
    int offset = 0;
    String queryTemplate =
        "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p COUNT(?s) AS ?count WHERE {?s ?p ?o.?p a owl:DatatypeProperty."
            + "{SELECT ?s ?o WHERE {?s <%s> ?o.} LIMIT %d OFFSET %d}"
            + "}";
    String query;
    Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>();
    DatatypeProperty prop;
    Integer oldCnt;
    boolean repeat = true;

    while (!terminationCriteriaSatisfied() && repeat) {
      query = String.format(queryTemplate, propertyToDescribe, limit, offset);
      ResultSet rs = executeSelectQuery(query);
      QuerySolution qs;
      repeat = false;
      while (rs.hasNext()) {
        qs = rs.next();
        prop = new DatatypeProperty(qs.getResource("p").getURI());
        int newCnt = qs.getLiteral("count").getInt();
        oldCnt = result.get(prop);
        if (oldCnt == null) {
          oldCnt = Integer.valueOf(newCnt);
        } else {
          oldCnt += newCnt;
        }
        result.put(prop, oldCnt);
        qs.getLiteral("count").getInt();
        repeat = true;
      }
      if (!result.isEmpty()) {
        currentlyBestAxioms = buildAxioms(result);
        offset += 1000;
      }
    }
  }
Example #6
0
  public String GetResult() {
    String result_s = "";
    while (results.hasNext()) {
      QuerySolution row = results.next();
      // RDFNode thing= row.get("rule");
      // result_s = row.getLiteral("id").getString();

      result_s = result_s + row.getLiteral("id").getString() + ",";
    }
    if (!result_s.equals("")) {
      result_s = result_s.substring(0, result_s.length() - 1);
    }
    return result_s;
  }
  private void runSPARQL1_0_Mode() {
    Model model = ModelFactory.createDefaultModel();
    int limit = 1000;
    int offset = 0;
    String baseQuery = "CONSTRUCT {?s ?p ?o.} WHERE {?s <%s> ?o. ?s ?p ?o.} LIMIT %d OFFSET %d";
    String query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset);
    Model newModel = executeConstructQuery(query);
    Map<DatatypeProperty, Integer> result = new HashMap<DatatypeProperty, Integer>();
    while (!terminationCriteriaSatisfied() && newModel.size() != 0) {
      model.add(newModel);
      query = "SELECT ?p (COUNT(?s) AS ?count) WHERE {?s ?p ?o.} GROUP BY ?p";

      DatatypeProperty prop;
      Integer oldCnt;
      ResultSet rs = executeSelectQuery(query, model);
      QuerySolution qs;
      while (rs.hasNext()) {
        qs = rs.next();
        prop = new DatatypeProperty(qs.getResource("p").getURI());
        int newCnt = qs.getLiteral("count").getInt();
        oldCnt = result.get(prop);
        if (oldCnt == null) {
          oldCnt = Integer.valueOf(newCnt);
        }
        result.put(prop, oldCnt);
        qs.getLiteral("count").getInt();
      }
      if (!result.isEmpty()) {
        currentlyBestAxioms = buildAxioms(result);
      }

      offset += limit;
      query = String.format(baseQuery, propertyToDescribe.getName(), limit, offset);
      newModel = executeConstructQuery(query);
    }
  }
Example #8
0
 private void cachingForTriples(Table table, String endpoint) {
   Model model = ModelFactory.createDefaultModel();
   List<String> resources = null;
   FileWriter fstream = null;
   try {
     fstream = new FileWriter("/home/mofeed/TrialStart/zicozico.nt");
   } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
   }
   BufferedWriter out = new BufferedWriter(fstream);
   try {
     for (Object id : table.getItemIds()) {
       Item item = table.getItem(id);
       Property sourceURI = item.getItemProperty("sourceURI"); // vaadin property not jena
       Resource resource = model.createResource();
       try {
         String sparqlQuery = "select distinct * where { <" + sourceURI.getValue() + "> ?p  ?o .}";
         Query query = QueryFactory.create(sparqlQuery);
         QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query);
         com.hp.hpl.jena.query.ResultSet results = qexec.execSelect();
         com.hp.hpl.jena.query.QuerySolution binding = null;
         while (results.hasNext()) {
           binding = results.next();
           String property = binding.getResource("?p").toString();
           String value;
           if (binding.get("?o").isResource()) value = binding.getResource("?o").toString();
           else value = binding.getLiteral("?o").toString();
           com.hp.hpl.jena.rdf.model.Property pt = ResourceFactory.createProperty(property);
           // resource.addProperty(pt, value);
           model.add(resource, pt, value);
         }
         qexec.close();
         model.write(out, null, "TURTLE");
       } catch (Exception e) {
         Notification.show(e.toString());
       }
       // model.add(s, p, o);
       // Property destinationURI = item.getItemProperty("destinationURI");
       // out.write(sourceURI.getValue()+"\n");
     }
     /*out.flush();
     fstream.flush();*/
     out.close();
   } catch (Exception e) { // Catch exception if any
     System.err.println("Error: " + e.getMessage());
   }
 }
  public PagingLoadResult<Example> getCurrentQueryResult(PagingLoadConfig config)
      throws SPARQLQueryException {
    logger.debug("Retrieving results for current query.");
    List<Example> queryResult = new ArrayList<Example>();

    String currentQuery = exampleFinder.getCurrentQuery();
    logger.debug("Current query:\n");
    logger.debug(currentQuery);
    int limit = config.getLimit();
    int offset = config.getOffset();
    int totalLength = 10;

    try {
      ResultSetRewindable rs =
          SparqlQuery.convertJSONtoResultSet(
              selectCache.executeSelectQuery(endpoint, getCountQuery(currentQuery)));
      totalLength = rs.next().getLiteral(rs.getResultVars().get(0)).getInt();
    } catch (Exception e) {
      e.printStackTrace();
    }

    try {
      ResultSetRewindable rs =
          SparqlQuery.convertJSONtoResultSet(
              selectCache.executeSelectQuery(
                  endpoint, modifyQuery(currentQuery + " LIMIT " + limit + " OFFSET " + offset)));

      String uri;
      String label = "";
      String imageURL = "";
      String comment = "";
      QuerySolution qs;
      while (rs.hasNext()) {
        qs = rs.next();
        uri = qs.getResource("x0").getURI();
        label = qs.getLiteral("label").getLexicalForm();
        queryResult.add(new Example(uri, label, imageURL, comment));
      }
    } catch (Exception e) {
      logger.error("Error while getting result for query \n" + currentQuery, e);
    }

    PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(queryResult);
    result.setOffset(offset);
    result.setTotalLength(totalLength);

    return result;
  }
Example #10
0
  @Override
  public void execute(Model model) {
    QueryExecution qe = QueryExecutionFactory.create(preparedQuery, model);
    ResultSet rs = qe.execSelect();

    QuerySolution qs;
    MapResult result;
    String x, y;
    if (rs.hasNext()) {
      qs = rs.next();
      x = qs.getLiteral("x").getString();
      y = qs.getLiteral("y").getString();
      result = new MapResult();
      result.put("x", x);
      result.put("y", y);
      this.results.add(result);
    }
    qe.close();
  }
  /**
   * Returns the string value of the first of the properties in the uriDescriptionList for the given
   * resource (as an URI). In case the resource does not have any of the properties mentioned, its
   * URI is returned. The value is obtained by querying the endpoint and the endpoint is queried
   * repeatedly until it gives a response (value or the lack of it)
   *
   * <p>It is highly recommended that the list contains properties like labels or titles, with test
   * values.
   *
   * @param uri - the URI for which a label is required
   * @return a String value, either a label for the parameter or its value if no label is obtained
   *     from the endpoint
   */
  private String getLabelForUri(String uri) {
    String result;

    if (uriLabelCache.containsKey(uri)) {
      return uriLabelCache.get(uri);
    }

    for (String prop : uriDescriptionList) {
      String innerQuery = "SELECT ?r WHERE {<" + uri + "> <" + prop + "> ?r } LIMIT 1";

      try {
        Query query = QueryFactory.create(innerQuery);
        QueryExecution qExec = QueryExecutionFactory.sparqlService(rdfEndpoint, query);
        boolean keepTrying = true;
        while (keepTrying) {
          keepTrying = false;
          try {
            ResultSet results = qExec.execSelect();

            if (results.hasNext()) {
              QuerySolution sol = results.nextSolution();
              result = EEASettings.parseForJson(sol.getLiteral("r").getLexicalForm());
              if (!result.isEmpty()) {
                uriLabelCache.put(uri, result);
                return result;
              }
            }
          } catch (Exception e) {
            keepTrying = true;
            logger.warn("Could not get label for uri {}. Retrying.", uri);
          } finally {
            qExec.close();
          }
        }
      } catch (QueryParseException qpe) {
        logger.error("Exception for query {}. The label cannot be obtained", innerQuery);
      }
    }
    return uri;
  }
Example #12
0
  private void getURIProperties(String subject, String endpoint, Table table) {

    try { // "select * where { <"+subject+"> ?p  ?o .   FILTER(langMatches(lang(?o), \"EN\"))}";
      // idea about limiting results
      String sparqlQuery = "select distinct * where { <" + subject + "> ?p  ?o .}";
      Query query = QueryFactory.create(sparqlQuery);
      QueryExecution qexec = QueryExecutionFactory.sparqlService(endpoint, query);
      com.hp.hpl.jena.query.ResultSet results = qexec.execSelect();
      com.hp.hpl.jena.query.QuerySolution binding = null;
      table.removeAllItems();
      while (results.hasNext()) {
        binding = results.next();
        String property = binding.getResource("?p").toString();
        String value;
        if (binding.get("?o").isResource()) value = binding.getResource("?o").toString();
        else value = binding.getLiteral("?o").toString();
        table.addItem(new Object[] {property, value}, new Integer(table.size() + 1));
      }
      qexec.close();
    } catch (Exception e) {
      Notification.show(e.toString());
    }
  }
  public void teste() {
    // String name = this.getName();
    String title = "";
    String service = "http://dblp.rkbexplorer.com/sparql";
    String query =
        "PREFIX akt:  <http://www.aktors.org/ontology/portal#> "
            + "PREFIX akt:  <http://www.aktors.org/ontology/portal#> "
            + " SELECT DISTINCT ?name ?title "
            + " WHERE "
            + " { "
            + " ?auth akt:full-name "
            + "\""
            + nameAuthorField
            + "\""
            + " . "
            + " ?pub a akt:Publication-Reference ;"
            + " akt:has-title ?title ;"
            + " akt:has-author ?auth ."
            + " } ";
    QueryExecution queryExecution = QueryExecutionFactory.sparqlService(service, query);
    try {
      ResultSet results = queryExecution.execSelect();
      while (results.hasNext()) {
        QuerySolution querySolution = results.nextSolution();
        // consume title
        Literal literal = querySolution.getLiteral("title");
        title = ("" + literal.getValue());

        // add titulo a lista tituloPublicacao
        this.tituloPublicacao.add(title);
      }
    } catch (QueryExceptionHTTP e) {
      System.out.println(service + " is DOWN");
    } finally {
      queryExecution.close();
    }
  }
  public PagingLoadResult<Example> getSPARQLQueryResultWithProperties(
      String query, List<String> properties, PagingLoadConfig config) throws AutoSPARQLException {
    List<Example> queryResult = new ArrayList<Example>();
    //		properties.remove("label");
    int limit = config.getLimit();
    int offset = config.getOffset();
    int totalLength = 10;

    if (currentQueryResultSize == -1) {
      try {
        ResultSetRewindable rs =
            SparqlQuery.convertJSONtoResultSet(
                selectCache.executeSelectQuery(endpoint, getCountQuery(query)));
        currentQueryResultSize = rs.next().getLiteral(rs.getResultVars().get(0)).getInt();
      } catch (Exception e) {
        e.printStackTrace();
        currentQueryResultSize = 10;
      }
    }
    totalLength = currentQueryResultSize;

    List<String> propertiesToDo = new ArrayList<String>(properties);

    for (Map<String, Object> prop2Value : propertiesCache.values()) {
      propertiesToDo.removeAll(prop2Value.keySet());
    }

    if (propertiesToDo.size() > 0) {
      String queryTriples = query.substring(18, query.length() - 1);
      StringBuilder newQuery = new StringBuilder();
      Map<String, String> var2URIMap = new HashMap<String, String>(propertiesToDo.size());

      if (propertiesToDo.size() == 1 && propertiesToDo.get(0).equals(RDFS.label.getURI())) {
        newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL{");
        newQuery.append(queryTriples);
        newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n");
        newQuery
            .append("OPTIONAL{?x0 <")
            .append("http://dbpedia.org/ontology/thumbnail")
            .append("> ?imageURL.}\n");
        newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))");
        newQuery.append("}");
      } else {
        for (String property : propertiesToDo) {
          var2URIMap.put(
              property2LabelMap.get(property).replace(" ", "_").replace("(", "").replace(")", ""),
              property);
        }

        newQuery.append("SELECT DISTINCT ?x0 ?label ?imageURL ");
        for (String var : var2URIMap.keySet()) {
          newQuery.append("?").append(var).append(" ");
          newQuery.append("?").append(var).append("_label ");
        }
        newQuery.append("{");
        newQuery.append(queryTriples);
        newQuery.append("?x0 <").append(RDFS.label).append("> ?label.\n");
        newQuery
            .append("OPTIONAL{?x0 <")
            .append("http://dbpedia.org/ontology/thumbnail")
            .append("> ?imageURL.}\n");
        for (Entry<String, String> entry : var2URIMap.entrySet()) {
          newQuery
              .append("OPTIONAL{?x0 <")
              .append(entry.getValue())
              .append("> ?")
              .append(entry.getKey())
              .append(".}\n");
        }
        for (Entry<String, String> entry : var2URIMap.entrySet()) {
          newQuery
              .append("OPTIONAL{?")
              .append(entry.getKey())
              .append(" <")
              .append(RDFS.label)
              .append("> ?")
              .append(entry.getKey())
              .append("_label.\n");
          newQuery.append("FILTER(LANGMATCHES(LANG(?" + entry.getKey() + "_label),'en'))}\n");
        }
        newQuery.append("FILTER(LANGMATCHES(LANG(?label),'en'))");
        newQuery.append("}");
      }

      logger.debug("Query with properties:\n" + newQuery.toString());
      try {
        ResultSetRewindable rs =
            SparqlQuery.convertJSONtoResultSet(
                selectCache.executeSelectQuery(endpoint, modifyQuery(newQuery + " LIMIT 1000")));

        String uri;
        String label = "";
        String imageURL = "";
        QuerySolution qs;
        RDFNode object;
        while (rs.hasNext()) {
          qs = rs.next();
          uri = qs.getResource("x0").getURI();
          label = qs.getLiteral("label").getLexicalForm();
          imageURL = qs.getResource("imageURL") != null ? qs.getResource("imageURL").getURI() : "";
          Map<String, Object> properties2Value = propertiesCache.get(uri);
          if (properties2Value == null) {
            properties2Value = new HashMap<String, Object>();
            properties2Value.put(RDFS.label.getURI(), label);
            properties2Value.put("http://dbpedia.org/ontology/thumbnail", imageURL);
            propertiesCache.put(uri, properties2Value);
          }

          Object value;
          String property;
          for (Entry<String, String> entry : var2URIMap.entrySet()) {
            value = "";
            property = entry.getValue();

            object = qs.get(entry.getKey() + "_label");
            if (object == null) {
              object = qs.get(entry.getKey());
            }
            if (object != null) {
              if (object.isURIResource()) {
                value = object.asResource().getURI();
              } else if (object.isLiteral()) {
                Literal lit = object.asLiteral();
                //								if(lit.getDatatypeURI().equals(XSD.BOOLEAN)){
                //									property2DatatypeMap.put(property, Boolean.class);
                //									value = lit.getBoolean();
                //								} else if(lit.getDatatypeURI().equals(XSD.INT)){
                //									property2DatatypeMap.put(property, Integer.class);
                //									value = lit.getInt();
                //								} else if(lit.getDatatypeURI().equals(XSD.DOUBLE)){
                //									property2DatatypeMap.put(property, Double.class);
                //									value = lit.getDouble();
                //								} else if(lit.getDatatypeURI().equals(XSD.FLOAT)){
                //									property2DatatypeMap.put(property, Float.class);
                //									value = lit.getFloat();
                //								} else {
                //									property2DatatypeMap.put(property, String.class);
                //									value = object.asLiteral().getLexicalForm();
                //								}
                value = object.asLiteral().getLexicalForm();
              }
            }
            Object oldValue = properties2Value.get(property);
            if (oldValue != null && value != null) {
              value = oldValue + ", " + value;
            }
            properties2Value.put(property, value);
          }
        }
      } catch (Exception e) {
        logger.error("Error while getting result for query \n" + newQuery, e);
      }
    }

    Example example;
    int cnt = 0;
    for (Entry<String, Map<String, Object>> uri2PropertyValues : propertiesCache.entrySet()) {
      //			if(cnt++ == limit+offset){
      //				break;
      //			}
      //			if(cnt > offset){
      example = new Example();
      example.setAllowNestedValues(false);
      example.set("uri", uri2PropertyValues.getKey());
      Object value;
      String property;
      for (Entry<String, Object> property2Value : uri2PropertyValues.getValue().entrySet()) {
        property = property2Value.getKey();
        value = property2Value.getValue();
        //					if(value == null){
        //						Class cls = property2DatatypeMap.get(property);
        //						if(cls == String.class){
        //							value = "";
        //						} else if(cls == Integer.class){
        //							value = Integer.valueOf(-1);
        //						} else if(cls == Double.class){
        //							value = Double.valueOf(-1);
        //						} else if(cls == Float.class){
        //							value = Float.valueOf(-1);
        //						} else if(cls == Boolean.class){
        //							value = Boolean.FALSE;
        //						}
        //					}
        example.set(property, value);
      }
      queryResult.add(example);
      //			}

    }
    if (config.getSortInfo().getSortField() != null) {
      final String sortField = config.getSortInfo().getSortField();
      Collections.sort(
          queryResult,
          config
              .getSortInfo()
              .getSortDir()
              .comparator(
                  new Comparator<Example>() {

                    @Override
                    public int compare(Example o1, Example o2) {
                      return ((String) o1.get(sortField)).compareTo((String) o2.get(sortField));
                    }
                  }));
    }
    int start = config.getOffset();
    int end = queryResult.size();
    if (limit > 0) {
      end = Math.min(start + limit, end);
    }
    //		queryResult = queryResult.subList(start, end);
    ArrayList<Example> tmp = new ArrayList<Example>();
    for (int i = start; i < end; i++) {
      tmp.add(queryResult.get(i));
    }

    PagingLoadResult<Example> result = new BasePagingLoadResult<Example>(tmp);
    result.setOffset(offset);
    result.setTotalLength(totalLength);

    return result;
  }