def builtin_evaluate(agent, value): if (isSymbol(value)): raise AssertionError, \ "A naked symbol is not evaluable" if isString(value): return value elif isList(value): elements = [builtin_evaluate(agent, v) for v in value] return List(elements) elif isInteger(value): return value elif isFloat(value): return value elif isStructure(value): sym = value.functor if sym == BACKQUOTE_SYMBOL: return builtin_quoted(agent, value[0]) else: argvalues = [builtin_evaluate(agent, v) for v in value] fullsym = Symbol(BUILTIN_PACKAGE_NAME + "." + sym.id) imp = agent.getImp(fullsym) #if not (isinstance(imp, FunImpInt)): raise AssertionError, \ # "Not a function: %s"%sym b, z = valuesBZ(argvalues) result = imp.call(agent, b, z) return result else: return value
def builtin_evaluate(agent, value): if isSymbol(value): raise AssertionError, "A naked symbol is not evaluable" if isString(value): return value elif isList(value): elements = [builtin_evaluate(agent, v) for v in value] return List(elements) elif isInteger(value): return value elif isFloat(value): return value elif isStructure(value): sym = value.functor if sym == BACKQUOTE_SYMBOL: return builtin_quoted(agent, value[0]) else: argvalues = [builtin_evaluate(agent, v) for v in value] fullsym = Symbol(BUILTIN_PACKAGE_NAME + "." + sym.id) imp = agent.getImp(fullsym) # if not (isinstance(imp, FunImpInt)): raise AssertionError, \ # "Not a function: %s"%sym b, z = valuesBZ(argvalues) result = imp.call(agent, b, z) return result else: return value
def value_km_str(x): "convert x to a KM string" # TODO: make this work for real if isString(x) or isInteger(x) \ or isFloat(x) or isSymbol(x): return value_str(x) else: return None
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 task_icl_mode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() patexprs = task.getZexpr() if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except AnyException: errid = NEWPM.displayError() return makeIclStr("<cannot handle task %s>"%task) try: return do_task_icl(agent, bindings, patexprs, kind, namesym, mode) except AnyException: errid = NEWPM.displayError() return "<err: %s>"%task
def task_icl_mode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() patexprs = task.getZexpr() if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except AnyException: errid = NEWPM.displayError() return makeIclStr("<cannot handle task %s>" % task) try: return do_task_icl(agent, bindings, patexprs, kind, namesym, mode) except AnyException: errid = NEWPM.displayError() return "<err: %s>" % task
def task_pyMode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() zexpr = task.getZexpr() patexprs = [zexpr[i] for i in range(len(zexpr))] if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except: NEWPM.displayError() return "<cannot handle task %s>" % task try: return do_task_py(agent, bindings, patexprs, kind, namesym, mode) except AnyException: NEWPM.displayError() return "<err: %s>" % task
def task_pyMode(agent, task, mode): try: kind = task.kind_string() namesym = task.goalsym() bindings = task.getBindings() zexpr = task.getZexpr() patexprs = [zexpr[i] for i in range(len(zexpr))] if not (isinstance(kind, basestring)): raise AssertionError if not (isSymbol(namesym)): raise AssertionError except: NEWPM.displayError() return "<cannot handle task %s>"%task try: return do_task_py(agent, bindings, patexprs, kind, namesym, mode) except AnyException: NEWPM.displayError() return "<err: %s>"%task
def encodeXMLValue(sparkValue): "Converts a SPARK value to a python value that can be passed by XML" if isinstance(sparkValue, UNCHANGED_TYPES): return sparkValue if sparkValue == sparkNULL(): return None elif isStructure(sparkValue): d = mapDict(sparkValue, encodeXMLValue) if d != None: return d else: # Use FUNCTOR/ARGS notation return {FUNCTOR:sparkValue.functor.name, ARGS:encodeXMLValues(sparkValue)} elif isList(sparkValue): return encodeXMLValues(sparkValue) elif isSymbol(sparkValue): return {SYM:sparkValue.name} elif isVariable(sparkValue): return {VAR:sparkValue.name} else: raise LowError("Cannot convert python type %r to XML"%sparkValue.__class__)
def oaa_construct(sym, *args): if not isSymbol(sym): raise LowError("oaastruct requires a symbol for its first argument, not %s", value_str(sym)) return Structure(sym, args)
def oaa_construct(sym, *args): if not isSymbol(sym): raise LowError( "oaastruct requires a symbol for its first argument, not %s", value_str(sym)) return Structure(sym, args)