Search in sources :

Example 31 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class SolrYard method update.

@Override
public final Iterable<Representation> update(Iterable<Representation> representations) throws YardException, IllegalArgumentException, NullPointerException {
    if (representations == null) {
        throw new IllegalArgumentException("The parsed Iterable over Representations MUST NOT be NULL!");
    }
    long start = System.currentTimeMillis();
    Set<String> ids = new HashSet<String>();
    for (Representation representation : representations) {
        if (representation != null) {
            ids.add(representation.getId());
        }
    }
    if (closed) {
        log.warn("The SolrYard '{}' was already closed!", config.getName());
    }
    // for debuging
    int numDocs = ids.size();
    try {
        // returns the ids found in the solrIndex
        ids = checkRepresentations(ids);
    } catch (SolrServerException e) {
        throw new YardException("Error while searching for alredy present documents " + "before executing the actual update for the parsed Representations", e);
    } catch (IOException e) {
        throw new YardException("Unable to access SolrServer", e);
    }
    long checked = System.currentTimeMillis();
    List<SolrInputDocument> inputDocs = new ArrayList<SolrInputDocument>(ids.size());
    List<Representation> updated = new ArrayList<Representation>();
    for (Representation representation : representations) {
        if (representation != null && ids.contains(representation.getId())) {
            // null parsed or not
            // already present
            inputDocs.add(createSolrInputDocument(representation));
            updated.add(representation);
        }
    }
    long created = System.currentTimeMillis();
    if (!inputDocs.isEmpty()) {
        try {
            final UpdateRequest update = new UpdateRequest();
            if (!immediateCommit) {
                update.setCommitWithin(commitWithin);
            }
            update.add(inputDocs);
            AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

                public UpdateResponse run() throws IOException, SolrServerException {
                    update.process(server);
                    if (immediateCommit) {
                        server.commit();
                    }
                    return null;
                }
            });
        } catch (PrivilegedActionException pae) {
            if (pae.getException() instanceof SolrServerException) {
                throw new YardException("Error while adding updated Documents to the SolrServer", pae.getException());
            } else if (pae.getException() instanceof IOException) {
                throw new YardException("Unable to access SolrServer", pae.getException());
            } else {
                throw RuntimeException.class.cast(pae.getException());
            }
        }
    }
    long ready = System.currentTimeMillis();
    log.info(String.format("Processed updateRequest for %d documents (%d in index " + "| %d updated) in %dms (checked %dms|created %dms| stored%dms)", numDocs, ids.size(), updated.size(), ready - start, checked - start, created - checked, ready - created));
    return updated;
}
Also used : UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) PrivilegedActionException(java.security.PrivilegedActionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) IOException(java.io.IOException) Constraint(org.apache.stanbol.entityhub.servicesapi.query.Constraint) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) HashSet(java.util.HashSet)

Example 32 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class ClerezzaYard method findRepresentation.

@Override
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);
    final ResultSet result = executeSparqlFieldQuery(query);
    //Note: An other possibility would be to first iterate over all results and add it to
    //      a list and create this Iterator than based on the List. This would
    //      be the preferenced way if changes in the graph could affect the
    //     Iteration over the SPARQL query results.
    Iterator<Representation> representationIterator = new AdaptingIterator<SolutionMapping, Representation>(result, new AdaptingIterator.Adapter<SolutionMapping, Representation>() {

        /**
                     * Adapter that gets the rootVariable of the Query (selecting the ID)
                     * and creates a Representation for it.
                     * @param solution a solution of the query
                     * @param type the type (no generics here)
                     * @return the representation or <code>null</code> if result is
                     * not an IRI or there is no Representation for the result.
                     */
        @Override
        public Representation adapt(SolutionMapping solution, Class<Representation> type) {
            RDFTerm resource = solution.get(query.getRootVariableName());
            if (resource instanceof IRI) {
                try {
                    return getRepresentation((IRI) resource, false);
                } catch (IllegalArgumentException e) {
                    log.warn("Unable to create Representation for ID " + resource + "! -> ignore query result");
                    return null;
                }
            } else {
                return null;
            }
        }
    }, Representation.class);
    //      created before the method returns.
    return new QueryResultListImpl<Representation>(query, representationIterator, Representation.class);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) SolutionMapping(org.apache.clerezza.rdf.core.sparql.SolutionMapping) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) SparqlFieldQuery(org.apache.stanbol.entityhub.query.clerezza.SparqlFieldQuery) ResultSet(org.apache.clerezza.rdf.core.sparql.ResultSet) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)

Example 33 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class SesameYard method store.

protected final Iterable<Representation> store(Iterable<Representation> representations, boolean allowCreate) throws IllegalArgumentException, YardException {
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        ArrayList<Representation> added = new ArrayList<Representation>();
        for (Representation representation : representations) {
            if (representation != null) {
                //reassign
                Representation stored = store(con, representation, allowCreate, false);
                //to check if the store was successful
                if (stored != null) {
                    added.add(stored);
                } else {
                    //can only be the case if allowCreate==false (update was called)
                    log.warn(String.format("Unable to update Representation %s in Yard %s because it is not present!", representation.getId(), getId()));
                }
            }
        //ignore null values in the parsed Iterable!
        }
        con.commit();
        return added;
    } catch (RepositoryException e) {
        throw new YardException("Unable to remove parsed Representations", e);
    } catch (IllegalArgumentException e) {
        try {
            //to avoid Exception logs in case store(..) throws an Exception
            //in the case allowCreate and canNotCreateIsError do not allow
            //the store operation
            con.rollback();
        } catch (RepositoryException ignore) {
        }
        throw e;
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) ArrayList(java.util.ArrayList) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException)

Example 34 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class SesameYard method findRepresentation.

@Override
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);
                //link the result with the query result
                model.add(queryRoot, queryResult, value);
                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 */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException) URI(org.openrdf.model.URI) TreeModel(org.openrdf.model.impl.TreeModel) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)

Example 35 with Representation

use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.

the class SesameYard method store.

/**
     * Generic store method used by store and update methods
     * @param representation the representation to store/update
     * @param allowCreate if new representation are allowed to be created
     * @param canNotCreateIsError if updates to existing one are allowed
     * @return the representation as added to the yard
     * @throws IllegalArgumentException
     * @throws YardException
     */
protected final Representation store(Representation representation, boolean allowCreate, boolean canNotCreateIsError) throws IllegalArgumentException, YardException {
    RepositoryConnection con = null;
    try {
        con = repository.getConnection();
        con.begin();
        Representation added = store(con, representation, allowCreate, canNotCreateIsError);
        con.commit();
        return added;
    } catch (RepositoryException e) {
        throw new YardException("Unable to remove parsed Representations", e);
    } catch (IllegalArgumentException e) {
        try {
            //to avoid Exception logs in case store(..) throws an Exception
            //in the case allowCreate and canNotCreateIsError do not allow
            //the store operation
            con.rollback();
        } catch (RepositoryException ignore) {
        }
        throw e;
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException)

Aggregations

Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)198 Test (org.junit.Test)117 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)32 HashSet (java.util.HashSet)31 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)25 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)16 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)15 ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)12 FieldQuery (org.apache.stanbol.entityhub.servicesapi.query.FieldQuery)12 ArrayList (java.util.ArrayList)11 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)10 IOException (java.io.IOException)9 IRI (org.apache.clerezza.commons.rdf.IRI)9 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)8 Graph (org.apache.clerezza.commons.rdf.Graph)8 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)8 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)8 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)8 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)8