use of org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource in project stanbol by apache.
the class TestClerezzaSpaces method setup.
@BeforeClass
public static void setup() throws Exception {
offline = new OfflineConfigurationImpl(new Hashtable<String, Object>());
ScopeRegistry reg = new ScopeRegistryImpl();
// This one is created from scratch
Graph ont2 = ClerezzaOWLUtils.createOntology(baseIri2.toString());
minorSrc = new GraphSource(ont2.getImmutableGraph());
dropSrc = getLocalSource("/ontologies/droppedcharacters.owl");
nonexSrc = getLocalSource("/ontologies/nonexistentcharacters.owl");
inMemorySrc = new ParentPathInputSource(new File(TestClerezzaSpaces.class.getResource("/ontologies/maincharacters.owl").toURI()));
OWLDataFactory df = OWLManager.getOWLDataFactory();
OWLClass cHuman = df.getOWLClass(IRI.create(baseIri + "/" + Constants.humanBeing));
OWLIndividual iLinus = df.getOWLNamedIndividual(IRI.create(baseIri + "/" + Constants.linus));
linusIsHuman = df.getOWLClassAssertionAxiom(cHuman, iLinus);
factory = new ClerezzaCollectorFactory(new ClerezzaOntologyProvider(tcManager, offline, parser), new Hashtable<String, Object>());
factory.setDefaultNamespace(IRI.create("http://stanbol.apache.org/ontology/"));
}
use of org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource in project stanbol by apache.
the class ScopeResource method registerScope.
/**
* At least one between corereg and coreont must be present. Registry iris supersede ontology iris.
*
* @param scopeid
* @param coreRegistry
* a. If it is a well-formed IRI it supersedes <code>coreOntology</code>.
* @param coreOntologies
* @param customRegistry
* a. If it is a well-formed IRI it supersedes <code>customOntology</code>.
* @param customOntologies
* @param activate
* if true, the new scope will be activated upon creation.
* @param uriInfo
* @param headers
* @return
*/
@PUT
@Consumes(MediaType.WILDCARD)
public Response registerScope(@PathParam("scopeid") String scopeid, @QueryParam("corereg") final List<String> coreRegistries, @QueryParam("coreont") final List<String> coreOntologies, @DefaultValue("false") @QueryParam("activate") boolean activate, @Context HttpHeaders headers) {
log.debug("Request URI {}", uriInfo.getRequestUri());
scope = onm.getScope(scopeid);
List<OntologyInputSource<?>> srcs = new ArrayList<OntologyInputSource<?>>(coreOntologies.size() + coreRegistries.size());
// First thing, check registry sources.
if (coreRegistries != null)
for (String reg : coreRegistries) if (reg != null && !reg.isEmpty())
try {
// Library IDs are sanitized differently
srcs.add(new LibrarySource(URIUtils.desanitize(IRI.create(reg)), regMgr));
} catch (Exception e1) {
throw new WebApplicationException(e1, BAD_REQUEST);
// Bad or not supplied core registry, try the ontology.
}
// Then ontology sources
if (coreOntologies != null)
for (String ont : coreOntologies) if (ont != null && !ont.isEmpty())
try {
srcs.add(new RootOntologySource(IRI.create(ont)));
} catch (OWLOntologyCreationException e2) {
// If this fails too, throw a bad request.
throw new WebApplicationException(e2, BAD_REQUEST);
}
// Now the creation.
try {
// Expand core sources
List<OntologyInputSource<?>> expanded = new ArrayList<OntologyInputSource<?>>();
for (OntologyInputSource<?> coreSrc : srcs) if (coreSrc != null) {
if (coreSrc instanceof SetInputSource) {
for (Object o : ((SetInputSource<?>) coreSrc).getOntologies()) {
OntologyInputSource<?> src = null;
if (o instanceof OWLOntology)
src = new RootOntologySource((OWLOntology) o);
else if (o instanceof Graph)
src = new GraphSource((Graph) o);
if (src != null)
expanded.add(src);
}
} else
// Must be denoting a single ontology
expanded.add(coreSrc);
}
scope = onm.createOntologyScope(scopeid, expanded.toArray(new OntologyInputSource[0]));
// Setup and register the scope. If no custom space was set, it will
// still be open for modification.
scope.setUp();
onm.setScopeActive(scopeid, activate);
} catch (DuplicateIDException e) {
throw new WebApplicationException(e, CONFLICT);
} catch (Exception ex) {
throw new WebApplicationException(ex, INTERNAL_SERVER_ERROR);
}
ResponseBuilder rb = Response.created(uriInfo.getAbsolutePath());
// addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
use of org.apache.stanbol.ontologymanager.sources.clerezza.GraphSource 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.ontologymanager.sources.clerezza.GraphSource in project stanbol by apache.
the class AbstractOntologyCollectorImpl method addOntology.
@Override
public synchronized OWLOntologyID addOntology(OntologyInputSource<?> ontologySource) throws UnmodifiableOntologyCollectorException {
// Check for error conditions.
if (locked)
throw new UnmodifiableOntologyCollectorException(this);
if (ontologySource == null)
throw new IllegalArgumentException("Ontology source cannot be null.");
log.debug("Adding ontology to collector {}", getID());
OWLOntologyID key = null;
if (ontologySource.hasRootOntology()) {
long before = System.currentTimeMillis();
Object o = ontologySource.getRootOntology();
// Check the origin anyhow, as it may be useful for setting aliases with physical locations etc.
if (ontologySource.hasOrigin())
key = ontologyProvider.loadInStore(o, false, ontologySource.getOrigin());
else
key = ontologyProvider.loadInStore(o, false);
if (key != null) {
managedOntologies.add(key);
// Note that imported ontologies are not considered as managed! TODO should we change this?
log.info("Add ontology completed in {} ms.", (System.currentTimeMillis() - before));
// Fire the event
fireOntologyAdded(key);
}
} else if (ontologySource.hasOrigin()) {
// Just the origin : see if it is satisfiable
log.debug("Checking origin satisfiability...");
Origin<?> origin = ontologySource.getOrigin();
Object ref = origin.getReference();
log.debug("Origin wraps a {}", ref.getClass().getCanonicalName());
if (ref instanceof org.semanticweb.owlapi.model.IRI)
try {
log.debug("Deferring addition to physical IRI {} (if available).", ref);
key = addOntology(new RootOntologySource((org.semanticweb.owlapi.model.IRI) ref));
} catch (OWLOntologyCreationException e) {
throw new RuntimeException(e);
}
else if (ref instanceof IRI) {
log.debug("Deferring addition to stored Clerezza graph {} (if available).", ref);
key = addOntology(new GraphSource((IRI) ref));
} else if (ref instanceof OWLOntologyID) {
OWLOntologyID idref = (OWLOntologyID) ref;
log.debug("Deferring addition to stored ontology with public key {} (if available).", ref);
if (!ontologyProvider.hasOntology(idref))
throw new MissingOntologyException(this, idref);
key = idref;
if (managedOntologies.add(idref))
fireOntologyAdded(idref);
} else
throw new IllegalArgumentException("Invalid origin " + origin);
} else
throw new IllegalArgumentException("Ontology source must provide either an ontology object, or a way to reference one (i.e. an origin).");
log.info("Public key : {}", key);
return key;
}
Aggregations