public SQLPPMappingToR2RMLConverter(SQLPPMapping ppMapping, OWLOntology ontology, RDF rdfFactory) { this.ppMappingAxioms = ppMapping.getTripleMaps(); this.prefixmng = ppMapping.getMetadata().getPrefixManager(); this.ontology = ontology; this.rdfFactory = rdfFactory; }
/*** * extract all the mappings from a datasource * * @return a new OBDA Model containing all the extracted mappings */ private SQLPPMapping extractPPMapping(Optional<SQLPPMapping> ppMapping) throws MappingException, SQLException { SQLPPMapping mapping; if (!ppMapping.isPresent()) { it.unibz.inf.ontop.spec.mapping.PrefixManager prefixManager = specificationFactory.createPrefixManager(ImmutableMap.of()); MappingMetadata mappingMetadata = specificationFactory.createMetadata(prefixManager, UriTemplateMatcher.create(Stream.empty())); mapping = ppMappingFactory.createSQLPreProcessedMapping(ImmutableList.of(), mappingMetadata); } else mapping = ppMapping.get(); currentMappingIndex = mapping.getTripleMaps().size() + 1; return bootstrapMappings(mapping); }
private void writePrefixDeclaration(BufferedWriter writer) throws IOException { final ImmutableMap<String, String> prefixMap = ppMapping.getMetadata().getPrefixManager().getPrefixMap(); if (prefixMap.size() == 0) { return; // do nothing if there is no prefixes to write } writer.write(OntopNativeMappingParser.PREFIX_DECLARATION_TAG); writer.write("\n"); for (String prefix : prefixMap.keySet()) { String uri = prefixMap.get(prefix); writer.write(prefix + (prefix.length() >= 9 ? "\t" : "\t\t") + uri + "\n"); } writer.write("\n"); }
/** * Assumption: one CQIE per mapping axiom (no nested union) */ private ImmutableMap<CQIE, PPMappingAssertionProvenance> convertIntoDatalog(SQLPPMapping ppMapping, RDBMetadata dbMetadata) throws InvalidMappingSourceQueriesException { /* * May also add views in the DBMetadata! */ ImmutableMap<CQIE, PPMappingAssertionProvenance> datalogMap = ppMapping2DatalogConverter.convert( ppMapping.getTripleMaps(), dbMetadata); LOGGER.debug("Original mapping size: {}", datalogMap.size()); // Normalizing language tags and equalities (SIDE-EFFECT!) normalizeMapping(datalogMap.keySet()); return datalogMap; }
@Override public MappingWithProvenance convert(SQLPPMapping ppMapping, RDBMetadata dbMetadata, ExecutorRegistry executorRegistry) throws InvalidMappingSourceQueriesException { ImmutableMap<CQIE, PPMappingAssertionProvenance> datalogMap = convertIntoDatalog(ppMapping, dbMetadata); return mappingConverter.convertMappingRules(datalogMap, ppMapping.getMetadata()); }
private SQLPPMapping bootstrapMappings(RDBMetadata metadata, SQLPPMapping ppMapping) throws DuplicateMappingException { if (baseIRI == null || baseIRI.isEmpty()) this.baseIRI = ppMapping.getMetadata().getPrefixManager().getDefaultPrefix(); Collection<DatabaseRelationDefinition> tables = metadata.getDatabaseRelations(); List<SQLPPTriplesMap> mappingAxioms = new ArrayList<>(); for (DatabaseRelationDefinition td : tables) { mappingAxioms.addAll(getMapping(td, baseIRI)); } List<SQLPPTriplesMap> mappings = new ArrayList<>(); mappings.addAll(ppMapping.getTripleMaps()); mappings.addAll(mappingAxioms); return ppMappingFactory.createSQLPreProcessedMapping(ImmutableList.copyOf(mappings), ppMapping.getMetadata()); }
/** * Assumption: one CQIE per mapping axiom (no nested union) */ private ImmutableMap<CQIE, PPMappingAssertionProvenance> convertIntoDatalog(SQLPPMapping ppMapping, RDBMetadata dbMetadata) throws InvalidMappingSourceQueriesException { /* * May also add views in the DBMetadata! */ ImmutableMap<CQIE, PPMappingAssertionProvenance> datalogMap = SQLPPMapping2DatalogConverter.convert( ppMapping.getTripleMaps(), dbMetadata); LOGGER.debug("Original mapping size: {}", datalogMap.size()); // Normalizing language tags and equalities (SIDE-EFFECT!) normalizeMapping(datalogMap.keySet()); return datalogMap; }
private String printTargetQuery(ImmutableList<TargetAtom> query) { return TargetQueryRenderer.encode(query, ppMapping.getMetadata().getPrefixManager()); }
private SQLPPMapping bootstrapMappings(RDBMetadata metadata, SQLPPMapping ppMapping) throws DuplicateMappingException { if (baseIRI == null || baseIRI.isEmpty()) this.baseIRI = ppMapping.getMetadata().getPrefixManager().getDefaultPrefix(); Collection<DatabaseRelationDefinition> tables = metadata.getDatabaseRelations(); List<SQLPPTriplesMap> mappingAxioms = new ArrayList<>(); for (DatabaseRelationDefinition td : tables) { mappingAxioms.addAll(getMapping(td, baseIRI)); } List<SQLPPTriplesMap> mappings = new ArrayList<>(); mappings.addAll(ppMapping.getTripleMaps()); mappings.addAll(mappingAxioms); return ppMappingFactory.createSQLPreProcessedMapping(ImmutableList.copyOf(mappings), ppMapping.getMetadata()); }
/*** * extract all the mappings from a datasource * * @return a new OBDA Model containing all the extracted mappings */ private SQLPPMapping extractPPMapping(Optional<SQLPPMapping> ppMapping) throws MappingException, SQLException { SQLPPMapping mapping; if (!ppMapping.isPresent()) { it.unibz.inf.ontop.spec.mapping.PrefixManager prefixManager = specificationFactory.createPrefixManager(ImmutableMap.of()); MappingMetadata mappingMetadata = specificationFactory.createMetadata(prefixManager, UriTemplateMatcher.create(Stream.empty(), termFactory)); mapping = ppMappingFactory.createSQLPreProcessedMapping(ImmutableList.of(), mappingMetadata); } else mapping = ppMapping.get(); currentMappingIndex = mapping.getTripleMaps().size() + 1; return bootstrapMappings(mapping); }
@Override public MappingWithProvenance convert(SQLPPMapping ppMapping, RDBMetadata dbMetadata, ExecutorRegistry executorRegistry) throws InvalidMappingSourceQueriesException { ImmutableMap<CQIE, PPMappingAssertionProvenance> datalogMap = convertIntoDatalog(ppMapping, dbMetadata); return mappingConverter.convertMappingRules(datalogMap, dbMetadata, executorRegistry, ppMapping.getMetadata()); }
public void parseMapping(Reader mappingReader, Properties properties) throws DuplicateMappingException, InvalidMappingException, IOException, MappingIOException { OntopMappingSQLAllConfiguration configuration = OntopMappingSQLAllConfiguration.defaultBuilder() .nativeOntopMappingReader(mappingReader) .properties(properties) .build(); SQLMappingParser mappingParser = configuration.getInjector().getInstance(SQLMappingParser.class); SQLPPMapping ppMapping = mappingParser.parse(mappingReader); prefixManager.addPrefixes(ppMapping.getMetadata().getPrefixManager().getPrefixMap()); // New map triplesMapMap = ppMapping.getTripleMaps().stream() .collect(collectTriplesMaps( SQLPPTriplesMap::getId, m -> m)); }
private void writeMappingDeclaration(BufferedWriter writer) throws IOException { writer.write(OntopNativeMappingParser.MAPPING_DECLARATION_TAG + " " + OntopNativeMappingParser.START_COLLECTION_SYMBOL); writer.write("\n"); boolean needLineBreak = false; for (SQLPPTriplesMap axiom : ppMapping.getTripleMaps()) { if (needLineBreak) { writer.write("\n"); } writer.write(OntopNativeMappingParser.Label.mappingId.name() + "\t" + axiom.getId() + "\n"); ImmutableList<TargetAtom> targetQuery = axiom.getTargetAtoms(); writer.write(OntopNativeMappingParser.Label.target.name() + "\t\t" + printTargetQuery(targetQuery) + "\n"); OBDASQLQuery sourceQuery = axiom.getSourceQuery(); writer.write(OntopNativeMappingParser.Label.source.name() + "\t\t" + printSourceQuery(sourceQuery) + "\n"); needLineBreak = true; } writer.write(OntopNativeMappingParser.END_COLLECTION_SYMBOL); writer.write("\n\n"); }
protected SQLPPMapping expandPPMapping(SQLPPMapping ppMapping, OntopMappingSQLSettings settings, RDBMetadata dbMetadata) throws MetaMappingExpansionException { MetaMappingExpander expander = new MetaMappingExpander(ppMapping.getTripleMaps()); final ImmutableList<SQLPPTriplesMap> expandedMappingAxioms; if (expander.hasMappingsToBeExpanded()) { try (Connection connection = LocalJDBCConnectionUtils.createConnection(settings)) { expandedMappingAxioms = expander.getExpandedMappings(connection, dbMetadata); } // Problem while creating the connection catch (SQLException e) { throw new MetaMappingExpansionException(e.getMessage()); } } else expandedMappingAxioms = expander.getNonExpandableMappings(); try { return new SQLPPMappingImpl(expandedMappingAxioms, ppMapping.getMetadata()); } catch (DuplicateMappingException e) { // Internal bug throw new IllegalStateException(e); } }
public void refresh() { OBDADataSource source = obdaModel.getDatasource(); List<SQLPPTriplesMap> mappingList = obdaModel.generatePPMapping().getTripleMaps(); HashMap<String, Integer> mappingStat = new HashMap<String, Integer>(); for (SQLPPTriplesMap mapping : mappingList) { String mappingId = mapping.getId(); int triplesCount = 0; try { OBDASQLQuery sourceQuery = mapping.getSourceQuery(); int tuples = getTuplesCount(sourceQuery, source); ImmutableList<TargetAtom> targetQuery = mapping.getTargetAtoms(); int atoms = targetQuery.size(); triplesCount = tuples * atoms; } catch (Exception e) { triplesCount = -1; // fails to count log.error(e.getMessage()); } mappingStat.put(mappingId, triplesCount); } statistics.put(source.getSourceID().toString(), mappingStat); }
protected SQLPPMapping expandPPMapping(SQLPPMapping ppMapping, OntopMappingSQLSettings settings, RDBMetadata dbMetadata) throws MetaMappingExpansionException { MetaMappingExpander expander = new MetaMappingExpander(ppMapping.getTripleMaps(), termFactory, substitutionFactory); final ImmutableList<SQLPPTriplesMap> expandedMappingAxioms; if (expander.hasMappingsToBeExpanded()) { try (Connection connection = LocalJDBCConnectionUtils.createConnection(settings)) { expandedMappingAxioms = expander.getExpandedMappings(connection, dbMetadata); } // Problem while creating the connection catch (SQLException e) { throw new MetaMappingExpansionException(e.getMessage()); } } else expandedMappingAxioms = expander.getNonExpandableMappings(); try { return new SQLPPMappingImpl(expandedMappingAxioms, ppMapping.getMetadata()); } catch (DuplicateMappingException e) { // Internal bug throw new IllegalStateException(e); } }
Set<RelationID> realTables = getRealTables(metadata.getQuotedIDFactory(), ppMapping.getTripleMaps()); implicitConstraints.ifPresent(c -> {
currentMappingIndex = ppMapping.getTripleMaps().size() + 1; baseURI = ppMapping.getMetadata().getPrefixManager().getDefaultPrefix();
Set<RelationID> realTables = getRealTables(metadata.getQuotedIDFactory(), ppMapping.getTripleMaps()); implicitConstraints.ifPresent(c -> {
/** * NOT THREAD-SAFE (not reentrant) */ private BootstrappingResults bootstrapMappingAndOntology(String baseIRI, Optional<SQLPPMapping> inputPPMapping, Optional<OWLOntology> inputOntology) throws MappingBootstrappingException { this.baseIRI = fixBaseURI(baseIRI); try { SQLPPMapping newPPMapping = extractPPMapping(inputPPMapping); OWLOntology ontology = inputOntology.isPresent() ? inputOntology.get() : OWLManager.createOWLOntologyManager().createOntology(IRI.create(baseIRI)); // update ontology OWLOntologyManager manager = ontology.getOWLOntologyManager(); Set<OWLDeclarationAxiom> declarationAxioms = extractDeclarationAxioms(manager, newPPMapping.getTripleMaps().stream() .flatMap(ax -> ax.getTargetAtoms().stream())); manager.addAxioms(ontology, declarationAxioms); return new DefaultBootstrappingResults(newPPMapping, ontology); } catch (SQLException | MappingException | OWLOntologyCreationException e) { throw new MappingBootstrappingException(e); } }