Esempio n. 1
0
File: oaa.py Progetto: jbalint/spark
        return self.base._bindings
    def zexpr(self):
        return self.base._zexpr
    def goalsym(self):
        return self.base._goalsym
    def kind_string(self):
        return "Do"

    def find_tframes(self, agent):
        return self.base._imp.tframes(agent, self, self.base._bindings, self.base._zexpr)

    def getParams(self):
        "Get the OAA parameters supplied with the goal"
        return self.base.getParams()

installConstructor(ESEvent)

class ExternalSolve(External):
    __slots__ = (
        "_oaa",
        "_iclgoal",
        "_params",
        "_id",
        "_zexpr",
        "_bindings",
        "_answerlist",
        "_goalsym",
        "_imp",
        )

    def __init__(self, oaa, iclgoal, params, restartGoalSymbol=None):
Esempio n. 2
0
        pass

    # bindings methods
    def bTermEval(self, agent, zitem):
        return zitem

    def bTermEvalP(self, agent, zitem):
        return True

    def bTermMatch(self, agent, zitem, value):
        return zitem == value

    constructor_mode = ""


installConstructor(ValuesBindings)

VALUES_BINDING = ValuesBindings()


def values_binding():
    return VALUES_BINDING


def valuesBZ(values):
    return (VALUES_BINDING, values)


################################################################

Esempio n. 3
0
class BasicFun(_Basic, FunImpInt):
    __slots__ = ()

    def __init__(self, mode, fun):
        _Basic.__init__(self, mode, fun)
        if self._output_indices: #len(self._output_indices) > 0:
            raise LowError("Mode string %r is not valid for a function", mode)

    def call(self, agent, bindings, zexpr):
        args = self.generate_args(agent, bindings, zexpr)
        return self._function(*args)

    def match_inverse(self, agent, bindings, zexpr, obj):
        raise LowError("Trying to match non-invertible function")

installConstructor(BasicFun)

class ReversibleFun(BasicFun):
    __slots__ = (
        "_inverse",
        )

    def __init__(self, mode, fun, inverse):
        BasicFun.__init__(self, mode, fun)
        self._inverse = make_callable(inverse)

    def match_inverse(self, agent, bindings, zexpr, obj):
        inverse = self._inverse
        # If the function requires the agent, so will the inverse
        if self._input_agent_p:
            result = self._inverse(agent, obj)
Esempio n. 4
0

class ProcClosureBindings(ClosureBindings):  # used by {proc [<args] ...}
    __slots__ = ()

    def rebindVariables(self, agent):
        vdict = self._variables
        for varname in vdict:
            if isinstance(varname, basestring):  # constraint
                value = vdict.get(Variable(varname))
                if value is not None:
                    con = vdict[varname]
                    matchValueToConstraint(agent, value, con, False)


installConstructor(ProcClosureBindings)


class ProcedureBindings(ProcClosureBindings):  # used by {defprocedure ...}
    __slots__ = ()

    def bformals(self, agent, bindings, zexpr, match):
        # non-standard
        fzitem = self._closureValue.closed_zexpr.keyget0("cue:")[0][0]
        if formals(agent, self, fzitem, bindings, zexpr, match):
            #print "cue matched", fzitem, self._variables
            return self
        else:
            return None

Esempio n. 5
0
    def level(self):
        return self._level

    def setLevel(self, level):
        self._level = level

    ################
    # ConstructibleValue methods

    def constructor_args(self):
        return [self._closureValue, self._level]

    constructor_mode = "VV"


installConstructor(ClosureBindings)

################################################################
# Task closure


class TaskClosureBindings(ClosureBindings):
    __slots__ = ()


installConstructor(TaskClosureBindings)


class TaskClosureValue(ClosureValue, ActImpInt):
    __slots__ = ()
    make_bindings_class = TaskClosureBindings
Esempio n. 6
0
        pass

    # bindings methods
    def bTermEval(self, agent, zitem):
        return zitem

    def bTermEvalP(self, agent, zitem):
        return True

    def bTermMatch(self, agent, zitem, value):
        return zitem == value

    constructor_mode = ""


installConstructor(ValuesBindings)

VALUES_BINDING = ValuesBindings()


def values_binding():
    return VALUES_BINDING


def valuesBZ(values):
    return (VALUES_BINDING, values)


################################################################

################
Esempio n. 7
0
File: oaa.py Progetto: jbalint/spark
    def goalsym(self):
        return self.base._goalsym

    def kind_string(self):
        return "Do"

    def find_tframes(self, agent):
        return self.base._imp.tframes(agent, self, self.base._bindings,
                                      self.base._zexpr)

    def getParams(self):
        "Get the OAA parameters supplied with the goal"
        return self.base.getParams()


installConstructor(ESEvent)


class ExternalSolve(External):
    __slots__ = (
        "_oaa",
        "_iclgoal",
        "_params",
        "_id",
        "_zexpr",
        "_bindings",
        "_answerlist",
        "_goalsym",
        "_imp",
    )
Esempio n. 8
0
        raise Unimplemented()


class ProcClosureBindings(ClosureBindings): # used by {proc [<args] ...}
    __slots__ = ()

    def rebindVariables(self, agent):
        vdict = self._variables
        for varname in vdict:
            if isinstance(varname, basestring): # constraint
                value = vdict.get(Variable(varname))
                if value is not None:
                    con = vdict[varname]
                    matchValueToConstraint(agent, value, con, False)

installConstructor(ProcClosureBindings)

class ProcedureBindings(ProcClosureBindings): # used by {defprocedure ...}
    __slots__ = ()

    def bformals(self, agent, bindings, zexpr, match):
        # non-standard
        fzitem = self._closureValue.closed_zexpr.keyget0("cue:")[0][0]
        if formals(agent, self, fzitem, bindings, zexpr, match):
            #print "cue matched", fzitem, self._variables
            return self
        else:
            return None

installConstructor(ProcedureBindings)
Esempio n. 9
0
        if formals(agent, self, fzitem, bindings, zexpr, match):
            return self
        else:
            return None
    def level(self):
        return self._level
    def setLevel(self, level):
        self._level = level
    ################
    # ConstructibleValue methods
    
    def constructor_args(self):
        return [self._closureValue, self._level]
    constructor_mode = "VV"

installConstructor(ClosureBindings)

################################################################
# Task closure

class TaskClosureBindings(ClosureBindings):
    __slots__ = ()
installConstructor(TaskClosureBindings)

class TaskClosureValue(ClosureValue, ActImpInt):
    __slots__ = ()
    make_bindings_class = TaskClosureBindings

    def tframes(self, agent, event, bindings, zexpr):
        b = self.make_bindings(agent, bindings, zexpr)
        #XXX needs a better name. update (kwc): using agent.nextid() now
Esempio n. 10
0
#ADVICE = Symbol("spark.builtin.Advice")

ADVICE_WEIGHT = {"strong_prefer":10000,
                 "strong_avoid":-10000,
                 "prefer":100,
                 "avoid":-100,
                 "weak_prefer":1,
                 "weak_avoid":-1}


class AppliedAdviceEvent(GoalMetaEvent):
    __slots__ = ()
    numargs = 2
    event_type = APPLIED_ADVICE
installConstructor(AppliedAdviceEvent)

def calc_weight(kind):
    if isinstance(kind, IntType):
        weight = kind
    else:
        weight = ADVICE_WEIGHT[kind.id]
    return weight
    
def resolveAdvice(agent, tframes, advice, event):
    #print "resolveAdvice", (agent, tframes, advice, event)
    values = {}
    for tframe in tframes:
        values[tframe] = 0
    for (tf, label, kind) in advice:
        values[tf] = values[tf] + calc_weight(kind)
Esempio n. 11
0
            facts = agent.factslist1(P_Features, sym)
            if facts:
                return facts[0][1]
            else:
                return ()
        except:
            NEWPM.displayError()
            return ()

    def constructor_args(self):
        return [self._name]

    constructor_mode = "V"


installConstructor(SparkEvent)


class ObjectiveEvent(SparkEvent):
    __slots__ = ("base", )
    name_base = "Objective"
    cvcategory = "EO"

    def __init__(self, base):
        SparkEvent.__init__(self, None)
        self.base = base

    def constructor_args(self):
        return [self.base]

    constructor_mode = "I"
Esempio n. 12
0
            if sym.modname is None:  # DNM - Hack for @exteval
                return ()
            facts = agent.factslist1(P_Features, sym)
            if facts:
                return facts[0][1]
            else:
                return ()
        except:
            NEWPM.displayError()
            return ()

    def constructor_args(self):
        return [self._name]
    constructor_mode = "V"

installConstructor(SparkEvent)

class ObjectiveEvent(SparkEvent):
    __slots__ = ("base",)
    name_base = "Objective"
    cvcategory = "EO"
    def __init__(self, base):
        SparkEvent.__init__(self, None)
        self.base = base

    def constructor_args(self):
        return [self.base]
    constructor_mode = "I"

    def e_sync_parent(self):
        # This is an asynchronous event
Esempio n. 13
0
        self.zexpr = zexpr

    def constructor_args(self):
        return TFrame.constructor_args(self) + [self.bindings, self.zexpr]
    constructor_mode = "VIIV"
    
    def tfcont(self, agent):
        result = task_context(self.event())
        if result is None:
            sf = MessageFailure(self, "GetSelfTask failed")
        elif not termMatch(agent, self.bindings, self.zexpr[0], result):
            sf = MessageFailure(self, "Result of GetSelfTask did not match output pattern")
        else:
            sf = SUCCESS
        self.tf_set_completed(agent, sf)
installConstructor(GetSelfTaskTFrame)

class GetSelfTask(ActImpInt):
    __slots__ = ()
    def __init__(self, symbol):
        pass
    def tframes(self, agent, event, bindings, zexpr):
        name = "GetSelfTask%d"%agent.nextid()
        return [GetSelfTaskTFrame(name, event, bindings, zexpr)]


getSelfTask = GetSelfTask

class GetSelfProcInstTFrame(TFrame):
    __slots__ = ("bindings", "zexpr",)
Esempio n. 14
0
    def rebind_parameters(self, agent):
        pass

    def constructor_args(self):
        return [self._name, self._event]

    constructor_mode = "VI"

    def state_args(self):
        if self._completed is not None:
            return (self._completed, )

    def set_state(self, ignore_agent, completed):
        self._completed = completed

installConstructor(TFrame)


def ancestor_tframes(event_or_tframe):
    result = []
    if isinstance(event_or_tframe, SparkEvent):
        tf = event_or_tframe.e_sync_parent()
    elif isinstance(event_or_tframe, TFrame):
        tf = event_or_tframe.tf_sync_parent()
    else:
        raise Exception("Invalid argument to ancestor_tframes: %s" %
                        event_or_tframe)
    while tf is not None:
        if not isinstance(tf.event(), PseudoGoalEvent):
            result.append(tf)
        tf = tf.tf_sync_parent()
Esempio n. 15
0
        pass

    def constructor_args(self):
        return [self._name, self._event]

    constructor_mode = "VI"

    def state_args(self):
        if self._completed is not None:
            return (self._completed,)

    def set_state(self, ignore_agent, completed):
        self._completed = completed


installConstructor(TFrame)


def ancestor_tframes(event_or_tframe):
    result = []
    if isinstance(event_or_tframe, SparkEvent):
        tf = event_or_tframe.e_sync_parent()
    elif isinstance(event_or_tframe, TFrame):
        tf = event_or_tframe.tf_sync_parent()
    else:
        raise Exception("Invalid argument to ancestor_tframes: %s" % event_or_tframe)
    while tf is not None:
        if not isinstance(tf.event(), PseudoGoalEvent):
            result.append(tf)
        tf = tf.tf_sync_parent()
    return result
Esempio n. 16
0
        self._contents[index] = value
    def __contains__(self, elt):
        return elt in self._contents
    def set(self, seq):
        self._contents[:] = seq
    def append(self, elt):
        self._contents.append(elt)
    def remove(self, elt):
        return self._contents.remove(elt)
    def pop(self, index = -1):
        return self._contents.pop(index)
    def reverse(self):
        self._contents.reverse()
    def coerceToSPARKForOAA(self):
        return List(self._contents)
installConstructor(Accumulator)

accumulator_append = Accumulator.append
accumulator_getitem = Accumulator.__getitem__
accumulator_setitem = Accumulator.__setitem__
accumulator_delitem = Accumulator.__delitem__
accumulator_set = Accumulator.set

def accumulator_values(acc):
    return List(acc[:])

def accumulator_set_key_value(acc, key, value):
    "Remove any existing key-value pair for key and add a new key value pair"
    contents = acc._contents
    newpair = List((key, value))
    for i in range(len(contents)):
Esempio n. 17
0
    constructor_mode = "VIIV"

    def tfcont(self, agent):
        result = task_context(self.event())
        if result is None:
            sf = MessageFailure(self, "GetSelfTask failed")
        elif not termMatch(agent, self.bindings, self.zexpr[0], result):
            sf = MessageFailure(
                self, "Result of GetSelfTask did not match output pattern")
        else:
            sf = SUCCESS
        self.tf_set_completed(agent, sf)


installConstructor(GetSelfTaskTFrame)


class GetSelfTask(ActImpInt):
    __slots__ = ()

    def __init__(self, symbol):
        pass

    def tframes(self, agent, event, bindings, zexpr):
        name = "GetSelfTask%d" % agent.nextid()
        return [GetSelfTaskTFrame(name, event, bindings, zexpr)]


getSelfTask = GetSelfTask