Search in sources :

Example 6 with Triple

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

the class TikaEngineTest method verifyBlankNodeOrIRI.

private static BlankNodeOrIRI verifyBlankNodeOrIRI(ContentItem ci, IRI subject, IRI property) {
    Iterator<Triple> it = ci.getMetadata().filter(subject, property, null);
    assertTrue(it.hasNext());
    RDFTerm r = it.next().getObject();
    assertFalse(it.hasNext());
    assertTrue(r instanceof BlankNodeOrIRI);
    return (BlankNodeOrIRI) r;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm)

Example 7 with Triple

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

the class TikaEngineTest method verifyValues.

private static Set<Literal> verifyValues(ContentItem ci, BlankNodeOrIRI subject, IRI property, IRI dataType, String... lexValues) {
    Iterator<Triple> it = ci.getMetadata().filter(subject, property, null);
    assertTrue(it.hasNext());
    Set<String> expected = new HashSet<String>(Arrays.asList(lexValues));
    Set<Literal> found = new HashSet<Literal>(expected.size());
    while (it.hasNext()) {
        RDFTerm r = it.next().getObject();
        if (dataType == null) {
            assertTrue(r instanceof Literal);
        } else {
            assertTrue(r instanceof Literal);
            assertEquals(dataType, ((Literal) r).getDataType());
        }
        assertTrue(expected.remove(((Literal) r).getLexicalForm()));
        found.add((Literal) r);
    }
    return found;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) HashSet(java.util.HashSet)

Example 8 with Triple

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

the class TikaEngineTest method testGEOMetadata.

@Test
public void testGEOMetadata() throws EngineException, IOException, ParseException {
    log.info(">>> testGEOMetadata <<<");
    //first validate Media RDFTerm Ontology
    IRI hasLocation = new IRI(NamespaceEnum.media + "hasLocation");
    IRI locationLatitude = new IRI(NamespaceEnum.media + "locationLatitude");
    IRI locationLongitude = new IRI(NamespaceEnum.media + "locationLongitude");
    //IRI locationAltitude = new IRI(NamespaceEnum.media+"locationAltitude");
    //"video/x-ms-asf");
    ContentItem ci = createContentItem("testJPEG_GEO.jpg", OCTET_STREAM.toString());
    assertFalse(engine.canEnhance(ci) == CANNOT_ENHANCE);
    engine.computeEnhancements(ci);
    Iterator<Triple> it = ci.getMetadata().filter(ci.getUri(), hasLocation, null);
    assertTrue(it.hasNext());
    RDFTerm r = it.next().getObject();
    assertFalse(it.hasNext());
    assertTrue(r instanceof BlankNodeOrIRI);
    BlankNodeOrIRI location = verifyBlankNodeOrIRI(ci, hasLocation);
    //lat
    verifyValue(ci, location, locationLatitude, XSD.double_, "12.54321");
    //long
    verifyValue(ci, location, locationLongitude, XSD.double_, "-54.1234");
    //second the GEO ont
    IRI lat = new IRI(NamespaceEnum.geo + "lat");
    IRI lon = new IRI(NamespaceEnum.geo + "long");
    //lat
    verifyValue(ci, lat, XSD.double_, "12.54321");
    //long
    verifyValue(ci, lon, XSD.double_, "-54.1234");
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) Test(org.junit.Test)

Example 9 with Triple

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

the class TikaEngineTest method verifyValue.

private static Literal verifyValue(ContentItem ci, BlankNodeOrIRI subject, IRI property, IRI dataType, String lexValue) throws ParseException {
    Iterator<Triple> it = ci.getMetadata().filter(subject, property, null);
    assertTrue(it.hasNext());
    RDFTerm r = it.next().getObject();
    assertFalse(it.hasNext());
    if (dataType != null) {
        assertEquals(dataType, ((Literal) r).getDataType());
    }
    //consider the time zone of the host running this test
    if (XSD.dateTime.equals(dataType) && lexValue.charAt(lexValue.length() - 1) != 'Z') {
        Date expectedDate = dateDefaultTimezone.parse(lexValue);
        assertEquals(expectedDate, lf.createObject(Date.class, ((Literal) r)));
    } else {
        assertEquals(lexValue, ((Literal) r).getLexicalForm());
    }
    return (Literal) r;
}
Also used : Triple(org.apache.clerezza.commons.rdf.Triple) Literal(org.apache.clerezza.commons.rdf.Literal) RDFTerm(org.apache.clerezza.commons.rdf.RDFTerm) Date(java.util.Date)

Example 10 with Triple

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

the class ClerezzaModelWriter method toRDF.

private Graph toRDF(QueryResultList<?> resultList) {
    final Graph resultGraph;
    Class<?> type = resultList.getType();
    if (String.class.isAssignableFrom(type)) {
        //create a new ImmutableGraph
        resultGraph = new IndexedGraph();
        for (Object result : resultList) {
            //add a triple to each reference in the result set
            resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, new IRI(result.toString())));
        }
    } else {
        //first determine the type of the resultList
        final boolean isSignType;
        if (Representation.class.isAssignableFrom(type)) {
            isSignType = false;
        } else if (Representation.class.isAssignableFrom(type)) {
            isSignType = true;
        } else {
            //incompatible type -> throw an Exception
            throw new IllegalArgumentException("Parsed type " + type + " is not supported");
        }
        //special treatment for RdfQueryResultList for increased performance
        if (resultList instanceof RdfQueryResultList) {
            resultGraph = ((RdfQueryResultList) resultList).getResultGraph();
            if (isSignType) {
                //if we build a ResultList for Signs, that we need to do more things
                //first remove all triples representing results
                Iterator<Triple> resultTripleIt = resultGraph.filter(QUERY_RESULT_LIST, QUERY_RESULT, null);
                while (resultTripleIt.hasNext()) {
                    resultTripleIt.next();
                    resultTripleIt.remove();
                }
                //to the Sign IDs
                for (Object result : resultList) {
                    IRI signId = new IRI(((Entity) result).getId());
                    addEntityTriplesToGraph(resultGraph, (Entity) result);
                    resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, signId));
                }
            }
        } else {
            //any other implementation of the QueryResultList interface
            //create a new graph
            resultGraph = new IndexedGraph();
            if (Representation.class.isAssignableFrom(type)) {
                for (Object result : resultList) {
                    IRI resultId;
                    if (!isSignType) {
                        addRDFTo(resultGraph, (Representation) result);
                        resultId = new IRI(((Representation) result).getId());
                    } else {
                        addRDFTo(resultGraph, (Entity) result);
                        resultId = new IRI(((Entity) result).getId());
                    }
                    //Note: In case of Representation this Triple points to
                    //      the representation. In case of Signs it points to
                    //      the sign.
                    resultGraph.add(new TripleImpl(QUERY_RESULT_LIST, QUERY_RESULT, resultId));
                }
            }
        }
    }
    return resultGraph;
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) Entity(org.apache.stanbol.entityhub.servicesapi.model.Entity) RdfRepresentation(org.apache.stanbol.entityhub.model.clerezza.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Triple(org.apache.clerezza.commons.rdf.Triple) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) JSONObject(org.codehaus.jettison.json.JSONObject) TripleImpl(org.apache.clerezza.commons.rdf.impl.utils.TripleImpl) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Aggregations

Triple (org.apache.clerezza.commons.rdf.Triple)151 IRI (org.apache.clerezza.commons.rdf.IRI)88 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)84 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)70 Graph (org.apache.clerezza.commons.rdf.Graph)45 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)41 HashSet (java.util.HashSet)34 Literal (org.apache.clerezza.commons.rdf.Literal)30 ArrayList (java.util.ArrayList)27 Lock (java.util.concurrent.locks.Lock)21 HashMap (java.util.HashMap)20 SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)19 OWLOntologyID (org.semanticweb.owlapi.model.OWLOntologyID)19 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)15 PlainLiteralImpl (org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl)12 Test (org.junit.Test)12 EngineException (org.apache.stanbol.enhancer.servicesapi.EngineException)10 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)9 GraphNode (org.apache.clerezza.rdf.utils.GraphNode)8 IOException (java.io.IOException)7