protected OWLClass getFreshQueryClass(String base) { long cnt = 0; String iriString = base; while (m_ontologyOperator.getOntology().containsClassInSignature(IRI.create(iriString))) { iriString = base + (cnt++); // not guaranteed to succeed.. however |long| amount of possibilities } return OWLManager.getOWLDataFactory().getOWLClass(IRI.create(iriString)); }
public class ReasonerUtilUnsatTest { OWLDataFactory dataFactory = OWLManager.getOWLDataFactory(); OWLOntologyManager manager; OWLOntology ont; OWLOntology unsatImport; ReasonerUtil util; @Before public void setup() throws Exception { String uri = Resources.getResource("ontologies/unsat_parent.owl").toURI().toString(); IRI iri = IRI.create(uri); manager = OWLManager.createOWLOntologyManager(); manager.addIRIMapper( new OWLOntologyIRIMapper() { @Override public IRI getDocumentIRI(IRI origIri) { String uri = null; try { uri = Resources.getResource("ontologies/unsat.owl").toURI().toString(); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } return IRI.create(uri); } }); ont = manager.loadOntologyFromOntologyDocument(iri); ReasonerConfiguration config = new ReasonerConfiguration(); config.setFactory(ElkReasonerFactory.class.getCanonicalName()); util = new ReasonerUtil(config, manager, ont); } @Test public void removesAxiomsInImportedOntologies() throws Exception { assertThat(util.getUnsatisfiableClasses(), is(not(empty()))); util.removeUnsatisfiableClasses(); assertThat(util.getUnsatisfiableClasses(), is(empty())); assertThat(util.reason(), is(true)); } }
protected void insertQueryAxiom(OWLClass queryClass) { // Main.getOntologyManager().addAxiom(m_ontologyOperator.getOntology(), // OWLManager.getOWLDataFactory().getOWLEquivalentClassesAxiom(queryClass, // m_referenceExpression)); m_ontologyOperator .getOntology() .getOWLOntologyManager() .addAxiom( m_ontologyOperator.getOntology(), // OWLManager.getOWLDataFactory().getOWLEquivalentClassesAxiom( OWLManager.getOWLDataFactory() .getOWLSubClassOfAxiom( queryClass, m_referenceExpression.accept( m_ontologyOperator.getFlatteningTransformer().getVisitor()))); m_ontologyOperator .getOntology() .getOWLOntologyManager() .applyChanges(m_ontologyOperator.getFlatteningTransformer().getVisitor().getChanges()); m_ontologyOperator.getFlatteningTransformer().getVisitor().resetChangeList(); m_ontologyOperator.ontologyChanged(); }
/** * We share this data between all the tests. Note: take care, do not leave the content of these * object changed, for example remove your example ontology from the testDataManager */ public class TestData { private static final Logger log = LoggerFactory.getLogger(TestData.class); /** Constants (local resources) */ static final String LOCAL_RESOURCE_FOAF = "foaf.rdf"; static URL FOAF_LOCATION = TestData.class.getResource(System.getProperty("file.separator") + LOCAL_RESOURCE_FOAF); static final String LOCAL_RESOURCE_DBPEDIA = "dbpedia_3.6.owl"; static URL DBPEDIA_LOCATION = TestData.class.getResource(System.getProperty("file.separator") + LOCAL_RESOURCE_DBPEDIA); static final String LOCAL_RESOURCE_TEST_1 = "test_1.owl"; static URL TEST_1_LOCATION = TestData.class.getResource(System.getProperty("file.separator") + LOCAL_RESOURCE_TEST_1); static final String LOCAL_RESOURCE_TEST_1_EXPECTED = "test_1_expected.owl"; static URL TEST_1_expected_LOCATION = TestData.class.getResource( System.getProperty("file.separator") + LOCAL_RESOURCE_TEST_1_EXPECTED); static final String LOCAL_RESOURCE_TEST_2 = "test_2.owl"; static URL TEST_2_LOCATION = TestData.class.getResource(System.getProperty("file.separator") + LOCAL_RESOURCE_TEST_2); static final String LOCAL_RESOURCE_TEST_2_EXPECTED = "test_2_expected.owl"; static URL TEST_2_expected_LOCATION = TestData.class.getResource( System.getProperty("file.separator") + LOCAL_RESOURCE_TEST_2_EXPECTED); static final String LOCAL_RESOURCE_TEST_3 = "test_3.owl"; static URL TEST_3_LOCATION = TestData.class.getResource(System.getProperty("file.separator") + LOCAL_RESOURCE_TEST_3); static final String LOCAL_RESOURCE_TEST_3_RULES = "test_3_rules.owl"; static URL TEST_3_rules_LOCATION = TestData.class.getResource( System.getProperty("file.separator") + LOCAL_RESOURCE_TEST_3_RULES); static final String LOCAL_RESOURCE_TEST_3_EXPECTED = "test_3_expected.owl"; static URL TEST_3_expected_LOCATION = TestData.class.getResource( System.getProperty("file.separator") + LOCAL_RESOURCE_TEST_3_EXPECTED); /** Constants */ // EXAMPLE static final String TEST_NS = "http://www.example.org/test/"; static final String TEST_1_NS = TEST_NS + "1/"; static final String TEST_1_expected_NS = TEST_1_NS + "expected/"; static final String TEST_2_NS = TEST_NS + "2/"; static final String TEST_2_expected_NS = TEST_2_NS + "expected/"; static final String TEST_3_NS = TEST_NS + "3/"; static final String TEST_3_rules_NS = TEST_3_NS + "rules/"; static final String TEST_3_expected_NS = TEST_3_NS + "expected/"; // FOAF static final String FOAF_NS = "http://xmlns.com/foaf/0.1/"; /** Manager, factory */ static final OWLOntologyManager manager = OWLManager.createOWLOntologyManager(); static final OWLDataFactory factory = OWLManager.getOWLDataFactory(); static { try { /** Configuration of the manager */ manager.setSilentMissingImportsHandling(true); manager.addMissingImportListener( new MissingImportListener() { @Override public void importMissing(MissingImportEvent event) { log.warn("Missing import! URI was: {}", event.getImportedOntologyURI()); } }); manager.addOntologyChangeListener( new OWLOntologyChangeListener() { @Override public void ontologiesChanged(List<? extends OWLOntologyChange> arg0) throws OWLException { for (OWLOntologyChange change : arg0) { log.debug("{} TO {}", change, change.getOntology()); } } }); /** Loading test ontologies once for all */ manager.loadOntologyFromOntologyDocument(FOAF_LOCATION.openStream()); manager.loadOntologyFromOntologyDocument(DBPEDIA_LOCATION.openStream()); // Test 1 manager.loadOntologyFromOntologyDocument(TEST_1_LOCATION.openStream()); manager.loadOntologyFromOntologyDocument(TEST_1_expected_LOCATION.openStream()); // Test 2 manager.loadOntologyFromOntologyDocument(TEST_2_LOCATION.openStream()); manager.loadOntologyFromOntologyDocument(TEST_2_expected_LOCATION.openStream()); // Test 3 manager.loadOntologyFromOntologyDocument(TEST_3_LOCATION.openStream()); manager.loadOntologyFromOntologyDocument(TEST_3_rules_LOCATION.openStream()); manager.loadOntologyFromOntologyDocument(TEST_3_expected_LOCATION.openStream()); } catch (OWLOntologyCreationException e) { log.error( "A {} have been thrown while creating the ontology" + ". Message was: {}", e.getClass(), e.getLocalizedMessage()); } catch (IOException e) { log.error( "A {} have been thrown while loading the ontology from the location {}", e.getClass(), FOAF_LOCATION.toString()); log.error("Message was: {}", e.getLocalizedMessage()); } } }
protected OWLOntology getOntologyAsOWLOntology( OWLOntologyID ontologyId, boolean merge, IRI universalPrefix) { // if (merge) throw new UnsupportedOperationException("Merge not implemented yet for // OWLOntology."); // Remove the check below. It might be an unmanaged dependency (TODO remove from collector and // reintroduce check?). // if (!hasOntology(ontologyIri)) return null; OWLOntology o; o = ontologyProvider.getStoredOntology(ontologyId, OWLOntology.class, merge); if (merge) { final Set<OWLOntology> set = new HashSet<OWLOntology>(); log.debug("Merging {} with its imports, if any.", o); set.add(o); // Actually, if the provider already performed the merge, this won't happen for (OWLOntology impo : o.getImportsClosure()) { log.debug("Imported ontology {} will be merged with {}.", impo, o); set.add(impo); } OWLOntologySetProvider provider = new OWLOntologySetProvider() { @Override public Set<OWLOntology> getOntologies() { return set; } }; OWLOntologyMerger merger = new OWLOntologyMerger(provider); try { o = merger.createMergedOntology( OWLManager.createOWLOntologyManager(), ontologyId.getOntologyIRI()); } catch (OWLOntologyCreationException e) { log.error("Failed to merge imports for ontology " + ontologyId, e); // do not reassign the root ontology } } else { // Rewrite import statements List<OWLOntologyChange> changes = new ArrayList<OWLOntologyChange>(); OWLDataFactory df = OWLManager.getOWLDataFactory(); /* * TODO manage import rewrites better once the container ID is fully configurable (i.e. instead of * going upOne() add "session" or "ontology" if needed). But only do this if we keep considering * imported ontologies as *not* managed. */ for (OWLImportsDeclaration oldImp : o.getImportsDeclarations()) { changes.add(new RemoveImport(o, oldImp)); String s = oldImp.getIRI().toString(); // FIXME Ugly way to check, but we'll get through with it if (s.contains("::")) s = s.substring(s.indexOf("::") + 2, s.length()); boolean managed = managedOntologies.contains(oldImp.getIRI()); // For space, always go up at least one String tid = getID(); if (backwardPathLength > 0) tid = tid.split("/")[0]; IRI target = IRI.create( (managed ? universalPrefix + "/" + tid + "/" : URIUtils.upOne(universalPrefix) + "/") + s); changes.add(new AddImport(o, df.getOWLImportsDeclaration(target))); } o.getOWLOntologyManager().applyChanges(changes); } return o; }
public class IdentifySplitConcepts extends AbstractAnalyzerAlgorithm { public static final MatchDescription SPLIT = new MatchDescription("Split Operation", MatchDescription.PRIMARY_MATCH_PRIORITY); public static final MatchDescription COPIED_FROM_SPLIT = new MatchDescription("Copied by split operation", MatchDescription.SECONDARY_MATCH_PRIORITY); public static final OWLDatatype STRING_DATATYPE = OWLManager.getOWLDataFactory().getOWLDatatype(XSDVocabulary.STRING.getIRI()); public static final String SPLIT_FROM_ANNOTATION_PROPERTY = "split.from.annotation"; private Changes changes; private OwlDiffMap diffMap; private CodeToEntityMapper mapper; private boolean disabled = false; private OWLAnnotationProperty splitFromProperty; public void initialise(Engine e) { this.changes = e.getChanges(); this.diffMap = changes.getRawDiffMap(); this.mapper = CodeToEntityMapper.get(e); OWLDataFactory factory = diffMap.getOWLDataFactory(); String splitFromPropertyName = (String) e.getParameters().get(SPLIT_FROM_ANNOTATION_PROPERTY); if (splitFromPropertyName == null) { disabled = true; return; } splitFromProperty = factory.getOWLAnnotationProperty(IRI.create(splitFromPropertyName)); } public void apply() { if (disabled) { return; } for (EntityBasedDiff diff : changes.getEntityBasedDiffs()) { apply(diff); } } private void apply(EntityBasedDiff diff) { IRI iriOfNewlyCreatedClass = null; String code = null; MatchedAxiom splitAxiomWithBadDescription = null; MatchedAxiom splitFrom = null; for (MatchedAxiom match : diff.getAxiomMatches()) { if (match.getDescription().equals(MatchedAxiom.AXIOM_ADDED) && isSplitFromAnnotationAssertion(match.getTargetAxiom())) { splitAxiomWithBadDescription = match; splitFrom = new MatchedAxiom(null, match.getTargetAxiom(), SPLIT); iriOfNewlyCreatedClass = (IRI) ((OWLAnnotationAssertionAxiom) match.getTargetAxiom()).getSubject(); code = ((OWLLiteral) ((OWLAnnotationAssertionAxiom) match.getTargetAxiom()).getValue()) .getLiteral(); break; } } if (splitFrom != null) { changes.removeMatch(splitAxiomWithBadDescription); changes.addMatch(splitFrom); handleNewSplitAxioms(iriOfNewlyCreatedClass, code, diff); } } private boolean isSplitFromAnnotationAssertion(OWLAxiom axiom) { return axiom instanceof OWLAnnotationAssertionAxiom && ((OWLAnnotationAssertionAxiom) axiom).getProperty().equals(splitFromProperty) && diffMap.getUnmatchedTargetAxioms().contains(axiom) && ((OWLAnnotationAssertionAxiom) axiom).getSubject() instanceof IRI && ((OWLAnnotationAssertionAxiom) axiom).getValue() instanceof OWLLiteral && ((OWLLiteral) ((OWLAnnotationAssertionAxiom) axiom).getValue()) .getDatatype() .equals(STRING_DATATYPE); } private void handleNewSplitAxioms(IRI iriOfNewlyCreatedClass, String code, EntityBasedDiff diff) { OWLDataFactory factory = diffMap.getOWLDataFactory(); OWLClass newlyCreatedClass = factory.getOWLClass(iriOfNewlyCreatedClass); OWLClass classThatWasSplit = getClassThatWasSplit(code); if (classThatWasSplit != null) { handleNewSplitAxioms(newlyCreatedClass, classThatWasSplit, diff); } } private void handleNewSplitAxioms( OWLClass newlyCreatedClass, OWLClass classThatWasSplit, EntityBasedDiff diff) { OWLOntology sourceOntology = diffMap.getSourceOntology(); Map<OWLEntity, IRI> newTargetToSplitSource = Collections.singletonMap((OWLEntity) newlyCreatedClass, classThatWasSplit.getIRI()); OWLObjectDuplicator duplicator = new OWLObjectDuplicator(newTargetToSplitSource, diffMap.getOWLDataFactory()); Set<OWLClassExpression> inferredParents = getInferredParents(sourceOntology, classThatWasSplit); for (MatchedAxiom match : new ArrayList<MatchedAxiom>(diff.getAxiomMatches())) { if (match.getDescription().equals(MatchedAxiom.AXIOM_ADDED) && cameFromSourceOntology( (OWLAxiom) duplicator.duplicateObject(match.getTargetAxiom()), sourceOntology, classThatWasSplit, inferredParents)) { MatchedAxiom modifiedBySplit = new MatchedAxiom(null, match.getTargetAxiom(), COPIED_FROM_SPLIT); changes.removeMatch(match); changes.addMatch(modifiedBySplit); } } } private boolean cameFromSourceOntology( OWLAxiom axiom, OWLOntology sourceOntology, OWLClass child, Set<OWLClassExpression> inferredParents) { if (sourceOntology.containsAxiom(axiom)) { return true; } else if (axiom instanceof OWLSubClassOfAxiom) { return ((OWLSubClassOfAxiom) axiom).getSubClass().equals(child) && inferredParents.contains(((OWLSubClassOfAxiom) axiom).getSuperClass()); } else { return false; } } private Set<OWLClassExpression> getInferredParents(OWLOntology sourceOntology, OWLClass child) { return getInferredParents(sourceOntology, child, new TreeSet<OWLClass>()); } private Set<OWLClassExpression> getInferredParents( OWLOntology sourceOntology, OWLClass child, Set<OWLClass> viewed) { Set<OWLClassExpression> result = new TreeSet<OWLClassExpression>(); if (!viewed.contains(child)) { viewed.add(child); Set<OWLClassExpression> parents = new TreeSet<OWLClassExpression>(); parents.addAll(child.getSuperClasses(sourceOntology)); parents.addAll(child.getEquivalentClasses(sourceOntology)); for (OWLClassExpression parent : parents) { if (parent instanceof OWLClass) { result.add(parent); result.addAll(getInferredParents(sourceOntology, (OWLClass) parent, viewed)); } else if (parent instanceof OWLObjectIntersectionOf) { Set<OWLClassExpression> inferredParents = ((OWLObjectIntersectionOf) parent).getOperands(); result.addAll(inferredParents); for (OWLClassExpression inferredParent : inferredParents) { if (inferredParent instanceof OWLClass) { result.addAll(getInferredParents(sourceOntology, (OWLClass) inferredParent, viewed)); } } } else { result.add(parent); } } } return result; } private OWLClass getClassThatWasSplit(String code) { if (code == null) { return null; } Collection<OWLEntity> possiblySplitEntities = mapper.getTargetEntities(code); OWLClass classThatWasSplit = null; for (OWLEntity possiblySplityEntity : possiblySplitEntities) { if (possiblySplityEntity instanceof OWLClass) { if (classThatWasSplit != null) { return null; } else { classThatWasSplit = (OWLClass) possiblySplityEntity; EntityBasedDiff classThatWasSplitDiff = changes.getTargetDiffMap().get(classThatWasSplit); if (classThatWasSplitDiff != null) { classThatWasSplit = (OWLClass) classThatWasSplitDiff .getSourceEntity(); // if it was created we do really want null } } } } return classThatWasSplit; } }