Search in sources :

Example 16 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class ContentItemBackendTest method parseRdfData.

/**
     * @param parser
     * @return
     * @throws IOException
     */
protected static Graph parseRdfData(ParsingProvider parser, String name) throws IOException {
    Graph rdfData = new IndexedGraph();
    InputStream in = getTestResource(name);
    assertNotNull("File '" + name + "' not found", in);
    ZipInputStream zipIn = new ZipInputStream(new BufferedInputStream(in));
    InputStream uncloseable = new UncloseableStream(zipIn);
    ZipEntry entry;
    while ((entry = zipIn.getNextEntry()) != null) {
        if (entry.getName().endsWith(".rdf")) {
            parser.parse(rdfData, uncloseable, SupportedFormat.RDF_XML, null);
        }
    }
    assertTrue(rdfData.size() > 0);
    zipIn.close();
    return rdfData;
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FilterInputStream(java.io.FilterInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 17 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class ExecutionMetadataHelper method initExecutionMetadataContentPart.

/**
     * Getter/Initialiser for the execution metadata content part of the parsed
     * content item. This part is expected to be registered with the URI
     * {@link ExecutionMetadata#CHAIN_EXECUTION}. If it does not already exist
     * this method creates an empty graph and register it with the parsed
     * content item otherwise it returns the existing part registered under that
     * URI.<p>
     * Typically users will also want to use 
     * {@link #initExecutionMetadata(Graph, Graph, IRI, String, boolean)}
     * to initialise the state based on the grpah returned by this method.
     * NOTES:<ul>
     * <li> If a content part is registered under the URI 
     * {@link ExecutionMetadata#CHAIN_EXECUTION} that is not of type
     * {@link Graph} this method will replace it with an empty {@link Graph}.
     * <li> This method acquires a write lock on the content item while checking
     * for the content part.
     * </ul>
     * @param contentItem the contentItem
     * @return the {@link Graph} with the execution metadata as registered as
     * content part with the URI {@link ExecutionMetadata#CHAIN_EXECUTION} to 
     * the {@link ContentItem}
     * @throws IllegalArgumentException if the parsed content itme is <code>null</code>.
     */
public static Graph initExecutionMetadataContentPart(ContentItem contentItem) {
    if (contentItem == null) {
        throw new IllegalArgumentException("The parsed ContentItme MUST NOT be NULL!");
    }
    Graph executionMetadata;
    contentItem.getLock().writeLock().lock();
    try {
        try {
            executionMetadata = contentItem.getPart(CHAIN_EXECUTION, Graph.class);
        } catch (NoSuchPartException e) {
            executionMetadata = new IndexedGraph();
            contentItem.addPart(CHAIN_EXECUTION, executionMetadata);
        }
    } finally {
        contentItem.getLock().writeLock().unlock();
    }
    return executionMetadata;
}
Also used : IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) NoSuchPartException(org.apache.stanbol.enhancer.servicesapi.NoSuchPartException) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 18 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class ExecutionPlanHelper method calculateExecutionPlan.

/**
     * Creates an execution plan based on the 
     * {@link ServiceProperties#ENHANCEMENT_ENGINE_ORDERING} of the parsed
     * EnhancementEngines. NOTE that the parsed list is modified as it is sorted by
     * using the {@link EnhancementEngineHelper#EXECUTION_ORDER_COMPARATOR}.<p>
     * A second parameter with the set of optional engines can be used to define
     * what {@link ExecutionPlan#EXECUTION_NODE} in the execution plan should be 
     * marked as {@link ExecutionPlan#OPTIONAL}.
     * @param chainName the name of the Chain to build the execution plan for
     * @param availableEngines the list of engines
     * @param optional the names of optional engines.
     * @param missing the names of missing engines
     * @param enhProps chain scoped enhancement properties. The key of the outer
     * map are the name of the engine or <code>null</code> for the chain. The
     * inner map uses the property as key and the value(s) as value. Multiple
     * values can be parsed as {@link Collection}. Single values will be
     * converted to RDF {@link TypedLiteral}s by using the {@link LiteralFactory}.
     * For types not supported by the LiteralFactory the <code>toString()</code>
     * method will be used. <code>null</code> can be parsed if no enhancement
     * properties are present.
     * @return the execution plan
     * @since 0.12.1
     */
public static ImmutableGraph calculateExecutionPlan(String chainName, List<EnhancementEngine> availableEngines, Set<String> optional, Set<String> missing, Map<String, Map<String, Object>> enhProps) {
    if (chainName == null || chainName.isEmpty()) {
        throw new IllegalArgumentException("The parsed ChainName MUST NOT be empty!");
    }
    Collections.sort(availableEngines, EXECUTION_ORDER_COMPARATOR);
    //now we have all required and possible also optional engines
    //  -> build the execution plan
    Graph ep = new IndexedGraph();
    BlankNodeOrIRI epNode = createExecutionPlan(ep, chainName, enhProps != null ? enhProps.get(null) : null);
    Integer prevOrder = null;
    Set<BlankNodeOrIRI> prev = null;
    Set<BlankNodeOrIRI> current = new HashSet<BlankNodeOrIRI>();
    for (String name : missing) {
        boolean optionalMissing = optional.contains(name);
        BlankNodeOrIRI node = writeExecutionNode(ep, epNode, name, optionalMissing, null, enhProps == null ? null : enhProps.get(name));
        if (!optionalMissing) {
            current.add(node);
        }
    // else add missing optional engines without any dependsOn restrictions
    }
    for (EnhancementEngine engine : availableEngines) {
        String name = engine.getName();
        Integer order = getEngineOrder(engine);
        if (prevOrder == null || !prevOrder.equals(order)) {
            prev = current;
            current = new HashSet<BlankNodeOrIRI>();
            prevOrder = order;
        }
        try {
            BlankNodeOrIRI executionNode = writeExecutionNode(ep, epNode, name, optional.contains(name), prev, enhProps == null ? null : enhProps.get(name));
            current.add(executionNode);
        } catch (RuntimeException e) {
            //add the engine and class to ease debugging in such cases
            log.error("Exception while writing ExecutionNode for Enhancement Eninge: " + engine + "(class: " + engine.getClass() + ")", e);
            //rethrow it
            throw e;
        }
    }
    return ep.getImmutableGraph();
}
Also used : ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) EnhancementEngineHelper.getString(org.apache.stanbol.enhancer.servicesapi.helper.EnhancementEngineHelper.getString) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) EnhancementEngine(org.apache.stanbol.enhancer.servicesapi.EnhancementEngine) HashSet(java.util.HashSet)

Example 19 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class AbstractOntologyCollectorImpl method getOntologyAsGraph.

protected Graph getOntologyAsGraph(OWLOntologyID ontologyId, boolean merge, org.semanticweb.owlapi.model.IRI universalPrefix) {
    if (merge)
        throw new UnsupportedOperationException("Merge not implemented yet for Clerezza triple collections.");
    /*
         * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of
         * going upOne() add "session" or "ontology" if needed). But only do this if we keep considering
         * imported ontologies as *not* managed.
         */
    // if (!merge) { // TODO
    Graph o = new IndexedGraph(ontologyProvider.getStoredOntology(ontologyId, Graph.class, merge));
    // Now rewrite import statements
    // Scan import statements for each owl:Ontology instance (hopefully one).
    String tid = getID();
    // case we go back to {scopeid}, whereas for sessions we maintain their original id.
    if (backwardPathLength > 0)
        tid = tid.split("/")[0];
    Iterator<Triple> it;
    List<Triple> newImports = new LinkedList<Triple>();
    synchronized (o) {
        it = o.filter(null, OWL.imports, null);
        // We use this list to avoid concurrent modification exceptions.
        List<Triple> replaceUs = new LinkedList<Triple>();
        while (it.hasNext()) replaceUs.add(it.next());
        for (Triple t : replaceUs) {
            String s = ((IRI) (t.getObject())).getUnicodeString();
            // FIXME note the different import targets in the OWLOntology and TripleColllection objects!
            // s = s.substring(s.indexOf("::") + 2, s.length());
            boolean managed = managedOntologies.contains(org.semanticweb.owlapi.model.IRI.create(s));
            IRI target = new IRI((managed ? universalPrefix + "/" + tid + "/" : URIUtils.upOne(universalPrefix) + "/") + s);
            o.remove(t);
            newImports.add(new TripleImpl(t.getSubject(), OWL.imports, target));
        }
    }
    for (Triple t : newImports) o.add(t);
    return o;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) LinkedList(java.util.LinkedList)

Example 20 with IndexedGraph

use of org.apache.stanbol.commons.indexedgraph.IndexedGraph in project stanbol by apache.

the class DereferenceEngineTest method setUpServices.

@BeforeClass
public static void setUpServices() throws IOException {
    testData = new IndexedGraph();
    long seed = System.currentTimeMillis();
    log.info("Test seed " + seed);
    Random random = new Random(seed);
    int numEntities = 0;
    for (int i = 0; i < NUM_ENTITIES; i++) {
        if (random.nextFloat() <= PERCENTAGE_PRESENT) {
            //do not create all entities
            IRI uri = new IRI("urn:test:entity" + i);
            testData.add(new TripleImpl(uri, RDF_TYPE, SKOS_CONCEPT));
            testData.add(new TripleImpl(uri, RDFS_LABEL, new PlainLiteralImpl("entity " + i, LANG_EN)));
            testData.add(new TripleImpl(uri, RDFS_LABEL, new PlainLiteralImpl("Entity " + i, LANG_DE)));
            testData.add(new TripleImpl(uri, SKOS_NOTATION, lf.createTypedLiteral(i)));
            numEntities++;
        }
    }
    log.info(" ... created {} Entities", numEntities);
    testMetadata = new IndexedGraph();
    int numLinks = 0;
    int numOtherLinks = 0;
    for (int i = 0; i < NUM_ENTITIES; i++) {
        float r = random.nextFloat();
        if (r < PERCENTAGE_LINKED) {
            IRI enhancementUri = new IRI("urn:test:enhancement" + i);
            IRI entityUri = new IRI("urn:test:entity" + i);
            //we do not need any other triple for testing in the contentItem
            testMetadata.add(new TripleImpl(enhancementUri, ENHANCER_ENTITY_REFERENCE, entityUri));
            numLinks++;
        } else if ((r - PERCENTAGE_LINKED) < PERCENTAGE_LINKED_OTHER) {
            IRI enhancementUri = new IRI("urn:test:enhancement" + i);
            IRI entityUri = new IRI("urn:test:entity" + i);
            //we do not need any other triple for testing in the contentItem
            testMetadata.add(new TripleImpl(enhancementUri, OTHER_ENTITY_REFERENCE, entityUri));
            numOtherLinks++;
        }
    }
    log.info("> created {} Entity references and {} references using an alternative proeprty ", numLinks, numOtherLinks);
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Random(java.util.Random) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) BeforeClass(org.junit.BeforeClass)

Aggregations

IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)34 Graph (org.apache.clerezza.commons.rdf.Graph)27 IRI (org.apache.clerezza.commons.rdf.IRI)20 HashSet (java.util.HashSet)9 InputStream (java.io.InputStream)8 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)8 RdfValueFactory (org.apache.stanbol.entityhub.model.clerezza.RdfValueFactory)8 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)8 Triple (org.apache.clerezza.commons.rdf.Triple)7 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)6 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)5 RdfQueryResultList (org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList)5 Test (org.junit.Test)5 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)4 RdfRepresentation (org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation)4 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)4 BeforeClass (org.junit.BeforeClass)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3