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);
}
}
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);
}
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());
}
}
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);
}
}
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);
}
Aggregations