#!/usr/bin/python """ NOTE ABOUT VERSION .060+ These versions use a revised graph structure seeurl . gro graph,node,predicate,object works withthout (args) next gress sym gress work TODO: revise gro with (args) sailor context { isa API, agent, pythonModule; description "This is a API for agents who can read and write on the (data ocean)". (wants to read) (Pentals, N-Triples, Semenglish, N3, RDF/XML, KIF, CycL); (wants to write) (Pentals, N-Triples, Semenglish, N3, RDF/XML, KIF, CycL, HTML); theory seeUrl ; preceededBy CyberMind; forums , ; author (Seth Russell); credits (sailor people); downloads , , , ; licenseUrl ; (study license) ; version 0.061; revised "06/07/2002 10am"; status "works"; knownBugs0 ""; knownBugs1 ""; inProgress0 ""; inProgress1 ""; nextToDo1 "change to ne to forms of methods"; nextToDo2 "complete output methods"; thinkingAbout0 ""; thinkingAbout1 ""; lastProgress "parseCommand doit"; priorProgress "wrote generative grammar". }. (sailor people) context { (Seth Russell) preferedEmail ; homePage ; knownHandels "Seth","SethR"; (sailor credits) (original authorship). (Sean B Palmer) preferedEmail ; homePage ?homePage; (sailor credits) parsePentals knownHandels "sbp"? (Daniel Biddle) preferedEmail ; homePage ?homePage; (sailor credits) "see sailorServer"; knownHandels "deltab"? (Mark Russell) preferedEmail ; homePage ?homePage; knownHandels ?knownHandels; (sailor credits) "prodced original CyberMind on which data structure is based"? }. (data ocean) context { description "The (data ocean) was mis-metaphored (semantic web) by (Tim Berners-Lee) ~1995. The (data ocean) is based on the memory structures of graphs containing labeled arrows relating nodes Graphs are related to the natural language concept called 'context'"; seeUrl . }. """ __license__= 'Copyright (C) 2001 Seth Russell. GNU GPL 2' import types import sys import re global ok, fail, oh fail=0 ok=1 oh=2 class wake: def __init__(self,MyName='I',ownedBy='nobody'): # Acquire my identity from imput arguments self.MyName=MyName self.Owner=ownedBy # The Logical State Value for Trivalent Logic # Logic is set by methods: ok, fail, oh # To values: 'ok', 'fail', 'oh' # These states are tested by methods: then, else, ohthen # the third logical value, 'oh', is sometimes refered to as 'surprise' # set to surprise here because attn is not yet on an arrow self.Logic='oh' # This variable exposes the descripiton of the last error # Usually Logic is set to 'oh' whenever an error occurs # @@@ ? @@@ any method which can error, must set error='' prior to execuition # @@@ Proposed change: strategy with surprise and pushing errors on a stack ? self.error='' # Things are identified in this system by integer serial numbers # A thing's integer serial numbers is refered to as it's id # A thing's type, given by thingType(id), is coded in the high order decimal digit of an id # 99,000,000 should be enough grains of sand for the moment # The following assignments fix the thingType for various kinds of things self.graphIdFix = 100000000 # thingType='Graph' self.nodeIdFix = 200000000 # thingType='Node' self.open3IdFix = 300000000 # thingType='3unused' self.arrowIdFix = 400000000 # thingType='Arrow' self.lexIdFix = 500000000 # thingType='Lex' self.open6IdFix = 600000000 # thingType='6unused' self.numberIdFix= 700000000 # thingType='Number' self.actionIdFix= 800000000 # thingType='Program' self.open9IdFix = 900000000 # thingType='9unused' # Start the ids where they left off in the Persistent Data Storage(PDS) # @@@@@ initilized to blank here because we havent gotten to that yet self.PDS='' if self.PDS == '': # If starting a new PDS, start all Ids from their fixes self.graphId=self.graphIdFix self.nodeId=self.nodeIdFix self.arrowId=self.arrowIdFix self.lexId=self.lexIdFix self.numberId=self.numberIdFix self.actionId=self.actionIdFix # The structure of mentography in python is a python list # of graphs, of nodes, of predicates, of objects # This list called graphs contains the actual mentograph in Memory # seeUrl , # self.graphs=[] # Initilize the attn pointers into these graphs # seeUrl # red pointers in mentograph are moved when navigating attn self.graph=0 self.node=0 self.predicate=0 self.object=0 # The following dictionaries provide fast associations between # the identity of things, names, literals, and the nodes and predicates in the graphs. # seeurl ; # The lexid assoicates names with their identities - used from comming from # external language to internal graphs self.lexid={} # reverse index from identity to name self.idlex={} # graphIndex = {G1:0, G2:1, ... } self.graphIndex={} # nodeIndex = { G1 : [(g,n), (g,n) ...] , N1 : [(g,n),(g,n) ...] } # maps identitiy of things to graphs,names, literals, and nodes in the graphs self.nodeIndex={} # predicateIndex = { P1 : [(g,n,p), (g,n,p) ...] , ... } # maps identities of properties to their use in predictes in the graphs # @@@@ currently this index is not kept updated by assertions self.predicateIndex = {} # objectIndex = { N2 : [(g,n,p,o), (g,n,p,o) ...] , N1 : [(g,n,p,o),(g,n,p,o)] ... } # maps identities of objects to their use in the graphs # @@@@ currently this index is not kept updated by assertions self.objectIndex = {} # the stack for mem and remem contains tuples of attn # [(graph,node,predicate,object) ... ] self.memStack=[] # commands that are permissable to run in the interperter # currently we are very permissable self.commands =dir(self) # random access memory of attn self.memos={} # --- MEMORY NEEDED FOR METHODS THAT RETURN OUTPUT FROM GENERATIVE GRAMMARS self.grammars={} self.grammarStack=[] self.grammar='semenglish' # -- lets define some grammars # ________________________________________Pentals________________________________ self.grammars['pentals']={} self.grammars['pentals']['arrow']=lambda subject, property, object, graph : '%s %s %s %s.' % (subject,property,object,graph) # _____________________________________Semenglish___________________________ self.grammars['semenglish']={} self.grammars['semenglish']['arrow']=\ lambda: '%s %s %s.' % \ (self.saySubject(), \ self.sayProperty(), \ self.sayObject()) self.grammars['semenglish']['predicate']=\ lambda: '%s %s' % \ (self.sayProperty(), \ self.sayObjects()) self.grammars['semenglish']['continueList']=', ' self.grammars['semenglish']['continuePredicate']=';\n ' self.grammars['semenglish']['node']=\ lambda: '%s\n %s.' % \ (self.saySubject(), \ self.sayPredicates()) # ________________________________________N3________________________________ self.grammars['n3']={} self.grammars['n3']['arrow']=lambda subject, property, object, graph: ':%s :%s :%s.' % (subject, property,object) # _______________________________________KIF________________________________ self.grammars['kif']={} self.grammars['kif']['arrow']=lambda subject, property, object, graph: '(%s %s %s)' % (property, subject,object) # from the input parameters making an instance of wake # Asserting the Pental (in default of any args) that {I ownedBy NoBody UrGraph.}. # assert first pent that {MyName ownedBy Owner UrGraph.} self.groGraph('UrGraph') self.groNode(self.Owner) self.groNode(self.MyName) self.groPredicate('ownedBy') self.groObject(self.Owner) # ----- END INITILIZE wake instance self.Logic='ok' return None # ----------------------------------------------------- # ---------------- AN QUIDTY INTERPERTER ------------ # ----------------------------------------------------- def Xdoit(self, toDo): # toDo is a list of lists like: [[command1, arg1], [command2, arg2] ... ] for do in toDo: if do[0] in self.commands: method = getattr(self, do[0]) if do[1]=='': method() else: method(do[1]) def doit(self, toDo): # toDo is a list of lists like: [ command1 , (command2, arg1, arg2) , [command2, command3], command4 ... ] # Note that subordinate lists are allowed # and that argument lists are tuples r=None for do in toDo: if type(do)==type('literal'): method=getattr(self, do) r=method() elif type(do) == type(()): # process methods with arguments method=getattr(self, do[0]) if len(do) == 2: r=method(do[1]) elif len(do) == 3: r=method(do[1],do[2]) elif len(do) == 4: r=method(do[1],do[2],do[3]) else: r='Error args>3 not supported '+do else: # assume we have another subordinate clause r=self.doit(do) return r # ----------------------------------------------------- # ---------------- NAVIGATION METHODS ----------------- # ----------------------------------------------------- # MOVING BETWEEN GRAPHS def firstGraph(self): self.graph=0 self.firstNode() def nextGraph(self): if self.graph+1>=len(self.graphs): self.firstGraph() self.Logic='fail' return fail else: self.graph+=1 self.firstNode() self.Logic='ok' return ok def lastGraph(self): self.graph=len(self.graphs)-1 self.firstNode() # MOVING BETWEEN NODES IN A GRAPH def firstNode(self): self.node,self.predicate,self.object=0,0,0 def jace(self): if self.node+1>=len(self.graphs[self.graph][0]): self.node,self.predicate,self.object=0,0,0 self.Logic='fail' return fail else: self.node+=1 self.predicate,self.object=0,0 self.Logic='ok' return ok def lastNode(self): self.node=len(self.graphs[self.graph][0])-1 self.predicate,self.object=0,0 # MOVING BETWEEN PREDICATES IN A NODE def firstPredicate(self): self.predicate,self.object=0,0 def sym(self): if self.predicate+1>=len(self.graphs[self.graph][0][self.node][0]): self.predicate,self.object=0,0 self.Logic='fail' return fail else: self.predicate+=1 self.object=0 self.Logic='ok' return ok def lastPredicate(self): self.predicate=len(self.graphs[self.graph][0][self.node][0])-1 self.object=0 return self.attn() def symTo(self,property): # assume property is a name or a python None self.Logic='fail' if property==None: if len(self.graphs[self.graph][0][self.node][0])==0: # if no predicates here, then symTo(None) is true self.Logic='ok' return ok else: id=None else: if self.lexid.has_key(property): id=self.lexid[property] else: return fail p=0 for i in self.graphs[self.graph][0][self.node][0]: if i[1]==id: self.Logic='ok' break p=+1 if self.Logic=='ok': self.predicate,self.object = p,0 return ok else: return fail # MOVING BETWEEN OBJECTS IN A PREDICATE def firstObject(self): self.object=0 def gress(self): if self.object+1>=len(self.graphs[self.graph][0][self.node][0][self.predicate][0]): self.object=0 self.Logic='fail' return fail else: self.object+=1 self.Logic='ok' return ok def lastObject(self): self.object=len(self.graphs[self.graph][0][self.node][0][self.predicate][0])-1 return self.attn() def gressTo(self,object): # assumes object is a name or a python None self.Logic='fail' if object==None: if len(self.graphs[self.graph][0][self.node][0][self.predicate][0])==0: # if not objects here, then gressTo(None) is true self.Logic='ok' return ok else: id=None else: if self.lexid.has_key(object): id=self.lexid[object] else: return fail ob=0 for i in self.graphs[self.graph][0][self.node][0][self.predicate][0]: if i[1]==id: self.Logic='ok' break ob=+1 if self.Logic=='ok': self.object = ob return ok else: return fail # MANIPULATING ATTENTION ONLY def at(self,called): # find a node in *any* graph associated with this name # if not found in any graph, make a node in attn graph # set attn to first arrow in node # Note, node may be a graph node if self.lexid.has_key(called): id=self.lexid[called] if self.nodeIndex.has_key(id) and len(self.nodeIndex[id]) != 0: a=self.nodeIndex[id] for n in a: if n[0]==self.graph: self.graph,self.node = n[0],n[1] self.Logic='ok' return ok # although it is somewhat ambiguous, return the first node in any context # indicate ambiguity by being suprpised self.graph,self.node=a[0] self.logic='oh' return oh # never seen this before, make a node with that name self.groNode(called) self.Logic='fail' return fail def mem(self): # push attn on the stack for future reference self.memStack.append((self.graph, self.node, self.predicate, self.object)) def remem(self): # pop attn off the stack # if nothing on stack report error and be surprised if len(self.memStack)>0: self.graph, self.node, self.predicate, self.object = self.memStack.pop() else: self.error='remem but nothing on memStack' self.Logic='oh' return oh def memo(self,memoName): attn=self.graph,self.node,self.predicate,self.object if self.memos.has_key(memoName): m=self.memos[memoName] m=[attn]+m self.memos[memoName]=m else: self.memos[memoName]=[attn] def atMemo(self,memoName): try: m=self.memos[memoName] self.graph,self.node,self.predicate,self.object=m[0] self.logic='ok' return m[0] except: print 'Error: atMemo("%s") but no memo("%s")' % (memoName,memoName) self.Logic='oh' return oh def rememo(self,memoName): try: m=self.memos[memoName] attn,stack=m[0],m[1:] if stack: self.memos[memoName]=stack else: del self.memos[memoName] self.graph,self.node,self.predicate,self.object=attn self.Logic='ok' return attn except: print 'Error: rememo("%s") but no memo("%s")' % (memoName,memoName) self.Logic='oh' return oh def attn(self): # just format attn as a tuple and return it to the python calling function return self.graph, self.node, self.predicate, self.object # VANCE - FOLLOW YOUR ARROW TO IT'S DESTINATION def vance(self): if type(self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][2]) is types.TupleType: self.graph,self.node=self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][2] self.predicate,self.object=0,0 self.Logic='ok' return ok else: # object is not a node, cannot vance to it self.Logic='fail' return fail def vanceProperty(self): if type(self.graphs[self.graph][0][self.node][0][self.predicate][2]) is types.TupleType: self.graph,self.node=self.graphs[self.graph][0][self.node][0][self.predicate][2] self.predicate,self.object=0,0 self.Logic='ok' return ok else: # should only happen when arrow is None self.Logic='fail' return fail # ----------------------------------------------------- # ---------------- GROWTH METHODS --------------------- # ----------------------------------------------------- # ISSUE NEW IDENTITIES def newGraphId(self,called=None): if called==None: # new anonymous graph self.graphId+=1 return self.graphId elif self.lexid.has_key(called): # we already have a graph\node called that return self.lexid[called] else: # assign a new graphId and put it in lexid and index self.graphId+=1 self.lexid[called]=self.graphId self.idlex[self.graphId]=called return self.graphId def newNodeId(self,called=None): # Note: property nodes are just nodes if called==None: # new anonymous node self.nodeId+=1 return self.nodeId elif self.lexid.has_key(called): # we already have a graph\node called that return self.lexid[called] else: # assign a new nodeId and put it in lexid and index self.nodeId+=1 self.lexid[called]=self.nodeId self.idlex[self.nodeId]=called return self.nodeId def newObjectId(self): # we dont give arrows identity at the moment # self.arrowId+=1 return None def newLexId(self, value, parseType='', language=''): # seeUrl http://rdfig.xmlhack.com/2002/05/01/2002-05-01.html#1020277030.932331 # seeUrl http://cvs.ilrt.org/cvsweb/~checkout~/redland/raptor/tests/test.nt if (value[0]=='"' and value[-1]=='"') or (value[0]=="'" and value[-1]=="'"): pass else: value='"'+value+'"' literal=parseType+value+language if self.lexid.has_key(literal): # we already have such a literal return self.lexid[literal] else: # assign a new lexId and put it in lexid and index self.lexId+=1 self.lexid[literal]=self.lexId self.idlex[self.lexId]=literal return self.lexId # GROW GRAPHS, NODES, PREDICATES AND ARROWS def groGraph(self,called=None): # Grow a new graph with one empty node # The name of the graph is passed as the optional argument 'called' # fails: if graph exists, yet moves attn there # surprised: if graph name exists yet is not a graph # seeUrl self.error='' if called != None: if self.lexid.has_key(called): # can't make new graph, it already exists id=self.lexid[called] if self.graphIndex.has_key(id): if thingType(id)!='Graph': self.error='Attempt to groGraph called "'+called+'" but that name is already used, and is not a Graph.' self.logic='oh' return oh # set the Graph we found at attn self.graph=self.graphIndex[id] self.firstNode() self.Logic='fail' return fail else: # should not happen self.error='Attempt to groGraph called "'+called+'" which name is already used, yet is not associated with a graph.' self.Logic='oh' return oh else: pass id=self.newGraphId(called) self.graphs.append(([],id)) self.graph=len(self.graphs)-1 self.node,self.predicate,self.object=0,0,0 # put it in the graph index self.graphIndex[id]=self.graph self.groNode(called) self.Logic='ok' return ok def groNode(self,called=None): # Grow a new node with one empty predicate # The name of the node is passed as the optional argument 'called' # fails: if node already exists, yet moves attn there # seeUrl id=self.newNodeId(called) # get the id for this node if self.nodeIndex.has_key(id): assn=self.nodeIndex[id] for i in assn: if i[0]==self.graph: # go to that node in this graph self.node=i[1] self.predicate,self.object=0,0 # but we failed to gro a new node # because one already existed with that id self.Logic='fail' return fail # move attn to new node self.graphs[self.graph][0].append(([],id)) self.node=len(self.graphs[self.graph][0])-1 self.predicate,self.object=0,0 # add it to the nodeIndex dictionary if self.nodeIndex.has_key(id): self.nodeIndex[id].append((self.graph,self.node)) else: self.nodeIndex[id]=[(self.graph,self.node)] # gro a default empty predicate on it self.groPredicate() self.Logic='ok' return ok def groPredicate(self,property=None): # Grow a new predicate # The property name of the predicate is passed as the optional argument # fails: if predicate already exists on this node, yet moves attn there if property==None: self.graphs[self.graph][0][self.node][0].append(([],None,None)) self.predicate=len(self.graphs[self.graph][0][self.node][0])-1 self.groObject() return ok if self.symTo(property): # predicate already exists on this node, cannot gro a new one self.Logic='fail' return fail self.mem() self.at(property) id=self.graphs[self.graph][0][self.node][1] propertyLocation=self.graph,self.node entry=[],id,propertyLocation self.remem() if self.symTo(None): self.graphs[self.graph][0][self.node][0][self.predicate]=entry else: # gro a empty arrow here self.graphs[self.graph][0][self.node][0].append(entry) self.predicate=len(self.graphs[self.graph][0][self.node][0])-1 self.object=0 self.groObject() self.Logic='ok' return ok def groObject(self,object=None, seq=None): # Gro a new arrow to the object and place attn on the arrow # fails if arrow to object already exists, yet goes there # @@@@@ support for seq missing if object==None: self.graphs[self.graph][0][self.node][0][self.predicate][0].append((0,None,None)) self.object=len(self.graphs[self.graph][0][self.node][0][self.predicate][0])-1 self.Logic='ok' return ok if self.gressTo(object): # object already exists on this node, cannot gro a new one self.Logic='fail' return fail self.mem() self.at(object) id=self.graphs[self.graph][0][self.node][1] objectLocation=self.graph,self.node entry=0,id,objectLocation self.remem() if self.gressTo(None): self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object]=entry else: self.graphs[self.graph][0][self.node][0][self.predicate][0].append(entry) # move attn to new arrow self.object=len(self.graphs[self.graph][0][self.node][0][self.predicate][0])-1 self.Logic='ok' return ok def gro(self,subject,predicate,object,graph=None,seq=None): # @@@@ process graph and seq arguments if present self.groNode(subject) self.groPredicate(predicate) self.groObject(object) # ----------------------------------------------------- # ---------------- READ IN DATA FROM OUTSIDE ---------- # ----------------------------------------------------- def readPentals(self,xstimulus): # returns a list of tuples [(subject,predicate,object,graph,seq,terminal), ...] stmts=parsePentals(xstimulus) #stmts is a list of tuples [(subject,predicate,object,graph,seq,terminal), ...] #stmts is a list of tuples [(s[0] ,s[1] ,s[2] ,s[3],s[4],s[5] ), ...] #@@@ does not process seq and terminal performatives yet for s in stmts: if s[3]!='': self.groGraph(s[3]) self.groNode(s[0]) self.groPredicate(s[1]) self.groObject(s[2]) def passert(self,xstimulus): self.readPentals(xstimulus) # -------------------------------------------------------------------- # METHODS TO RETURN OUTPUT FROM GENERATIVE GRAMMARS # -------------------------------------------------------------------- def nameOf(self,id): if self.idlex.has_key(id): return self.idlex[id] else: return '_:'+repr(id) def startGrammar(self,xgrammar): self.grammarStack=[self.grammar]+self.grammarStack self.grammar=xgrammar def endGrammar(self): try: self.grammar, stack = self.grammarStack[0],self.grammarStack[1:] self.grammarStack=stack except: self.grammar='semenglish' def sayObject(self): return self.grammars[self.grammar]['arrow']\ (self.saySubject(),self.sayProperty(),self.sayObject(),self.sayGraphName()) def sayGraphName(self): return self.nameOf(self.graphs[self.graph][1]) def saySubject(self): return self.nameOf(self.graphs[self.graph][0][self.node][1]) def sayProperty(self): return self.nameOf(self.graphs[self.graph][0][self.node][0][self.predicate][1]) def sayObject(self): object=self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object] if object==None: return self.nameOf(None) elif type(object[2]) is types.TupleType: self.mem() if self.vance(): r=self.saySubject() self.remem() return r else: return self.nameOf(object[2]) def sayPredicate(self): return self.punctuate('predicate', (self.saySubject(),self.sayProperty(),self.sayObjectList(),self.sayGraphName())) def punctuate(self,xevent): if self.grammars[self.grammar].has_key(xevent): return self.grammars[self.grammar][xevent] else: return '' # return '(No production for %s in %s)' % (xevent,self.grammar) def sayObjects(self): self.mem() self.object=0 x=self.sayObject() while self.gress(): x=x+self.punctuate('continueList')+self.sayObject() self.remem() return x def sayPredicates(self): self.mem() self.predicate,self.object=0,0 x=self.grammars['semenglish']['predicate']() while self.sym(): x=x+self.punctuate('continuePredicate')+self.grammars['semenglish']['predicate']() self.remem() return x def sayObjectList(self): x=self.punctuate('listStart') self.mem() self.firstObject() self.Logic='ok' while self.Logic=='ok': x=self.sayObject() if not self.gress(): break x=x+self.punctuate('listContinue') x=x+self.punctuate('listEnd') self.Logic='ok' self.remem() return x def say(self): return self.grammars[self.grammar]['arrow']() def Xsay(self): predicateId=self.graphs[self.graph][0][self.node][0][self.predicate][1] if predicateId==None: return fail self.mem() predicateToken=self.nodeIndex[predicateId][0] print predicateToken, self.sayObject() while self.gress(): print ',', self.sayObject() print self.remem() def sayBal(self): if self.ifEmptyPredicate(): return ok self.mem() self.saySubject() print self.firstPredicate() print ' ', self.say() while self.sym(): print ' ', self.say() # print '.' self.remem() def ifEmptyPredicate(self): if self.graphs[self.graph][0][self.node][0][self.predicate][1]==None: self.Logic='ok' return ok self.Logic='fail' return fail def sayGraph(self): self.mem() self.firstNode() self.sayBal() while self.jace(): self.sayBal() self.remem() def sayGraphServer(self): saveStreams=sys.stdin, sys.stdout f=open('temp.txt', 'w') sys.stdout=f self.sayGraph() f.close() f=open('temp.txt', 'r') s='' for i in f: s=s+i f.close() l=s.replace('<','<') r=l.replace('>','>') return r # ----------------------------------------------------- # ---------------- DEBUGGING TOOLS -------------------- # ----------------------------------------------------- def shownodeIndex(self): a=self.nodeIndex.items() for i in a: print i[0],i[1] def showlexid(self): a=self.lexid.items() for i in a: print i[0],i[1] def showAttn(self): gId=self.graphs[self.graph][1] print 'graph=',gId,self.idlex[gId] nId=self.graphs[self.graph][0][self.node][1] print 'node=',nId,self.idlex[gId] pId=self.graphs[self.graph][0][self.node][0][self.predicate][1] print 'predicate=',pId,self.idlex[gId] object=self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][1] stId=self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][1] seq=self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][0] print 'object=',object,' stId=',stId,' seq=',seq def showGraph(self): self.memo('attn') graphName=self.nameOf(self.graphs[self.graph][1]) print graphName, print self.graphs[self.graph][1] self.firstNode() self.Logic='ok' while self.Logic=='ok': self.showNode() self.jace() self.rememo('attn') def showNode(self): self.mem() nodeName=self.nameOf(self.graphs[self.graph][0][self.node][1]) print ' ', print nodeName, print self.graphs[self.graph][0][self.node][1], print self.attn() self.firstPredicate() self.Logic='ok' while self.Logic=='ok': self.showPredicate() self.sym() self.remem() def showPredicate(self): predicateName=self.nameOf(self.graphs[self.graph][0][self.node][0][self.predicate][1]) print ' ', print predicateName, print self.graphs[self.graph][0][self.node][0][self.predicate][1], print self.graphs[self.graph][0][self.node][0][self.predicate][2] self.showObjects() def showObjects(self): self.mem() self.firstObject() self.Logic='ok' while self.Logic=='ok': objectName=self.nameOf(self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][1]) print ' ', print objectName, print self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][1], print self.graphs[self.graph][0][self.node][0][self.predicate][0][self.object][2],self.showPlace() self.gress() self.remem() return None def showPlace(self): self.mem() if self.attn()==self.atMemo('attn'): self.remem() return '<---' else: self.remem() return '' def dispAttnLogic(self): return (self.graph, self.node, self.predicate, self.object),self.Logic # --------- END WAKE METHODS ------------ # ---------FUNCTIONS IN THE MODULE AGENT FOLLOW---------------- def tokenType(called): # Tokens are elements of text read from external languages # This function classifies tokens # Symbals are tokens that refer to things but are not things in themselves # Symbals must contain at least one alphabetic character and not be enclosed in quotes # Symbals are sometimes called constants or variables in logical languages like KIF or CycL # Digits are numbers # Literals are data to be taken as is --- URI are literals # Punctuation is everything else # seeUrl http://robustai.net/mentography/types.gif if type(called)!=type('a'): return 'NotToken' elif called.isdigit(): return 'Digits' elif called.isalnum(): return 'Symbal' elif called[-1]=='"' or called[-1]=="'": return 'Literal' elif called[0]=='<' and called[-1]==">": return 'Uri' else: return 'Punctuation' def thingType(id): # This function classifies things according to # the high order digit of their id # seeUrl http://robustai.net/mentography/types.gif if type(id)!=type(100000000): return 'NotThing' if id < 100000000 or id > 999999999 : return 'NotThing' thingTypes=('NotThing','Graph','Node','3unused','Arrow','Lex','6unused','Number','Program','9unused') return thingTypes[int(str(id)[0])] def parsePentals(s, t=[]): # This lexical analizer was written by (Sean B Palmer) # revised to speed up and support compoundPhrase # seeUrl # returns a list of tuples [(subject,predicate,object,graph,seq,terminal), ...] t = t[:] ws = r'[ \t]' name = r'(?:\S+)' literal = r'(?:"[^"\\]*(?:\\.[^"\\]*)*")' comp = r'(?:\('+ws+r'*'+name+r'(?:'+ws+r'+'+name+r')+'+ws+r'*\))' term = r'('+literal+r'|'+comp+r'|'+name+r')' rt = r'^'+term+ws+r'+'+term+ws+r'+'+term+r'(?:'+ws+r'+'+term+r')?' \ r'(?:'+ws+r'+'+term+r')?'+ws+r'?([!?.])$' for line in s.replace('\r\n', '\n').replace('\r', '\n').split('\n'): match = re.compile(rt).findall(line)[:1] if match: t.append(match[0]) return t def parseCommand(s): name='[a-zA-Z_]\\w*' literal='(?:"[^"\\\\]*(?:\\\\.[^"\\\\]*)*")' lpara=r'\(' rpara=r'\)' token='('+literal+'|'+lpara+'|'+rpara+'|'+name+')' tokens=re.compile(token).findall(s) tree=[] i=0 hold='donothing' while i