def _sbi_pi_arraylist_rep(agent, task, pi_name): """generate icl representation for a tframe, return ArrayList""" task_id = IclStruct('task_id', IclStr(task.name())) # DNM - CHANGE TO objectId task_icl_rep = task_icl(agent, task) procedure_name = IclStruct('procedure', IclStr(pi_name)) return [task_id, task_icl_rep, procedure_name]
def value_to_icl(x): # DOESN'T HANDLE INFINITE STRUCTURES WELL "Map a value to an ICL object" if isinstance(x, types.IntType): return IclInt(x) elif isinstance(x, types.LongType): return IclInt(x) elif isinstance(x, types.FloatType): return IclFloat(x) elif isinstance(x, basestring): try: return IclStr(str(x)) except: #OAA has a hard-limit on string length return IclDataQ(String(str(x)).getBytes()) elif isinstance(x, types.TupleType): al = ArrayList(len(x)) for elt in x: al.add(value_to_icl(elt)) return IclList(al) elif isStructure(x): s = x.functor.name nargs = len(x) if nargs == 0: # treat as '@@'("absname") args = (s, ) s = NULLARY_FUNCTOR else: args = x al = ArrayList(nargs) for elt in args: al.add(value_to_icl(elt)) return IclStruct(s, al) elif isinstance(x, IclTerm): return x elif isVariable(x) and x.isLocal(): return ICL_CONSTRUCTOR.createVariable(-1, -1, x.name) # name = x.name # if name[1] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ": # return IclVar(name[1:]) # else: # return IclVar("_"+name[1:]) elif isSymbol(x): # treat as '@'("absname") al = ArrayList(1) al.add(IclStr(x.name)) return IclStruct(ATOM_FUNCTOR, al) elif x is None: # treat as '#'(0) al = ArrayList(1) al.add(IclInt(0)) return IclStruct(REF_FUNCTOR, al) elif hasattr(x, "coerceToSPARKForOAA"): return value_to_icl(x.coerceToSPARKForOAA()) else: # treat as '#'(<id>) id = getId(x) print "Unusual object type=%s id=%s being passed to OAA: %r" \ % (type(x), id, x) al = ArrayList(1) al.add(IclInt(id)) return IclStruct(REF_FUNCTOR, al)
def oaaStart(agent, opt_host, opt_port, opt_agentname): if opt_host is None: opt_host = "" if opt_port is None: opt_port = 0 if opt_agentname is None: opt_agentname = "RandomAgent" solvablefacts = agent.factslist0(Solvable) al = ArrayList(len(solvablefacts)) # may need more if multiple arities debug("solvablefacts= %s", solvablefacts) #debug("act_arity_imp= %s/%s", act_arity_imp, ACT_ARITY) #debug("TEST") #debug("pred_imp=%s", current_agent(bindings)._pred_imp) for (str, sym) in solvablefacts: #from spark.internal.parse.newparse import symbol_get_syminfo decl = agent.getDecl(sym) minarity = decl.modes[0].sigreq if decl.modes[0].sigrep: maxarity = minarity + 8 else: maxarity = minarity for arity in range(minarity, maxarity + 1): #debug("Adding %s/%s", str, arity) args = ArrayList(arity) for i in range(arity): args.add(IclVar("_" + ` i `)) al.add(IclStruct(str, args)) # THREADRUN solvables = IclList(al) if opt_host == "": hosticl = IclVar("Host") else: hosticl = IclStr(opt_host) if opt_port == 0: porticl = IclVar("Port") else: porticl = IclInt(opt_port) address = IclStruct("tcp", hosticl, porticl) libcom = LibCom(LibComTcpProtocol(), []) oaa = LibOaa(libcom) if not libcom.comConnect(CONNECTION_ID, address, EMPTY_ICL_LIST): raise OAAError("Cannot connect to %s" % address) # register callback #print "Registering callback" oaa.oaaRegisterCallback(APP_DO_EVENT, \ OAASolveEventListener(oaa, agent)) # register solvables #print "Registering solvables", solvables oaa.oaaRegister(CONNECTION_ID, opt_agentname, solvables, \ EMPTY_ICL_LIST) # call ready #print "Calling oaaReady" oaa.oaaReady(True) # print messages # EPILOGUE _set_oaa(agent, oaa) return [opt_host, opt_port, opt_agentname]
def createStructure(self, start, end, functor_name, args): nargs = len(args) if nargs == 0: # treat as '@@'("absname") args = (IclString(functor_name), ) functor_name = NULLARY_FUNCTOR nargs = 1 al = ArrayList(nargs) for elt in args: al.add(elt) return IclStruct(functor_name, al)
def _sbi_task_icl_rep(agent, task, pi_name): """generate icl representation for a task with no tframe, return IclList. subroutine for _sub_build_intention_icl""" #assert(task._tframe is None) event_icl_list = _sbi_pi_arraylist_rep(agent, task, pi_name) #return intention with no subtasks return IclStruct('intention', event_icl_list[0], \ event_icl_list[1], event_icl_list[2], \ IclList(ArrayList()))
def construct_goal(agent, namestring, args): al = ArrayList(len(args)) varindex = 0 for arg in args: if arg is not None: #debug("Adding %s", arg) al.add(value_to_icl(arg)) else: #debug("Adding var") al.add(IclVar("Var_%d" % varindex)) varindex = varindex + 1 goal = IclStruct(namestring, al) oaa = _oaa(agent) return goal
def add_solution(self, agent): #print "ExternalSolve found a solution" zexpr = self._zexpr bindings = self._bindings goal = self._iclgoal nargs = len(zexpr) answerargs = ArrayList(nargs) for zitem in zexpr: result = termEvalEnd(agent, bindings, zitem) answerargs.add(value_to_icl(result)) # TODO: not optimized #results = termEvalEnd(agent, bindings, zexpr) #for i in range(nargs): # if termEvalP(agent, bindings, zexpr[i]): # answerargs.add(goal.getTerm(i)) # else: # answerargs.add(value_to_icl(results[i])) self._answerlist.add(IclStruct(getFunctor(goal), answerargs))
def createSymbol(self, start, end, string): al = ArrayList(1) al.add(IclStr(string)) return IclStruct(ATOM_FUNCTOR, al)
def _sub_build_intention_icl(agent, tframe): """builds the IclList representation of a single intention/taskexprtframe. subroutine of build_intention_icl""" ################################################## # locals: # intention_rep: holds the list representation # of the the intention as it is being constructed. # it has all of the IclStruct params except the subtasks # subtask_list: holds a list of IclLists that represent subtasks # subtasks: list of the subevents of the current tframe # tframe_children: list of child tframes according to the agent ################################################## ################################################## # setup the intention representation, if necessary ################################################## intention_rep = None #print "current tframe", tframe.name(), tframe.__class__.__name__ if isinstance(tframe, TaskExprTFrame) and not isinstance( tframe.event(), PseudoGoalEvent): name = tframe_procedure_instance(tframe).name() intention_rep = _sbi_pi_arraylist_rep(agent, tframe.event(), name) #note: at this point, we still need to fill in the children slot ################################################## # build list of sub-tasks ################################################## subtask_list = [] # - traverse goals w/o pi's first subtasks = procedure_instance_subtasks(agent, tframe) for task in subtasks: #print "examining task: ", task.name(), task.__class__.__name__, task._tframe # look for tasks that don't have a tframe yet and # add their descriptions if isinstance(task, GoalEvent) and (task._tframe is None or isinstance( task._tframe, BasicImpTFrame)): # currently signify no proc with an empty stringx # appends an IclList subtask_list.append(_sbi_task_icl_rep(agent, task, "")) # - traverse tframe children tframe_children = agent.tframe_children(tframe) for child in tframe_children: #print "examining frame: ", child.name() subtree = _sub_build_intention_icl(agent, child) if subtree is not None: subtask_list.extend(subtree) ################################################## # compute return value ################################################## # if we have a taskexpr tframe (intention_rep not None), # put the subtask list into the children slot if intention_rep is not None: subtask_arraylist = _sbi_list_arraylist(subtask_list) return [IclStruct('intention', intention_rep[0], \ intention_rep[1], intention_rep[2], IclList(subtask_arraylist))] # we don't have a taskexpr tframe, but still include the task children in the # icl representation else: return subtask_list
def makeIclStruct(functor_name, args): from spark.io.oaa import ArrayList, IclStruct al = ArrayList(len(args)) for elt in args: al.add(elt) return IclStruct(functor_name, al)