@Override public boolean match(SQLPPTriplesMap object) { ImmutableList<TargetAtom> atoms = object.getTargetAtoms(); boolean isMatch = false; for (String keyword : vecKeyword) { for (TargetAtom atom : atoms) { isMatch = isMatch || TreeModelTools.match(keyword.trim(), atom); } if (isMatch) { break; // end loop if a match is found! } } return bNegation != isMatch; }
private ImmutableList<TargetAtom> getNewTargetAtoms(SQLPPTriplesMap formerTriplesMap, IRI removedPredicateIRI, AtomicInteger counter) { return formerTriplesMap.getTargetAtoms().stream() .filter(a -> { if (a.getPredicateIRI() .filter(i -> i.equals(removedPredicateIRI)) .isPresent()) { counter.incrementAndGet(); return false; } return true; }) .collect(ImmutableCollectors.toList()); }
public void updateMappingsSourceQuery(String triplesMapId, OBDASQLQuery sourceQuery) { SQLPPTriplesMap formerTriplesMap = getTriplesMap(triplesMapId); if (formerTriplesMap != null) { SQLPPTriplesMap newTriplesMap = new OntopNativeSQLPPTriplesMap(triplesMapId, sourceQuery, formerTriplesMap.getTargetAtoms()); triplesMapMap.put(triplesMapId, newTriplesMap); fireMappingUpdated(); } }
private ImmutableList<SQLPPTriplesMap> splitMappingAxiom(SQLPPTriplesMap mappingAxiom, String delimiterSubstring) { Multimap<ImmutableFunctionalTerm, TargetAtom> subjectTermToTargetTriples = ArrayListMultimap.create(); for (TargetAtom targetTriple : mappingAxiom.getTargetAtoms()) { ImmutableFunctionalTerm subjectTerm = getFirstFunctionalTerm(targetTriple) .orElseThrow(() -> new IllegalStateException("Invalid OBDA mapping")); subjectTermToTargetTriples.put(subjectTerm, targetTriple); } // If the partition per target triple subject is non trivial if (subjectTermToTargetTriples.size() > 1) { // Create ids for the new mapping axioms Map<ImmutableFunctionalTerm, String> subjectTermToMappingIndex = new HashMap<>(); int i = 1; for (ImmutableFunctionalTerm subjectTerm : subjectTermToTargetTriples.keySet()) { subjectTermToMappingIndex.put(subjectTerm, mappingAxiom.getId() + delimiterSubstring + i); i++; } // Generate one mapping axiom per subject return subjectTermToTargetTriples.asMap().entrySet().stream() .map(e -> mappingAxiom.extractPPMappingAssertions( subjectTermToMappingIndex.get(e.getKey()), ImmutableList.copyOf(e.getValue()))) .collect(ImmutableCollectors.toList()); } return ImmutableList.of(mappingAxiom); }
private void bootstrapMappingAndOntologyProtege(String baseUri) throws DuplicateMappingException, SQLException { List<SQLPPTriplesMap> sqlppTriplesMaps = bootstrapMapping(activeOBDAModel.generatePPMapping(), baseUri); // update protege ontology OWLOntologyManager manager = owlManager.getActiveOntology().getOWLOntologyManager(); Set<OWLDeclarationAxiom> declarationAxioms = directMappingEngine.extractDeclarationAxioms(manager, sqlppTriplesMaps.stream() .flatMap(ax -> ax.getTargetAtoms().stream())); List<AddAxiom> addAxioms = declarationAxioms.stream() .map(ax -> new AddAxiom(owlManager.getActiveOntology(), ax)) .collect(Collectors.toList()); owlManager.applyChanges(addAxioms); }
/*** * Sets the current mapping to the input. Note, if the current mapping is * set, this means that this dialog is "updating" a mapping, and not * creating a new one. */ public void setMapping(SQLPPTriplesMap mapping) { this.mapping = mapping; cmdInsertMapping.setText("Update"); txtMappingID.setText(mapping.getId()); OBDASQLQuery sourceQuery = mapping.getSourceQuery(); String srcQuery = SourceQueryRenderer.encode(sourceQuery); txtSourceQuery.setText(srcQuery); ImmutableList<TargetAtom> targetQuery = mapping.getTargetAtoms(); String trgQuery = TargetQueryRenderer.encode(targetQuery, prefixManager); txtTargetQuery.setText(trgQuery); } }
public SQLPPMapping generatePPMapping() { ImmutableList<SQLPPTriplesMap> triplesMaps = ImmutableList.copyOf(triplesMapMap.values()); try { UriTemplateMatcher uriTemplateMatcher = UriTemplateMatcher.create( triplesMaps.stream() .flatMap(ax -> ax.getTargetAtoms().stream()) .flatMap(targetAtom -> targetAtom.getSubstitution().getImmutableMap().values().stream()) .filter(t -> t instanceof ImmutableFunctionalTerm) .map(t -> (ImmutableFunctionalTerm) t), termFactory); return ppMappingFactory.createSQLPreProcessedMapping(triplesMaps, // TODO: give an immutable prefix manager!! specificationFactory.createMetadata(prefixManager, uriTemplateMatcher)); /** * No mapping so should never happen */ } catch(DuplicateMappingException e) { throw new RuntimeException("A DuplicateMappingException has been thrown while no mapping has been given." + "What is going on? Message: " + e.getMessage()); } }
public void updateMappingId(String formerMappingId, String newMappingId) throws DuplicateMappingException { //if the id are the same no need to update the mapping if(!formerMappingId.equals(newMappingId)) { SQLPPTriplesMap formerTriplesMap = getTriplesMap(formerMappingId); if (formerTriplesMap != null) { SQLPPTriplesMap newTriplesMap = new OntopNativeSQLPPTriplesMap(newMappingId, formerTriplesMap.getSourceQuery(), formerTriplesMap.getTargetAtoms()); addTriplesMap(newTriplesMap, false); triplesMapMap.remove(formerMappingId); fireMappingUpdated(); } } }
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"); }
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); }
@Override public boolean match(SQLPPTriplesMap object) { boolean isMatch = false; for (String keyword : vecKeyword) { // Check in the Mapping ID final String mappingId = object.getId(); isMatch = MappingIDTreeModelFilter.match(keyword.trim(), mappingId); if (isMatch) { break; // end loop if a match is found! } // Check in the Mapping Target Query for (TargetAtom targetAtom : object.getTargetAtoms()) { isMatch = isMatch || TreeModelTools.match(keyword.trim(), targetAtom); } if (isMatch) { break; // end loop if a match is found! } // Check in the Mapping Source Query final OBDASQLQuery query = object.getSourceQuery(); isMatch = MappingSQLStringTreeModelFilter.match(keyword.trim(), query.toString()); if (isMatch) { break; // end loop if a match is found! } } // no match found! return bNegation != isMatch; } }
public MetaMappingExpander(Collection<SQLPPTriplesMap> mappings) { ImmutableList.Builder<SQLPPTriplesMap> builder1 = ImmutableList.builder(); ImmutableList.Builder<Expansion> builder2 = ImmutableList.builder(); for (SQLPPTriplesMap mapping : mappings) { ImmutableList<ImmutableFunctionalTerm> toBeExpanded = mapping.getTargetAtoms().stream() .filter(atom -> atom.getFunctionSymbol().isTriplePredicate()) .collect(ImmutableCollectors.toList()); if (toBeExpanded.isEmpty()) { builder1.add(mapping); } else { builder2.addAll(toBeExpanded.stream() .map(target -> new Expansion(mapping.getId(), mapping.getSourceQuery(), target)) .iterator()); ImmutableList<ImmutableFunctionalTerm> toBeLeft = mapping.getTargetAtoms().stream() .filter(atom -> !atom.getFunctionSymbol().isTriplePredicate()) .collect(ImmutableCollectors.toList()); if (!toBeLeft.isEmpty()) { // TODO: see how to keep the provenance SQLPPTriplesMap newMapping = new OntopNativeSQLPPTriplesMap( IDGenerator.getNextUniqueID(mapping.getId() + "#"), mapping.getSourceQuery(), toBeLeft); builder1.add(newMapping); } } } nonExpandableMappings = builder1.build(); mappingsToBeExpanded = builder2.build(); }
ImmutableList<TargetAtom> toBeExpanded = mapping.getTargetAtoms().stream() .filter(targetAtom -> {
public SQLPPMapping createMappings() { OntopMappingConfiguration defaultConfiguration = OntopMappingConfiguration.defaultBuilder() .build(); SpecificationFactory specificationFactory = defaultConfiguration.getInjector().getInstance(SpecificationFactory.class); PrefixManager prefixManager = specificationFactory.createPrefixManager(ImmutableMap.of()); ImmutableList<SQLPPTriplesMap> mappingAxioms = dataRepository.getMappings(); UriTemplateMatcher uriTemplateMatcher = UriTemplateMatcher.create( mappingAxioms.stream() .flatMap(ax -> ax.getTargetAtoms().stream()) .flatMap(atom -> atom.getSubstitution().getImmutableMap().values().stream()) .filter(t -> t instanceof ImmutableFunctionalTerm) .map(t -> (ImmutableFunctionalTerm) t), termFactory); try { return new SQLPPMappingImpl(mappingAxioms, specificationFactory.createMetadata(prefixManager, uriTemplateMatcher)); } catch (DuplicateMappingException e) { throw new IllegalStateException(e.getMessage()); } } }
private SQLPPMapping parse(R2RMLManager manager) throws DuplicateMappingException, InvalidMappingException { try { //TODO: make the R2RMLManager simpler. ImmutableList<SQLPPTriplesMap> sourceMappings = manager.getMappings(manager.getModel()); UriTemplateMatcher uriTemplateMatcher = UriTemplateMatcher.create( sourceMappings.stream() .flatMap(ax -> ax.getTargetAtoms().stream()) .flatMap(atom -> atom.getSubstitution().getImmutableMap().values().stream()) .filter(t -> t instanceof ImmutableFunctionalTerm) .map(t -> (ImmutableFunctionalTerm) t), termFactory); //TODO: try to extract prefixes from the R2RML mappings PrefixManager prefixManager = specificationFactory.createPrefixManager(ImmutableMap.of()); MappingMetadata mappingMetadata = specificationFactory.createMetadata(prefixManager, uriTemplateMatcher); return ppMappingFactory.createSQLPreProcessedMapping(sourceMappings, mappingMetadata); } catch (InvalidR2RMLMappingException e) { throw new InvalidMappingException(e.getMessage()); } }
/** * 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); } }
/** * 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); } }
int formerCount = counter.get(); ImmutableList<TargetAtom> newTargetAtoms = formerTriplesMap.getTargetAtoms().stream() .map(a -> { if (a.getPredicateIRI()
public void run(File file) throws Exception { /** * Uses the predefined data source for creating the OBDAModel. */ OBDADataSource dataSource = obdaModelController.getSources().get(0); OntopMappingSQLAllConfiguration configuration = OntopMappingSQLAllConfiguration.defaultBuilder() .properties(DataSource2PropertiesConvertor.convert(dataSource)) .r2rmlMappingFile(file) .build(); SQLPPMapping parsedModel = configuration.loadProvidedPPMapping(); try{ /** * TODO: improve this inefficient method (batch processing, not one by one) */ for (SQLPPTriplesMap mapping : parsedModel.getTripleMaps()) { if (mapping.getTargetAtoms().toString().contains("BNODE")) { JOptionPane.showMessageDialog(getWorkspace(), "The mapping " + mapping.getId() + " contains BNode. -ontoPro- does not support it yet."); } else { obdaModelController.addTriplesMap(mapping, false); } } } catch (DuplicateMappingException dm) { JOptionPane.showMessageDialog(getWorkspace(), "Duplicate mapping id found. Please correct the Resource node name: " + dm.getLocalizedMessage()); throw new RuntimeException("Duplicate mapping found: " + dm.getMessage()); } }
private void prepareTextPanes(SQLPPTriplesMap value, boolean selected) { ImmutableList<TargetAtom> targetQuery = value.getTargetAtoms(); String trgQuery = TargetQueryRenderer.encode(targetQuery, prefixManager); trgQueryTextPane.setText(trgQuery);