-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusAgent.java
More file actions
63 lines (56 loc) · 1.58 KB
/
Copy pathPlusAgent.java
File metadata and controls
63 lines (56 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package test;
public class PlusAgent implements Agent {
private final String name;
private final Topic input1;
private final Topic input2;
private final Topic output;
private double x;
private double y;
public PlusAgent(String[] subs, String[] pubs) {
if (subs == null || subs.length < 2) {
throw new IllegalArgumentException("PlusAgent requires at least 2 subs");
}
if (pubs == null || pubs.length < 1) {
throw new IllegalArgumentException("PlusAgent requires at least 1 pub");
}
this.name = "PlusAgent";
TopicManagerSingleton.TopicManager tm = TopicManagerSingleton.get();
this.input1 = tm.getTopic(subs[0].trim());
this.input2 = tm.getTopic(subs[1].trim());
this.output = tm.getTopic(pubs[0].trim());
this.input1.subscribe(this);
this.input2.subscribe(this);
this.output.addPublisher(this);
reset();
}
@Override
public String getName() {
return name;
}
@Override
public void reset() {
x = 0.0;
y = 0.0;
}
@Override
public void callback(String topic, Message msg) {
if (msg == null) {
return;
}
double val = msg.asDouble;
if (Double.isNaN(val)) {
return;
}
if (input1.name.equals(topic)) {
x = val;
} else if (input2.name.equals(topic)) {
y = val;
} else {
return;
}
output.publish(new Message(x + y));
}
@Override
public void close() {
}
}