public static List<Function> cloneList(List<Function> list) { List<Function> newlist = new ArrayList<>(list.size()); for (Function atom : list) { Function clone = (Function)atom.clone(); newlist.add(clone); } return newlist; }
public static List<Function> cloneList(List<Function> list) { List<Function> newlist = new ArrayList<>(list.size()); for (Function atom : list) { Function clone = (Function)atom.clone(); newlist.add(clone); } return newlist; }
@Override public CQIEImpl clone() { Function copyHead = (Function)head.clone(); List<Function> copyBody = new ArrayList<Function>(body.size() + 10); for (Function atom : body) { if (atom != null) { copyBody.add((Function) atom.clone()); } } CQIEImpl newquery = new CQIEImpl(copyHead, copyBody); newquery.rehash = this.rehash; newquery.string = null; newquery.hash = this.hash; return newquery; }
TMappingRule(Function head, TMappingRule baseRule) { this.filterAtoms = new ArrayList<>(baseRule.filterAtoms.size()); for (List<Function> baseList: baseRule.filterAtoms) filterAtoms.add(cloneList(baseList)); this.databaseAtoms = cloneList(baseRule.databaseAtoms); this.head = (Function)head.clone(); this.stripped = DATALOG_FACTORY.getCQIE(head, databaseAtoms); this.cqc = baseRule.cqc; }
TMappingRule(TMappingRule baseRule, List<List<Function>> filterAtoms) { this.databaseAtoms = cloneList(baseRule.databaseAtoms); this.head = (Function)baseRule.head.clone(); this.filterAtoms = filterAtoms; this.stripped = DATALOG_FACTORY.getCQIE(head, databaseAtoms); this.cqc = baseRule.cqc; }
TMappingRule(Function head, TMappingRule baseRule, DatalogFactory datalogFactory, TermFactory termFactory, EQNormalizer eqNormalizer, boolean isClass) { this.filterAtoms = new ArrayList<>(baseRule.filterAtoms.size()); this.datalogFactory = datalogFactory; this.termFactory = termFactory; this.eqNormalizer = eqNormalizer; this.isClass = isClass; for (List<Function> baseList: baseRule.filterAtoms) filterAtoms.add(cloneList(baseList)); this.databaseAtoms = cloneList(baseRule.databaseAtoms); this.head = (Function)head.clone(); this.stripped = this.datalogFactory.getCQIE(head, databaseAtoms); this.cqc = baseRule.cqc; }
TMappingRule(TMappingRule baseRule, List<List<Function>> filterAtoms, DatalogFactory datalogFactory, TermFactory termFactory, EQNormalizer eqNormalizer) { this.databaseAtoms = cloneList(baseRule.databaseAtoms); this.head = (Function)baseRule.head.clone(); this.filterAtoms = filterAtoms; this.datalogFactory = datalogFactory; this.termFactory = termFactory; this.eqNormalizer = eqNormalizer; this.isClass = baseRule.isClass(); this.stripped = this.datalogFactory.getCQIE(head, databaseAtoms); this.cqc = baseRule.cqc; }
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; }
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 = termFactory.getVariable("?FreshVar" + freshVarCount); valueMap.put(c, var); filters.add(termFactory.getFunctionEQ(var, c)); } atom.setTerm(i, var); } } return atom; }
Function copyRuleHead = (Function) rule.getHead().clone(); SubstitutionUtilities.applySubstitution(copyRuleHead, theta);
/** * Unifies two atoms in a conjunctive query returning a new conjunctive * query. To to this we calculate the MGU for atoms, duplicate the query q * into q', remove i and j from q', apply the mgu to q', and * * @param q * @param i * @param j (j > i) * @return null if the two atoms are not unifiable, else a new conjunctive * query produced by the unification of j and i * @throws Exception */ private static CQIE unify(CQIE q, int i, int j) { Function atom1 = q.getBody().get(i); Function atom2 = q.getBody().get(j); Substitution mgu = UnifierUtilities.getMGU(atom1, atom2); if (mgu == null) return null; CQIE unifiedQ = SubstitutionUtilities.applySubstitution(q, mgu); unifiedQ.getBody().remove(i); unifiedQ.getBody().remove(j - 1); Function newatom = (Function) atom1.clone(); SubstitutionUtilities.applySubstitution(newatom, mgu); unifiedQ.getBody().add(i, newatom); return unifiedQ; }
/** * Unifies two atoms in a conjunctive query returning a new conjunctive * query. To to this we calculate the MGU for atoms, duplicate the query q * into q', remove i and j from q', apply the mgu to q', and * * @param q * @param i * @param j (j > i) * @return null if the two atoms are not unifiable, else a new conjunctive * query produced by the unification of j and i * @throws Exception */ public static CQIE unify(CQIE q, int i, int j) { Function atom1 = q.getBody().get(i); Function atom2 = q.getBody().get(j); Substitution mgu = getMGU(atom1, atom2); if (mgu == null) return null; CQIE unifiedQ = SubstitutionUtilities.applySubstitution(q, mgu); unifiedQ.getBody().remove(i); unifiedQ.getBody().remove(j - 1); Function newatom = (Function) atom1.clone(); SubstitutionUtilities.applySubstitution(newatom, mgu); unifiedQ.getBody().add(i, newatom); return unifiedQ; }
@Override public boolean composeFunctions(Function first, Function second) { // Basic case: if predicates are different or their arity is different, // then no unifier if ((first.getArity() != second.getArity() || !first.getFunctionSymbol().equals(second.getFunctionSymbol()))) { return false; } Function firstAtom = (Function) first.clone(); Function secondAtom = (Function) second.clone(); int arity = first.getArity(); // Computing the disagreement set for (int termidx = 0; termidx < arity; termidx++) { // Checking if there are already substitutions calculated for the // current terms. If there are any, then we have to take the // substituted terms instead of the original ones. Term term1 = firstAtom.getTerm(termidx); Term term2 = secondAtom.getTerm(termidx); if (!composeTerms(term1, term2)) return false; // Applying the newly computed substitution to the 'replacement' of // the existing substitutions substitutionUtilities.applySubstitution(firstAtom, this, termidx + 1); substitutionUtilities.applySubstitution(secondAtom, this, termidx + 1); } return true; }
@Override public boolean composeFunctions(Function first, Function second) { // Basic case: if predicates are different or their arity is different, // then no unifier if ((first.getArity() != second.getArity() || !first.getFunctionSymbol().equals(second.getFunctionSymbol()))) { return false; } Function firstAtom = (Function) first.clone(); Function secondAtom = (Function) second.clone(); int arity = first.getArity(); // Computing the disagreement set for (int termidx = 0; termidx < arity; termidx++) { // Checking if there are already substitutions calculated for the // current terms. If there are any, then we have to take the // substituted terms instead of the original ones. Term term1 = firstAtom.getTerm(termidx); Term term2 = secondAtom.getTerm(termidx); if (!composeTerms(term1, term2)) return false; // Applying the newly computed substitution to the 'replacement' of // the existing substitutions SubstitutionUtilities.applySubstitution(firstAtom, this, termidx + 1); SubstitutionUtilities.applySubstitution(secondAtom, this, termidx + 1); } return true; }
/** * 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; }
private Term evalStr(Function term) { Term innerTerm = term.getTerm(0); if (innerTerm instanceof Function) { Function function = (Function) innerTerm; Predicate predicate = function.getFunctionSymbol(); Term parameter = function.getTerm(0); if (function.isDataTypeFunction()) { if (TYPE_FACTORY.isString(predicate) ) { // R: was datatype.equals(OBDAVocabulary.RDFS_LITERAL_URI) return TERM_FACTORY.getTypedTerm( TERM_FACTORY.getVariable(parameter.toString()), COL_TYPE.STRING); } else { return TERM_FACTORY.getTypedTerm( TERM_FACTORY.getFunctionCast(TERM_FACTORY.getVariable(parameter.toString()), TERM_FACTORY.getConstantLiteral(TYPE_FACTORY.getDatatypeURI(COL_TYPE.STRING).stringValue())), COL_TYPE.STRING); } } else if (predicate instanceof URITemplatePredicate) { return TERM_FACTORY.getTypedTerm(function.clone(), COL_TYPE.STRING); } else if (predicate instanceof BNodePredicate) { return TermConstants.NULL; } } return term; }
private Term evalStr(Function term) { Term innerTerm = term.getTerm(0); if (innerTerm instanceof Function) { Function function = (Function) innerTerm; Predicate predicate = function.getFunctionSymbol(); Term parameter = function.getTerm(0); if (function.isDataTypeFunction()) { if (isXsdString(predicate) ) { // R: was datatype.equals(OBDAVocabulary.RDFS_LITERAL_URI) return termFactory.getTypedTerm( termFactory.getVariable(parameter.toString()), typeFactory.getXsdStringDatatype()); } else { return termFactory.getTypedTerm( termFactory.getFunctionCast(termFactory.getVariable(parameter.toString()), termFactory.getConstantLiteral(typeFactory.getXsdStringDatatype().getIRI().getIRIString())), typeFactory.getXsdStringDatatype()); } } else if (predicate instanceof URITemplatePredicate) { return termFactory.getTypedTerm(function.clone(), typeFactory.getXsdStringDatatype()); } else if (predicate instanceof BNodePredicate) { return valueNull; } } return term; }
.filter(atom -> !isDataOrLeftJoinOrJoinAtom(atom)) .map(atom -> { Function newAtom = (Function) atom.clone();
private static CQIE generateTripleMapping(CQIE rule) { Function newhead; Function currenthead = rule.getHead(); if (currenthead.getArity() == 1) { // head is Class(x) Forming head as triple(x,uri(rdf:type), uri(Class)) Function rdfTypeConstant = TERM_FACTORY.getUriTemplate(TERM_FACTORY.getConstantLiteral(IriConstants.RDF_TYPE)); String classname = currenthead.getFunctionSymbol().getName(); Term classConstant = TERM_FACTORY.getUriTemplate(TERM_FACTORY.getConstantLiteral(classname)); newhead = ATOM_FACTORY.getTripleAtom(currenthead.getTerm(0), rdfTypeConstant, classConstant); } else if (currenthead.getArity() == 2) { // head is Property(x,y) Forming head as triple(x,uri(Property), y) String propname = currenthead.getFunctionSymbol().getName(); Function propConstant = TERM_FACTORY.getUriTemplate(TERM_FACTORY.getConstantLiteral(propname)); newhead = ATOM_FACTORY.getTripleAtom(currenthead.getTerm(0), propConstant, currenthead.getTerm(1)); } else { // head is triple(x,uri(Property),y) newhead = (Function) currenthead.clone(); } return DATALOG_FACTORY.getCQIE(newhead, rule.getBody()); } }