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) {
}
}
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class SesameContextTest method testStoreToContextEnabledYard.
/**
* Test visibility of Entities added to specific contexts
* @throws YardException
*/
@Test
public void testStoreToContextEnabledYard() throws YardException {
//add a new entity to yard 2
String context2added = "http://www.test.org/addedEntity";
Representation rep = RdfValueFactory.getInstance().createRepresentation(context2added);
rep.addReference(rdfType.stringValue(), skosConcept.stringValue());
rep.addNaturalText(skosPrefLabel.stringValue(), "added Entity", "en");
rep.addNaturalText(skosPrefLabel.stringValue(), "hinzugefüte Entity", "de");
yard2.store(rep);
//test visibility to other yards
Assert.assertFalse(yard1.isRepresentation(context2added));
Assert.assertTrue(yard2.isRepresentation(context2added));
Assert.assertTrue(unionYard.isRepresentation(context2added));
//remove it and test again
yard2.remove(context2added);
Assert.assertFalse(yard1.isRepresentation(context2added));
Assert.assertFalse(yard2.isRepresentation(context2added));
Assert.assertFalse(unionYard.isRepresentation(context2added));
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class SolrYardTest method testFieldQuery.
@Test
public void testFieldQuery() throws YardException {
// NOTE: this does not test if the updated view of the representation is
// stored, but only that the update method works correctly
Yard yard = getYard();
String id1 = "urn:yard.test.testFieldQuery:representation.id1";
String id2 = "urn:yard.test.testFieldQuery:representation.id2";
String field = "urn:the.field:used.for.testFieldQuery";
Representation test1 = create(id1, true);
Representation test2 = create(id2, true);
// change the representations to be sure to force an update even if the
// implementation checks for changes before updating a representation
test1.add(field, "This is the text content of a field with value1.");
test2.add(field, "This is the text content of a field with value2.");
Iterable<Representation> updatedIterable = yard.update(Arrays.asList(test1, test2));
assertNotNull(updatedIterable);
FieldQuery query = yard.getQueryFactory().createFieldQuery();
query.setConstraint(field, new TextConstraint(Arrays.asList("text content")));
QueryResultList<Representation> results = yard.find(query);
assertEquals(2, results.size());
// fetch the light / minimal representation
query = yard.getQueryFactory().createFieldQuery();
query.setConstraint(field, new TextConstraint(Arrays.asList("value2")));
results = yard.find(query);
assertEquals(1, results.size());
Representation result = results.iterator().next();
assertEquals("urn:yard.test.testFieldQuery:representation.id2", result.getId());
assertEquals(null, result.getFirst(field));
// fetch the full representation
results = yard.findRepresentation(query);
assertEquals(1, results.size());
result = results.iterator().next();
assertEquals("urn:yard.test.testFieldQuery:representation.id2", result.getId());
assertEquals("This is the text content of a field with value2.", result.getFirst(field));
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class EntityPersisterRunnable method store.
/**
* Stores the parsed Representations to the {@link #yard} and
* {@link #sendError(String, String, Exception)} for entities that could
* not be stored!
* @param toStore the Representations to store. This method removes all
* Elements of this map while doing the work
*/
private Set<QueueItem<Representation>> store(Map<String, QueueItem<Representation>> toStore) {
String errorMsg;
YardException yardException = null;
Set<QueueItem<Representation>> stored = new HashSet<QueueItem<Representation>>();
Collection<Representation> reps = new ArrayList<Representation>(toStore.size());
for (QueueItem<Representation> item : toStore.values()) {
reps.add(item.getItem());
}
try {
for (Representation r : yard.store(reps)) {
QueueItem<Representation> old = toStore.remove(r.getId());
//create a new QueueItem and copy the metadata of the old one
stored.add(new QueueItem<Representation>(r, old));
}
errorMsg = "Entity %s was not indexed by the Yard %s";
} catch (YardException e) {
errorMsg = "Unable to store Entity %s to Yard %s because of an YardException";
yardException = e;
}
//the remaining Items in to store have some errors
for (QueueItem<Representation> entry : toStore.values()) {
sendError(entry.getItem().getId(), entry, String.format(errorMsg, entry.getItem().getId(), yard.getId()), yardException);
}
//clear the
toStore.clear();
return stored;
}
use of org.apache.stanbol.entityhub.servicesapi.model.Representation in project stanbol by apache.
the class EntityProcessorRunnable method run.
@Override
public void run() {
while (!isQueueFinished()) {
QueueItem<Representation> item = consume();
if (item != null) {
Long start = Long.valueOf(System.currentTimeMillis());
item.setProperty(PROCESS_STARTED, start);
Iterator<EntityProcessor> it = processors.iterator();
Representation processed = item.getItem();
log.trace("> process {}", processed);
EntityProcessor processor = null;
while (processed != null && it.hasNext()) {
processor = it.next();
log.trace(" - with {}", processor);
processed = processor.process(processed);
}
if (processed == null) {
log.debug("Item {} filtered by processor {}", item.getItem().getId(), processor);
} else {
log.trace(" - done");
for (String key : keys) {
//consume the property and add it to the
//transformed representation
Object value = item.removeProperty(key);
if (value != null) {
processed.add(key, value);
}
}
QueueItem<Representation> produced = new QueueItem<Representation>(processed, item);
Long completed = Long.valueOf(System.currentTimeMillis());
produced.setProperty(PROCESS_COMPLETE, completed);
produced.setProperty(PROCESS_DURATION, Float.valueOf((float) (completed.longValue() - start.longValue())));
produce(produced);
}
}
}
setFinished();
}
Aggregations