@Override public Mapping rewrite(Mapping mapping) { if (!enabled) return mapping; ImmutableTable<RDFAtomPredicate, IRI, IQ> mappingUpdate = mapping.getRDFAtomPredicates().stream() .flatMap(p -> mapping.getRDFPropertyDefinition(p, OWL.SAME_AS) .map(sameAsDef -> completeSameAsDefinition(sameAsDef, p)) .map(sameAsDef -> Tables.immutableCell(p, OWL.SAME_AS, sameAsDef)) .map(Stream::of) .orElseGet(Stream::empty)) .collect(ImmutableCollectors.toTable()); return mapping.update(mappingUpdate, ImmutableTable.of()); }
private static Stream<VocabularyEntry> extractTripleVocabulary(Mapping mapping, TriplePredicate triplePredicate) { Stream<VocabularyEntry> vocabularyPropertyStream = mapping.getRDFProperties(triplePredicate).stream() .map(p -> new VocabularyEntry(p, 2)); Stream<VocabularyEntry> vocabularyClassStream = mapping.getRDFClasses(triplePredicate).stream() .map(p -> new VocabularyEntry(p, 1)); return Stream.concat(vocabularyClassStream,vocabularyPropertyStream); }
@Override public Mapping rewrite(Mapping mapping, DBMetadata dbMetadata) { if(enabled){ ImmutableList<CQIE> rules = mapping2DatalogConverter.convert(mapping) .collect(ImmutableCollectors.toList()); ImmutableList<CQIE> updatedRules = MappingSameAs.addSameAsInverse(rules); return datalog2MappingConverter.convertMappingRules(updatedRules, dbMetadata, mapping.getExecutorRegistry(), mapping.getMetadata()); } return mapping; } }
@Override public Stream<CQIE> convert(Mapping mapping) { return mapping.getRDFAtomPredicates().stream() .flatMap(p -> mapping.getQueries(p).stream()) .flatMap(this::convertMappingQuery); }
@Override public Mapping normalize(Mapping mapping) { AtomicInteger i = new AtomicInteger(0); ImmutableTable<RDFAtomPredicate, IRI, IQ> newPropertyTable = normalize(mapping.getRDFPropertyQueries(), i); ImmutableTable<RDFAtomPredicate, IRI, IQ> newClassTable = normalize(mapping.getRDFClassQueries(), i); return specificationFactory.createMapping(mapping.getMetadata(), newPropertyTable, newClassTable); }
private Optional<IQ> getStarClassDefinition(RDFAtomPredicate predicate) { return queryMerger.mergeDefinitions(mapping.getRDFClasses(predicate).stream() .flatMap(i -> mapping.getRDFClassDefinition(predicate, i) .map(Stream::of) .orElseGet(Stream::empty)) .collect(ImmutableCollectors.toList())); }
private static SameAsTargets extractSameAsTargets(ImmutableSet<Constant> sameAsIriTemplates, Mapping mapping, RDFAtomPredicate rdfAtomPredicate) { if (sameAsIriTemplates.isEmpty()) return new SameAsTargets(ImmutableSet.of(), ImmutableSet.of()); Stream<IRI> classIris = mapping.getRDFClasses(rdfAtomPredicate).stream() .filter(classIri -> classify(sameAsIriTemplates, mapping.getRDFClassDefinition(rdfAtomPredicate, classIri).get(), rdfAtomPredicate, true) == PredicateClassification.SUBJECT_ONLY); ImmutableMultimap<PredicateClassification, IRI> propertyIris = mapping.getRDFProperties(rdfAtomPredicate).stream() .filter(iri -> !iri.equals(OWL.SAME_AS)) .map(iri -> Maps.immutableEntry( classify(sameAsIriTemplates, mapping.getRDFPropertyDefinition(rdfAtomPredicate, iri).get(), rdfAtomPredicate, false), iri )) .collect(ImmutableCollectors.toMultimap()); return new SameAsTargets( Stream.concat( classIris, propertyIris.get(PredicateClassification.SUBJECT_ONLY).stream()) .collect(ImmutableCollectors.toSet()), ImmutableSet.copyOf(propertyIris.get(PredicateClassification.AT_LEAST_OBJECT))); }
saturatedMapping.getRDFAtomPredicates().stream() .flatMap(p -> saturatedMapping.getQueries(p).stream()) .iterator())); this.inputQueryTranslator = translationFactory.createInputQueryTranslator(saturatedMapping.getMetadata() .getUriTemplateMatcher()); this.sameAsRewriter = translationFactory.createSameAsRewriter(saturatedMapping);
private ImmutableTable<RDFAtomPredicate, IRI, IQ> mergeMappingPropertyTables(ImmutableSet<Mapping> mappings) { ImmutableMap<Map.Entry<RDFAtomPredicate, IRI>, Collection<IQ>> multiTable = mappings.stream() .flatMap(m -> extractCellStream(m, p -> m.getRDFProperties(p).stream(), (p, i) -> m.getRDFPropertyDefinition(p, i).get())) .collect(ImmutableCollectors.toMultimap( c -> Maps.immutableEntry(c.getRowKey(), c.getColumnKey()), Table.Cell::getValue)) .asMap(); return multiTable.entrySet().stream() .map(e -> Tables.immutableCell( e.getKey().getKey(), e.getKey().getValue(), mergeDefinitions(e.getValue()))) .collect(ImmutableCollectors.toTable()); }
private Stream<Table.Cell<RDFAtomPredicate, IRI, IQ>> extractCellStream( Mapping m, Function<RDFAtomPredicate, Stream<IRI>> iriExtractor, BiFunction<RDFAtomPredicate, IRI, IQ> iqExtractor) { return m.getRDFAtomPredicates().stream() .flatMap(p -> iriExtractor.apply(p) .map(i -> Tables.immutableCell(p, i, iqExtractor.apply(p, i)))); }
@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()); }
private UriTemplateMatcher mergeURITemplateMatchers(ImmutableSet<Mapping> mappings) { return UriTemplateMatcher.merge( mappings.stream() .map(m -> m.getMetadata().getUriTemplateMatcher()), termFactory ); }
private static ImmutableSet<Constant> extractSameAsIRITemplates(Mapping mapping, RDFAtomPredicate rdfAtomPredicate) { Optional<IQ> definition = mapping.getRDFPropertyDefinition(rdfAtomPredicate, OWL.SAME_AS); return definition .map(SameAsTargets::extractIRITemplates) .orElseGet(ImmutableSet::of); }
@Override public Stream<CQIE> convert(Mapping mapping) { return mapping.getQueries().stream() .flatMap(this::convertMappingQuery); }
private Optional<IQ> getRDFClassDefinition(RDFAtomPredicate predicate, ImmutableList<? extends VariableOrGroundTerm> arguments) { return predicate.getClassIRI(arguments) .map(i -> mapping.getRDFClassDefinition(predicate, i)) .orElseGet(() -> getStarClassDefinition(predicate)); }
/** * Due to a Java compiler bug (hiding .orElseThrow() in a sub-method does the trick) */ private static IntermediateQuery getDefinition(Mapping mapping, AtomPredicate predicate) { return mapping.getDefinition(predicate) .orElseThrow(() -> new MappingMergingException("This atom predicate should have a definition")); } }
@Override public Mapping merge(ImmutableSet<Mapping> mappings) { if (mappings.isEmpty()) { throw new IllegalArgumentException("The set of mappings is assumed to be nonempty"); } MappingMetadata metadata = mergeMetadata(mappings); ImmutableMap<AtomPredicate, IntermediateQuery> mappingMap = mergeMappingMaps(mappings); // TODO: check that the ExecutorRegistry is identical for all mappings ? return specificationFactory.createMapping( metadata, mappingMap, mappings.iterator().next().getExecutorRegistry() ); }
private ImmutableTable<RDFAtomPredicate, IRI, IQ> mergeMappingClassTables(ImmutableSet<Mapping> mappings) { ImmutableMap<Map.Entry<RDFAtomPredicate, IRI>, Collection<IQ>> multiTable = mappings.stream() .flatMap(m -> extractCellStream(m, p -> m.getRDFClasses(p).stream(), (p, i) -> m.getRDFClassDefinition(p, i).get())) .collect(ImmutableCollectors.toMultimap( c -> Maps.immutableEntry(c.getRowKey(), c.getColumnKey()), Table.Cell::getValue)) .asMap(); return multiTable.entrySet().stream() .map(e -> Tables.immutableCell( e.getKey().getKey(), e.getKey().getValue(), mergeDefinitions(e.getValue()))) .collect(ImmutableCollectors.toTable()); }
/** * TODO: refactor so as to work with quads */ private static ImmutableMap<IRI, VocabularyEntry> extractVocabulary(@Nonnull Mapping mapping) { return mapping.getRDFAtomPredicates().stream() .filter(p -> p instanceof TriplePredicate) .map(p -> (TriplePredicate) p) .findFirst() .map(p -> extractTripleVocabulary(mapping, p) .collect(ImmutableCollectors.toMap(e -> e.name, e -> e))) .orElseGet(ImmutableMap::of); }
private UriTemplateMatcher mergeURITemplateMatchers(ImmutableSet<Mapping> mappings) { return UriTemplateMatcher.merge( mappings.stream() .map(m -> m.getMetadata().getUriTemplateMatcher()) ); }