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 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 procedure_task_structure(agent, proc, prefix=""): """Get the task structure of a procedure. If prefix is not "" then assume we are looking at task instances rather than task expressions. Also check for deadlines and only out put structure if there is at least one deadline.""" print "procedure_task_structure%s"%((agent, proc, prefix),) body = proc.closed_zexpr.keyget0("body:") todo = find_first_taskexpr(body) done = [] label_task = {} successors = {} predecessors = {} for expr in todo: predecessors[prefix + expr.label()] = [] while todo: expr = todo.pop() done.append(expr) label = prefix + expr.label() label_task[label] = expr sucs = find_next_taskexpr(expr) #print "next of", expr, "is", sucs successors[label] = [prefix + suc.label() for suc in sucs] for suc in sucs: if suc not in done: todo.append(suc) slabel = prefix + suc.label() try: predecessors[slabel].append(label) except KeyError: predecessors[slabel] = [label] #print successors results = [] is_a_deadline = False for (label, preds) in predecessors.items(): taskexpr = label_task[label] statement = "?" etime = "none" rtime = "none" deadline = "none" try: sym = taskexpr.actexpr.actsym statement = sym.id for (_sym, prop) in agent.factslist1(PROPERTY, sym): #print "Property of %r: %r"%(sym, prop) if isStructure(prop): if prop.functor == AVG_EXEC_TIME: etime = prop[0] elif prop.functor == AVG_RECOVERY_TIME: rtime = prop[0] #print "Deadlines for %r are %r"%(label, agent.factslist1(DEADLINE, label)) if prefix != "": for (_taskid, dl) in agent.factslist1(DEADLINE, label): is_a_deadline = True deadline = dl except AttributeError: print "Not a do task: %s"%taskexpr results.append((label, statement, etime, rtime, deadline, tuple(preds))) if prefix != "" and not is_a_deadline: return None # Shift results across to list if all the predecessors are present #print results #count = len(results) transfered = [] labels = [] while len(transfered) < len(results): for result in results: if result not in transfered: #print "checking result", result, labels for p in result[5]: if p not in labels: #print " pred not transfered", result, p break else: # no p was not in labels #print " transfering", result transfered.append(result) labels.append(result[0]) #count = count - 1 #if not (count >= 0): raise AssertionError, \ # "Too many loops" return tuple(transfered)
def procedure_task_structure(agent, proc, prefix=""): """Get the task structure of a procedure. If prefix is not "" then assume we are looking at task instances rather than task expressions. Also check for deadlines and only out put structure if there is at least one deadline.""" print "procedure_task_structure%s" % ((agent, proc, prefix), ) body = proc.closed_zexpr.keyget0("body:") todo = find_first_taskexpr(body) done = [] label_task = {} successors = {} predecessors = {} for expr in todo: predecessors[prefix + expr.label()] = [] while todo: expr = todo.pop() done.append(expr) label = prefix + expr.label() label_task[label] = expr sucs = find_next_taskexpr(expr) #print "next of", expr, "is", sucs successors[label] = [prefix + suc.label() for suc in sucs] for suc in sucs: if suc not in done: todo.append(suc) slabel = prefix + suc.label() try: predecessors[slabel].append(label) except KeyError: predecessors[slabel] = [label] #print successors results = [] is_a_deadline = False for (label, preds) in predecessors.items(): taskexpr = label_task[label] statement = "?" etime = "none" rtime = "none" deadline = "none" try: sym = taskexpr.actexpr.actsym statement = sym.id for (_sym, prop) in agent.factslist1(PROPERTY, sym): #print "Property of %r: %r"%(sym, prop) if isStructure(prop): if prop.functor == AVG_EXEC_TIME: etime = prop[0] elif prop.functor == AVG_RECOVERY_TIME: rtime = prop[0] #print "Deadlines for %r are %r"%(label, agent.factslist1(DEADLINE, label)) if prefix != "": for (_taskid, dl) in agent.factslist1(DEADLINE, label): is_a_deadline = True deadline = dl except AttributeError: print "Not a do task: %s" % taskexpr results.append( (label, statement, etime, rtime, deadline, tuple(preds))) if prefix != "" and not is_a_deadline: return None # Shift results across to list if all the predecessors are present #print results #count = len(results) transfered = [] labels = [] while len(transfered) < len(results): for result in results: if result not in transfered: #print "checking result", result, labels for p in result[5]: if p not in labels: #print " pred not transfered", result, p break else: # no p was not in labels #print " transfering", result transfered.append(result) labels.append(result[0]) #count = count - 1 #if not (count >= 0): raise AssertionError, \ # "Too many loops" return tuple(transfered)