public static CQIE rule(Function head, List<Function> body){ return DATALOG_FACTORY.getCQIE(head, body); }
public static DatalogProgram program(Collection<CQIE> rules){ DatalogProgram datalogProgram = DATALOG_FACTORY.getDatalogProgram(); datalogProgram.appendRule(rules); return datalogProgram; }
@Override public Function f(Function firstAtom, Function secondAtom) { return DATALOG_FACTORY.getSPARQLJoin(firstAtom, secondAtom, TRUE_EQ); } }, firstAtom);
public Boolean isLeftJoinOrJoinAtom(Function atom) { Predicate predicate = atom.getFunctionSymbol(); return predicate.equals(datalogFactory.getSparqlLeftJoinPredicate()) || predicate.equals(datalogFactory.getSparqlJoinPredicate()); }
private CQIE createRule(DatalogProgram pr, String headName, List<Term> headParameters, Function... body) { Predicate pred = datalogFactory.getSubqueryPredicate(headName, headParameters.size()); Function head = termFactory.getFunction(pred, headParameters); CQIE rule = datalogFactory.getCQIE(head, body); pr.appendRule(rule); return rule; }
@Override public DatalogProgram getSameAsRewriting(DatalogProgram pr) { if (targetPredicates == null) targetPredicates = SameAsTargets.extract(saturatedMapping); DatalogProgram result = datalogFactory.getDatalogProgram(pr.getQueryModifiers()); for (CQIE q: pr.getRules()) { List<Function> body = new ArrayList<>(q.getBody().size()); for (Function a : q.getBody()) { Function ap = addSameAs(a, result, "sameAs" + (rules++)); body.add(ap); } result.appendRule(datalogFactory.getCQIE(q.getHead(), body)); } return result; }
} else { dataAtoms.add(atom); if (atom.getFunctionSymbol().equals(datalogFactory.getSparqlLeftJoinPredicate())) foldJoinTrees(atom.getTerms(), false); if (atom.getFunctionSymbol().equals(datalogFactory.getSparqlJoinPredicate())) foldJoinTrees(atom.getTerms(), true); Function joinAtom = datalogFactory.getSPARQLJoin(dataAtoms.remove(0), dataAtoms.remove(0)); joinAtom.getTerms().addAll(booleanAtoms); booleanAtoms.clear();
/** * This method is used to chase foreign key constraint rule in which the rule * has only one atom in the body. * * IMPORTANT: each rule is applied only ONCE to each atom * * @param atoms * @return set of atoms */ private Set<Function> chaseAtoms(Collection<Function> atoms) { Set<Function> derivedAtoms = new HashSet<>(); for (Function fact : atoms) { derivedAtoms.add(fact); for (LinearInclusionDependency d : dependencies.get(fact.getFunctionSymbol())) { CQIE rule = datalogFactory.getFreshCQIECopy(datalogFactory.getCQIE(d.getHead(), d.getBody())); Function ruleBody = rule.getBody().get(0); Substitution theta = unifierUtilities.getMGU(ruleBody, fact); if (theta != null && !theta.isEmpty()) { Function ruleHead = rule.getHead(); Function newFact = (Function)ruleHead.clone(); // unify to get fact is needed because the dependencies are not necessarily full // (in other words, they may contain existentials in the head) substitutionUtilities.applySubstitution(newFact, theta); derivedAtoms.add(newFact); } } } return derivedAtoms; }
Function body = datalogFactory.getSPARQLJoin(wrapNonTriplePattern(a1), wrapNonTriplePattern(a2)); ImmutableSet<Variable> vars = Sets.union(a1.variables, a2.variables).immutableCopy(); Function body = datalogFactory.getSPARQLLeftJoin(wrapNonTriplePattern(a1), wrapNonTriplePattern(a2));
for (int i = 0; i < body.size(); i++) { Function currentAtom = (Function) body.get(i); if (currentAtom.getFunctionSymbol().equals(datalogFactory.getSparqlJoinPredicate())) { unfoldJoinTrees(currentAtom.getTerms()); body.remove(i);
private Function getFreshHead(List<Term> terms) { Predicate pred = datalogFactory.getSubqueryPredicate("" + predicateIdx, terms.size()); predicateIdx++; return termFactory.getFunction(pred, terms); }
ImmutableExpression filter2 = filter.get(); Expression mutFilter = immutabilityTools.convertToMutableBooleanExpression(filter2); Function newLJAtom = datalogFactory.getSPARQLLeftJoin(atomsListLeft, atomsListRight, Optional.of(mutFilter)); body.add(newLJAtom); return body; }else{ Function newLJAtom = datalogFactory.getSPARQLLeftJoin(atomsListLeft, atomsListRight, Optional.empty()); body.add(newLJAtom); return body; datalogFactory.getDummyPredicate(++dummyPredCounter), new ArrayList<>()));
private DistinctVariableOnlyDataAtom generateProjectionAtom(ImmutableSet<Variable> projectedVariables) { AtomPredicate newPredicate = ATOM_FACTORY.getAtomPredicate(DATALOG_FACTORY.getSubqueryPredicatePrefix() + ++subQueryCounter, projectedVariables.size()); return ATOM_FACTORY.getDistinctVariableOnlyDataAtom(newPredicate, ImmutableList.copyOf(projectedVariables)); }
ImmutableExpression filter2 = filter.get(); Expression mutFilter = ImmutabilityTools.convertToMutableBooleanExpression(filter2); Function newLJAtom = DATALOG_FACTORY.getSPARQLLeftJoin(atomsListLeft, atomsListRight, Optional.of(mutFilter)); body.add(newLJAtom); return body; } else { Function newLJAtom = DATALOG_FACTORY.getSPARQLLeftJoin(atomsListLeft, atomsListRight, Optional.empty()); body.add(newLJAtom); return body;
ccBody.add(ccAtom); outputRules.add(datalogFactory.getCQIE(cqieAtom, ccBody)); DatalogProgram programAfterRewriting = datalogFactory.getDatalogProgram(program.getQueryModifiers(), outputRules); IQ convertedIQ = datalogConverter.convertDatalogProgram(programAfterRewriting, ImmutableList.of(), query.getProjectionAtom().getArguments());
/** * 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)); } } }
private DistinctVariableOnlyDataAtom generateProjectionAtom(ImmutableSet<Variable> projectedVariables) { AtomPredicate newPredicate = datalogFactory.getSubqueryPredicate("" + ++subQueryCounter, projectedVariables.size()); return atomFactory.getDistinctVariableOnlyDataAtom(newPredicate, ImmutableList.copyOf(projectedVariables)); }
/*** * Creates mappings with heads as "triple(x,y,z)" from mappings with binary * and unary atoms" * * TODO: clean it */ private static ImmutableList<CQIE> generateTripleMappings(ImmutableSet<CQIE> saturatedRules) { return saturatedRules.stream() .filter(r -> !r.getHead().getFunctionSymbol().getName().startsWith(DATALOG_FACTORY.getSubqueryPredicatePrefix())) .map(r -> generateTripleMapping(r)) .collect(ImmutableCollectors.toList()); }
public static CQIE rule(Function head, Function... body){ return DATALOG_FACTORY.getCQIE(head, body); }
/** * 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; }