private Function wrapNonTriplePattern(TranslationResult sub) { if (sub.atoms.size() > 1 || sub.atoms.get(0).isAlgebraFunction()) { Function head = getFreshHead(new ArrayList<>(sub.variables)); appendRule(head, sub.atoms); return head; } return sub.atoms.get(0); }
/** * Extracts all the data atoms (without preserving the algebraic structure) */ private static Stream<Function> extractDataAtoms(Collection<? extends Term> atoms) { return atoms.stream() .filter(t -> t instanceof Function) .map(f -> (Function) f) .flatMap(a -> { if (a.isDataFunction()) { return Stream.of(a); } else if (a.isAlgebraFunction()) { return extractDataAtoms(a.getTerms()); } else { return Stream.empty(); } }); }
public static void addMinimalEqualityToLeftJoin(CQIE query) { for (Function f : query.getBody()) { if (f.isAlgebraFunction()) { addMinimalEqualityToLeftJoin(f); } } }
/*** * 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. */ public void addMinimalEqualityToLeftOrNestedInnerJoin(CQIE query) { for (Function f : query.getBody()) { if (f.isAlgebraFunction()) { addMinimalEqualityToLeftOrNestedInnerJoin(f); } } }
/** * Extracts all the data atoms (without preserving the algebraic structure) */ private static Stream<Function> extractDataAtoms(Collection<? extends Term> atoms) { return atoms.stream() .filter(t -> t instanceof Function) .map(f -> (Function) f) .flatMap(a -> { if (a.isDataFunction()) { return Stream.of(a); } else if (a.isAlgebraFunction()) { return extractDataAtoms(a.getTerms()); } else { return Stream.empty(); } }); }
/** * Extracts all the data atoms (without preserving the algebraic structure) */ private static Stream<Function> extractDataAtoms(Collection<? extends Term> atoms) { return atoms.stream() .filter(t -> t instanceof Function) .map(f -> (Function) f) .flatMap(a -> { if (a.isDataFunction()) { return Stream.of(a); } else if (a.isAlgebraFunction()) { return extractDataAtoms(a.getTerms()); } else { return Stream.empty(); } }); }
/** * Normalizes a left-join or join meta-atom. */ private static PullOutEqLocalNormResult normalizeCompositeAtom(Function atom, VariableDispatcher variableDispatcher) { /** * Meta-atoms (algebra) */ if (atom.isAlgebraFunction()) { Predicate functionSymbol = atom.getFunctionSymbol(); if (functionSymbol.equals(DatalogAlgebraOperatorPredicates.SPARQL_LEFTJOIN)) { return normalizeLeftJoin(atom, variableDispatcher); } else if (functionSymbol.equals(DatalogAlgebraOperatorPredicates.SPARQL_JOIN)) { return normalizeJoin(atom, variableDispatcher); } } throw new IllegalArgumentException("A composite (join, left join) atom was expected, not " + atom); }
if (atom.isAlgebraFunction()) { Predicate functionSymbol = atom.getFunctionSymbol(); ImmutableList<Function> joinAtoms = convert(atom.getTerms()); if (functionSymbol == DatalogAlgebraOperatorPredicates.SPARQL_JOIN) { boolean parenthesis = joinAtoms.get(0).isAlgebraFunction() || joinAtoms.get(1).isAlgebraFunction(); boolean parenthesis = joinAtoms.get(1).isAlgebraFunction();
/** * Returns the table definition for the given atom. If the atom is a simple * table or view, then it returns the value as defined by the * AliasIndex. If the atom is a Join or Left Join, it will call * getTableDefinitions on the nested term list. */ private String getTableDefinition(Function atom, AliasIndex index, String indent) { if (atom.isAlgebraFunction()) { Predicate functionSymbol = atom.getFunctionSymbol(); ImmutableList<Function> joinAtoms = convert(atom.getTerms()); if (functionSymbol.equals(datalogFactory.getSparqlJoinPredicate())) { // nested joins we need to add parenthesis later boolean parenthesis = joinAtoms.get(0).isAlgebraFunction() || joinAtoms.get(1).isAlgebraFunction(); return getTableDefinitions(joinAtoms, index, "JOIN", parenthesis, indent + INDENT); } else if (functionSymbol.equals(datalogFactory.getSparqlLeftJoinPredicate())) { // in case of left join we want to add the parenthesis only for the right tables // we ignore nested joins from the left tables boolean parenthesis = joinAtoms.get(1).isAlgebraFunction(); return getTableDefinitions(joinAtoms, index, "LEFT OUTER JOIN", parenthesis, indent + INDENT); } } else if (!atom.isOperation() && !atom.isDataTypeFunction()) { return index.getViewDefinition(atom); // a database atom } return null; }
|| funcRoot.isDataTypeFunction() || funcRoot.isAlgebraFunction()) { for (Term term : funcRoot.getTerms()) { queueInAtom.add(term);
if (atom.isDataFunction()) { TermUtils.addReferencedVariablesTo(currentLevelVariables, atom); } else if (atom.isAlgebraFunction()) { currentLevelVariables.addAll(getDefinedVariables(atom.getTerms())); } else {
/**** * Gets all the variables that are defined in this list of atoms, except in * atom i * * @param atoms * @return */ private static Set<Variable> getDefinedVariables(List<Term> atoms) { Set<Variable> currentLevelVariables = new HashSet<>(); for (Term l : atoms) { Function atom = (Function) l; if (atom.isOperation()) { continue; } else if (atom.isAlgebraFunction()) { currentLevelVariables.addAll(getDefinedVariables(atom.getTerms())); } else { TermUtils.addReferencedVariablesTo(currentLevelVariables, atom); } } return currentLevelVariables; }
/*** * 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); } }
private void addMinimalEqualityToLeftOrNestedInnerJoin(Function algebraFunctionalTerm) { int booleanAtoms = 0; for (Term term : algebraFunctionalTerm.getTerms()) { Function f = (Function) term; if (f.isAlgebraFunction()) { addMinimalEqualityToLeftOrNestedInnerJoin(f); } if (f.isOperation()) booleanAtoms++; } if (algebraFunctionalTerm.isAlgebraFunction() && booleanAtoms == 0) { Function trivialEquality = termFactory.getFunctionEQ( termFactory.getConstantLiteral("1", typeFactory.getXsdIntegerDatatype()), termFactory.getConstantLiteral("1", typeFactory.getXsdIntegerDatatype())); algebraFunctionalTerm.getTerms().add(trivialEquality); } }
/** * updates * * @param rule */ public void updateRuleIndexByBodyPredicate(CQIE rule) { for (Function bodyAtom : rule.getBody()) { if (bodyAtom.isDataFunction()) { Predicate functionSymbol = bodyAtom.getFunctionSymbol(); if (!ruleIndexByBodyPredicate.containsEntry(functionSymbol, rule)){ ruleIndexByBodyPredicate.put(functionSymbol, rule); } } else if (bodyAtom.isAlgebraFunction() || bodyAtom.isOperation()) { updateRuleIndexByBodyPredicate_traverseBodyAtom(rule, bodyAtom); // BC: should we reintroduce arithmetic functions? //} else if (bodyAtom.isArithmeticFunction() || bodyAtom.isDataTypeFunction()){ } else if (bodyAtom.isDataTypeFunction()){ continue; } else { throw new IllegalStateException("Unknown Function"); } } }
/** * updates * * @param rule */ public void updateRuleIndexByBodyPredicate(CQIE rule) { for (Function bodyAtom : rule.getBody()) { if (bodyAtom.isDataFunction()) { Predicate functionSymbol = bodyAtom.getFunctionSymbol(); if (!ruleIndexByBodyPredicate.containsEntry(functionSymbol, rule)){ ruleIndexByBodyPredicate.put(functionSymbol, rule); } } else if (bodyAtom.isAlgebraFunction() || bodyAtom.isOperation()) { updateRuleIndexByBodyPredicate_traverseBodyAtom(rule, bodyAtom); // BC: should we reintroduce arithmetic functions? //} else if (bodyAtom.isArithmeticFunction() || bodyAtom.isDataTypeFunction()){ } else if (bodyAtom.isDataTypeFunction()){ continue; } else { throw new IllegalStateException("Unknown Function"); } } }
/** * Removes the old indexes given by a rule. * * @param rule */ public void removeOldRuleIndexByBodyPredicate(CQIE rule) { for (Function bodyAtom : rule.getBody()) { if (bodyAtom.isDataFunction()) { Predicate functionSymbol = bodyAtom.getFunctionSymbol(); if (ruleIndexByBodyPredicate.containsEntry(functionSymbol, rule)){ ruleIndexByBodyPredicate.remove(functionSymbol, rule); } } else if (bodyAtom.isAlgebraFunction() || bodyAtom.isOperation()) { removeRuleIndexByBodyPredicate_traverseBodyAtom(rule, bodyAtom); // BC: should we reintroduce arithmetic functions?//} // else if (bodyAtom.isArithmeticFunction() || bodyAtom.isDataTypeFunction()){ } else if (bodyAtom.isDataTypeFunction()){ continue; } else { throw new IllegalStateException("Unknown Function"); } } }
/** * Collects (recursively) the set of variables that participate in data atoms * (either in this atom directly or in nested ones, excluding those on the * right-hand side of left joins. * * @param vars * @param atom * @return */ private void collectVariableReferencesWithLeftJoin(Set<Variable> vars, Function atom) { if (atom.isDataFunction()) { TermUtils.addReferencedVariablesTo(vars, atom); } else if (atom.isAlgebraFunction()) { Predicate functionSymbol = atom.getFunctionSymbol(); if (functionSymbol.equals(datalogFactory.getSparqlJoinPredicate())) { // if it's a join, we need to collect all the variables of each nested atom convert(atom.getTerms()).stream() .filter(f -> !f.isOperation()) .forEach(f -> collectVariableReferencesWithLeftJoin(vars, f)); } else if (functionSymbol.equals(datalogFactory.getSparqlLeftJoinPredicate())) { // if it's a left join, only of the first data/algebra atom (the left atom) collectVariableReferencesWithLeftJoin(vars, (Function) atom.getTerm(0)); } } }
/** * Collects (recursively) the set of variables that participate in data atoms * (either in this atom directly or in nested ones, excluding those on the * right-hand side of left joins. * * @param vars * @param atom * @return */ private void collectVariableReferencesWithLeftJoin(Set<Variable> vars, Function atom) { if (atom.isDataFunction()) { TermUtils.addReferencedVariablesTo(vars, atom); } else if (atom.isAlgebraFunction()) { Predicate functionSymbol = atom.getFunctionSymbol(); if (functionSymbol == DatalogAlgebraOperatorPredicates.SPARQL_JOIN) { // if it's a join, we need to collect all the variables of each nested atom convert(atom.getTerms()).stream() .filter(f -> !f.isOperation()) .forEach(f -> collectVariableReferencesWithLeftJoin(vars, f)); } else if (functionSymbol == DatalogAlgebraOperatorPredicates.SPARQL_LEFTJOIN) { // if it's a left join, only of the first data/algebra atom (the left atom) collectVariableReferencesWithLeftJoin(vars, (Function) atom.getTerm(0)); } } }
} else if (bodyAtom.isAlgebraFunction() || bodyAtom.isOperation()) { updatePredicateDependencyGraph_traverseBodyAtom(dependencyList, bodyAtom);