Search in sources :

Example 16 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class ModelsTest method testGetPropertyInvalidInput.

@Test
public void testGetPropertyInvalidInput() {
    Literal lit1 = VF.createLiteral(1.0);
    model1.add(foo, bar, lit1);
    model1.add(foo, bar, foo);
    try {
        Models.getProperty(model1, foo, null).orElse(null);
        fail("should have resulted in exception");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        Models.getProperty(model1, null, bar).orElse(null);
        fail("should have resulted in exception");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Test(org.junit.Test)

Example 17 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class ModelsTest method testObject.

public void testObject() {
    Literal lit = VF.createLiteral(1.0);
    model1.add(foo, bar, lit);
    model1.add(foo, bar, foo);
    Value result = Models.object(model1).orElse(null);
    assertNotNull(result);
    assertTrue(result.equals(lit) || result.equals(foo));
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value)

Example 18 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class QueryResults method bindingSetsMatch.

private static boolean bindingSetsMatch(BindingSet bs1, BindingSet bs2, Map<BNode, BNode> bNodeMapping) {
    if (bs1.size() != bs2.size()) {
        return false;
    }
    for (Binding binding1 : bs1) {
        Value value1 = binding1.getValue();
        Value value2 = bs2.getValue(binding1.getName());
        if (value1 instanceof BNode && value2 instanceof BNode) {
            BNode mappedBNode = bNodeMapping.get(value1);
            if (mappedBNode != null) {
                // bNode 'value1' was already mapped to some other bNode
                if (!value2.equals(mappedBNode)) {
                    // 'value1' and 'value2' do not match
                    return false;
                }
            } else {
                // possible mapping candidate
                if (bNodeMapping.containsValue(value2)) {
                    // 'value2' is already mapped to some other value.
                    return false;
                }
            }
        } else {
            // values are not (both) bNodes
            if (value1 instanceof Literal && value2 instanceof Literal) {
                // do literal value-based comparison for supported datatypes
                Literal leftLit = (Literal) value1;
                Literal rightLit = (Literal) value2;
                IRI dt1 = leftLit.getDatatype();
                IRI dt2 = rightLit.getDatatype();
                if (dt1 != null && dt2 != null && dt1.equals(dt2) && XMLDatatypeUtil.isValidValue(leftLit.getLabel(), dt1) && XMLDatatypeUtil.isValidValue(rightLit.getLabel(), dt2)) {
                    Integer compareResult = null;
                    if (dt1.equals(XMLSchema.DOUBLE)) {
                        compareResult = Double.compare(leftLit.doubleValue(), rightLit.doubleValue());
                    } else if (dt1.equals(XMLSchema.FLOAT)) {
                        compareResult = Float.compare(leftLit.floatValue(), rightLit.floatValue());
                    } else if (dt1.equals(XMLSchema.DECIMAL)) {
                        compareResult = leftLit.decimalValue().compareTo(rightLit.decimalValue());
                    } else if (XMLDatatypeUtil.isIntegerDatatype(dt1)) {
                        compareResult = leftLit.integerValue().compareTo(rightLit.integerValue());
                    } else if (dt1.equals(XMLSchema.BOOLEAN)) {
                        Boolean leftBool = Boolean.valueOf(leftLit.booleanValue());
                        Boolean rightBool = Boolean.valueOf(rightLit.booleanValue());
                        compareResult = leftBool.compareTo(rightBool);
                    } else if (XMLDatatypeUtil.isCalendarDatatype(dt1)) {
                        XMLGregorianCalendar left = leftLit.calendarValue();
                        XMLGregorianCalendar right = rightLit.calendarValue();
                        compareResult = left.compare(right);
                    }
                    if (compareResult != null) {
                        if (compareResult.intValue() != 0) {
                            return false;
                        }
                    } else if (!value1.equals(value2)) {
                        return false;
                    }
                } else if (!value1.equals(value2)) {
                    return false;
                }
            } else if (!value1.equals(value2)) {
                return false;
            }
        }
    }
    return true;
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) BNode(org.eclipse.rdf4j.model.BNode) Literal(org.eclipse.rdf4j.model.Literal) Value(org.eclipse.rdf4j.model.Value)

Example 19 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class SimpleLiteralTest method testStringLong.

/**
 * Test method for {@link org.eclipse.rdf4j.model.impl.SimpleLiteral#SimpleLiteral(java.lang.String)}.
 */
@Test
public final void testStringLong() throws Exception {
    StringBuilder testBuilder = new StringBuilder(1000000);
    for (int i = 0; i < 1000000; i++) {
        testBuilder.append(Integer.toHexString(i % 16));
    }
    Literal test = new SimpleLiteral(testBuilder.toString());
    assertEquals(testBuilder.toString(), test.getLabel());
    assertFalse(test.getLanguage().isPresent());
    assertEquals(XMLSchema.STRING, test.getDatatype());
}
Also used : Literal(org.eclipse.rdf4j.model.Literal) Test(org.junit.Test)

Example 20 with Literal

use of org.eclipse.rdf4j.model.Literal in project rdf4j by eclipse.

the class SimpleLiteralTest method testStringIRIEmptyNull.

/**
 * Test method for
 * {@link org.eclipse.rdf4j.model.impl.SimpleLiteral#SimpleLiteral(java.lang.String, org.eclipse.rdf4j.model.IRI)}
 * .
 */
@Test
public final void testStringIRIEmptyNull() throws Exception {
    String label = "";
    IRI datatype = null;
    Literal test = new SimpleLiteral(label, datatype);
    assertEquals("", test.getLabel());
    assertFalse(test.getLanguage().isPresent());
    assertEquals(XMLSchema.STRING, test.getDatatype());
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) Literal(org.eclipse.rdf4j.model.Literal) Test(org.junit.Test)

Aggregations

Literal (org.eclipse.rdf4j.model.Literal)98 Test (org.junit.Test)52 IRI (org.eclipse.rdf4j.model.IRI)34 Value (org.eclipse.rdf4j.model.Value)17 BNode (org.eclipse.rdf4j.model.BNode)14 Statement (org.eclipse.rdf4j.model.Statement)14 Model (org.eclipse.rdf4j.model.Model)13 Resource (org.eclipse.rdf4j.model.Resource)9 LinkedHashModel (org.eclipse.rdf4j.model.impl.LinkedHashModel)8 RDFHandlerException (org.eclipse.rdf4j.rio.RDFHandlerException)7 InputStream (java.io.InputStream)6 Date (java.util.Date)6 BindingSet (org.eclipse.rdf4j.query.BindingSet)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ValueFactory (org.eclipse.rdf4j.model.ValueFactory)4 SimpleValueFactory (org.eclipse.rdf4j.model.impl.SimpleValueFactory)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4