/** * Clone into another ontology with the same axioms and same manager. NB : In a future version this function may return an ontology that share axiom with * previous for memory saving. * * @param version that will have the new ontology. * @return a new ontology with the axioms of the given one. * @throws OWLOntologyCreationException if we can't create the ontology. * @since 2.5.1 */ default public OWLHelper derivate(final double version) throws OWLOntologyCreationException { final Optional<OWLHelper> result = look(this.getOntology().getOntologyID().getOntologyIRI().get(), version); if (!result.isPresent()) throw new OWLOntologyCreationException("Can't derivate to version " + version); if (result.get().getOntology().getAxiomCount() != 0) getLogger().warning(() -> "The ontology you try to derivate from " + getVersion() + " to version " + version + " already exist."); result.get().addAxioms(getOntology().axioms()); return result.get(); }
/** * Compute the types of an individual. Use this function only if you mix Named and Anonymous individuals. * * @param ind the individual named _or_ anonymous * @return the classes of the individual. * @since 2.5.1 */ default public NodeSet<OWLClass> getTypes(final OWLIndividual ind) { if (ind instanceof OWLAnonymousIndividual) { // We create a temporary named Individual to allow the reasoner to work. final OWLNamedIndividual individual = getFactory().getOWLNamedIndividual(IRI.create(_protocol + OWLHelper.class.getPackage().getName() + _webSeparator + OWLHelper.class.getSimpleName() + _entitySeparator + IRIUtils.randId(OWLHelper.class.getSimpleName()))); final Stream<OWLAxiom> axioms = Stream.of( // getFactory().getOWLDeclarationAxiom(individual), // getFactory().getOWLSameIndividualAxiom(individual, ind) // The temporary named is the same as the anonymous one. ); getManager().addAxioms(getOntology(), axioms); final NodeSet<OWLClass> result = getReasoner().getTypes(individual, false); getManager().removeAxioms(getOntology(), axioms); return result; } else return getReasoner().getTypes((OWLNamedIndividual) ind, false); }
/** * @param ontologyIRI is the id of the ontology without version. The ontology name. * @param version is the short representation you want for this ontology. * @return the complete ontologyID * @since 2.6.0 */ public static OWLOntologyID getVersion(final IRI ontologyIRI, final double version) { return new OWLOntologyID(ontologyIRI, buildVersion(ontologyIRI, version)); }
default void remove(final OWLHelper helper) { helper.dispose(); helper.getManager().removeOntology(helper.getOntology()); } }
/** * @return the namespace utils that can resolve prefix. * @since 2.5.1 */ public default Optional<PrefixDocumentFormat> getNamespaces() { final OWLDocumentFormat format = getManager().getOntologyFormat(getOntology()); return (format.isPrefixOWLDocumentFormat()) ? Optional.of((PrefixDocumentFormat) format) : Optional.empty(); }
/** * @return the axioms as a single blob string. * @throws OWLOntologyStorageException if we can't store the ontology. * @throws IOException if there is an problem when reading. * @since 1.2 * @since 2.5.1 */ default public String serializeAxioms() throws OWLOntologyStorageException, IOException { try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) { getManager().saveOntology(getOntology(), stream); return stream.toString(); } catch (final OWLOntologyStorageException | IOException e) { getLogger().log(Level.SEVERE, "Problem at serialisation of axioms : " + getOntology().getOntologyID(), e); throw e; } }
@Test public void testOnlyProperties() throws OWLOntologyCreationException { try (final OWLManagerGroup group = new OWLManagerGroup()) { final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + "owlapi.only.properties"), 1.0); final OWLHelper owl = new OWLGenericTools(group, ontId, true); // final OWLNamedIndividual x1 = i.apply("I1"); // final OWLNamedIndividual x2 = i.apply("I2"); //owl.addAxiom(OWL.propertyAssertion(x1, propA, x2)); owl.addAxiom(OWL.inverseProperties(o.apply("A"), o.apply("B"))); //owl.addAxiom(OWL.declaration(ClsA)); owl.getReasoner().getObjectPropertyDomains(o.apply("A")); } // The test is just about not crash. }
final OWLHelper h = OWLHelper.createLightHelper(OpenlletReasonerFactory.getInstance().createReasoner(ontology)); h.addAxioms(// final OWLReasoner r = h.getReasoner(); h.addAxioms(// h.getReasoner().getTypes(i).entities().forEach(System.out::println); h.getOntology().axioms().map(OWLAxiom::toString).distinct().sorted().forEach(System.out::println); h.addAxiom(OWL.classAssertion(l, OWL.Thing)); // As a test you can comment this line to see how the result change. h.addAxiom(OWL.propertyAssertion(l, w, OWL.Individual(UUID.randomUUID().toString()))); h.getReasoner().getTypes(l).entities().forEach(System.out::println); h.addAxioms(// h.getOntology().axioms().map(OWLAxiom::toString).distinct().sorted().forEach(System.out::println);
/** * Axiom are parsed from the stream then add into the ontology. * * @param input is a stream of axioms * @throws OWLOntologyCreationException if we can't load the ontology. * @throws IOException if there is an problem when reading. * @since 2.5.1 */ default public void deserializeAxiomsInto(final String input) throws OWLOntologyCreationException, IOException { try (final InputStream stream = new ByteArrayInputStream(input.getBytes())) { addAxioms(getManager().loadOntologyFromOntologyDocument(stream).axioms()); } catch (final OWLOntologyAlreadyExistsException e) { if (e.getOntologyID().equals(getOntology().getOntologyID())) { getLogger().severe("The ontology already exists with the name of the Tools : " + e.getOntologyID()); throw e; } else { getManager().removeOntology(e.getOntologyID()); deserializeAxiomsInto(input); // WARN : if the file define 2 ontologies and one is already define, then an infinite loop here can occur. } } }
group.getPersistentManager(); final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + ontologyName), 1.0); final OWLHelper owl = new OWLGenericTools(group, ontId, false); owl.addAxiom(OWL.declaration(ClsA)); owl.addAxiom(OWL.declaration(ClsB)); owl.addAxiom(OWL.propertyAssertion(Ind1, propA, Ind1)); owl.addAxiom(OWL.propertyAssertion(Ind1, propB, OWL.constant(hardString))); assertTrue(owl.getObject(Ind1, propA).get().getIRI().equals(Ind1.getIRI())); group.getPersistentManager(); final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + ontologyName), 1.0); final OWLHelper owl = new OWLGenericTools(group, ontId, false); owl.addAxioms(expected.stream()); group.getPersistentManager(); final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + ontologyName), 1.0); final OWLHelper owl = new OWLGenericTools(group, ontId, false); final String[] foundStrings = owl.getOntology().axioms().map(OWLAxiom::toString).sorted().toArray(String[]::new); final String[] expectedStrings = expected.stream().map(OWLAxiom::toString).sorted().toArray(String[]::new); assertTrue("hard String !", owl.getValue(Ind1, propB).map(x -> x.getLiteral().equals(hardString)).orElse(false));
final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + "owlapi.tests"), 1.0); owl.declareIndividual(ClsA, Ind1); final List<OWLClass> entities = owl.getReasoner().getTypes(Ind1).entities().collect(Collectors.toList()); assertTrue(entities.size() == 2); assertTrue(entities.contains(ClsA)); owl.addAxiom(SWRL.rule(// final List<OWLClass> entities = owl.getReasoner().getTypes(Ind1).entities().collect(Collectors.toList()); assertTrue(entities.size() == 3); assertTrue(entities.contains(ClsA)); owl.addAxiom(SWRL.rule(// final List<OWLClass> entities = owl.getReasoner().getTypes(Ind1).entities().collect(Collectors.toList()); assertTrue(entities.size() == 4); assertTrue(entities.contains(ClsA)); owl.addClass(Ind1, ClsD); owl.addAxiom(SWRL.rule(// final List<OWLClass> entities = owl.getReasoner().getTypes(Ind1).entities().collect(Collectors.toList()); assertTrue(entities.size() == 6); assertTrue(entities.contains(ClsA)); owl.addClass(Ind1, OWL.not(ClsF)); // Mark the negation to enforce the open world assertion.
@Test public void testAddAndRemove() throws OWLOntologyCreationException { try (final OWLManagerGroup group = new OWLManagerGroup()) { final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + "owlapi.add.remove"), 1.0); final OWLHelper owl = new OWLGenericTools(group, ontId, true); owl.addAxiom(OWL.declaration(OWL.DataProperty(NS + "propA"))); owl.addAxiom(OWL.declaration(OWL.Class(NS + "clsA"))); owl.addAxiom(OWL.equivalentClasses(OWL.Class(NS + "clsA"), // OWL.value(OWL.DataProperty(NS + "propA"), OWL.constant(12))// )); assertTrue(owl.getReasoner().instances(OWL.Class(NS + "clsA")).count() == 0); final OWLNamedIndividual x1 = OWL.Individual(NS + "I1"); owl.addAxiom(OWL.classAssertion(x1, OWL.Class(NS + "clsA"))); assertTrue(owl.getReasoner().instances(OWL.Class(NS + "clsA")).count() == 1); owl.removeAxiom(OWL.classAssertion(x1, OWL.Class(NS + "clsA"))); assertTrue(owl.getReasoner().instances(OWL.Class(NS + "clsA")).count() == 0); } // The test is just about not crash. }
/** * @return the shortest representation of the version of an ontology. Defaulting on 'zero' if no-version information. * @since 2.5.1 */ default public double getVersion() { final IRI version = getOntology().getOntologyID().getVersionIRI().get(); if (null == version) return 0; try { final String fragment = getFragment(version); final int index = fragment.lastIndexOf(_innerSeparator.charAt(0)); return Double.parseDouble(fragment.substring(index + 1)); } catch (final Exception e) { getLogger().log(Level.SEVERE, version.toString() + " isn't a standard version format", e); throw new OWLException("Plz use " + OWLHelper.class.getSimpleName() + " to manage your versions.", e); } }
/** * @param buff is the target for the axioms rendered in DL syntax * @param msg is insert before and after the axioms to detach it from its background (use "" if you don't know what to do with that). * @return the given buffer * @since 2.5.1 */ default public StringBuffer ontologyToString(final StringBuffer buff, final String msg) { final DLSyntaxObjectRenderer syntax = new DLSyntaxObjectRenderer(); buff.append("====\\/==" + msg + "===\\/====\n"); getOntology().axioms().map(syntax::render).filter(x -> x != null && x.length() > 0).sorted().map(x -> x + "\n").forEach(buff::append); buff.append("====/\\==" + msg + "===/\\====\n"); return buff; }
private void test(final OWLHelper owl) { final int index = _random.nextInt(_allAxioms.size()); final OWLAxiom axiom = _allAxioms.remove(index); _removedAxioms.add(axiom); owl.getOntology().add(_allAxioms); final OpenlletReasoner r = (OpenlletReasoner) owl.getReasoner(); try { r.isConsistent(); final KnowledgeBase kb = r.getKB(); kb.realize(); kb.classify(); // The error have disappear; lets roll back. final OWLAxiom correct = _removedAxioms.remove(_removedAxioms.size() - 1); System.out.println(correct + "\t may be part of the problem"); _allAxioms.add(correct); } catch (@SuppressWarnings("unused") final Exception e) { // So the error is still present. We have a new minimal ontology. registerOntology(owl.getOntology()); } }
@Test public void testSubProperties() throws OWLOntologyCreationException { try (final OWLManagerGroup group = new OWLManagerGroup()) { final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + "owlapi.inc.properties"), 1.0); final OWLHelper owl = new OWLGenericTools(group, ontId, true); owl.addAxiom(OWL.subPropertyOf(o.apply("P2"), o.apply("P1"))); // p2 extends p1 owl.addAxiom(OWL.propertyAssertion(i.apply("I1"), o.apply("P1"), i.apply("I2"))); owl.addAxiom(OWL.propertyAssertion(i.apply("I3"), o.apply("P2"), i.apply("I4"))); assertFalse(owl.getObject(i.apply("I1"), o.apply("P2")).isPresent()); assertTrue(owl.getObject(i.apply("I3"), o.apply("P1")).get().equals(i.apply("I4"))); } }
final OWLOntologyID ontId = OWLHelper.getVersion(IRI.create(NS + "owlapi.inc.transtive.properties"), 1.0); final OWLHelper owl = new OWLGenericTools(group, ontId, true); final OWLObjectProperty p1 = OWL.ObjectProperty(NS + "P1"); owl.addAxiom(OWL.subPropertyOf(p1, p2)); // p2 extends [P1] final OWLObjectProperty[] chain = { p2, p1 }; owl.addAxiom(OWL.subPropertyOf(chain, p2)); // p2 extends [P2, P1] owl.addAxiom(OWL.inverseProperties(p1, p3)); // p3 inverse of p1 owl.addAxiom(OWL.subPropertyOf(p3, p4)); // p4 extends [P3] final OWLObjectProperty[] chain = { p4, p3 }; owl.addAxiom(OWL.subPropertyOf(chain, p4)); // p4 extends [P4, P3] owl.addAxiom(OWL.propertyAssertion(i1, p1, i2)); owl.addAxiom(OWL.propertyAssertion(i2, p1, i3)); owl.addAxiom(OWL.propertyAssertion(i3, p1, i4)); owl.addAxiom(OWL.propertyAssertion(i4, p1, i5)); owl.addAxiom(OWL.propertyAssertion(i5, p1, i6)); owl.addAxiom(OWL.propertyAssertion(i6, p1, i7)); owl.addAxiom(OWL.propertyAssertion(i7, p1, i8)); owl.addAxiom(OWL.propertyAssertion(i8, p1, ia)); owl.addAxiom(OWL.propertyAssertion(ia, p1, ib)); assertTrue("direct", owl.getObjects(i5, p1).map(x -> x.toString()).sorted().collect(Collectors.joining("")).equals("" + i6)); assertTrue("transitive", owl.getObjects(i5, p2).map(x -> x.toString()).sorted().collect(Collectors.joining("")).equals("" + i6 + i7 + i8 + ia + ib)); assertTrue("inverse", owl.getObjects(i5, p3).map(x -> x.toString()).sorted().collect(Collectors.joining("")).equals("" + i4)); assertTrue("inverse transitive", owl.getObjects(i5, p4).map(x -> x.toString()).sorted((a, b) -> -a.compareTo(b)).collect(Collectors.joining("")).equals("" + i4 + i3 + i2 + i1));
/** * Seek the asked ontology. First in the volatile ontologies, then in the stored ontologies that are already stored. * * @param ontology the iri of the ontology you are looking for. * @param version of the ontology * @param isVolatile * @return an ontology if found. * @since 2.6.0 */ default Optional<OWLHelper> getOntology(final IRI ontology, final double version, final boolean isVolatile) { return getOntology(OWLHelper.getVersion(ontology, version), isVolatile); }
@Test public void testRestrictionConjonction3() throws OWLOntologyCreationException { try (final OWLManagerGroup group = new OWLManagerGroup()) { final OWLOntology ontology = group.getVolatileManager().loadOntologyFromOntologyDocument(new File("src/test/resources/test_data/restriction_conjonction.owl")); final OWLHelper owl = new OWLGenericTools(group, ontology, true); final OWLReasoner r = owl.getReasoner(); assertTrue(r.isConsistent()); ontology.individualsInSignature().forEach(r::getTypes); ontology.classesInSignature().forEach(r::getInstances); } }
/** * When you have finish use this Helper, you must call {dispose() and eventually getGroup().close()} * * @param ontology an already build ontology. * @return an helper * @since 2.6.3 */ public static OWLHelper createLightHelper(final InputStream inputStream) { try { return createLightHelper(OWLManager.createOWLOntologyManager().loadOntologyFromOntologyDocument(inputStream)); } catch (final OWLOntologyCreationException exception) { throw new OpenError(exception); } }