@Override public ImmutableList<ImmutableTerm> getSubstitutedTerms() { return atom.getArguments().stream() .map(substitution::apply) .collect(ImmutableCollectors.toList()); }
private Optional<Variable> extractTripleObjectVariable(IntermediateQuery mappingAssertion) throws TripleObjectTypeInferenceException { ImmutableList<Variable> projectedVariables = mappingAssertion.getProjectionAtom().getArguments(); switch (projectedVariables.size()) { // Class case 1: return Optional.empty(); // Property case 2: return Optional.of(projectedVariables.get(1)); // Triple predicate case 3: return Optional.of(projectedVariables.get(2)); default: throw new TripleObjectTypeInferenceException(mappingAssertion, "Unexpected arity of the projection atom"); } }
/** * Renames the projected variables */ private DistinctVariableOnlyDataAtom transformProjectionAtom(DistinctVariableOnlyDataAtom atom) { ImmutableList<Variable> newArguments = atom.getArguments().stream() .map(renamingSubstitution::applyToVariable) .collect(ImmutableCollectors.toList()); return atomFactory.getDistinctVariableOnlyDataAtom(atom.getPredicate(), newArguments); }
/** * Renames the projected variables */ @Override protected DistinctVariableOnlyDataAtom transformProjectionAtom(DistinctVariableOnlyDataAtom atom) { ImmutableList<Variable> newArguments = atom.getArguments().stream() .map(renamingSubstitution::applyToVariable) .collect(ImmutableCollectors.toList()); return ATOM_FACTORY.getDistinctVariableOnlyDataAtom(atom.getPredicate(), newArguments); } }
private Variable getVarFromRDFAtom(DistinctVariableOnlyDataAtom atom, Position position) { switch (position) { case SUBJECT: return getRDFAtomPredicate(atom).getSubject(atom.getArguments()); case OBJECT: return getRDFAtomPredicate(atom).getObject(atom.getArguments()); case PROPERTY: return getRDFAtomPredicate(atom).getProperty(atom.getArguments()); default: throw new UnexpectedPositionException(position); } }
/** * Hacked logic: because of ORDER conditions that are expected to use signature variables, * this method DOES NOT look for conflicts between signature variables and variables only appearing in the sub-tree. * * See the history for a better logic breaking this ugly hack. * * TODO: after getting rid of Datalog for encoding SPARQL queries, could try to clean it */ private IQ enforceSignature(IQ iq, ImmutableList<Variable> signature) { ImmutableList<Variable> projectedVariables = iq.getProjectionAtom().getArguments(); if (projectedVariables.equals(signature)) return iq; if (projectedVariables.size() != signature.size()) throw new IllegalArgumentException("The arity of the signature does not match the iq"); InjectiveVar2VarSubstitution renamingSubstitution = substitutionFactory.getInjectiveVar2VarSubstitution( IntStream.range(0, projectedVariables.size()) .boxed() .map(i -> Maps.immutableEntry(projectedVariables.get(i), signature.get(i))) .filter(e -> !e.getKey().equals(e.getValue())) .collect(ImmutableCollectors.toMap())); return queryTransformerFactory.createRenamer(renamingSubstitution) .transform(iq); }
@Override public DistinctVariableOnlyDataAtom applyToDistinctVariableOnlyDataAtom(DistinctVariableOnlyDataAtom dataAtom) throws ConversionException { ImmutableList<? extends ImmutableTerm> newArguments = apply(dataAtom.getArguments()); if (!newArguments.stream().allMatch(t -> t instanceof Variable)) { throw new ConversionException("The substitution applied to a DistinctVariableOnlyDataAtom has " + " produced some non-Variable arguments " + newArguments); } ImmutableList<Variable> variableArguments = (ImmutableList<Variable>) newArguments; if (variableArguments.size() == ImmutableSet.copyOf(variableArguments).size()) return atomFactory.getDistinctVariableOnlyDataAtom(dataAtom.getPredicate(), variableArguments); else { throw new ConversionException("The substitution applied a DistinctVariableOnlyDataAtom has introduced" + " redundant variables: " + newArguments); } }
/** * When such substitution DO NOT EXIST, returns an EMPTY OPTIONAL. * When NO renaming is NEEDED returns an EMPTY SUBSTITUTION. * */ private static Optional<InjectiveVar2VarSubstitution> computeRenamingSubstitution( DistinctVariableOnlyDataAtom sourceProjectionAtom, DistinctVariableOnlyDataAtom targetProjectionAtom) { int arity = sourceProjectionAtom.getEffectiveArity(); if (!sourceProjectionAtom.getPredicate().equals(targetProjectionAtom.getPredicate()) || (arity != targetProjectionAtom.getEffectiveArity())) { return Optional.empty(); } else { ImmutableMap<Variable, Variable> newMap = FunctionalTools.zip( sourceProjectionAtom.getArguments(), targetProjectionAtom.getArguments()).stream() .distinct() .filter(e -> !e.getKey().equals(e.getValue())) .collect(ImmutableCollectors.toMap()); return Optional.of(SUBSTITUTION_FACTORY.getInjectiveVar2VarSubstitution(newMap)); } }
/** * When such substitution DO NOT EXIST, returns an EMPTY OPTIONAL. * When NO renaming is NEEDED returns an EMPTY SUBSTITUTION. * */ private Optional<InjectiveVar2VarSubstitution> computeRenamingSubstitution( DistinctVariableOnlyDataAtom sourceProjectionAtom, DistinctVariableOnlyDataAtom targetProjectionAtom) { int arity = sourceProjectionAtom.getEffectiveArity(); if (!sourceProjectionAtom.getPredicate().equals(targetProjectionAtom.getPredicate()) || (arity != targetProjectionAtom.getEffectiveArity())) { return Optional.empty(); } else { ImmutableMap<Variable, Variable> newMap = FunctionalTools.zip( sourceProjectionAtom.getArguments(), targetProjectionAtom.getArguments()).stream() .distinct() .filter(e -> !e.getKey().equals(e.getValue())) .collect(ImmutableCollectors.toMap()); return Optional.of(substitutionFactory.getInjectiveVar2VarSubstitution(newMap)); } } }
private static ImmutableSet<Constant> extractIRITemplates(RDFAtomPredicate predicate, IQ definition) { ImmutableList<Variable> projectedVariables = definition.getProjectionAtom().getArguments(); return Stream.of(predicate.getSubject(projectedVariables), predicate.getObject(projectedVariables)) .flatMap(v -> extractIRITemplates(v, definition.getTree())) .collect(ImmutableCollectors.toSet()); }
private static ImmutableSubstitution<VariableOrGroundTerm> extractSubstitution(DistinctVariableOnlyDataAtom sourceAtom, DataAtom targetAtom) { if (!sourceAtom.getPredicate().equals(targetAtom.getPredicate())) { throw new IllegalStateException("Incompatible predicates"); } else if (sourceAtom.getEffectiveArity() != targetAtom.getEffectiveArity()) { throw new IllegalStateException("Different arities"); } ImmutableMap<Variable, VariableOrGroundTerm> newMap = FunctionalTools.zip( sourceAtom.getArguments(), (ImmutableList<VariableOrGroundTerm>) targetAtom.getArguments()).stream() .collect(ImmutableCollectors.toMap()); return SUBSTITUTION_FACTORY.getSubstitution(newMap); }
private ImmutableSubstitution<VariableOrGroundTerm> extractSubstitution(DistinctVariableOnlyDataAtom sourceAtom, DataAtom targetAtom) { if (!sourceAtom.getPredicate().equals(targetAtom.getPredicate())) { throw new IllegalStateException("Incompatible predicates"); } else if (sourceAtom.getEffectiveArity() != targetAtom.getEffectiveArity()) { throw new IllegalStateException("Different arities"); } ImmutableMap<Variable, VariableOrGroundTerm> newMap = FunctionalTools.zip( sourceAtom.getArguments(), (ImmutableList<VariableOrGroundTerm>) targetAtom.getArguments()).stream() .collect(ImmutableCollectors.toMap()); return substitutionFactory.getSubstitution(newMap); } }
private static PredicateClassification classify(ImmutableSet<Constant> sameAsIriTemplates, IQ definition, RDFAtomPredicate rdfAtomPredicate, boolean isClass) { ImmutableList<Variable> variables = definition.getProjectionAtom().getArguments(); /* * Current limitation: is the object is concerned about SameAs rewriting, the subject is supposed also * concerned. * TODO: enrich the classification */ if (!isClass && extractIRITemplates(rdfAtomPredicate.getObject(variables), definition.getTree()) .anyMatch(sameAsIriTemplates::contains)) { return PredicateClassification.AT_LEAST_OBJECT; } return extractIRITemplates(rdfAtomPredicate.getSubject(variables), definition.getTree()) .filter(sameAsIriTemplates::contains) .findAny() .map(t -> PredicateClassification.SUBJECT_ONLY) .orElse(PredicateClassification.NONE); }
private void validateAssertion(IQ mappingAssertion, PPMappingAssertionProvenance provenance, Ontology ontology, ImmutableMultimap<IRI, Datatype> datatypeMap) throws MappingOntologyMismatchException { ImmutableList<Variable> projectedVariables = mappingAssertion.getProjectionAtom().getArguments(); MappingTools.RDFPredicateInfo predicateClassification = MappingTools.extractRDFPredicate(mappingAssertion); Optional<RDFTermType> tripleObjectType = predicateClassification.isClass() ? Optional.empty() : extractTripleObjectType(mappingAssertion); checkTripleObject(predicateClassification.getIri(), tripleObjectType, provenance, ontology, datatypeMap); }
public static RDFPredicateInfo extractRDFPredicate(IQ mappingAssertion) { DistinctVariableOnlyDataAtom projectionAtom = mappingAssertion.getProjectionAtom(); RDFAtomPredicate rdfAtomPredicate = Optional.of(projectionAtom.getPredicate()) .filter(p -> p instanceof RDFAtomPredicate) .map(p -> (RDFAtomPredicate) p) .orElseThrow(() -> new MappingPredicateIRIExtractionException("The following mapping assertion " + "is not having a RDFAtomPredicate: " + mappingAssertion)); ImmutableSet<ImmutableList<? extends ImmutableTerm>> possibleSubstitutedArguments = mappingAssertion.getTree().getPossibleVariableDefinitions().stream() .map(s -> s.apply(projectionAtom.getArguments())) .collect(ImmutableCollectors.toSet()); IRI propertyIRI = extractIRI(possibleSubstitutedArguments, rdfAtomPredicate::getPropertyIRI); return propertyIRI.equals(RDF.TYPE) ? new RDFPredicateInfo(true, extractIRI(possibleSubstitutedArguments, rdfAtomPredicate::getClassIRI)) : new RDFPredicateInfo(false, propertyIRI); }
throws TripleObjectTypeInferenceException { ImmutableList<Variable> projectedVariables = mappingAssertion.getProjectionAtom().getArguments(); Variable objectVariable = projectedVariables.get(2);
? predicate.getObject(projectionAtom.getArguments()) : predicate.getProperty(projectionAtom.getArguments());
private IQ canonizeWithJoin(IQ assertion, IntensionalQueryMerger intensionalQueryMerger, Position pos) { Optional<Variable> replacedVar = getReplacedVar(assertion, pos); if (replacedVar.isPresent()) { Variable newVariable = createFreshVariable(assertion, intensionalQueryMerger, replacedVar.get()); IntensionalDataNode idn = getIDN(replacedVar.get(), newVariable); RDFAtomPredicate pred = getRDFAtomPredicate(assertion.getProjectionAtom()); DistinctVariableOnlyDataAtom projAtom = atomFactory.getDistinctVariableOnlyDataAtom( pred, replaceProjVars( pred, assertion.getProjectionAtom().getArguments(), pos, newVariable)); IQ intensionalCanonizedQuery = iqFactory.createIQ( projAtom, getIntensionalCanonizedTree(assertion, projAtom, idn)); IQ canonizedQuery = intensionalQueryMerger.optimize(intensionalCanonizedQuery) .liftBinding(); return canonizedQuery.getTree().isDeclaredAsEmpty() // No matching canonical IRI template ? assertion : canonizedQuery; } return assertion; }
private IQ completeSameAsDefinition(IQ originalDefinition, RDFAtomPredicate rdfAtomPredicate) { ImmutableList<Variable> originalProjectedVariables = originalDefinition.getProjectionAtom().getArguments();
List<Predicate> extensionalPredicates = depGraph.getExtensionalPredicates(); ImmutableList<String> signature = intermediateQuery.getProjectionAtom().getArguments().stream() .map(Variable::getName) .collect(ImmutableCollectors.toList());