Skip to content

Commit 24453bb

Browse files
committed
✨ Using agent_name to filter msgs
1 parent 6f11575 commit 24453bb

File tree

4 files changed

+67
-51
lines changed

4 files changed

+67
-51
lines changed

jason_ros/src/jason_ros/hw_bridge.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ def arg_parser():
2323

2424

2525
def act(msg, args):
26-
action_controller = args[0]
27-
pub = args[1]
26+
if args[2] == "" or args[2]== msg.agent_name:
27+
action_controller = args[0]
28+
pub = args[1]
2829

29-
action_status = jason_ros_msgs.msg.ActionStatus()
30-
action_status.id = msg.header.seq
30+
action_status = jason_ros_msgs.msg.ActionStatus()
31+
action_status.id = msg.header.seq
32+
action_status.agent_name = msg.agent_name
3133

32-
action_status.result = action_controller.perform_action(
33-
msg.action_name, msg.parameters)
34+
action_status.result = action_controller.perform_action(
35+
msg.action_name, msg.parameters)
3436

35-
pub.publish(action_status)
37+
pub.publish(action_status)
3638

3739

3840
def main():

jason_ros/src/jason_ros/hw_controller.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ def read_manifest(self, *args):
191191
if man_path.is_file():
192192
reader.read(str(man_path))
193193
self.get_info(reader)
194+
else:
195+
print("Can't find: ", man_path)
196+
194197

195198
def get_info(self, reader):
196199
comm_list = reader.sections()
@@ -248,14 +251,15 @@ def perform_action(self, action_name, params):
248251

249252

250253
class PerceptionController(CommController):
251-
def __init__(self):
254+
def __init__(self, agent_name):
252255
CommController.__init__(self)
253256
self.default_path = "perceptions_manifest"
254257
self.subsciber_dict = dict()
255258
self.perceptions = dict()
256-
self.rate = None
259+
self.rate = 30
257260
self.p_event = Event()
258261
self.p_lock = RLock()
262+
self.agent_name = agent_name
259263

260264
def get_info(self, reader):
261265
default_section = reader.defaults()
@@ -315,6 +319,7 @@ def subscriber_callback(self, msg, name):
315319
[string.replace("'", "") for string in map(str, perception_param)]
316320

317321
perception = jason_ros_msgs.msg.Perception()
322+
perception.agent_name = self.agent_name
318323
perception.perception_name = name
319324
perception.parameters = perception_param
320325
if(self.comm_dict[name].buf == "add"):

jason_ros/src/rosjava_agents/src/java/jasonros/RosArch.java

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -76,58 +76,61 @@ public void reasoningCycleStarting() {
7676
public List<Literal> perceive() {
7777
jason_ros_msgs.Perception perception = rosNode.getPerception();
7878
while (perception != null) {
79-
TransitionSystem ts = getTS();
80-
Literal bel = createLiteral(perception.getPerceptionName());
81-
bel.addAnnot(ts.getAg().getBB().TPercept);
79+
String agent_name = perception.getAgentName();
80+
if(agent_name.equals(this.getAgName())){
81+
TransitionSystem ts = getTS();
82+
Literal bel = createLiteral(perception.getPerceptionName());
83+
bel.addAnnot(ts.getAg().getBB().TPercept);
8284

83-
for (String param : perception.getParameters()) {
85+
for (String param : perception.getParameters()) {
8486
try {
85-
Term p = parseTerm(param);
86-
if (p.isVar()) {
87-
p = new StringTermImpl(param);
88-
}
89-
bel.addTerm(p);
87+
Term p = parseTerm(param);
88+
if (p.isVar()) {
89+
p = new StringTermImpl(param);
90+
}
91+
bel.addTerm(p);
9092
} catch (ParseException e) {
91-
bel.addTerm(new StringTermImpl(param));
93+
bel.addTerm(new StringTermImpl(param));
9294
}
93-
}
95+
}
9496

95-
boolean bufUpdate = perception.getUpdate();
96-
if (bufUpdate) {
97+
boolean bufUpdate = perception.getUpdate();
98+
if (bufUpdate) {
9799
Iterator<Literal> ibb =
98-
ts.getAg().getBB().getCandidateBeliefs(new PredicateIndicator(
99-
perception.getPerceptionName(), perception.getParameters().size()));
100+
ts.getAg().getBB().getCandidateBeliefs(new PredicateIndicator(
101+
perception.getPerceptionName(), perception.getParameters().size()));
100102
boolean addBelief = true;
101103
while (ibb != null && ibb.hasNext()) {
102-
Literal l = ibb.next();
103-
if (l.equals(bel)) {
104-
addBelief = false;
105-
} else {
106-
ibb.remove(); // remove l as perception from BB
107-
108-
// only produce -bel event if the agent has plans for the event
109-
Trigger te = new Trigger(TEOperator.del, TEType.belief, l);
110-
if (ts.getC().hasListener() || ts.getAg().getPL().hasCandidatePlan(te)) {
111-
l = ASSyntax.createLiteral(l.getFunctor(), l.getTermsArray());
112-
l.addAnnot(ts.getAg().getBB().TPercept);
113-
te.setLiteral(l);
114-
ts.getC().addEvent(new Event(te, Intention.EmptyInt));
115-
}
104+
Literal l = ibb.next();
105+
if (l.equals(bel)) {
106+
addBelief = false;
107+
} else {
108+
ibb.remove(); // remove l as perception from BB
109+
110+
// only produce -bel event if the agent has plans for the event
111+
Trigger te = new Trigger(TEOperator.del, TEType.belief, l);
112+
if (ts.getC().hasListener() || ts.getAg().getPL().hasCandidatePlan(te)) {
113+
l = ASSyntax.createLiteral(l.getFunctor(), l.getTermsArray());
114+
l.addAnnot(ts.getAg().getBB().TPercept);
115+
te.setLiteral(l);
116+
ts.getC().addEvent(new Event(te, Intention.EmptyInt));
116117
}
118+
}
117119
}
118120
if (addBelief) {
119-
try {
120-
ts.getAg().addBel(bel);
121-
} catch (RevisionFailedException e) {
122-
System.out.println("Error adding new belief");
123-
}
121+
try {
122+
ts.getAg().addBel(bel);
123+
} catch (RevisionFailedException e) {
124+
System.out.println("Error adding new belief");
125+
}
124126
}
125-
} else {
127+
} else {
126128
try {
127-
getTS().getAg().addBel(bel);
129+
getTS().getAg().addBel(bel);
128130
} catch (RevisionFailedException e) {
129-
System.out.println("Error adding new belief");
131+
System.out.println("Error adding new belief");
130132
}
133+
}
131134
}
132135
perception = rosNode.getPerception();
133136
}
@@ -142,17 +145,20 @@ public void act(ActionExec action) {
142145
}
143146

144147
public void actionsStatus(jason_ros_msgs.ActionStatus data) {
145-
int id = data.getId();
146-
boolean result = data.getResult();
148+
String agent_name = data.getAgentName();
149+
if(agent_name.equals(this.getAgName())){
150+
int id = data.getId();
151+
boolean result = data.getResult();
147152

148-
ActionExec action = actionsWaiting.get(id);
153+
ActionExec action = actionsWaiting.get(id);
149154

150-
if (action != null) {
155+
if (action != null) {
151156
action.setResult(result);
152157
actionExecuted(action);
153158
actionsWaiting.remove(id);
154-
} else {
159+
} else {
155160
System.out.println("Action not found.");
161+
}
156162
}
157163
}
158164

jason_ros/src/rosjava_agents/src/java/jasonros/RosJasonNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class RosJasonNode extends AbstractNodeMain {
3838

3939
List<jason_ros_msgs.ActionStatus> actions_status = new ArrayList<jason_ros_msgs.ActionStatus>();
4040
boolean connected;
41+
String agent_name = "";
4142

4243
@Override
4344
public GraphName getDefaultNodeName() {
@@ -99,6 +100,7 @@ public int publishAction(ActionExec action) {
99100
nodeConfiguration.getTopicMessageFactory().newFromType(std_msgs.Header._TYPE);
100101
act.setHeader(header);
101102

103+
act.setAgentName(this.agent_name);
102104
act.setActionName(action.getActionTerm().getFunctor());
103105

104106
if (action.getActionTerm().hasTerm()) {
@@ -140,6 +142,7 @@ public boolean Connected() {
140142
}
141143

142144
public void setNameParameter(String agent_name){
145+
this.agent_name = agent_name;
143146
parameterTree.set("jason/agent_name", agent_name);
144147
}
145148
}

0 commit comments

Comments
 (0)