# OntoBroker Client for Python # # (p) + (c) 2002 Ontoprise GmbH, maintainer: Eddie Moench # translated in Python by Raphael Volz # Germany # import socket, string, re 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 = '' # Simulate Java DataInputStream.readLine() while(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,'')[0] s.close() # print "*",result,"*" return result def nextPage(self, pageSize): """ Demands the next page (pagesize answers). returns -1, if all answers are not yet available """ def connectForQuery(): """ Connects for a query """ # TODO return 1 def query(self, query): """ Sends a F-Logic query to the server, starts evaluation and transmission of answers from the server return (void) """ query += '\nstop\n' # connecting s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a TCP-Socket s.connect((self.__host, self.__port)) self.__fetchAnswers = 1 s.send(query) result = '' answers = [] # Simulate Java DataInputStream.readLine() while(1) : data = s.recv(1024) # receive 1024 Bytes at max if not data: break result = result + string.strip(data) s.close() answers = string.split(result[7:-4],'0:') # filters start and stop 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 setCommand(c, h, p): con = Client(h, p) return con.command(c) def query(q, h, p): con = Client(h, p) return con.query(q) def queryList(q, h, p, vars = 0): """ This function returns the OntoBroker results in a List (Array in some other languages) """ input = query(q, h, p) newList = [] if not vars: input = input[1:] # this iteration separates the string in a list for item in input: item = string.strip(item) item = string.replace(item, '\\"', '"') helpList = string.split(item, '","') helpList[0] = helpList[0][1:] helpList[-1] = helpList[-1][:-1] newList.append(helpList) return newList def queryDict(q, h, p): """ This function returns the OntoBroker results in a Dictionary """ input = query(q, h, p) newDict = [] # this iteration separates the string in a list for i in range(0,len(input)): input[i] = string.strip(input[i]) input[i] = string.replace(input[i], '\\"', '"') helpList = string.split(input[i], '","') helpList[0] = helpList[0][1:] helpList[-1] = helpList[-1][:-1] if not i: vars = helpList else: helpDict = {} for j in range(0,len(helpList)): helpDict[vars[j]] = helpList[j] newDict.append(helpDict) return newDict def queryScript(sf, h, p): con = Client(h, p) f = open(sf) lList = string.split(f.read(),'\n') f.close() results = [] for line in lList: if (string.find(line,"add ") != -1) or (string.find(line,"del ") != -1): con.command(line) else: results += con.query(line) return results