Search in sources :

Example 46 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class TestOWLUtils method namedIRI.

/*
     * Extracting the OWL ontology identifier on a *whole* ontology.
     */
@Test
public void namedIRI() throws Exception {
    InputStream inputStream = getClass().getResourceAsStream("/owl/maincharacters.owl");
    Graph mg = TcManager.getInstance().createGraph(uri);
    parser.parse(mg, inputStream, "application/rdf+xml", uri);
    assertNotNull(OWLUtils.extractOntologyID(mg.getImmutableGraph()));
}
Also used : Graph(org.apache.clerezza.commons.rdf.Graph) InputStream(java.io.InputStream) Test(org.junit.Test)

Example 47 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class OWLAPIToClerezzaConverterTest method testOWLOntologyToGraph.

@Test
public void testOWLOntologyToGraph() {
    /*
         * Transform the OWLOntology into a Clerezza Graph.
         */
    Graph mGraph = OWLAPIToClerezzaConverter.owlOntologyToClerezzaGraph(ontology);
    /*
         * Print all the triples contained in the Clerezza Graph.
         */
    Iterator<Triple> tripleIt = mGraph.iterator();
    while (tripleIt.hasNext()) {
        Triple triple = tripleIt.next();
        log.info(triple.toString());
    }
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Test(org.junit.Test)

Example 48 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class EnhancementEngineHelper method getChainExecutionProperties.

/**
     * Getter for the {@link Chain} scoped (chain and chain-engine scoped) properties 
     * for the parsed enhancement engine and content item.
     * @param engine the enhancement engine
     * @param ci the content item
     * @return the chain scoped enhancement properties. This will not include any
     * request scoped properties.
     * @since 0.12.1 (<a href="https://issues.apache.org/jira/browse/STANBOL-1361">STANBOL-1361</a>)
     */
public static Map<String, Object> getChainExecutionProperties(EnhancementEngine engine, ContentItem ci) {
    if (engine == null) {
        throw new IllegalArgumentException("The parsed EnhancementEngine MUST NOT be NULL");
    }
    if (ci == null) {
        throw new IllegalArgumentException("The parsed ContentItem MUST NOT be NULL");
    }
    Map<String, Object> chainExProps = new HashMap<String, Object>();
    Map<String, Object> engineExProps = new HashMap<String, Object>();
    ci.getLock().readLock().lock();
    try {
        Graph em = ExecutionMetadataHelper.getExecutionMetadata(ci);
        //(1.a) retrieve EnhancementProperties from the ep:ExecutionPlan
        log.debug("> extract EnhancementProperties form the ExecutionPlan");
        BlankNodeOrIRI executionPlanNode = ExecutionMetadataHelper.getExecutionPlanNode(em, ExecutionMetadataHelper.getChainExecution(em, ci.getUri()));
        extractEnhancementProperties(chainExProps, em, executionPlanNode, "Chain Execution");
        //(1.b) retrieve Enhancement Properties from the ep:ExectutionNode
        //      for the parsed EnhancementEngine
        log.debug("> extract EnhancementProperties form the ExecutionNode of Engine {}", engine.getName());
        Iterator<Triple> engineExecutions = em.filter(null, ExecutionPlan.ENGINE, new PlainLiteralImpl(engine.getName()));
        //      there are multiple we will merge the properties of those
        while (engineExecutions.hasNext()) {
            BlankNodeOrIRI engineExecution = engineExecutions.next().getSubject();
            if (em.contains(new TripleImpl(executionPlanNode, ExecutionPlan.HAS_EXECUTION_NODE, engineExecution))) {
                extractEnhancementProperties(engineExProps, em, engineExecution, "Engine Execution");
            }
        //else engine execution of a different execution plan
        }
    } catch (NoSuchPartException e) {
        //no execution metadata are present
        log.debug("  - no ExecutionMetadata are present ...");
    } finally {
        ci.getLock().readLock().unlock();
    }
    //finally merge the chain-engine scoped properties into the chain scoped properties
    chainExProps.putAll(engineExProps);
    return chainExProps;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) Graph(org.apache.clerezza.commons.rdf.Graph) HashMap(java.util.HashMap) PlainLiteralImpl(org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) NoSuchPartException(org.apache.stanbol.enhancer.servicesapi.NoSuchPartException) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)

Example 49 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class RdfEntityFactoryTest method testInterfaceHierarchies.

@Test
public void testInterfaceHierarchies() throws Exception {
    Graph graph = new SimpleGraph();
    RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
    String testUri = "urn:RdfEntityFactoryTest:SubTestEntity";
    String testUri2 = "urn:RdfEntityFactoryTest:TestEntity2";
    String testUri3 = "urn:RdfEntityFactoryTest:TestEntity";
    IRI node = new IRI(testUri);
    IRI node2 = new IRI(testUri2);
    IRI node3 = new IRI(testUri3);
    SubTestRdfEntity entity = factory.getProxy(node, SubTestRdfEntity.class);
    TestRdfEntity entity2 = factory.getProxy(node2, TestRdfEntity.class, SubTestRdfEntity.class, TestRdfEntity2.class);
    TestRdfEntity entity3 = factory.getProxy(node3, TestRdfEntity.class);
    //Start with checking the types for entity2
    //first type cast to the hierarchy
    assertTrue(entity instanceof TestRdfEntity);
    assertTrue(entity instanceof RdfEntity);
    // test if the rdf:type triples are present in the Graph
    Set<String> typeStrings = getRdfTypes(graph, node);
    assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
    typeStrings = null;
    //now the same for entity2
    //first type cast to the hierarchy
    assertTrue(entity2 instanceof SubTestRdfEntity);
    assertTrue(entity2 instanceof TestRdfEntity2);
    assertTrue(entity2 instanceof RdfEntity);
    // test if the rdf:type triples are present in the Graph
    typeStrings = getRdfTypes(graph, node2);
    assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity2.class.getAnnotation(Rdf.class).id()));
    typeStrings = null;
    //Now check Entity3
    assertTrue(!(entity3 instanceof SubTestRdfEntity));
    assertTrue(entity3 instanceof TestRdfEntity);
    //Now create an new Entity for the same Node that implements SubEntity2
    SubTestRdfEntity entity4 = factory.getProxy(node3, SubTestRdfEntity.class);
    //check if entity4 implements SubTestRefEntity
    assertTrue(entity4 instanceof SubTestRdfEntity);
    //now check if the additional type was added to node3
    typeStrings = getRdfTypes(graph, node3);
    assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
    //and that entity3 still dose not implement SubTestEntity
    // ... because adding/removing rdf:type triples in the graph can not affect existing proxy instances!
    assertTrue(!(entity3 instanceof SubTestRdfEntity));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfEntityFactory(org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory) Rdf(org.apache.stanbol.enhancer.rdfentities.Rdf) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) RdfEntity(org.apache.stanbol.enhancer.rdfentities.RdfEntity) Test(org.junit.Test)

Example 50 with Graph

use of org.apache.clerezza.commons.rdf.Graph in project stanbol by apache.

the class RdfEntityFactoryTest method testObjectProperties.

@Test
public void testObjectProperties() throws Exception {
    Graph graph = new SimpleGraph();
    RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
    String testUri = "urn:RdfEntityFactoryTest:TestEntity";
    String testUri2 = "urn:RdfEntityFactoryTest:TestEntity2";
    IRI node = new IRI(testUri);
    IRI node2 = new IRI(testUri2);
    TestRdfEntity entity = factory.getProxy(node, TestRdfEntity.class);
    TestRdfEntity2 entity2 = factory.getProxy(node2, TestRdfEntity2.class);
    URI testURI = new URI("urn:test:URI");
    entity.setURI(testURI);
    assertEquals(testURI, entity.getURI());
    URL testURL = new URL("http://www.iks-project.eu");
    entity.setURL(testURL);
    assertEquals(testURL, entity.getURL());
    entity.setIRI(node2);
    assertEquals(node2, entity.getIRI());
    entity2.setTestEntity(entity);
    assertEquals(entity, entity2.getTestEntity());
    Collection<TestRdfEntity> testEntities = entity2.getTestEntities();
    //check that entity is not in the collection
    assertTrue(testEntities.isEmpty());
    Set<IRI> testIRIs = new HashSet<IRI>();
    int NUM = 10;
    for (int i = 0; i < NUM; i++) {
        IRI testNode = new IRI(testUri + ':' + '_' + i);
        testIRIs.add(testNode);
        testEntities.add(factory.getProxy(testNode, TestRdfEntity.class));
    }
    //now get a new collection and test if the added entities are there
    //add to a list to check for duplicates
    Collection<IRI> resultIRIs = new ArrayList<IRI>();
    for (TestRdfEntity e : entity2.getTestEntities()) {
        //I used IRIs for the generation ...
        assertTrue(e.getId() instanceof IRI);
        resultIRIs.add((IRI) e.getId());
    }
    //now cross check
    assertTrue(testIRIs.containsAll(resultIRIs));
    assertTrue(resultIRIs.containsAll(testIRIs));
//now one could try to remove some Elements ...
// ... but things like that are already tested for Integers in testPrimitiveDataTypes
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) ArrayList(java.util.ArrayList) URI(java.net.URI) URL(java.net.URL) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfEntityFactory(org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Graph (org.apache.clerezza.commons.rdf.Graph)172 IRI (org.apache.clerezza.commons.rdf.IRI)110 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)66 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)57 Triple (org.apache.clerezza.commons.rdf.Triple)45 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)43 Test (org.junit.Test)38 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)36 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)34 IOException (java.io.IOException)27 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)26 HashSet (java.util.HashSet)24 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)24 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)24 InputStream (java.io.InputStream)21 HashMap (java.util.HashMap)20 Language (org.apache.clerezza.commons.rdf.Language)17 Blob (org.apache.stanbol.enhancer.servicesapi.Blob)17 ArrayList (java.util.ArrayList)16 LiteralFactory (org.apache.clerezza.rdf.core.LiteralFactory)15