Search in sources :

Example 1 with Literal

use of fr.lirmm.graphik.graal.api.core.Literal in project graal by graphik-team.

the class RDF4jUtils method valueToTerm.

public Term valueToTerm(Value value) {
    if (value instanceof Resource) {
        return DefaultTermFactory.instance().createConstant(urizer.output(value.toString()));
    } else {
        //  Literal
        org.eclipse.rdf4j.model.Literal l = (org.eclipse.rdf4j.model.Literal) value;
        URI uri = URIUtils.createURI(l.getDatatype().toString());
        String label = l.getLabel();
        if (uri.equals(URIUtils.RDF_LANG_STRING)) {
            String opt = l.getLanguage();
            label += "@";
            label += opt;
        }
        return DefaultTermFactory.instance().createLiteral(uri, label);
    }
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) Resource(org.eclipse.rdf4j.model.Resource) URI(fr.lirmm.graphik.util.URI)

Example 2 with Literal

use of fr.lirmm.graphik.graal.api.core.Literal in project graal by graphik-team.

the class DlgpParserTest method backslashedDoubleQuoteTest.

// /////////////////////////////////////////////////////////////////////////
// MISCS
// /////////////////////////////////////////////////////////////////////////
@Test
public void backslashedDoubleQuoteTest() throws ParseException {
    Atom a = DlgpParser.parseAtom("p(\"test\\\"test\").");
    Term identifier = a.getTerm(0);
    Assert.assertTrue(identifier instanceof Literal);
    URI datatype = ((Literal) identifier).getDatatype();
    Assert.assertEquals(URIUtils.XSD_STRING, datatype);
    Object value = ((Literal) identifier).getValue();
    Assert.assertEquals("test\"test", value);
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultURI(fr.lirmm.graphik.util.DefaultURI) URI(fr.lirmm.graphik.util.URI) Atom(fr.lirmm.graphik.graal.api.core.Atom) Test(org.junit.Test)

Example 3 with Literal

use of fr.lirmm.graphik.graal.api.core.Literal in project graal by graphik-team.

the class AbstractRdbmsConjunctiveQueryTranslator method formatFromColumnType.

@Override
public String formatFromColumnType(DBColumn col, Term term) throws AtomSetException {
    switch(col.getType()) {
        case Types.BOOLEAN:
        case Types.INTEGER:
        case Types.BIGINT:
        case Types.SMALLINT:
        case Types.TINYINT:
        case Types.DECIMAL:
        case Types.DOUBLE:
            if (term instanceof Literal) {
                Literal l = (Literal) term;
                URI datatype = l.getDatatype();
                switch(col.getType()) {
                    case Types.BOOLEAN:
                        if (URIUtils.XSD_BOOLEAN.equals(l.getDatatype()) || l.getValue() instanceof Boolean) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                    case Types.INTEGER:
                    case Types.BIGINT:
                    case Types.SMALLINT:
                    case Types.TINYINT:
                        if (URIUtils.XSD_INTEGER.equals(l.getDatatype()) || l.getValue() instanceof Integer || l.getValue() instanceof BigInteger) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                    case Types.DECIMAL:
                        if (URIUtils.XSD_DECIMAL.equals(l.getDatatype()) || l.getValue() instanceof Float || l.getValue() instanceof BigDecimal) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                    case Types.DOUBLE:
                        if (URIUtils.XSD_DOUBLE.equals(l.getDatatype()) || l.getValue() instanceof Double) {
                            return term.getLabel();
                        }
                        throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type (" + datatype.toString() + ")");
                }
            } else {
                throw new AtomSetException("No correspondance between the database field (Boolean) and the Term type.");
            }
        case Types.VARCHAR:
        case Types.NVARCHAR:
        case Types.LONGVARCHAR:
        case Types.LONGNVARCHAR:
            return '\'' + term.getIdentifier().toString() + '\'';
        default:
            throw new AtomSetException("Column type not supported (see java.sql.Types): " + col.getType());
    }
}
Also used : BigInteger(java.math.BigInteger) Literal(fr.lirmm.graphik.graal.api.core.Literal) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) BigInteger(java.math.BigInteger) URI(fr.lirmm.graphik.util.URI) BigDecimal(java.math.BigDecimal)

Example 4 with Literal

use of fr.lirmm.graphik.graal.api.core.Literal in project graal by graphik-team.

the class RDF4jUtils method createValue.

private Value createValue(Term t) throws MalformedLangStringException {
    if (t instanceof Literal) {
        Literal l = (Literal) t;
        if (l.getDatatype().equals(URIUtils.RDF_LANG_STRING)) {
            String value = l.getValue().toString();
            int pos = value.lastIndexOf('@');
            if (pos < 0) {
                throw new MalformedLangStringException("The following label does not contains lang part: " + value);
            }
            String label = value.substring(0, pos + 1);
            String lang = value.substring(pos + 1);
            return valueFactory.createLiteral(label, lang);
        } else {
            return valueFactory.createLiteral(l.getValue().toString(), valueFactory.createIRI(l.getDatatype().toString()));
        }
    } else {
        return createURI(t);
    }
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal)

Example 5 with Literal

use of fr.lirmm.graphik.graal.api.core.Literal in project graal by graphik-team.

the class PureQuery method removeAnswerPredicate.

public static void removeAnswerPredicate(ConjunctiveQuery query) {
    Term[] ans = query.getAnswerVariables().toArray(new Term[query.getAnswerVariables().size()]);
    CloseableIteratorWithoutException<Atom> ita = query.getAtomSet().iterator();
    InMemoryAtomSet toRemove = new LinkedListAtomSet();
    InMemoryAtomSet toAdd = new LinkedListAtomSet();
    while (ita.hasNext()) {
        Atom a = ita.next();
        if (a.getPredicate().equals(ansPredicate)) {
            Term ansTerm = ans[(Integer) ((Literal) a.getTerm(0)).getValue()];
            if (!ansTerm.equals(a.getTerm(1))) {
                toAdd.add(DefaultAtomFactory.instance().create(Predicate.EQUALITY, ansTerm, a.getTerm(1)));
            }
            toRemove.add(a);
        }
    }
    query.getAtomSet().removeAll(toRemove);
    query.getAtomSet().addAll(toAdd);
}
Also used : Literal(fr.lirmm.graphik.graal.api.core.Literal) InMemoryAtomSet(fr.lirmm.graphik.graal.api.core.InMemoryAtomSet) LinkedListAtomSet(fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet) Term(fr.lirmm.graphik.graal.api.core.Term) DefaultAtom(fr.lirmm.graphik.graal.core.DefaultAtom) Atom(fr.lirmm.graphik.graal.api.core.Atom)

Aggregations

Literal (fr.lirmm.graphik.graal.api.core.Literal)9 Atom (fr.lirmm.graphik.graal.api.core.Atom)4 Term (fr.lirmm.graphik.graal.api.core.Term)3 URI (fr.lirmm.graphik.util.URI)3 Test (org.junit.Test)3 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)2 DefaultAtom (fr.lirmm.graphik.graal.core.DefaultAtom)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 LinkedListAtomSet (fr.lirmm.graphik.graal.core.atomset.LinkedListAtomSet)1 DefaultURI (fr.lirmm.graphik.util.DefaultURI)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 BigDecimal (java.math.BigDecimal)1 BigInteger (java.math.BigInteger)1 HashSet (java.util.HashSet)1 Resource (org.eclipse.rdf4j.model.Resource)1 Theory (org.junit.experimental.theories.Theory)1