/
BehaviorsSkill.java
182 lines (155 loc) · 5.99 KB
/
BehaviorsSkill.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.janusproject.kernel.bic;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import io.sarl.core.Behaviors;
import io.sarl.core.InnerContextAccess;
import io.sarl.core.Schedules;
import io.sarl.lang.core.Address;
import io.sarl.lang.core.Agent;
import io.sarl.lang.core.Behavior;
import io.sarl.lang.core.Event;
import io.sarl.lang.core.EventListener;
import io.sarl.lang.core.EventSpace;
import io.sarl.lang.core.Scope;
import io.sarl.lang.core.Skill;
import io.sarl.lang.util.ClearableReference;
/**
* Janus implementation of SARL's {@link Behaviors} built-in capacity.
*
* @author $Author: srodriguez$
* @author $Author: ngaud$
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public class BehaviorsSkill extends BuiltinSkill implements Behaviors {
private static int installationOrder = -1;
private final Address agentAddressInInnerDefaultSpace;
private ClearableReference<Skill> skillBufferInternalEventBusCapacity;
private ClearableReference<Skill> skillBufferInnerContextAccess;
private ClearableReference<Skill> skillBufferSchedules;
/**
* @param agent - owner of this skill.
* @param agentAddressInInnerDefaultSpace - address of the owner of this skill in its inner default space.
*/
BehaviorsSkill(Agent agent, Address agentAddressInInnerDefaultSpace) {
super(agent);
this.agentAddressInInnerDefaultSpace = agentAddressInInnerDefaultSpace;
}
/** Replies the InternalEventBusCapacity skill as fast as possible.
*
* @return the skill
*/
protected final InternalEventBusCapacity getInternalEventBusCapacitySkill() {
if (this.skillBufferInternalEventBusCapacity == null || this.skillBufferInternalEventBusCapacity.get() == null) {
this.skillBufferInternalEventBusCapacity = $getSkill(InternalEventBusCapacity.class);
}
return $castSkill(InternalEventBusCapacity.class, this.skillBufferInternalEventBusCapacity);
}
/** Replies the InnerContextAccess skill as fast as possible.
*
* @return the skill
*/
protected final InnerContextAccess getInnerContextAccessSkill() {
if (this.skillBufferInnerContextAccess == null || this.skillBufferInnerContextAccess.get() == null) {
this.skillBufferInnerContextAccess = $getSkill(InnerContextAccess.class);
}
return $castSkill(InnerContextAccess.class, this.skillBufferInnerContextAccess);
}
/** Replies the Schedules skill as fast as possible.
*
* @return the skill
*/
protected final SchedulesSkill getSchedulesSkill() {
if (this.skillBufferSchedules == null || this.skillBufferSchedules.get() == null) {
this.skillBufferSchedules = $getSkill(Schedules.class);
}
return $castSkill(SchedulesSkill.class, this.skillBufferSchedules);
}
@Override
public int getInstallationOrder() {
if (installationOrder < 0) {
installationOrder = installationOrder(this);
}
return installationOrder;
}
@Override
public boolean hasRegisteredBehavior() {
return getSkill(InternalEventBusCapacity.class).hasRegisteredEventListener(Behavior.class);
}
@Override
public Collection<Behavior> getRegisteredBehaviors() {
final Collection<Behavior> behaviors = new ArrayList<>();
getSkill(InternalEventBusCapacity.class).getRegisteredEventListeners(Behavior.class, behaviors);
return behaviors;
}
@Override
public synchronized Behavior registerBehavior(Behavior attitude) {
return registerBehavior(attitude, null);
}
@Override
public Behavior registerBehavior(Behavior attitude, Function1<? super Event, ? extends Boolean> filter) {
getInternalEventBusCapacitySkill().registerEventListener(attitude, true, filter);
return attitude;
}
@Override
public synchronized Behavior unregisterBehavior(Behavior attitude) {
getSchedulesSkill().unregisterTasksForBehavior(attitude);
getInternalEventBusCapacitySkill().unregisterEventListener(attitude, true);
return attitude;
}
@SuppressWarnings("unchecked")
@Override
public void wake(Event evt) {
wake(evt, $DEFAULT_VALUE$WAKE_0);
}
@Override
public synchronized void wake(Event evt, Scope<Address> scope) {
// Use the inner space so all behaviors (even agents inside the holon
// running in distant kernels) are notified. The event will return into
// the agent via the inner default space add call internalReceiveEvent
// for real posting
final InnerContextAccess context = getInnerContextAccessSkill();
if ((!(context instanceof InnerContextSkill)) || ((InnerContextSkill) context).hasInnerContext()) {
final EventSpace defSpace = context.getInnerContext().getDefaultSpace();
evt.setSource(defSpace.getAddress(getOwner().getID()));
defSpace.emit(evt, scope);
} else {
// Do not call getInnerContext(), which is creating the inner context automatically.
// In place, try to send the event inside the agent only (and its behaviors).
final InternalEventBusCapacity eventBus = getSkill(InternalEventBusCapacity.class);
if (scope == null || scope.matches(eventBus.getInnerDefaultSpaceAddress())) {
final EventListener listener = eventBus.asEventListener();
assert listener != null;
evt.setSource(this.agentAddressInInnerDefaultSpace);
listener.receiveEvent(evt);
}
}
}
@Override
public EventListener asEventListener() {
return getInternalEventBusCapacitySkill().asEventListener();
}
}