Search in sources :

Example 6 with Literal

use of org.openrdf.model.Literal in project blueprints by tinkerpop.

the class PropertyGraphSailConnection method literalToObject.

private Object literalToObject(final Value v) {
    if (v instanceof Literal) {
        Literal l = (Literal) v;
        URI type = l.getDatatype();
        if (null == type) {
            return l.getLabel();
        } else {
            if (type.equals(XMLSchema.STRING)) {
                return l.stringValue();
            } else if (type.equals(XMLSchema.LONG)) {
                return l.longValue();
            } else if (type.equals(XMLSchema.INT)) {
                return l.intValue();
            } else if (type.equals(XMLSchema.INTEGER)) {
                return l.integerValue();
            } else if (type.equals(XMLSchema.BYTE)) {
                return l.byteValue();
            } else if (type.equals(XMLSchema.BOOLEAN)) {
                return l.booleanValue();
            } else if (type.equals(XMLSchema.SHORT)) {
                return l.shortValue();
            } else if (type.equals(XMLSchema.FLOAT)) {
                return l.floatValue();
            } else if (type.equals(XMLSchema.DOUBLE)) {
                return l.doubleValue();
            } else {
                return null;
            }
        }
    } else {
        return null;
    }
}
Also used : Literal(org.openrdf.model.Literal) URI(org.openrdf.model.URI)

Example 7 with Literal

use of org.openrdf.model.Literal in project blueprints by tinkerpop.

the class SailTest method testGetStatementsPO_SG.

@Test
public void testGetStatementsPO_SG() throws Exception {
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/test/PO_SG#a");
        URI uriB = sail.getValueFactory().createURI("http://example.org/test/PO_SG#b");
        URI foo = sail.getValueFactory().createURI("http://example.org/ns#foo");
        URI firstName = sail.getValueFactory().createURI("http://example.org/ns#firstName");
        Literal plainLitA = sail.getValueFactory().createLiteral("arbitrary plain literal 8765675");
        Literal fooLabel = sail.getValueFactory().createLiteral("foo", XMLSchema.STRING);
        int before, after;
        // Add statement to the implicit null context.
        sc.removeStatements(null, null, null, uriA);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, uriA, uriB, false);
        sc.addStatement(uriA, uriA, uriB);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, uriA, uriB, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // Add plain literal statement to the default context.
        sc.removeStatements(null, null, plainLitA);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, uriA, plainLitA, false);
        sc.addStatement(uriA, uriA, plainLitA);
        sc.addStatement(uriA, uriB, plainLitA);
        sc.addStatement(uriB, uriB, plainLitA);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, uriA, plainLitA, false);
        assertEquals(0, before);
        assertEquals(1, after);
        // Add string-typed literal statement to the default context.
        sc.removeStatements(null, null, fooLabel);
        sc.commit();
        sc.begin();
        before = countStatements(sc, null, firstName, fooLabel, false);
        sc.addStatement(foo, firstName, fooLabel);
        sc.commit();
        sc.begin();
        after = countStatements(sc, null, firstName, fooLabel, false);
        assertEquals(0, before);
        assertEquals(1, after);
        assertEquals(foo, toSet(sc.getStatements(null, firstName, fooLabel, false)).iterator().next().getSubject());
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) Literal(org.openrdf.model.Literal) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 8 with Literal

use of org.openrdf.model.Literal in project stanbol by apache.

the class RdfRepresentationTest method testTypedLiteralToTextConversion.

/**
     * {@link TypedLiteral}s are used to represent literal values for different
     * xsd dataTypes within Clerezza. This method tests of {@link TypedLiteral}s
     * with the data type xsd:string are correctly treated like {@link String}
     * values. This tests especially if they are treated as natural language
     * texts without language.
     */
@Test
public void testTypedLiteralToTextConversion() {
    String field = "urn:test.RdfRepresentation:test.field";
    Literal stringLiteral = valueFactory.getSesameFactory().createLiteral("This is a stirng value", XMLSchema.STRING);
    //also add an integer to test that other typed literals are not used as texts
    Literal integerLiteral = valueFactory.getSesameFactory().createLiteral(5);
    Representation rep = createRepresentation(null);
    rep.add(field, Arrays.asList(stringLiteral, integerLiteral));
    //test if the literal is returned when asking for natural language text without language
    Iterator<Text> noLangTexts = rep.get(field, (String) null);
    assertTrue(noLangTexts.hasNext());
    assertEquals(stringLiteral.getLabel(), noLangTexts.next().getText());
    assertFalse(noLangTexts.hasNext());
    //test that string literals are returned when asking for all natural language text values
    Iterator<Text> texts = rep.getText(field);
    assertTrue(texts.hasNext());
    assertEquals(stringLiteral.getLabel(), texts.next().getText());
    assertFalse(texts.hasNext());
}
Also used : Literal(org.openrdf.model.Literal) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Text(org.apache.stanbol.entityhub.servicesapi.model.Text) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 9 with Literal

use of org.openrdf.model.Literal in project stanbol by apache.

the class RdfRepresentationTest method testTypedLiteralToValueConversion.

/**
     * {@link TypedLiteral}s are used to represent literal values for different
     * xsd dataTypes within Clerezza. This method tests if xsd dataTypes are
     * converted to the corresponding java types. 
     * This is dependent on the {@link LiteralFactory} implementation used by
     * the {@link RdfRepresentation} implementation.
     */
@SuppressWarnings("unchecked")
@Test
public void testTypedLiteralToValueConversion() {
    String field = "urn:test.RdfRepresentation:test.field";
    Integer integerValue = 5;
    Literal integerLiteral = valueFactory.getSesameFactory().createLiteral(integerValue);
    Date dateValue = new Date();
    Literal dateLiteeral = valueFactory.getSesameFactory().createLiteral(dateValue);
    Double doubleValue = Math.PI;
    Literal doubleLiteral = valueFactory.getSesameFactory().createLiteral(doubleValue);
    String stringValue = "This is a string literal value";
    Literal stringLiteral = valueFactory.getSesameFactory().createLiteral(stringValue, XMLSchema.STRING);
    Representation rep = createRepresentation(null);
    Collection<Literal> typedLiterals = Arrays.asList(integerLiteral, doubleLiteral, stringLiteral, dateLiteeral);
    rep.add(field, typedLiterals);
    //now check that such values are available via Sesame Literal
    Iterator<Literal> typedLiteralValues = rep.get(field, Literal.class);
    int size = 0;
    while (typedLiteralValues.hasNext()) {
        Literal next = typedLiteralValues.next();
        assertTrue(typedLiterals.contains(next));
        size++;
    }
    assertTrue(typedLiterals.size() == size);
    //now check that the values are available via the java object types
    //1) integer
    Iterator<Integer> intValues = rep.get(field, Integer.class);
    assertTrue(intValues.hasNext());
    assertEquals(integerValue, intValues.next());
    assertFalse(intValues.hasNext());
    //2) double
    Iterator<Double> doubleValues = rep.get(field, Double.class);
    assertTrue(doubleValues.hasNext());
    assertEquals(doubleValue, doubleValues.next());
    assertFalse(doubleValues.hasNext());
    //3) string
    Iterator<String> stringValues = rep.get(field, String.class);
    assertTrue(stringValues.hasNext());
    String value = stringValues.next();
    assertEquals(stringValue, value);
    assertFalse(stringValues.hasNext());
    //4) date
    Iterator<Date> dateValues = rep.get(field, Date.class);
    assertTrue(dateValues.hasNext());
    assertEquals(dateValue, dateValues.next());
    assertFalse(dateValues.hasNext());
}
Also used : Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) Date(java.util.Date) Literal(org.openrdf.model.Literal) RepresentationTest(org.apache.stanbol.entityhub.test.model.RepresentationTest) Test(org.junit.Test)

Example 10 with Literal

use of org.openrdf.model.Literal in project stanbol by apache.

the class RdfRepresentation method removeAllNaturalText.

@Override
public void removeAllNaturalText(String field, String... languages) throws IllegalArgumentException {
    if (field == null) {
        throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
    } else if (field.isEmpty()) {
        throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
    }
    ValueTypeFilter<Literal> vtf = new ValueTypeFilter<Literal>(languages);
    Iterator<Statement> statements = model.filter(subject, sesameFactory.createURI(field), null).iterator();
    while (statements.hasNext()) {
        Statement statement = statements.next();
        if (vtf.evaluate(statement.getObject())) {
            statements.remove();
        }
    }
}
Also used : Statement(org.openrdf.model.Statement) Literal(org.openrdf.model.Literal)

Aggregations

Literal (org.openrdf.model.Literal)23 URI (org.openrdf.model.URI)12 Test (org.junit.Test)10 Statement (org.openrdf.model.Statement)5 NotifyingSailConnection (org.openrdf.sail.NotifyingSailConnection)5 SailConnection (org.openrdf.sail.SailConnection)5 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)3 RepresentationTest (org.apache.stanbol.entityhub.test.model.RepresentationTest)3 ValueFactory (org.openrdf.model.ValueFactory)3 BindingSet (org.openrdf.query.BindingSet)3 Edge (com.tinkerpop.blueprints.Edge)2 Vertex (com.tinkerpop.blueprints.Vertex)2 Collection (java.util.Collection)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 Text (org.apache.stanbol.entityhub.servicesapi.model.Text)2 Resource (org.openrdf.model.Resource)2 Value (org.openrdf.model.Value)2 LiteralImpl (org.openrdf.model.impl.LiteralImpl)2