0

I've written this code to replace object property value from Ontology:

public void changeObjectPropertyValue(String file, OWLOntology ontology, String ind, String propertyFragment, String newValueFragment) throws OWLOntologyCreationException {
    File  ontologyFile = new File(file);
    OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
    OWLOntology ontology1 = manager.loadOntologyFromOntologyDocument(ontologyFile);
    
    OWLDataFactory factory = manager.getOWLDataFactory();
    
    IRI ontologyIRI = ontology1.getOntologyID().getOntologyIRI();

    OWLNamedIndividual individualToReplaceValueOn = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#" + ind));
    OWLNamedIndividual newValueInd = factory.getOWLNamedIndividual(IRI.create(ontologyIRI + "#" + newValueFragment));
    OWLObjectProperty theObjectProperty = factory.getOWLObjectProperty(IRI.create(ontologyIRI + "#" + propertyFragment));
    OWLIndividual theOldValue = EntitySearcher.getObjectPropertyValues(individualToReplaceValueOn, theObjectProperty, ontology1).findFirst().get();

    OWLAxiom oldAxiom = factory.getOWLObjectPropertyAssertionAxiom(
            theObjectProperty,
            individualToReplaceValueOn,
            theOldValue);

    OWLAxiom newAxiom = factory.getOWLObjectPropertyAssertionAxiom(
            theObjectProperty,
            individualToReplaceValueOn,
            newValueInd);

    List<OWLOntologyChange> changes = new Vector<OWLOntologyChange>();

    changes.add(new RemoveAxiom(ontology1, oldAxiom));
    changes.add(new AddAxiom(ontology1, newAxiom));
    man.applyChanges(changes);
}

To try to execute the above code appears the following error:

Exception in thread "main" java.lang.NoSuchMethodError: 'java.util.stream.Stream org.semanticweb.owlapi.model.OWLOntology.objectPropertyAssertionAxioms(org.semanticweb.owlapi.model.OWLIndividual)'
    at org.semanticweb.owlapi.search.EntitySearcher.getObjectPropertyValues(EntitySearcher.java:1245)
OWLIndividual theOldValue = EntitySearcher
                .getObjectPropertyValues(individualToReplaceValueOn, theObjectProperty, ontology1).findFirst().get();

When executing, the method appears or an error appears for that line. Is it a library error? Is there a jar missing?

Below are the jars dependencies:

<dependencies>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-parsers</artifactId>
        <version>4.5.24</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-osgidistribution</artifactId>
        <version>4.5.24</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-impl</artifactId>
        <version>4.5.24</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-apibinding</artifactId>
        <version>4.5.24</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>org.semanticweb.hermit</artifactId>
        <version>1.3.8.500</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-distribution</artifactId>
        <version>4.5.24</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-api</artifactId>
        <version>4.5.24</version>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.owlapi</groupId>
        <artifactId>owlapi-osgidistribution</artifactId>
        <version>5.5.0</version>
    </dependency>
</dependencies>

What should I do now to run all the code? Even with the dependencies, errors still appear. Could you please help me?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Monteiro
  • 11
  • 7
  • The error is quite straightforward. There's no method by that name. However, the method name in the error message doesn't even appear in the code you've provided. – miken32 Feb 02 '23 at 23:22
  • Is it a library error? Is there a jar missing? – Monteiro Feb 03 '23 at 00:14
  • there is i difference in version 4 and version 5 - version 4 returns a collection, version 5 a stream. which one do you use? – UninformedUser Feb 03 '23 at 09:20
  • As @UninformedUser suggests, the issue is library versions. That error, paired with your ability to compile the code, suggests you have two conflicting versions in your runtime classpath. Please add information about how you manage dependencies (Maven?), or any other way you have of showing which libraries are on the classpath. – Ignazio Feb 03 '23 at 12:11
  • @IgnazioI followed your directions! Could you help me, please? – Monteiro Feb 04 '23 at 01:26
  • You seem to be mixing version 4.5.24 and 5.5.0... – Mark Rotteveel Feb 04 '23 at 08:28
  • @MarkRotteveel the problem is that if you don't put the owlapi-osgidistribution 5.5.0 version, it doesn't recognize the .findAny().get() method of OWLIndividual theOldValue = EntitySearcher .getObjectPropertyValues(individualToReplaceValueOn, theObjectProperty, ontology).findAny().get(); Could you help me, please? – Monteiro Feb 04 '23 at 12:00
  • If you want to use 5.5.0, then you need to use version 5.5.0 for the dependencies where you're currently specifying 4.5.24. – Mark Rotteveel Feb 04 '23 at 12:42
  • @MarkRotteveel Now i modified all the dependencies to 5.5.0, but now the following error appears: Exception in thread "main" java.util.NoSuchElementException: No value present at java.base/java.util.Optional.get(Optional.java:143) – Monteiro Feb 04 '23 at 13:07
  • 1
    That sounds like a usage error on you part: your query didn't find anything, so it returned an empty optional. You should call `get()` unconditionally on an `Optional`: check with `isPresent` or `ifPresent`, `orElse` or `orElseGet`, etc. – Mark Rotteveel Feb 04 '23 at 13:10
  • 1
    In any case, I have rolled back your latest edit, as it fundamentally changes the question. However, that secondary question would be answered by [How to prevent NoSuchElementException using Optional class in Java](https://stackoverflow.com/questions/72407971/how-to-prevent-nosuchelementexception-using-optional-class-in-java) – Mark Rotteveel Feb 04 '23 at 13:13

0 Answers0