Skip to content

Commit

Permalink
[ontopy] compat layer for base clients
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthou committed Jan 12, 2024
1 parent 8d2a6f2 commit f6f468b
Show file tree
Hide file tree
Showing 17 changed files with 69 additions and 212 deletions.
1 change: 1 addition & 0 deletions ontopy/ontologenius/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from .clients import *
from .clientsIndex import *
from .compat import *

from .OntologyManipulator import OntologyManipulator
from .OntologyManipulatorIndex import OntologyManipulatorIndex
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/ActionClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .ClientBase import ClientBase

class ActionClient(ClientBase):
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/ClassClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .OntologyClient import OntologyClient

class ClassClient(OntologyClient):
Expand Down
101 changes: 27 additions & 74 deletions ontopy/ontologenius/clients/ClientBase.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import rospy
from ..compat.ros import Ontoros, OntoService

from ontologenius.srv import OntologeniusService
from ontologenius.srv import OntologeniusService, OntologeniusServiceRequest

class ClientBase:
"""The ClientBase class provides an abstraction for any ROS services.
Expand All @@ -15,7 +15,7 @@ def __init__(self, name):
"""Constructs a ROS client linked to the service name(str)."""
self._name = name
self.error_code = 0
self._client = rospy.ServiceProxy('ontologenius/' + self._name, OntologeniusService, True)
self._client = Ontoros.createService('ontologenius/' + self._name, OntologeniusService)

def nb(self):
"""Gives the total number (int) of service calls from all ClientBase instances since the last reset."""
Expand All @@ -37,83 +37,47 @@ def call(self, action, param):
If the service call fails, the function returns None
"""
ClientBase._cpt += 1
try:
response = self._client(action, param)
request = OntologeniusServiceRequest(action, param)
response = self._client.call(request, ClientBase._verbose)
if(response is None):
self.error_code = -1
return None
else:
self.error_code = response.code
return response.values
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure to call ontologenius/" + self._name)
self._client = rospy.ServiceProxy('ontologenius/' + self._name, OntologeniusService, True)
try:
response = self._client(action, param)
self.error_code = response.code
if ClientBase._verbose == True:
print("Restored ontologenius/" + self._name)
return response.values
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure of service restoration")
self.error_code = -1
return None

def callStr(self, action, param):
"""Call the service set up in the constructor of ClientBase with the request
defined with action(str) and param(str) and returns all the first result (str).
If the service call fails, the function returns None
"""
ClientBase._cpt += 1
try:
response = self._client(action, param)
request = OntologeniusServiceRequest(action, param)
response = self._client.call(request, ClientBase._verbose)
if(response is None):
self.error_code = -1
return None
else:
self.error_code = response.code
if len(response.values) > 0:
return response.values[0]
else:
return ''
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure to call ontologenius/" + self._name)
self._client = rospy.ServiceProxy('ontologenius/' + self._name, OntologeniusService, True)
try:
response = self._client(action, param)
self.error_code = response.code
if ClientBase._verbose == True:
print("Restored ontologenius/" + self._name)
if len(response.values) > 0:
return response.values[0]
else:
return ''
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure of service restoration")
self.error_code = -1
return None

def callNR(self, action, param):
"""Call the service set up in the constructor of ClientBase with the
request defined with action(str) and param(str).
If the service call fails, the function returns False
"""
ClientBase._cpt += 1
try:
response = self._client(action, param)
request = OntologeniusServiceRequest(action, param)
response = self._client.call(request, ClientBase._verbose)
if(response is None):
self.error_code = -1
return False
else:
self.error_code = response.code
return True
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure to call ontologenius/" + self._name)
self._client = rospy.ServiceProxy('ontologenius/' + self._name, OntologeniusService, True)
try:
response = self._client(action, param)
self.error_code = response.code
if ClientBase._verbose == True:
print("Restored ontologenius/" + self._name)
return True
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure of service restoration")
self.error_code = -1
return False

def callBool(self, action, param):
"""Call the service set up in the constructor of ClientBase with the
Expand All @@ -122,22 +86,11 @@ def callBool(self, action, param):
service is different from SUCCESS.
"""
ClientBase._cpt += 1
try:
response = self._client(action, param)
request = OntologeniusServiceRequest(action, param)
response = self._client.call(request, ClientBase._verbose)
if(response is None):
self.error_code = -1
return False
else:
self.error_code = response.code
return response.code == 0
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure to call ontologenius/" + self._name)
self._client = rospy.ServiceProxy('ontologenius/' + self._name, OntologeniusService, True)
try:
response = self._client(action, param)
if ClientBase._verbose == True:
print("Restored ontologenius/" + self._name)
self.error_code = response.code
return response.code == 0
except (rospy.ServiceException, rospy.exceptions.TransportTerminated) as e:
if ClientBase._verbose == True:
print("Failure of service restoration")
self.error_code = -1
return False
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/DataPropertyClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .OntologyClient import OntologyClient

class DataPropertyClient(OntologyClient):
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/IndividualClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .OntologyClient import OntologyClient

class IndividualClient(OntologyClient):
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/ManagerClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .ClientBase import ClientBase

class ManagerClient(ClientBase):
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/ObjectPropertyClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .OntologyClient import OntologyClient

class ObjectPropertyClient(OntologyClient):
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/OntologyClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .ClientBase import ClientBase

class OntologyClient(ClientBase):
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clients/ReasonerClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .ClientBase import ClientBase

class ReasonerClient(ClientBase):
Expand Down
2 changes: 0 additions & 2 deletions ontopy/ontologenius/clientsIndex/ClassIndexClient.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import rospy

from .OntologyIndexClient import OntologyIndexClient

class ClassIndexClient(OntologyIndexClient):
Expand Down
Loading

0 comments on commit f6f468b

Please sign in to comment.