/**
  * Execute an ASK query
  *
  * @param query
  * @return
  */
 public boolean executeAskQuery(String query) {
   Query query1 = QueryFactory.create(query);
   QueryExecution qexec = QueryExecutionFactory.create(query1, ontologie);
   try {
     if (query.toLowerCase().contains("ask")) {
       return (qexec.execAsk());
     }
   } finally {
     qexec.close();
   }
   return false;
 }
Beispiel #2
0
 public Boolean executeAskQuery(Query sparql, QuerySolution initialBindings) {
   try {
     Data data = getData();
     QueryExecution queryExec = getQueryExecution(sparql, initialBindings, data);
     boolean result = queryExec.execAsk();
     queryExec.close();
     data.close();
     return result;
   } catch (DataException e) {
     e.printStackTrace();
     return null;
   }
 }
  public static void main(String[] args) {
    Store store = HBaseRdfFactory.connectStore(args[0]);
    Model model = HBaseRdfFactory.connectNamedModel(store, args[1]);
    int numOfRuns = new Integer(args[2]).intValue();

    for (int i = 1; i <= numOfRuns; i++) {
      long startTime = System.nanoTime();
      try {
        String queryString =
            " PREFIX xsd:  <http://www.w3.org/2001/XMLSchema#> "
                + " PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
                + " PREFIX foaf: <http://xmlns.com/foaf/0.1/> "
                + " PREFIX dc:   <http://purl.org/dc/elements/1.1/> "
                + " ASK { "
                + " 		?erdoes rdf:type foaf:Person . "
                + "		?erdoes foaf:name \"Paul Erdoes\"^^xsd:string . "
                + "		{ "
                + "			?document dc:creator ?erdoes . "
                + "			?document dc:creator ?author . "
                + "			?document2 dc:creator ?author . "
                + "			?document2 dc:creator ?author2 . "
                + "			?author2 foaf:name ?name "
                + "			FILTER (?author!=?erdoes && ?document2!=?document && ?author2!=?erdoes && ?author2!=?author) "
                + "		} UNION { "
                + "					?document dc:creator ?erdoes . "
                + "					?document dc:creator ?author . "
                + "					?author foaf:name ?name "
                + "					FILTER (?author!=?erdoes) "
                + "				} "
                + "	  } ";
        QueryExecution qexec = QueryExecutionFactory.create(queryString, model);
        if (qexec.execAsk()) System.out.println("executed ask query correctly");
        else System.out.println("did not execute ask query");
        qexec.close();
      } catch (Exception e) {
        e.printStackTrace();
      }
      long endTime = System.nanoTime();
      System.out.println("Time to run Q12b:: " + (endTime - startTime) * 1e-6 + " ms.");
    }
  }
Beispiel #4
0
  void runTestAsk(Query query, QueryExecution qe) throws Exception {
    boolean result = qe.execAsk();
    if (results != null) {
      if (results.isBoolean()) {
        boolean b = results.getBooleanResult();
        assertEquals("ASK test results do not match", b, result);
      } else {
        Model resultsAsModel = results.getModel();
        StmtIterator sIter =
            results.getModel().listStatements(null, RDF.type, ResultSetGraphVocab.ResultSet);
        if (!sIter.hasNext()) throw new QueryTestException("Can't find the ASK result");
        Statement s = sIter.nextStatement();
        if (sIter.hasNext()) throw new QueryTestException("Too many result sets in ASK result");
        Resource r = s.getSubject();
        Property p = resultsAsModel.createProperty(ResultSetGraphVocab.getURI() + "boolean");

        boolean x = r.getRequiredProperty(p).getBoolean();
        if (x != result) assertEquals("ASK test results do not match", x, result);
      }
    }
    return;
  }
  // evict
  public boolean evict() {

    this.dropQueryString = "";

    // limit the eviction times so as to save time during benchmark
    if (this.evictCounter > this.totalEvictTimes) {
      this.evictCounter = 1;
      return false;
    } else {
      this.evictCounter++;
    }
    // if evict the whole cache each time
    if (this.evictAmount == this.size) {
      while (this.cacheContentOfGraphIds.size() != 0) {
        GraphIdCounterPair x = this.cacheContentOfGraphIds.poll();
        this.dropQueryString += "drop graph <" + x.graphId + ">;";
      }
    }
    // when evictAmount < size
    else {
      // check if the amount of the expired data exceeds the evictAmount
      while (this.toDeleteCounter < this.evictAmount) {
        GraphIdCounterPair x = this.cacheContentOfGraphIds.poll();
        this.dropQueryString += "drop graph <" + x.graphId + ">;";
        ++toDeleteCounter;
      }
    }
    // if this.toDeleteCounter >= this.evictAmount, dropQueryString will be
    // empty.
    if (!this.dropQueryString.equals("")) {
      // delete the data from the database
      AGQuery sparql = AGQueryFactory.create(dropQueryString);
      QueryExecution qe = AGQueryExecutionFactory.create(sparql, model);
      qe.execAsk();
      qe.close();
    }
    return true;
  }
  public String executeQuery(String query, String t) {
    String res = "";
    Query query1 = QueryFactory.create(query);
    QueryExecution qexec = QueryExecutionFactory.create(query1, ontologie);
    Pattern p = Pattern.compile("([0-9]+\\.[0-9])");

    try {
      if (query.toLowerCase().contains("ask")) {
        boolean resultsASK = qexec.execAsk();
        if (resultsASK) {
          return "true";
        } else {
          return "false";
        }
      } else if (query.toLowerCase().contains("select")) {
        ResultSet results = qexec.execSelect();
        res = "<table border=\"1\"><tr><th>Variable</th><th>Value</th></tr>";
        while (results.hasNext()) {
          QuerySolution next = results.next();
          Iterator<String> varNames = next.varNames();

          while (varNames.hasNext()) {
            String next1 = varNames.next(); // nomde la variable dans le select
            System.out.println("next1= " + next1);
            String e = next.get(next1).toString(); // valeur que prend  la variable
            // ignorer les rdf:type de type resource, owl:prop....
            if (e.equals("http://www.w3.org/2000/01/rdf-schema#Class")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Property")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#Datatype")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#List")
                || e.equals("http://www.w3.org/2004/03/trix/rdfg-1Graph")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#subPropertyOf")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#range")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#Resource")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#Literal")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#label")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#subClassOf")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#subject")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#object")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#nil")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#range")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#rest")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#first")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#comment")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#seeAlso")
                || e.equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#seeAlso")
                || e.equals("http://www.w3.org/2000/01/rdf-schema#domain")) {
              continue;
            }
            if (t != null && !t.isEmpty()) { // appliquer le trust
              //                            String[] split_res=next.get(next1).toString().split("
              // ");
              //                            for(String s:split_res){
              Matcher m = p.matcher(next.get(next1).toString());
              if (m.find()) {
                float parseInt = Float.parseFloat(next.get(next1).toString());
                float parseInt1 = Float.parseFloat(t);
                e = Float.toString(parseInt * parseInt1);
              }
              //                            res = res + "<tr> <td><b>" + next1 + ": </b></td><td>" +
              // e+"</td></tr>";
              //                            }
            } // else
            {
              res = res = res + " <tr> <td><b>" + next1 + ": </b></td><td>" + e + "</td></tr>";
            }
          }
          res = res = res + " <tr> <td>New line</td></tr>";
        }

        res = res + "</table>";
        ResultSetFormatter.out(System.out, results, query1);
      } else { // Cas du describe
        Model describeModel = qexec.execDescribe();

        res = describeModel.toString();
      }
    } finally {
      qexec.close();
    }

    return res;
  }
  // sparql
  public void sparql() throws IOException, RepositoryException, QueryEvaluationException {

    // find all expired data to avoid them participating the query:
    this.toDeleteCounter = 0;
    this.dropQueryString = "";
    ArrayList<GraphIdCounterPair> expiredData = new ArrayList<GraphIdCounterPair>();
    LocalTime evictionTime = LocalTime.now();
    for (GraphIdCounterPair x : this.cacheContentOfGraphIds) {
      System.out.print(
          this.evictCounter
              + ", "
              + this.size
              + ", "
              + this.evictAmount
              + ", "
              + x.graphId
              + ", "
              + x.arrivalTime
              + ", "
              + x.expirationTime);
      if (x.expirationTime.isBefore(evictionTime)) {
        expiredData.add(x);
        dropQueryString += "drop graph <" + x.graphId + ">;";
        System.out.println(", expired");
        ++toDeleteCounter;
      } else {
        System.out.println();
      }
    }
    System.out.println("[INFO] " + expiredData.size() + " data expired!");
    if (!expiredData.isEmpty()) {
      // delete expired data from the cache
      for (GraphIdCounterPair x : expiredData) {
        this.cacheContentOfGraphIds.remove(x);
      }
      // delete the expired data from the database
      QueryExecution qe =
          AGQueryExecutionFactory.create(AGQueryFactory.create(dropQueryString), model);
      qe.execAsk();
      qe.close();
    }

    // after deleting expired data, load the cache again
    this.streamEmulation();
    System.out.println(this.reasoner.getEntailmentRegime());
    this.infModel = new AGInfModel(this.reasoner, this.model);
    String queryString =
        "select distinct ?s "
            + "where { ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>"
            + "<http://swat.cse.lehigh.edu/onto/univ-bench.owl#Professor>.}";
    AGRepositoryConnection conn = this.client.getAGConn();
    TupleQuery tupleQuery = conn.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
    tupleQuery.setIncludeInferred(true);
    long sparqlStartTime = System.currentTimeMillis();
    TupleQueryResult resultSet = tupleQuery.evaluate();
    long sparqlEndTime = System.currentTimeMillis();
    this.aveSparql += (sparqlEndTime - sparqlStartTime);

    ArrayList<String> results = new ArrayList<String>();
    while (resultSet.hasNext()) {
      String result = resultSet.next().toString();
      System.out.println("result");
      int length = result.length();
      results.add(result.substring(3, length - 1));
    }
    resultSet.close();

    this.fMeasureBench(results);
    this.infModel.close();
  }