Exemplo n.º 1
0
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
Exemplo n.º 2
0
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
Exemplo n.º 3
0
Arquivo: ppl.py Projeto: jbalint/spark
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
Exemplo n.º 4
0
Arquivo: ppl.py Projeto: jbalint/spark
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
Exemplo n.º 5
0
Arquivo: oaa.py Projeto: jbalint/spark
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)
Exemplo n.º 6
0
Arquivo: oaa.py Projeto: jbalint/spark
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)
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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__)
Exemplo n.º 12
0
Arquivo: oaa.py Projeto: jbalint/spark
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)
Exemplo n.º 13
0
Arquivo: oaa.py Projeto: jbalint/spark
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)