# OntoBroker Client for Python # # (p) + (c) 2002 for the original Java code by Ontoprise GmbH # Translated by Raphael Volz, University of Karlsruhe # Modified by Eddie Moench, Ontoprise GmbH # Germany import socket, string class Client: """ Connects to an OntoBroker IE""" __host = 'localhost' __port = 1234 __pagesize = -1 __pagetop = 0 __fetchAnswers = 0 __answers = [] def __init__(self, host, port): """ Connect to the inference server at """ self.__port = port self.__host = host def __connect(self, times, seconds): """ Connect to server times for """ def command(self, commandString): """ Sends a command to Ontobroker and delivers a result message from the server. Commands: isalive reload inferoff inferon clear print add F-Logic fact del F-Logic fact return (void) """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.__host, self.__port)) s.send(commandString) result = '' while((string.find(result,'\n') == -1) and (string.find(result,'\n') == -1)) : data = s.recv(1024) if not data: break result = result + data result = string.split(result,'\n')[0] result = string.split(result,'\r')[0] s.close() return result def nextPage(self, pageSize): """ Demands the next page (pagesize answers). returns -1, if all answers are not yet available """ def query(self, query): """ Sends a F-Logic query to the server, starts evaluation and transmission of answers from the server return (void) """ query = query + '\r\nstop\r\n' # Konnektieren s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.__host, self.__port)) self.__fetchAnswers = 1 s.send(query) result = '' answers = [] while(1) : data = s.recv(1024) if not data: break result = result + data s.close() answers = string.split(result,'\r\n') answers[:] = answers[1:-2] # filters start, stop and last line for answer in answers: print answer self.__answers = answers[:] return answers def getRow(self, row): """ Returns answer number as a comma separated list in the following form. The index starts with 0 ! no of query:variable_1, variable_2 ... or no of query:substitution_1, substitution_2 ... """ return self.__answers[row] def numberOfRows(self) : """ Returns the number of answers returned by the server at this point of time """ return len(self.__answers) # ...is one more than #of results b/c of "X","Y" is a row def query(q, h, p): con = Client(h, p) return con.query(q) def makeList(q, h, p): input = query(q, h, p) newList = [] # this iteration separates the string in a list for item in input: item = item[string.find(item, ':') + 1:] newList.append(string.split(item, '",')) return newList