Search in sources :

Example 6 with ValueFactory

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

the class RepresentationTest method testMultipleAddAndRemove.

@Test
public void testMultipleAddAndRemove() throws MalformedURLException, URISyntaxException {
    String field = "urn:the.field:used.for.this.Test";
    ValueFactory vf = getValueFactory();
    Representation rep = createRepresentation(null);
    Reference ref = vf.createReference("http://www.test.org/test");
    Text text = vf.createText("test", "en");
    Integer i = 42;
    Double d = Math.PI;
    URI uri = new URI("http://www.test.org/uriTest");
    URL url = new URL("http://www.test.org/urlTest");
    String[] textAsArray = new String[] { "Test text as Array", "en" };
    Collection<Object> values = Arrays.asList(ref, text, i, d);
    Collection<Object> convertedValues = Arrays.asList((Object) url, uri, textAsArray);
    Collection<Object> allValues = Arrays.asList(ref, text, i, d, uri, url, textAsArray);
    // test adding of collections
    rep.add(field, values);
    assertTrue(asCollection(rep.get(field)).size() == 4);
    rep.remove(field, values);
    assertFalse(rep.get(field).hasNext());
    // test adding of Iterators
    rep.add(field, values.iterator());
    assertTrue(asCollection(rep.get(field)).size() == 4);
    rep.remove(field, values.iterator());
    assertFalse(rep.get(field).hasNext());
    // test adding of Enumerations
    Vector<Object> v = new Vector<Object>(values);
    rep.add(field, v.elements());
    assertTrue(asCollection(rep.get(field)).size() == 4);
    rep.remove(field, v.elements());
    assertFalse(rep.get(field).hasNext());
    // test adding and removing elements that need to be converted
    // only for collections this time -> want to test only converting is
    // applied for both add and remove
    rep.add(field, convertedValues);
    assertTrue(asCollection(rep.get(field)).size() == 3);
    rep.remove(field, convertedValues);
    assertFalse(rep.get(field).hasNext());
    // a final test to ensure, that remove does not only delete all values
    rep.add(field, allValues);
    assertTrue(asCollection(rep.get(field)).size() == 7);
    rep.remove(field, convertedValues);
    assertTrue(asCollection(rep.get(field)).size() == 4);
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) URI(java.net.URI) URL(java.net.URL) Vector(java.util.Vector) Test(org.junit.Test)

Example 7 with ValueFactory

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

the class SiteManagerRootResource method executeLDPathQuery.

/**
     * Execute a Query that uses LDPath to process results.
     * @param query the query
     * @param mediaType the mediaType for the response
     * @param headers the http headers of the request
     * @return the response
     */
private Response executeLDPathQuery(SiteManager manager, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
    QueryResultList<Representation> result;
    ValueFactory vf = new RdfValueFactory(new IndexedGraph());
    SiteManagerBackend backend = new SiteManagerBackend(manager);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    //copy the selected fields, because we might need to delete some during
    //the preparation phase
    Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
    //first prepare (only execute the query if the parameters are valid)
    Program<Object> program;
    try {
        program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
    } catch (LDPathParseException e) {
        log.warn("Unable to parse LDPath program used as select for a Query to the '/sites' endpoint:");
        log.warn("FieldQuery: \n {}", query);
        log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
        log.warn("Exception:", e);
        return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
    } catch (IllegalStateException e) {
        log.warn("parsed LDPath program is not compatible with the Query " + "parsed to the '/sites' endpoint!", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //2. execute the query
    // we need to adapt from Entity to Representation
    //TODO: should we add the metadata to the result?
    Iterator<Representation> resultIt = new AdaptingIterator<Entity, Representation>(manager.findEntities(query).iterator(), new AdaptingIterator.Adapter<Entity, Representation>() {

        @Override
        public Representation adapt(Entity value, Class<Representation> type) {
            return value.getRepresentation();
        }
    }, Representation.class);
    //process the results
    Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
    result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
    ResponseBuilder rb = Response.ok(result);
    rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) AdaptingIterator(org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SiteManagerBackend(org.apache.stanbol.entityhub.ldpath.backend.SiteManagerBackend) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Example 8 with ValueFactory

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

the class ReferencedSiteRootResource method executeLDPathQuery.

/**
     * Execute a Query that uses LDPath to process results.
     * @param query the query
     * @param mediaType the mediaType for the response
     * @param headers the http headers of the request
     * @return the response
     */
private Response executeLDPathQuery(Site site, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
    QueryResultList<Representation> result;
    ValueFactory vf = new RdfValueFactory(new IndexedGraph());
    SiteBackend backend = new SiteBackend(site, vf);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    //copy the selected fields, because we might need to delete some during
    //the preparation phase
    Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
    //first prepare (only execute the query if the parameters are valid)
    Program<Object> program;
    try {
        program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
    } catch (LDPathParseException e) {
        log.warn("Unable to parse LDPath program used as select for Query:");
        log.warn("FieldQuery: \n {}", query);
        log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
        log.warn("Exception:", e);
        return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
    } catch (IllegalStateException e) {
        log.warn("parsed LDPath program is not compatible with parsed Query!", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //2. execute the query
    Iterator<Representation> resultIt;
    try {
        // we need to adapt from Entity to Representation
        resultIt = new AdaptingIterator<Entity, Representation>(site.findEntities(query).iterator(), new AdaptingIterator.Adapter<Entity, Representation>() {

            @Override
            public Representation adapt(Entity value, Class<Representation> type) {
                return value.getRepresentation();
            }
        }, Representation.class);
    } catch (SiteException e) {
        String message = String.format("Unable to Query Site '%s' (message: %s)", site.getId(), e.getMessage());
        log.error(message, e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //process the results
    Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
    result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
    ResponseBuilder rb = Response.ok(result);
    rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) SiteBackend(org.apache.stanbol.entityhub.ldpath.backend.SiteBackend) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ManagedSiteException(org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Example 9 with ValueFactory

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

the class EntityhubRootResource method executeLDPathQuery.

/**
     * Execute a Query that uses LDPath to process results.
     * @param query the query
     * @param mediaType the mediaType for the response
     * @param headers the http headers of the request
     * @return the response
     */
private Response executeLDPathQuery(Entityhub entityhub, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
    QueryResultList<Representation> result;
    ValueFactory vf = new RdfValueFactory(new IndexedGraph());
    EntityhubBackend backend = new EntityhubBackend(entityhub);
    EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
    //copy the selected fields, because we might need to delete some during
    //the preparation phase
    Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
    //first prepare (only execute the query if the parameters are valid)
    Program<Object> program;
    try {
        program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
    } catch (LDPathParseException e) {
        log.warn("Unable to parse LDPath program used as select for Query:");
        log.warn("FieldQuery: \n {}", query);
        log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
        log.warn("Exception:", e);
        return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
    } catch (IllegalStateException e) {
        log.warn("parsed LDPath program is not compatible with parsed Query!", e);
        return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //2. execute the query
    Iterator<Representation> resultIt;
    try {
        // go directly to the yard and query there for Representations
        resultIt = entityhub.getYard().findRepresentation(query).iterator();
    } catch (EntityhubException e) {
        String message = String.format("Exception while performing the " + "FieldQuery on the EntityHub (message: %s)", e.getMessage());
        log.error(message, e);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, mediaType).build();
    }
    //process the results
    Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
    result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
    ResponseBuilder rb = Response.ok(result);
    rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : EntityhubBackend(org.apache.stanbol.entityhub.ldpath.backend.EntityhubBackend) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) LDPathSelect(org.apache.stanbol.entityhub.ldpath.query.LDPathSelect) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) RdfValueFactory(org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) HashSet(java.util.HashSet)

Example 10 with ValueFactory

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

the class ValueFactoryTest method testRef.

/**
     * Internally used to create and test {@link Reference}s for the different tests
     * 
     * @param refObject
     *            the object representing the reference
     * @return the created {@link Reference} that can be used to perform further tests.
     */
private Reference testRef(Object refObject) {
    ValueFactory vf = getValueFactory();
    Reference ref = vf.createReference(refObject);
    // check not null
    assertNotNull(ref);
    // check reference is not null
    assertNotNull(ref.getReference());
    return ref;
}
Also used : Reference(org.apache.stanbol.entityhub.servicesapi.model.Reference) ValueFactory(org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)

Aggregations

ValueFactory (org.apache.stanbol.entityhub.servicesapi.model.ValueFactory)17 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)14 Reference (org.apache.stanbol.entityhub.servicesapi.model.Reference)7 Test (org.junit.Test)7 URI (java.net.URI)4 HashSet (java.util.HashSet)4 LDPathParseException (org.apache.marmotta.ldpath.exception.LDPathParseException)4 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)4 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)4 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)3 LDPathSelect (org.apache.stanbol.entityhub.ldpath.query.LDPathSelect)3 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)3 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)3 Yard (org.apache.stanbol.entityhub.servicesapi.yard.Yard)3 URL (java.net.URL)2 ManagedSiteException (org.apache.stanbol.entityhub.servicesapi.site.ManagedSiteException)2 AdaptingIterator (org.apache.stanbol.entityhub.servicesapi.util.AdaptingIterator)2 YardTest (org.apache.stanbol.entityhub.test.yard.YardTest)2 StringReader (java.io.StringReader)1