Exemple #1
0
    def execute(self, agent, intention):
        # Boolean constants.
        term = pyson.evaluate(self.term, intention.scope)
        if term is True:
            yield
            return
        elif term is False:
            return

        try:
            group = term.literal_group()
        except AttributeError:
            raise PysonError(
                "expected boolean or literal in query context, got: '%s'" %
                term)

        # Query on the belief base.
        for belief in agent.beliefs[group]:
            for _ in pyson.unify_annotated(term, belief, intention.scope,
                                           intention.stack):
                yield

        choicepoint = object()

        # Follow rules.
        for rule in agent.rules[group]:
            rule = copy.deepcopy(rule)

            intention.stack.append(choicepoint)

            if pyson.unify(term, rule.head, intention.scope, intention.stack):
                for _ in rule.query.execute(agent, intention):
                    yield

            pyson.reroll(intention.scope, intention.stack, choicepoint)
Exemple #2
0
    def execute(self, agent, intention):
        choicepoint = object()
        intention.stack.append(choicepoint)

        success = any(True for _ in self.query.execute(agent, intention))

        pyson.reroll(intention.scope, intention.stack, choicepoint)

        if not success:
            yield
Exemple #3
0
def _range_2(agent, term, intention):
    choicepoint = object()

    for i in range(int(pyson.grounded(term.args[0], intention.scope))):
        intention.stack.append(choicepoint)

        if pyson.unify(term.args[1], i, intention.scope, intention.stack):
            yield

        pyson.reroll(intention.scope, intention.stack, choicepoint)
Exemple #4
0
def _member(agent, term, intention):
    choicepoint = object()

    for member in pyson.evaluate(term.args[1], intention.scope):
        intention.stack.append(choicepoint)

        if pyson.unify(term.args[0], member, intention.scope, intention.stack):
            yield

        pyson.reroll(intention.scope, intention.stack, choicepoint)
Exemple #5
0
def _count(agent, term, intention):
    query = pyson.runtime.TermQuery(term.args[0])

    choicepoint = object()
    count = 0
    intention.stack.append(choicepoint)
    for _ in query.execute(agent, intention):
        count += 1
    pyson.reroll(intention.scope, intention.stack, choicepoint)

    if pyson.unify(count, term.args[1], intention.scope, intention.stack):
        yield
Exemple #6
0
def _substring(agent, term, intention):
    needle = pyson_str(pyson.grounded(term.args[0], intention.scope))
    haystack = pyson_str(pyson.grounded(term.args[1], intention.scope))

    choicepoint = object()

    pos = haystack.find(needle)
    while pos != -1:
        intention.stack.append(choicepoint)

        if pyson.unify(term.args[2], pos, intention.scope, intention.stack):
            yield

        pyson.reroll(intention.scope, intention.stack, choicepoint)
        pos = haystack.find(needle, pos + 1)
Exemple #7
0
    def remove_belief(self, term, intention):
        term = pyson.evaluate(term, intention.scope)

        try:
            group = term.literal_group()
        except AttributeError:
            raise PysonError("expected belief literal, got: '%s'" % term)

        choicepoint = object()

        relevant_beliefs = self.beliefs[group]

        for belief in relevant_beliefs:
            intention.stack.append(choicepoint)

            if pyson.unify(term, belief, intention.scope, intention.stack):
                relevant_beliefs.remove(belief)
                return True

            pyson.reroll(intention.scope, intention.stack, choicepoint)

        return False
Exemple #8
0
def pop_choicepoint(agent, intention):
    choicepoint = intention.choicepoint_stack.pop()
    pyson.reroll(intention.scope, intention.stack, choicepoint)
    return True