private ImmutableList<TargetAtom> constructTargetQuery(ImmutableFunctionalTerm classTerm, ObjectRDFType type) { Variable X = termFactory.getVariable("X"); ImmutableFunctionalTerm subjectTerm; if (!type.isBlankNode()) subjectTerm = termFactory.getImmutableUriTemplate(X); else { subjectTerm = termFactory.getImmutableBNodeTemplate(X); } ImmutableTerm predTerm = termFactory.getImmutableUriTemplate(termFactory.getConstantLiteral( org.eclipse.rdf4j.model.vocabulary.RDF.TYPE.toString())); TargetAtom targetAtom = targetAtomFactory.getTripleTargetAtom(subjectTerm, predTerm ,classTerm); return ImmutableList.of(targetAtom); }
private ImmutableTerm createObjectTerm(String column, FunctionSymbol datatype) { ImmutableTerm object; if(column.matches("\"([\\w.]+)?\"")){ object = termFactory.getConstantLiteral(column.substring(1, column.length()-1)); } else { List<FormatString> columnStrings = parse(column); if (columnStrings.size() > 1) { throw new RuntimeException("Unsupported column mapping: " + column); } String columnName = columnStrings.get(0).toString(); object = termFactory.getVariable(columnName); } if (datatype == null) { return object; } else { return termFactory.getImmutableFunctionalTerm(datatype, object); } }
@Inject private ExpressionEvaluator(DatalogTools datalogTools, TermFactory termFactory, TypeFactory typeFactory, UnifierUtilities unifierUtilities, ExpressionNormalizer normalizer, ImmutabilityTools immutabilityTools, RDF rdfFactory) { this.termFactory = termFactory; this.typeFactory = typeFactory; this.datalogTools = datalogTools; valueFalse = termFactory.getBooleanConstant(false); valueTrue = termFactory.getBooleanConstant(true); valueNull = termFactory.getNullConstant(); this.unifierUtilities = unifierUtilities; this.normalizer = normalizer; this.immutabilityTools = immutabilityTools; this.rdfFactory = rdfFactory; }
private ImmutableTerm typeTerm(String text, IRI datatype) { ValueConstant integerConstant = termFactory.getConstantLiteral(text, datatype); return termFactory.getImmutableTypedTerm(integerConstant, datatype); }
private Term typeTerm(String text, COL_TYPE col_type) { ValueConstant integerConstant = TERM_FACTORY.getConstantLiteral(text, COL_TYPE.LITERAL); return TERM_FACTORY.getTypedTerm(integerConstant, col_type); }
cons = str.substring(0, i); str = str.substring(str.indexOf("}", i)+1, str.length()); terms.add(termFactory.getConstantLiteral(cons)); }else{ str = str.substring(str.indexOf("}")+1); terms.add(termFactory.getVariable(joinCond + (var))); if (!str.equals("")){ cons = str; terms.add(termFactory.getConstantLiteral(cons)); uriTemplate = termFactory.getConstantLiteral(string); terms.add(0, uriTemplate); // the URI template is always on the return termFactory.getImmutableUriTemplate(ImmutableList.copyOf(terms)); uriTemplate = termFactory.getConstantLiteral(string); terms.add(0, uriTemplate); // the URI template is always on the return termFactory.getImmutableUriTemplate(ImmutableList.copyOf(terms)); uriTemplate = termFactory.getConstantBNode(string); terms.add(0, uriTemplate); // the URI template is always on the return termFactory.getImmutableBNodeTemplate(ImmutableList.copyOf(terms)); return termFactory.getImmutableTypedTerm(uriTemplate, XSD.STRING); case 4://concat
private ImmutableFunctionalTerm createGroundFunctionalTerm(IRI iri) { return termFactory.getImmutableUriTemplate(termFactory.getConstantLiteral(iri.getIRIString())); } }
private ImmutableFunctionalTerm getUriFunctionTerm(String text) { final String PLACEHOLDER = "{}"; List<ImmutableTerm> terms = new LinkedList<>(); List<FormatString> tokens = parse(text); StringBuilder sb = new StringBuilder(); for (FormatString token : tokens) { if (token instanceof FixedString) { // if part of URI template sb.append(token.toString()); } else if (token instanceof ColumnString) { sb.append(PLACEHOLDER); Variable column = termFactory.getVariable(token.toString()); terms.add(column); } } ValueConstant uriTemplate = termFactory.getConstantLiteral(sb.toString()); // complete URI template terms.add(0, uriTemplate); return termFactory.getImmutableUriTemplate(ImmutableList.copyOf(terms)); }
/** * Definition row graph: an RDF graph consisting of the following triples: * <p/> * - the row type triple. * - a literal triple for each column in a table where the column value is non-NULL. * */ public ImmutableList<ImmutableFunctionalTerm> getCQ(DatabaseRelationDefinition table) { ImmutableList.Builder<ImmutableFunctionalTerm> atoms = ImmutableList.builder(); //Class Atom ImmutableTerm sub = generateSubject(table, false); atoms.add(df.getImmutableFunctionalTerm(df.getClassPredicate(getTableIRI(table.getID())), sub)); //DataType Atoms for (Attribute att : table.getAttributes()) { Predicate.COL_TYPE type = typeMapper.getPredicate(att.getType()); Variable objV = df.getVariable(att.getID().getName()); ImmutableTerm obj = df.getImmutableTypedTerm(objV, type); atoms.add(df.getImmutableFunctionalTerm(df.getDataPropertyPredicate(getLiteralPropertyIRI(att)), sub, obj)); } return atoms.build(); }
private Variable generateNewVariable(String prefix) { String newVariableName; do { newVariableName = prefix + (count++); } while (knownVariableMap.containsKey(newVariableName)); Variable newVariable = termFactory.getVariable(newVariableName); knownVariableMap.put(newVariableName, newVariable); return newVariable; }
return termFactory.getConstantLiteral(stringValue, typeFactory.getUnsupportedDatatype()); case NULL: return null; return termFactory.getConstantIRI(rdfFactory.createIRI(stringValue.trim())); return termFactory.getConstantBNode(scopedLabel); case LANG_STRING: return termFactory.getConstantLiteral(stringValue); else return termFactory.getConstantLiteral(stringValue, language); return termFactory.getBooleanConstant(bvalue); return termFactory.getConstantLiteral(stringValue, XSD.DECIMAL); case FLOAT: return termFactory.getConstantLiteral(extractFloatingValue(stringValue), XSD.FLOAT); case DOUBLE: return termFactory.getConstantLiteral(extractFloatingValue(stringValue), XSD.DOUBLE); return termFactory.getConstantLiteral(stringValue, XSD.INT); return termFactory.getConstantLiteral(stringValue, XSD.LONG); case UNSIGNED_INT: return termFactory.getConstantLiteral(stringValue, XSD.UNSIGNED_INT); return termFactory.getConstantLiteral(extractIntegerValue(stringValue), XSD.INTEGER); case NEGATIVE_INTEGER:
private static Function get_REPLACE(ImmutableList<Term> terms, net.sf.jsqlparser.expression.Function expression) { Term flags = TERM_FACTORY.getConstantLiteral(""); switch (terms.size()) { case 2: return TERM_FACTORY.getFunction( ExpressionOperation.REPLACE, terms.get(0), terms.get(1), TERM_FACTORY.getConstantLiteral(""), flags); case 3: return TERM_FACTORY.getFunction( ExpressionOperation.REPLACE, terms.get(0), terms.get(1), terms.get(2), flags); } throw new InvalidSelectQueryRuntimeException("Wrong number of arguments in SQL function", expression); }
private Constant getConstant(ProjectionElem node, OntopBindingSet bindingSet) throws OntopResultConversionException, OntopConnectionException { Constant constant = null; String node_name = node.getSourceName(); ValueExpr ve = null; if (extMap != null) { ve = extMap.get(node_name); if (ve != null && ve instanceof Var) throw new OntopResultConversionException("Invalid query. Found unbound variable: " + ve); } // if (node_name.charAt(0) == '-') { if (ve instanceof org.eclipse.rdf4j.query.algebra.ValueConstant) { org.eclipse.rdf4j.query.algebra.ValueConstant vc = (org.eclipse.rdf4j.query.algebra.ValueConstant) ve; if (vc.getValue() instanceof IRI) { constant = termFactory.getConstantIRI(rdfFactory.createIRI(vc.getValue().stringValue())); } else if (vc.getValue() instanceof Literal) { constant = termFactory.getConstantLiteral(vc.getValue().stringValue()); } else { constant = termFactory.getConstantBNode(vc.getValue().stringValue()); } } else { constant = bindingSet.getConstant(node_name); } return constant; }
private ImmutableList<TargetAtom> constructTargetQuery(ImmutableFunctionalTerm iriTerm, ObjectRDFType type1, RDFTermType type2) { Variable X = termFactory.getVariable("X"); Variable Y = termFactory.getVariable("Y"); ImmutableFunctionalTerm subjectTerm; if (!type1.isBlankNode()) subjectTerm = termFactory.getImmutableUriTemplate(X); else { subjectTerm = termFactory.getImmutableBNodeTemplate(X); } ImmutableFunctionalTerm objectTerm; if (type2 instanceof ObjectRDFType) { objectTerm = ((ObjectRDFType)type2).isBlankNode() ? termFactory.getImmutableBNodeTemplate(Y) : termFactory.getImmutableUriTemplate(Y); } else { RDFDatatype datatype = (RDFDatatype) type2; if (datatype.getLanguageTag().isPresent()) { LanguageTag languageTag = datatype.getLanguageTag().get(); objectTerm = termFactory.getImmutableTypedTerm(Y, languageTag.getFullString()); } else { objectTerm = termFactory.getImmutableTypedTerm(Y, datatype); } } TargetAtom targetAtom = targetAtomFactory.getTripleTargetAtom(subjectTerm,iriTerm,objectTerm); return ImmutableList.of(targetAtom); }
@Override public ImmutableTerm visitLitString(LitStringContext ctx) { String str = ctx.STRING_LITERAL_QUOTE().getText(); if (str.contains("{")) { return getNestedConcat(str); } return termFactory.getConstantLiteral(str.substring(1, str.length() - 1), XSD.STRING); // without the double quotes }
return TERM_FACTORY.getConstantURI(stringValue.trim()); return TERM_FACTORY.getConstantBNode(scopedLabel); return TERM_FACTORY.getConstantLiteral(stringValue); else return TERM_FACTORY.getConstantLiteral(stringValue, language); return TERM_FACTORY.getBooleanConstant(bvalue); return TERM_FACTORY.getConstantLiteral(stringValue,type); return TERM_FACTORY.getConstantLiteral(formatter.format(bigDecimal),type); case LONG: case UNSIGNED_INT: return TERM_FACTORY.getConstantLiteral(stringValue, type); return TERM_FACTORY.getConstantLiteral(integerString, type); return TERM_FACTORY.getConstantLiteral( DateTimeFormatter.ISO_DATE_TIME.format(temporal),type); return TERM_FACTORY.getConstantLiteral( DateTimeFormatter.ISO_DATE.format(convertToJavaDate(value)),Predicate.COL_TYPE.DATE); return TERM_FACTORY.getConstantLiteral(DateTimeFormatter.ISO_TIME.format(convertToTime(value)), Predicate.COL_TYPE.TIME); return TERM_FACTORY.getConstantLiteral(stringValue, type);
if (QueryUtils.isGrounded(object)) { ValueConstant c = ((ValueConstant) ((Function) object).getTerm(0)); // it has to be a URI constant Predicate predicate = TERM_FACTORY.getClassPredicate(c.getValue()); atom = TERM_FACTORY.getFunction(predicate, subject); } else { atom = ATOM_FACTORY.getTripleAtom(subject, pred, object); Term uriOfPred = TERM_FACTORY.getUriTemplate(pred); Term uriOfObject = TERM_FACTORY.getUriTemplate(object); atom = ATOM_FACTORY.getTripleAtom(subject, uriOfPred, uriOfObject); } else { ValueConstant pr = (ValueConstant) ((Function) pred).getTerm(0); if (object instanceof Variable) { predicate = TERM_FACTORY.getDataPropertyPredicate(pr.getValue()); } else { if (object instanceof Function) { if (((Function) object).getFunctionSymbol() instanceof URITemplatePredicate) { predicate = TERM_FACTORY.getObjectPropertyPredicate(pr.getValue()); } else { predicate = TERM_FACTORY.getDataPropertyPredicate(pr.getValue()); throw new IllegalArgumentException("predicate should be a URI Function"); atom = TERM_FACTORY.getFunction(predicate, subject, object);
/*** * Adds a trivial equality to a LeftJoin in case the left join doesn't have * at least one boolean condition. This is necessary to have syntactically * correct LeftJoins in SQL. * * @param leftJoin */ private static void addMinimalEqualityToLeftJoin(Function leftJoin) { int booleanAtoms = 0; boolean isLeftJoin = leftJoin.isAlgebraFunction(); for (Term term : leftJoin.getTerms()) { Function f = (Function) term; if (f.isAlgebraFunction()) { addMinimalEqualityToLeftJoin(f); } if (f.isOperation()) booleanAtoms += 1; } if (isLeftJoin && booleanAtoms == 0) { Function trivialEquality = TERM_FACTORY.getFunctionEQ(TERM_FACTORY.getConstantLiteral("1", COL_TYPE.INTEGER), TERM_FACTORY.getConstantLiteral("1", COL_TYPE.INTEGER)); leftJoin.getTerms().add(trivialEquality); } }
@Override public void visit(SWRLLiteralArgument node) { OWLLiteral literal=node.getLiteral(); if (literal.isBoolean()) terms.add(termFactory.getBooleanConstant(literal.parseBoolean())); else if(literal.hasLang()) terms.add(termFactory.getConstantLiteral(literal.getLiteral(), literal.getLang())); else if (literal.isDouble()) terms.add(termFactory.getConstantLiteral(literal.getLiteral(), typeFactory.getXsdDoubleDatatype())); else if (literal.isFloat()) terms.add(termFactory.getConstantLiteral(literal.getLiteral(), typeFactory.getXsdDecimalDatatype())); else if (literal.isInteger()) terms.add(termFactory.getConstantLiteral(literal.getLiteral(), typeFactory.getXsdIntegerDatatype())); else termFactory.getConstantLiteral(literal.getLiteral()); }
private Function replaceConstants(Function a, List<Function> filters) { Function atom = (Function)a.clone(); for (int i = 0; i < atom.getTerms().size(); i++) { Term term = atom.getTerm(i); if (term instanceof Constant) { // Found a constant, replacing with a fresh variable // and adding the new equality atom Constant c = (Constant)term; Variable var = valueMap.get(c); if (var == null) { freshVarCount++; var = TERM_FACTORY.getVariable("?FreshVar" + freshVarCount); valueMap.put(c, var); filters.add(TERM_FACTORY.getFunctionEQ(var, c)); } atom.setTerm(i, var); } } return atom; }