Package org.openrdf.repository

Examples of org.openrdf.repository.RepositoryConnection


    public QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
        if(parsedQuery == null){
            throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
        }
        final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
        RepositoryConnection con = null;
        TupleQueryResult results = null;
        try {
            con = repository.getConnection();
            con.begin();
            //execute the query
            int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(),
                getConfig().getMaxQueryResultNumber());
            results = executeSparqlFieldQuery(con, query, limit, false);
            //parse the results
            List<String> ids = limit > 0 ? new ArrayList<String>(limit) : new ArrayList<String>();
            while(results.hasNext()){
                BindingSet result = results.next();
                Value value = result.getValue(query.getRootVariableName());
                if(value instanceof Resource){
                    ids.add(value.stringValue());
                }
            }
            con.commit();
            return new QueryResultListImpl<String>(query,ids,String.class);
        } catch (RepositoryException e) {
            throw new YardException("Unable to execute findReferences query", e);
        } catch (QueryEvaluationException e) {
            throw new YardException("Unable to execute findReferences query", e);
        } finally {
            if(results != null) { //close the result if present
                try {
                    results.close();
                } catch (QueryEvaluationException ignore) {/* ignore */}
            }
            if(con != null){
                try {
                    con.close();
                } catch (RepositoryException ignore) {/* ignore */}
            }
        }
    }
View Full Code Here


    public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
        if(parsedQuery == null){
            throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
        }
        final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
        RepositoryConnection con = null;
        TupleQueryResult results = null;
        try {
            con = repository.getConnection();
            con.begin();
            //execute the query
            int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(),
                getConfig().getMaxQueryResultNumber());
            results = executeSparqlFieldQuery(con,query, limit, false);
            //parse the results and generate the Representations
            //create an own valueFactors so that all the data of the query results
            //are added to the same Sesame Model
            Model model = new TreeModel();
            RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
            List<Representation> representations = limit > 0 ?
                    new ArrayList<Representation>(limit) : new ArrayList<Representation>();
            while(results.hasNext()){
                BindingSet result = results.next();
                Value value = result.getValue(query.getRootVariableName());
                if(value instanceof URI){
                    //copy all data to the model and create the representation
                    RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI)value);
                    model.add(queryRoot, queryResult, value); //link the result with the query result
                    representations.add(rep);
                } //ignore non URI results
            }
            con.commit();
            return new SesameQueryResultList(model, query, representations);
        } catch (RepositoryException e) {
            throw new YardException("Unable to execute findReferences query", e);
        } catch (QueryEvaluationException e) {
            throw new YardException("Unable to execute findReferences query", e);
        } finally {
            if(results != null) { //close the result if present
                try {
                    results.close();
                } catch (QueryEvaluationException ignore) {/* ignore */}
            }
            if(con != null){
                try {
                    con.close();
                } catch (RepositoryException ignore) {/* ignore */}
            }
        }
    }
View Full Code Here

    public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
        if(parsedQuery == null){
            throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
        }
        final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
        RepositoryConnection con = null;
        TupleQueryResult results = null;
        try {
            con = repository.getConnection();
            con.begin();
            //execute the query
            int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(),
                getConfig().getMaxQueryResultNumber());
            results = executeSparqlFieldQuery(con,query, limit, true);
            //parse the results and generate the Representations
            //create an own valueFactors so that all the data of the query results
            //are added to the same Sesame Model
            Model model = new TreeModel();
            RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
            List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit)
                    : new ArrayList<Representation>();
            Map<String,URI> bindings = new HashMap<String,URI>(query.getFieldVariableMappings().size());
            for(Entry<String,String> mapping : query.getFieldVariableMappings().entrySet()){
                bindings.put(mapping.getValue(), sesameFactory.createURI(mapping.getKey()));
            }
            while(results.hasNext()){
                BindingSet result = results.next();
                Value value = result.getValue(query.getRootVariableName());
                if(value instanceof URI){
                    URI subject = (URI) value;
                    //link the result with the query result
                    model.add(queryRoot, queryResult, subject);
                    //now copy over the other selected data
                    for(String binding : result.getBindingNames()){
                        URI property = bindings.get(binding);
                        if(property != null){
                            model.add(subject, property, result.getValue(binding));
                        } //else no mapping for the query.getRootVariableName()
                    }
                    //create a representation and add it to the results
                    representations.add(valueFactory.createRdfRepresentation(subject));
                } //ignore non URI results
            }
            con.commit();
            return new SesameQueryResultList(model, query, representations);
        } catch (RepositoryException e) {
            throw new YardException("Unable to execute findReferences query", e);
        } catch (QueryEvaluationException e) {
            throw new YardException("Unable to execute findReferences query", e);
        } finally {
            if(results != null) { //close the result if present
                try {
                    results.close();
                } catch (QueryEvaluationException ignore) {/* ignore */}
            }
            if(con != null){
                try {
                    con.close();
                } catch (RepositoryException ignore) {/* ignore */}
            }
        }
    }
View Full Code Here

            return;
        }

        try
        {
            RepositoryConnection repositoryConnection = rdfRepository.getConnection();
            repositoryConnection.export( rdfxmlWriter );
        }
        catch ( RepositoryException e )
        {
            e.printStackTrace();
        }
View Full Code Here

            return;
        }

        try
        {
            RepositoryConnection repositoryConnection = rdfRepository.getConnection();
            repositoryConnection.export( rdfxmlWriter );
        }
        catch ( RepositoryException e )
        {
            e.printStackTrace();
        }
View Full Code Here

            throw new MojoExecutionException( e.getMessage() );
        }

        try
        {
            RepositoryConnection repositoryConnection = rdfRepository.getConnection();
            repositoryConnection.export( rdfxmlWriter );
        }
        catch ( RepositoryException e )
        {
            throw new MojoExecutionException( e.getMessage() );
        }
View Full Code Here

 
  public List<Statement> sparqlQuery(String sparqlQuery) {
    List<Statement> graph = new ArrayList<Statement>();

    try {
      RepositoryConnection repCon = rdfRep.getConnection();
      try {
        GraphQueryResult graphResult = repCon.prepareGraphQuery(QueryLanguage.SPARQL, sparqlQuery).evaluate();

        try {
          while (graphResult.hasNext()) {
            graph.add(graphResult.next());
          }         
        } finally {
          graphResult.close();
        }             
      } finally {
        repCon.close();
      }     

    } catch (Exception e) {
      e.printStackTrace();
    }
View Full Code Here

  @Override
  public List<Statement> getStatements(Resource subject, URI predicate, Value object, boolean allowInference) {
    List<Statement> resGraph = new ArrayList<Statement>();

    try {
      RepositoryConnection repCon = rdfRep.getConnection();

      try {
       
       
        RepositoryResult<Statement> statements = repCon.getStatements(subject, predicate, object, allowInference);

        try {
          resGraph.addAll(statements.asList());
        }
        finally {
          statements.close();
        }
      } finally {
        repCon.close();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
View Full Code Here

   * @see org.data2semantics.tools.rdf.RDFDataSet#removeStatements(org.openrdf.model.Resource, org.openrdf.model.URI, org.openrdf.model.Value)
   */
  @Override
  public void removeStatements(Resource subject, URI predicate, Value object) {
    try {
      RepositoryConnection repCon = rdfRep.getConnection();

      try {
        repCon.remove(subject, predicate, object);
      } finally {
        repCon.close();
      }

    } catch (Exception e) {
      e.printStackTrace();
   
View Full Code Here

  public static void extractObjectProperties(Repository repository, String GraphIRI, String filename) {

    try {
      PrintWriter resultFile = new PrintWriter(new File(filename));
      RepositoryConnection con = repository.getConnection();
      try {

        long start = System.currentTimeMillis();
        String from = GraphIRI == null || GraphIRI.trim().isEmpty() ? "" : "FROM <" + GraphIRI + "> ";

        String queryString =
            "SELECT DISTINCT ?c1 ?p ?c2 (COUNT(?p) as ?count) " +
                from +
                "WHERE { ?x rdf:type ?c1. " +
                "?y rdf:type ?c2. " +
                "?x ?p ?y. " +
                "FILTER(!STRSTARTS(STR(?c1), \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\")) " +
                "FILTER(!STRSTARTS(STR(?c2), \"http://www.w3.org/1999/02/22-rdf-syntax-ns#\")) " +
                "FILTER(!STRSTARTS(STR(?c1), \"http://www.w3.org/2000/01/rdf-schema#\")) " +
                "FILTER(!STRSTARTS(STR(?c2), \"http://www.w3.org/2000/01/rdf-schema#\")) " +
                "FILTER(!STRSTARTS(STR(?c1), \"http://www.w3.org/2002/07/owl#\")) " +
                "FILTER(!STRSTARTS(STR(?c2), \"http://www.w3.org/2002/07/owl#\")) " +
                "} " +
                "GROUP BY ?c1 ?p ?c2";

        System.out.println(queryString);

        TupleQuery tupleQuery = con.prepareTupleQuery(QueryLanguage.SPARQL, queryString);
        TupleQueryResult result = tupleQuery.evaluate();
        try {
          while (result.hasNext()) {
            BindingSet bindingSet = result.next();
            Value valueOfC1 = bindingSet.getValue("c1");
            Value valueOfP = bindingSet.getValue("p");
            Value valueOfC2 = bindingSet.getValue("c2");
            Value valueOfCount = bindingSet.getValue("count");

            if (valueOfC1 != null) {
              System.out.print(valueOfC1.stringValue() + "\t");
              resultFile.print(valueOfC1.stringValue() + ",");
            }
            if (valueOfP != null) {
              System.out.println(valueOfP.stringValue() + "\t");
              resultFile.print(valueOfP.stringValue() + ",");
            }
            if (valueOfC2 != null) {
              System.out.println(valueOfC2.stringValue() + "\t");
              resultFile.print(valueOfC2.stringValue() + ",");
            }
            if (valueOfCount != null) {
              System.out.println(valueOfCount.stringValue() + "\t");
              resultFile.print(valueOfCount.stringValue());
            }
            System.out.println();
            resultFile.print("\n");
         
        } finally {
          long responseTime = System.currentTimeMillis() - start;
          logger.info("response time: " + (responseTime/1000F));
          result.close();
        }

      } catch (MalformedQueryException e) {
        e.printStackTrace();
      } catch (QueryEvaluationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      } finally {
        con.close();
        resultFile.close();
      }

    }
    catch (RepositoryException e) {
View Full Code Here

TOP

Related Classes of org.openrdf.repository.RepositoryConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.