Skip to content

Commit 17a41c0

Browse files
committed
The annotations are now literal
1 parent b1393aa commit 17a41c0

File tree

7 files changed

+74
-27
lines changed

7 files changed

+74
-27
lines changed

agentspeak/__init__.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,6 @@ def unify(self, right, scope, stack):
564564

565565
if isinstance(right, Var):
566566
return right.unify(self, scope, stack)
567-
568567
try:
569568
if self.functor != right.functor:
570569
return False
@@ -573,12 +572,11 @@ def unify(self, right, scope, stack):
573572
return False
574573
except AttributeError:
575574
return False
576-
575+
577576
return all(unify(l, r, scope, stack) for l, r in zip(self.args, right.args))
578577

579578
def unify_annotated(self, right, scope, stack):
580579
right = evaluate(right, scope)
581-
582580
if isinstance(right, Var):
583581
choicepoint = object()
584582
if right.unify(self, scope, stack):
@@ -596,7 +594,6 @@ def unify_annotated(self, right, scope, stack):
596594
for right_annot in right.annots:
597595
if right_annot in closed_right_annots:
598596
continue
599-
600597
for _ in unify_annotated(left_annot, right_annot, scope, stack):
601598
found_right = True
602599
closed_right_annots.add(right_annot)
@@ -837,9 +834,7 @@ def unify_annotated(left, right, scope, stack):
837834
reroll(scope, stack, choicepoint)
838835

839836

840-
def unifies_annotated(left, right):
841-
scope = {}
842-
stack = collections.deque()
837+
def unifies_annotated(left, right, scope = {}, stack = collections.deque()):
843838
for _ in unify_annotated(left, right, scope, stack):
844839
return True
845840
return False

agentspeak/parser.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ def __str__(self):
233233
class AstPlan(AstNode):
234234
def __init__(self):
235235
super(AstPlan, self).__init__()
236-
self.annotations = []
237-
self.dicts_annotations = None
236+
self.annotation = None
237+
self.annotation_terms = None
238238
self.event = None
239239
self.context = None
240240
self.body = None
@@ -249,9 +249,9 @@ def signature(self):
249249
def __str__(self):
250250
builder = []
251251

252-
for annotation in self.annotations:
252+
if self.annotation is not None:
253253
builder.append("@")
254-
builder.append(str(annotation))
254+
builder.append(str(self.annotation))
255255
builder.append("\n")
256256

257257
builder.append(str(self.event))
@@ -902,11 +902,12 @@ def parse_event(tok, tokens, log):
902902

903903
def parse_plan(tok, tokens, log):
904904
plan = AstPlan()
905-
while tok.lexeme == "@":
905+
if tok.lexeme == "@":
906906
tok = next(tokens)
907-
907+
print('HOLAAAA')
908908
tok, annotation = parse_literal(tok, tokens, log)
909-
plan.annotations.append(annotation)
909+
plan.annotation = annotation
910+
plan.annotation_terms = annotation.annotations
910911

911912
tok, event = parse_event(tok, tokens, log)
912913
plan.event = event
@@ -1355,7 +1356,8 @@ def visit_event(self, ast_event):
13551356
return ast_event
13561357

13571358
def visit_plan(self, ast_plan):
1358-
ast_plan.annotations = [annotation.accept(TermFoldVisitor(self.log)) for annotation in ast_plan.annotations]
1359+
if ast_plan.annotation is not None:
1360+
ast_plan.annotation = ast_plan.annotation.accept(TermFoldVisitor(self.log))
13591361
ast_plan.event = ast_plan.event.accept(self)
13601362
ast_plan.context = ast_plan.context.accept(LogicalFoldVisitor(self.log)) if ast_plan.context else None
13611363
ast_plan.body = ast_plan.body.accept(self) if ast_plan.body else None
@@ -1422,9 +1424,9 @@ def validate(ast_agent, log):
14221424
for op in plan.event.head.accept(FindOpVisitor()):
14231425
log.error("plan head is supposed to be unifiable, but contains non-const expression", loc=op.loc, extra_locs=[plan.loc])
14241426

1425-
for annotation in plan.annotations:
1426-
# Warning annotations ignored
1427-
log.warning("plan annotations are ignored as of yet", loc=annotation.loc, extra_locs=[plan.loc])
1427+
if plan.annotation is not None:
1428+
for op in plan.annotation.accept(FindOpVisitor()):
1429+
log.warning("plan annotations can not contain this expression", loc=op.loc, extra_locs=[plan.loc])
14281430

14291431
if plan.event.goal_type != GoalType.belief and plan.event.trigger == Trigger.removal:
14301432
log.warning("recovery plans are ignored as of yet", loc=plan.loc)

agentspeak/runtime.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,8 @@ def _tell_how(self, term):
541541
ast_plan.body.accept(BuildInstructionsVisitor(variables, actions, body, log))
542542

543543
# Converts the Astplan to Plan
544-
plan = Plan(ast_plan.event.trigger, ast_plan.event.goal_type, head, context, body, ast_plan.body, ast_plan.dicts_annotations)
544+
545+
plan = Plan(ast_plan.event.trigger, ast_plan.event.goal_type, head, context, body, ast_plan.body, ast_plan.annotation)
545546

546547
plan.args = [str(i) for i in ast_plan.event.head.terms] + [str(j) for i in ast_plan.event.head.annotations for j in i.terms]
547548

@@ -648,14 +649,11 @@ def remove_belief(self, term, intention):
648649
choicepoint = object()
649650

650651
relevant_beliefs = self.beliefs[group]
651-
652652
for belief in relevant_beliefs:
653653
intention.stack.append(choicepoint)
654-
655-
if agentspeak.unify(term, belief, intention.scope, intention.stack):
654+
if agentspeak.unifies_annotated(term, belief, intention.scope, intention.stack):
656655
relevant_beliefs.remove(belief)
657656
return True
658-
659657
agentspeak.reroll(intention.scope, intention.stack, choicepoint)
660658

661659
return False
@@ -740,15 +738,15 @@ def plan_to_str(plan):
740738
context = plan.context
741739

742740
body = plan.str_body
743-
741+
744742
if len(plan.head.args):
745743
pattern = r"_X_[0-9a-fA-F]{3}_[0-9a-fA-F]+"
746744
head = re.sub(pattern, lambda m: plan.args.pop(0), str(plan.head))
747745
else:
748746
head = str(plan.head)
749747

750748
if plan.annotation:
751-
label = str(plan.annotation[0])
749+
label = str(plan.annotation)
752750
else:
753751
label = ""
754752
return f"{plan.trigger.value}{plan.goal_type.value}{head} : {context} <- {body}."
@@ -802,7 +800,7 @@ def build_agent_from_ast(self, source, ast_agent, actions, agent_cls=Agent, name
802800

803801
str_body = str(ast_plan.body)
804802

805-
plan = Plan(ast_plan.event.trigger, ast_plan.event.goal_type, head, context, body, ast_plan.body, ast_plan.annotations)
803+
plan = Plan(ast_plan.event.trigger, ast_plan.event.goal_type, head, context, body, ast_plan.body, ast_plan.annotation)
806804

807805
plan.args = [str(i) for i in ast_plan.event.head.terms] + [str(j) for i in ast_plan.event.head.annotations for j in i.terms]
808806

agentspeak/stdlib.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ def _send(agent, term, intention):
139139
message = agentspeak.Literal("plain_text", (term.args[2], ), frozenset())
140140
else:
141141
message = agentspeak.freeze(term.args[2], intention.scope, {})
142-
143142

144143
tagged_message = message.with_annotation(
145144
agentspeak.Literal("source", (agentspeak.Literal(agent.name), )))

examples/annotations/receiver.asl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
!start.
3+
4+
@p1[hello(world),time(sunny)]
5+
+!start
6+
<-
7+
.print("hello world").
8+
9+
+!start
10+
<-
11+
.print("by world").
12+
13+
+hello(world)[source(sender1)]
14+
<-
15+
.print("I receive a message from sender1");
16+
-hello(world)[source(sender1)];
17+
-hello(world);
18+
!stop.
19+
20+
+hello(world)[source(S)]
21+
<-
22+
.print("I receive a message from", S).
23+
24+
25+
+!stop: hello(world)[source(S)]
26+
<-
27+
.print("almost one belief remains").
28+
29+
+!stop
30+
<-
31+
.print("all delete").

examples/annotations/sender.asl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!start.
2+
3+
+!start
4+
<-
5+
.send(receiver,tell, hello(world)).

examples/annotations/test.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
5+
import agentspeak.runtime
6+
import agentspeak.stdlib
7+
8+
env = agentspeak.runtime.Environment()
9+
10+
with open(os.path.join(os.path.dirname(__file__), "sender.asl")) as source:
11+
agents = env.build_agents(source, 2, agentspeak.stdlib.actions)
12+
13+
with open(os.path.join(os.path.dirname(__file__), "receiver.asl")) as source:
14+
agents.append(env.build_agent(source, agentspeak.stdlib.actions))
15+
16+
if __name__ == "__main__":
17+
env.run()

0 commit comments

Comments
 (0)