Search in sources :

Example 1 with LDPath

use of org.apache.marmotta.ldpath.LDPath in project stanbol by apache.

the class LdpathSourceProcessor method setConfiguration.

@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void setConfiguration(Map<String, Object> config) {
    indexingConfig = (IndexingConfig) config.get(IndexingConfig.KEY_INDEXING_CONFIG);
    Object indexingSource;
    //we need to check for both EntityDataProvider and EntityDataIterator
    indexingSource = indexingConfig.getEntityDataProvider();
    if (indexingSource == null) {
        indexingSource = indexingConfig.getDataIterable();
    }
    if (indexingSource == null) {
        throw new IllegalStateException("Indexing Configuration does not contain" + "neither an EntityDataProvider nor an EntityIdIterator!");
    }
    if (indexingSource instanceof RDFBackend<?>) {
        //NOTE we use the EntityhubConfiguration to have the same pre-registered
        //     namespaces as the other components.
        this.backend = (RDFBackend) indexingSource;
        this.configuration = new EntityhubConfiguration(vf);
        this.transformer = configuration.getTransformers();
        this.ldPath = new LDPath(backend, configuration);
    } else {
        throw new IllegalArgumentException("The configured IndexingSource '" + indexingSource.getClass().getSimpleName() + "' does not support " + "LDPath (does not implement RDFBackend)! This Processor " + "can only be used with IndexingSources that support LDPath!");
    }
    Object value = config.get(PARAMETER_LD_PATH);
    final File ldpathFile;
    if (value != null && !value.toString().isEmpty()) {
        ldpathFile = indexingConfig.getConfigFile(value.toString());
        if (ldpathFile == null || !ldpathFile.exists()) {
            throw new IllegalArgumentException("Configured '" + PARAMETER_LD_PATH + "' file was not found!");
        }
        if (!ldpathFile.isFile()) {
            throw new IllegalArgumentException("Configured '" + PARAMETER_LD_PATH + "' file exists but is not a File!");
        }
    } else {
        throw new IllegalArgumentException("Missing required configuration '" + PARAMETER_LD_PATH + "' - the file containing the LDPath program used by this " + LdpathProcessor.class.getSimpleName() + "!");
    }
    //The backend needs not to be initialised to parse a program as
    //parsing only requires the "value converter" methods that need also to
    //work without initialising
    //if this is a Problem one can also move parsing to the init method
    parseLdPathProgram(ldpathFile);
    value = config.get(PARAMETER_APPEND);
    if (value instanceof Boolean) {
        this.appendMode = ((Boolean) value).booleanValue();
    } else if (value != null && !value.toString().isEmpty()) {
        this.appendMode = Boolean.parseBoolean(value.toString());
    } else {
        this.appendMode = DEFAULT_APPEND_MODE;
    }
}
Also used : EntityhubConfiguration(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath.EntityhubConfiguration) LDPath(org.apache.marmotta.ldpath.LDPath) RDFBackend(org.apache.marmotta.ldpath.api.backend.RDFBackend) File(java.io.File)

Example 2 with LDPath

use of org.apache.marmotta.ldpath.LDPath in project stanbol by apache.

the class BackendTest method testSingleRepresentationBackend.

@Test
public void testSingleRepresentationBackend() throws Exception {
    Representation paris = yard.getRepresentation("http://dbpedia.org/resource/Paris");
    assertNotNull(paris);
    SingleRepresentationBackend backend = new SingleRepresentationBackend();
    backend.setRepresentation(paris);
    LDPath<Object> ldPath = new LDPath<Object>(backend);
    StringBuilder sb = new StringBuilder();
    sb.append("myTest = .[rdf:type is <http://dbpedia.org/ontology/Place>]/rdfs:label;");
    Program<Object> program = ldPath.parseProgram(new StringReader(sb.toString()));
    Map<String, Collection<?>> result = program.execute(backend, yard.getValueFactory().createReference(paris.getId()));
    Assert.assertNotNull(result);
    Assert.assertTrue(result.containsKey("myTest"));
    Collection<?> values = result.get("myTest");
    Assert.assertNotNull(values);
    Assert.assertFalse(values.isEmpty());
    sb = new StringBuilder();
    sb.append("myTest = .[rdf:type is <http://dbpedia.org/ontology/Place2>]/rdfs:label;");
    program = ldPath.parseProgram(new StringReader(sb.toString()));
    result = program.execute(backend, yard.getValueFactory().createReference(paris.getId()));
    Assert.assertNotNull(result);
    values = result.get("myTest");
    Assert.assertTrue(values == null || values.isEmpty());
}
Also used : StringReader(java.io.StringReader) LDPath(org.apache.marmotta.ldpath.LDPath) Collection(java.util.Collection) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Test(org.junit.Test)

Example 3 with LDPath

use of org.apache.marmotta.ldpath.LDPath in project stanbol by apache.

the class ContentItemBackendTest method testContentWithAdditionalMetadata.

@Test
public void testContentWithAdditionalMetadata() throws IOException, LDPathParseException {
    byte[] content = "text content".getBytes();
    IRI uri = ContentItemHelper.makeDefaultUrn(content);
    ContentItem contentItem = ciFactory.createContentItem(uri, new ByteArraySource(content, "text/plain; charset=UTF-8"));
    Graph tc = new SimpleGraph();
    Literal literal = LiteralFactory.getInstance().createTypedLiteral("Michael Jackson");
    IRI subject = new IRI("dummyUri");
    tc.add(new TripleImpl(subject, new IRI("http://xmlns.com/foaf/0.1/givenName"), literal));
    contentItem.addPart(new IRI(uri.getUnicodeString() + "_additionalMetadata"), tc);
    ContentItemBackend ciBackend = new ContentItemBackend(contentItem, true);
    LDPath<RDFTerm> ldPath = new LDPath<RDFTerm>(ciBackend, EnhancerLDPath.getConfig());
    Collection<RDFTerm> result = ldPath.pathQuery(subject, "foaf:givenName", null);
    assertTrue("Additional metadata cannot be found", result.contains(literal));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) LDPath(org.apache.marmotta.ldpath.LDPath) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Literal(org.apache.clerezza.commons.rdf.Literal) ContentItemBackend(org.apache.stanbol.enhancer.ldpath.backend.ContentItemBackend) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) ByteArraySource(org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource) Test(org.junit.Test)

Aggregations

LDPath (org.apache.marmotta.ldpath.LDPath)3 Test (org.junit.Test)2 File (java.io.File)1 StringReader (java.io.StringReader)1 Collection (java.util.Collection)1 Graph (org.apache.clerezza.commons.rdf.Graph)1 IRI (org.apache.clerezza.commons.rdf.IRI)1 Literal (org.apache.clerezza.commons.rdf.Literal)1 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)1 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)1 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)1 RDFBackend (org.apache.marmotta.ldpath.api.backend.RDFBackend)1 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)1 ContentItemBackend (org.apache.stanbol.enhancer.ldpath.backend.ContentItemBackend)1 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)1 ByteArraySource (org.apache.stanbol.enhancer.servicesapi.impl.ByteArraySource)1 EntityhubConfiguration (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath.EntityhubConfiguration)1 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)1