@Override public Optional<IRI> getPredicateIRI() { return Optional.of(atom.getPredicate()) .filter(p -> p instanceof RDFAtomPredicate) .map(p -> (RDFAtomPredicate) p) .flatMap(p -> p.getPredicateIRI(getSubstitutedTerms())); }
private static ImmutableSet<Constant> extractIRITemplates(IQ definition) { return Optional.of(definition.getProjectionAtom().getPredicate()) .filter(p -> p instanceof RDFAtomPredicate) .map(p -> extractIRITemplates((RDFAtomPredicate) p, definition)) .orElseGet(ImmutableSet::of); }
private String extractPredicateIRI(IntermediateQuery mappingAssertion) { AtomPredicate projectionAtomPredicate = mappingAssertion.getProjectionAtom().getPredicate(); if (projectionAtomPredicate.equals(PredicateImpl.QUEST_TRIPLE_PRED)) throw new RuntimeException("TODO: extract the RDF predicate from a triple atom"); else return projectionAtomPredicate.getName(); }
/** * 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); } }
@Override public Optional<IntermediateQuery> getDefinition(AtomPredicate predicate) { IntermediateQuery query = definitions.get(predicate); return query != null && query.getProjectionAtom().getPredicate().getArity() == predicate.getArity()? Optional.of(query): Optional.empty(); }
private ImmutableTable<RDFAtomPredicate, IRI, IQ> extractTable( ImmutableMap<IQ, MappingTools.RDFPredicateInfo> iqClassificationMap, boolean isClass) { return iqClassificationMap.entrySet().stream() .filter(e -> e.getValue().isClass() == isClass) .map(e -> Tables.immutableCell( (RDFAtomPredicate) e.getKey().getProjectionAtom().getPredicate(), e.getValue().getIri(), e.getKey())) .collect(ImmutableCollectors.toTable()); }
private ImmutableTable<RDFAtomPredicate, IRI, IQ> extractTable( ImmutableMap<IQ, MappingTools.RDFPredicateInfo> iqClassificationMap, boolean isClass) { ImmutableMultimap<Map.Entry<RDFAtomPredicate, IRI>, IQ> multimap = iqClassificationMap.entrySet().stream() .filter(e -> e.getValue().isClass() == isClass) .collect(ImmutableCollectors.toMultimap( e -> Maps.immutableEntry( (RDFAtomPredicate) e.getKey().getProjectionAtom().getPredicate(), e.getValue().getIri()), Map.Entry::getKey)); return multimap.asMap().entrySet().stream() .map(e -> Tables.immutableCell( e.getKey().getKey(), e.getKey().getValue(), mergeDefinitions(e.getValue()))) .collect(ImmutableCollectors.toTable()); }
@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)); } } }
@Override public Mapping toRegularMapping() { ImmutableMultimap<AtomPredicate, IntermediateQuery> assertionMultimap = getMappingAssertions().stream() .collect(ImmutableCollectors.toMultimap( q -> q.getProjectionAtom().getPredicate(), q -> q)); ImmutableMap<AtomPredicate, IntermediateQuery> definitionMap = assertionMultimap.asMap().values().stream() .map(queryMerger::mergeDefinitions) .filter(Optional::isPresent) .map(Optional::get) .collect(ImmutableCollectors.toMap( q -> q.getProjectionAtom().getPredicate(), q -> q)); return specFactory.createMapping(mappingMetadata, definitionMap, executorRegistry); }
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 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); }
@Override public Mapping normalize(Mapping mapping) { Stream<IntermediateQuery> queryStream = mapping.getPredicates().stream() .map(mapping::getDefinition) .filter(Optional::isPresent) .map(Optional::get); ImmutableMap<AtomPredicate, IntermediateQuery> normalizedMappingMap = renameQueries(queryStream) .collect(ImmutableCollectors.toMap( q -> q.getProjectionAtom().getPredicate(), q -> q )); return specificationFactory.createMapping(mapping.getMetadata(), normalizedMappingMap, mapping.getExecutorRegistry()); }
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); }
.map(mappingIQNormalizer::normalize) .collect(ImmutableCollectors.toMap( q -> q.getProjectionAtom().getPredicate(), q -> q ));
RDFAtomPredicate predicate = (RDFAtomPredicate)projectionAtom.getPredicate();
RDFAtomPredicate predicate = (RDFAtomPredicate) targetAtom.getProjectionAtom().getPredicate();
for (TargetAtom atom : body) { String subject, predicate, object = ""; String originalString = atom.getProjectionAtom().getPredicate().toString(); if (isUnary(atom.getProjectionAtom())) { ImmutableTerm subjectTerm = atom.getSubstitutedTerm(0);