owlcs / owlapi

OWL API main repository
813 stars 314 forks source link

No such method "setAcceptingHTTPCompression(boolean)" when using Manchester parser #1143

Closed alkidbaci closed 1 month ago

alkidbaci commented 1 month ago

Hello, I am using Jpype in python to execute some java code via the jpype Java Virtual Machine. I'm trying to use the ManchesterOWLSyntaxClassExpressionParser but I get an error (showed in the end) when parsing the string class expression. I think I'm using compatible versions of packages and cant really think of any another reason for this error. Would appreciate some help.

My code:

import jpype
import jpype.imports
from jpype.types import *

# Start the JVM
jpype.startJVM(classpath=['HermiT.jar','org.semanticweb.hermit-1.4.3.517.jar', 'owlapi-distribution-5.1.9.jar','slf4j-api-1.7.25.jar','slf4j-jdk14-1.7.25.jar')

# Import Java classes
from org.semanticweb.owlapi.apibinding import OWLManager
from org.semanticweb.owlapi.model import IRI
from org.semanticweb.owlapi.reasoner import ConsoleProgressMonitor, SimpleConfiguration, OWLReasonerFactory, OWLReasoner
from java.io import File
from org.semanticweb.HermiT import ReasonerFactory
from org.semanticweb.owlapi.manchestersyntax.parser import ManchesterOWLSyntaxClassExpressionParser
from org.semanticweb.owlapi.util import BidirectionalShortFormProviderAdapter
from org.semanticweb.owlapi.expression import ShortFormEntityChecker
from org.semanticweb.owlapi.util import SimpleShortFormProvider
from java.util import HashSet

# Create OWLOntologyManager and load ontology
file = File('family-benchmark_rich_background.owl')
manager = OWLManager.createOWLOntologyManager()
ontology = manager.loadOntologyFromOntologyDocument(file)
data_factory = manager.getOWLDataFactory()

# Configure HermiT reasoner
reasoner_factory = ReasonerFactory()
reasoner = reasoner_factory.createReasoner(ontology)

# Initialize a Manchester parser
short_form_provider = SimpleShortFormProvider()
ontology_set = HashSet()
ontology_set.add(ontology)
bidi_provider = BidirectionalShortFormProviderAdapter(manager, ontology_set, short_form_provider)
entity_checker = ShortFormEntityChecker(bidi_provider)  
parser = ManchesterOWLSyntaxClassExpressionParser(data_factory, entity_checker)
class_expression_string = "Brother and Father"

# Parse and print
class_expression = parser.parse(class_expression_string )   
individuals = reasoner.getInstances(class_expression, False).getFlattened()
[print(individual) for individual in individuals]

# Shutdown the JVM
jpype.shutdownJVM()

Error:

Traceback (most recent call last):
  File "ManchesterOWLSyntaxClassExpressionParser.java", line 49, in org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxClassExpressionParser.parse
Exception: Java Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/pg-xai2/tmp/script.py", line 66, in <module>
    class_expression = parser.parse("Brother")   
                       ^^^^^^^^^^^^^^^^^^^^^^^
java.lang.java.lang.NoSuchMethodError: java.lang.NoSuchMethodError: 'org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration org.semanticweb.owlapi.model.OWLOntologyLoaderConfiguration.setAcceptingHTTPCompression(boolean)'
ignazio1977 commented 1 month ago

Looks like you have two owlapi versions in the classpath. The HermiT jar looks like the original HermiT release, which would have included an ancient owlapi version. That's the most likely candidate.

alkidbaci commented 1 month ago

Yes it seems like this is definitely related with the dependencies. I removed HermiT.jar from the classpath list but I'm getting other errors related with missing dependencies. I'm not very familiar with java stuff. Would really appreciate if you have some reference to some python code that uses owlapi via jpype (preferably using the hermit reasoner also) or some other guide to use owlapi from python. If there is no such reference that you are aware of, then please consider this issue closed. Thank you.

ignazio1977 commented 1 month ago

I'm not knowledgeable on how jpipe works, but basically you'd need the imports closure for HermiT, i.e., the list of jars that make up hermit, their dependencies and the dependencies of the dependencies.

Hermit and owlapi are maven projects so seeing the full closure is relatively simple in a java context. Less so in python I guess.

I'll see if I can find a list.

ignazio1977 commented 1 month ago

https://github.com/owlcs/releases/tree/master/HermiT

The releases project contains single jars for HermiT and OWLAPI. The HermiT jar here is the latest snapshot available, and should contain OWLAPI 5.1.9 and all dependencies needed. See if, by itself, this jar is suffcient for jpipe.

If not, the missing dependencies are likely in

/https://github.com/owlcs/releases/blob/master/owlapi/5/owlapi-dependencypack-5.1.9.jar

which contains the whole of owlapi 5 and its dependencies.

jpipe might have difficulties accessing jars within jars, in which case you can rename the files to zip files (jar files are zip files with an extra folder in there) and open them with any zip utility, to extract all the jars contained within and add them manually to your classpath.

alkidbaci commented 1 month ago

Thank you very much, this was really helpful. I finally made it work.