use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.
the class TextAnnotationNewModelEngineTest method init.
@BeforeClass
public static void init() throws IOException, ConfigurationException {
InputStream in = TextAnnotationNewModelEngineTest.class.getClassLoader().getResourceAsStream(TEST_ENHANCEMENTS);
Assert.assertNotNull("Unable to load reaource '" + TEST_ENHANCEMENTS + "' via Classpath", in);
origEnhancements = new IndexedGraph();
rdfParser.parse(origEnhancements, in, SupportedFormat.RDF_XML, null);
Assert.assertFalse(origEnhancements.isEmpty());
//parse the ID of the ContentItem form the enhancements
Iterator<Triple> it = origEnhancements.filter(null, Properties.ENHANCER_EXTRACTED_FROM, null);
Assert.assertTrue(it.hasNext());
RDFTerm id = it.next().getObject();
Assert.assertTrue(id instanceof IRI);
ciUri = (IRI) id;
//validate that the enhancements in the file are valid
//NOTE: the input data are no longer fully valid to test some features of this engine
// because of that this initial test is deactivated
// EnhancementStructureHelper.validateAllTextAnnotations(
// origEnhancements, SINGLE_SENTENCE, null,
// false); //those do not yet contain fise:selection-prefix/suffix values
//init the engine
engine = new TextAnnotationsNewModelEngine();
Dictionary<String, Object> config = new Hashtable<String, Object>();
config.put(EnhancementEngine.PROPERTY_NAME, "test-engine");
config.put(TextAnnotationsNewModelEngine.PROPERTY_PREFIX_SUFFIX_SIZE, Integer.valueOf(10));
ctx = new MockComponentContext(config);
engine.activate(ctx);
}
use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.
the class RefactorEnhancementEngine method computeEnhancements.
@Override
public void computeEnhancements(ContentItem ci) throws EngineException {
// Prepare the OntoNet environment. First we create the OntoNet session in which run the whole
final Session session;
try {
session = sessionManager.createSession();
} catch (SessionLimitException e1) {
throw new EngineException("OntoNet session quota reached. The Refactor Engine requires its own new session to execute.");
}
if (session == null)
throw new EngineException("Failed to create OntoNet session. The Refactor Engine requires its own new session to execute.");
log.debug("Refactor enhancement job will run in session '{}'.", session.getID());
// Retrieve and filter the metadata graph for entities recognized by the engines.
final Graph metadataGraph = ci.getMetadata(), signaturesGraph = new IndexedGraph();
// FIXME the Stanbol Enhancer vocabulary should be retrieved from somewhere in the enhancer API.
final IRI ENHANCER_ENTITY_REFERENCE = new IRI("http://fise.iks-project.eu/ontology/entity-reference");
Iterator<Triple> tripleIt = metadataGraph.filter(null, ENHANCER_ENTITY_REFERENCE, null);
while (tripleIt.hasNext()) {
// Get the entity URI
RDFTerm obj = tripleIt.next().getObject();
if (!(obj instanceof IRI)) {
log.warn("Invalid IRI for entity reference {}. Skipping.", obj);
continue;
}
final String entityReference = ((IRI) obj).getUnicodeString();
log.debug("Trying to resolve entity {}", entityReference);
// Populate the entity signatures graph, by querying either the Entity Hub or the dereferencer.
if (engineConfiguration.isEntityHubUsed()) {
Graph result = populateWithEntity(entityReference, signaturesGraph);
if (result != signaturesGraph && result != null) {
log.warn("Entity Hub query added triples to a new graph instead of populating the supplied one!" + " New signatures will be discarded.");
}
} else
try {
OntologyInputSource<Graph> source = new GraphContentSourceWithPhysicalIRI(dereferencer.resolve(entityReference), org.semanticweb.owlapi.model.IRI.create(entityReference));
signaturesGraph.addAll(source.getRootOntology());
} catch (FileNotFoundException e) {
log.error("Failed to dereference entity " + entityReference + ". Skipping.", e);
continue;
}
}
try {
/*
* The dedicated session for this job will store the following: (1) all the (merged) signatures
* for all detected entities; (2) the original content metadata graph returned earlier in the
* chain.
*
* There is no chance that (2) could be null, as it was previously controlled by the JobManager
* through the canEnhance() method and the computeEnhancement is always called iff the former
* returns true.
*/
session.addOntology(new GraphSource(signaturesGraph));
session.addOntology(new GraphSource(metadataGraph));
} catch (UnmodifiableOntologyCollectorException e1) {
throw new EngineException("Cannot add enhancement graph to OntoNet session for refactoring", e1);
}
try {
/*
* Export the entire session (incl. entities and enhancement graph) as a single merged ontology.
*
* TODO the refactorer should have methods to accommodate an OntologyCollector directly instead.
*/
OWLOntology ontology = session.export(OWLOntology.class, true);
log.debug("Refactoring recipe IRI is : " + engineConfiguration.getRecipeId());
/*
* We pass the ontology and the recipe IRI to the Refactor that returns the refactored graph
* expressed by using the given vocabulary.
*
* To perform the refactoring of the ontology to a given vocabulary we use the Stanbol Refactor.
*/
Recipe recipe = ruleStore.getRecipe(new IRI(engineConfiguration.getRecipeId()));
log.debug("Recipe {} contains {} rules.", recipe, recipe.getRuleList().size());
log.debug("The ontology to be refactor is {}", ontology);
Graph tc = refactorer.graphRefactoring(OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology), recipe);
/*
* The newly generated ontology is converted to Clarezza format and then added os substitued to
* the old mGraph.
*/
if (engineConfiguration.isInGraphAppendMode()) {
log.debug("Metadata of the content will replace old ones.", this);
} else {
metadataGraph.clear();
log.debug("Content metadata will be appended to the existing ones.", this);
}
metadataGraph.addAll(tc);
} catch (RefactoringException e) {
String msg = "Refactor engine execution failed on content item " + ci + ".";
log.error(msg, e);
throw new EngineException(msg, e);
} catch (NoSuchRecipeException e) {
String msg = "Refactor engine could not find recipe " + engineConfiguration.getRecipeId() + " to refactor content item " + ci + ".";
log.error(msg, e);
throw new EngineException(msg, e);
} catch (Exception e) {
throw new EngineException("Refactor Engine has failed.", e);
} finally {
/*
* The session needs to be destroyed anyhow.
*
* Clear contents before destroying (FIXME only do this until this is implemented in the
* destroySession() method).
*/
for (OWLOntologyID id : session.listManagedOntologies()) {
try {
String key = ontologyProvider.getKey(id.getOntologyIRI());
ontologyProvider.getStore().deleteGraph(new IRI(key));
} catch (Exception ex) {
log.error("Failed to delete triple collection " + id, ex);
continue;
}
}
sessionManager.destroySession(session.getID());
}
}
use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.
the class RefactorEnhancementEngine method populateWithEntity.
/**
* Fetch the OWLOntology containing the graph associated to an entity from Linked Data. It uses the Entity
* Hub for accessing LOD and fetching entities.
*
* @param entityURI
* {@link String}
* @return the {@link OWLOntology} of the entity
*/
private Graph populateWithEntity(String entityURI, Graph target) {
log.debug("Requesting signature of entity {}", entityURI);
Graph graph = target != null ? target : new IndexedGraph();
// Query the Entity Hub
Entity signature = referencedSiteManager.getEntity(entityURI);
if (signature != null) {
RdfRepresentation rdfSignature = RdfValueFactory.getInstance().toRdfRepresentation(signature.getRepresentation());
graph.addAll(rdfSignature.getRdfGraph());
}
return graph;
}
use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.
the class LDPathHelper method executeLDPath.
/**
* Executes the LDPath program on the contexts stored in the backend and
* returns the result as an RDF graph
* @param contexts the contexts to execute the program on
* @param ldpath the LDPath program to execute
* @param backend the {@link RDFBackend} to use
* @return The results stored within an RDF graph
* @throws LDPathParseException if the parsed LDPath program is invalid
*/
private static Graph executeLDPath(RDFBackend<Object> backend, String ldpath, Set<String> contexts) throws LDPathParseException {
Graph data = new IndexedGraph();
RdfValueFactory vf = new RdfValueFactory(data);
EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
Program<Object> program = ldPath.parseProgram(getReader(ldpath));
if (log.isDebugEnabled()) {
log.debug("Execute on Context(s) '{}' LDPath program: \n{}", contexts, program.getPathExpression(backend));
}
/*
* NOTE: We do not need to process the Representations returned by
* EntityhubLDPath#exdecute, because the RdfValueFactory used uses
* the local variable "Graph data" to backup all created
* RdfRepresentation. Because of this all converted data will be
* automatically added the Graph. The only thing we need to do is to
* wrap the Graph in the response.
*/
for (String context : contexts) {
ldPath.execute(vf.createReference(context), program);
}
return data;
}
use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.
the class RdfValueFactoryTest method testNullNodeRepresentation.
@Test(expected = IllegalArgumentException.class)
public void testNullNodeRepresentation() {
Graph graph = new IndexedGraph();
valueFactory.createRdfRepresentation(null, graph);
}
Aggregations