@Override public void removeRules(Collection<CQIE> rs) { for (CQIE rule : rs) { this.rules.remove(rule); Predicate predicate = rule.getHead().getFunctionSymbol(); List<CQIE> indexedRules = this.predicateIndex.get(predicate); if (indexedRules != null) indexedRules.remove(rule); } }
public static boolean isBoolean(CQIE query) { return query.getHead().getArity() == 0; }
/** * Check if query cq1 is contained in cq2, syntactically. That is, if the * head of cq1 and cq2 are equal according to toString().equals and each * atom in cq2 is also in the body of cq1 (also by means of toString().equals(). */ private static boolean isContainedIn(CQIE cq1, CQIE cq2) { if (!cq2.getHead().equals(cq1.getHead())) return false; for (Function atom : cq2.getBody()) if (!cq1.getBody().contains(atom)) return false; return true; }
public static boolean isBoolean(CQIE query) { return query.getHead().getArity() == 0; }
/** * Check if query cq1 is contained in cq2, syntactically. That is, if the * head of cq1 and cq2 are equal according to toString().equals and each * atom in cq2 is also in the body of cq1 (also by means of toString().equals(). */ @Override public boolean isContainedIn(CQIE cq1, CQIE cq2) { if (!cq2.getHead().equals(cq1.getHead())) return false; for (Function atom : cq2.getBody()) if (!cq1.getBody().contains(atom)) return false; return true; }
/** * This method takes a rule and populates the ruleIndex field. * @param rule */ private void updateRuleIndexes(CQIE rule) { Function head = rule.getHead(); ruleIndex.put(head.getFunctionSymbol(), rule); updateRuleIndexByBodyPredicate(rule); }
private static ImmutableMap<CQIE, ImmutableList<Optional<TermType>>> extractTermTypeMap(Collection<CQIE> rules) throws IncompatibleTermException { return rules.stream() .collect(ImmutableCollectors.toMap( // Key mapper rule -> rule, // Value mapper rule -> rule.getHead().getTerms().stream() .map(TermTypeInferenceTools::inferType) .collect(ImmutableCollectors.toList()) )); }
private ImmutableMap<CQIE, ImmutableList<Optional<TermType>>> extractTermTypeMap(Collection<CQIE> rules) throws IncompatibleTermException { return rules.stream() .collect(ImmutableCollectors.toMap( // Key mapper rule -> rule, // Value mapper rule -> rule.getHead().getTerms().stream() .map(immutabilityTools::convertIntoImmutableTerm) .map(termTypeInferenceTools::inferType) .collect(ImmutableCollectors.toList()) )); }
/*** * True if the first query is contained in the second query * (in other words, the first query is more specific, it has fewer answers) * * @param q1 * @param q2 * @return true if the first query is contained in the second query */ @Override public boolean isContainedIn(CQIE q1, CQIE q2) { if (!q2.getHead().getFunctionSymbol().equals(q1.getHead().getFunctionSymbol())) return false; return (computeHomomorphsim(q1, q2) != null); }
private static ImmutableMap<CQIE, ImmutableList<Optional<TermType>>> extractTermTypeMap(Collection<CQIE> rules) throws IncompatibleTermException { return rules.stream() .collect(ImmutableCollectors.toMap( // Key mapper rule -> rule, // Value mapper rule -> rule.getHead().getTerms().stream() .map(TermTypeInferenceTools::inferType) .collect(ImmutableCollectors.toList()) )); }
/** * This method takes a rule and populates the ruleIndex field. * @param rule */ private void updateRuleIndexes(CQIE rule) { Function head = rule.getHead(); ruleIndex.put(head.getFunctionSymbol(), rule); updateRuleIndexByBodyPredicate(rule); }
private Substitution computeHomomorphism(CQIE query) { SubstitutionBuilder sb = new SubstitutionBuilder(termFactory); // get the substitution for the head first // it will ensure that all answer variables are mapped either to constants or // to answer variables in the base (but not to the labelled nulls generated by the chase) boolean headResult = extendHomomorphism(sb, query.getHead(), head); if (!headResult) return null; Substitution sub = computeSomeHomomorphism(sb, query.getBody(), factMap); return sub; } }
/*** * 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()); }
/** * add the inverse of the same as present in the mapping */ public static ImmutableList<CQIE> addSameAsInverse(ImmutableList<CQIE> mappingRules) { Stream<CQIE> newRuleStream = mappingRules.stream() // the targets are already split. We have only one target atom .filter(r -> r.getHead().getFunctionSymbol().getName().equals(IriConstants.SAME_AS)) .map(r -> { Function head = r.getHead(); Function inversedHead = TERM_FACTORY.getFunction(head.getFunctionSymbol(), head.getTerm(1), head.getTerm(0)); return DATALOG_FACTORY.getCQIE(inversedHead, new ArrayList<>(r.getBody())); }); return Stream.concat(mappingRules.stream(), newRuleStream) .collect(ImmutableCollectors.toList()); } }
private RDFPredicate extractRDFPredicate(CQIE mappingAssertion) { Function headAtom = mappingAssertion.getHead(); if (!(headAtom.getFunctionSymbol() instanceof RDFAtomPredicate)) throw new MinorOntopInternalBugException("Mapping assertion without an RDFAtomPredicate found"); RDFAtomPredicate predicate = (RDFAtomPredicate) headAtom.getFunctionSymbol(); ImmutableList<ImmutableTerm> arguments = headAtom.getTerms().stream() .map(immutabilityTools::convertIntoImmutableTerm) .collect(ImmutableCollectors.toList()); return predicate.getClassIRI(arguments) .map(iri -> new RDFPredicate(true, iri)) .orElseGet(() -> predicate.getPropertyIRI(arguments) .map(i -> new RDFPredicate(false, i)) .orElseThrow(() -> new MinorOntopInternalBugException("Could not extract a predicate IRI from " + headAtom))); }
public Substitution computeHomomorphsim(CQIE q1, CQIE q2) { IndexedCQ indexedQ1 = indexedCQcache.get(q1); if (indexedQ1 == null) { Collection<Function> q1body = q1.getBody(); if (dependencies != null) q1body = chaseAtoms(q1body); indexedQ1 = new IndexedCQ(q1.getHead(), q1body); indexedCQcache.put(q1, indexedQ1); } // just to speed up the check in case there can be no match for (Function q2atom : q2.getBody()) if (!indexedQ1.factMap.containsKey(q2atom.getFunctionSymbol())) { // in particular, !q2atom.isDataFunction() return null; } return indexedQ1.computeHomomorphism(q2); }
private Substitution computeHomomorphism(CQIE query) { SubstitutionBuilder sb = new SubstitutionBuilder(); // get the substitution for the head first // it will ensure that all answer variables are mapped either to constants or // to answer variables in the base (but not to the labelled nulls generated by the chase) boolean headResult = HomomorphismUtilities.extendHomomorphism(sb, query.getHead(), head); if (!headResult) return null; Substitution sub = HomomorphismUtilities.computeHomomorphism(sb, query.getBody(), factMap); return sub; } }
@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; }
public void insertDataTyping(CQIE rule) throws UnknownDatatypeException { Function atom = rule.getHead(); Predicate predicate = atom.getFunctionSymbol(); if (predicate.getArity() == 2) { // we check both for data and object property Term term = atom.getTerm(1); // the second argument only Map<String, List<IndexedPosition>> termOccurenceIndex = createIndex(rule.getBody()); // Infer variable datatypes insertVariableDataTyping(term, atom, 1, termOccurenceIndex); // Infer operation datatypes from variable datatypes insertOperationDatatyping(term, atom, 1); } }
public void insertDataTyping(CQIE rule) throws UnknownDatatypeException { Function atom = rule.getHead(); //case of data and object property if(!Datalog2QueryTools.isURIRDFType(atom.getTerm(1))){ Term object = atom.getTerm(2); // the object, third argument only Map<String, List<IndexedPosition>> termOccurenceIndex = createIndex(rule.getBody()); // Infer variable datatypes insertVariableDataTyping(object, atom, 2, termOccurenceIndex); // Infer operation datatypes from variable datatypes insertOperationDatatyping(object, atom, 2); } }