/
DefaultContextInteractionsSkill.java
167 lines (142 loc) · 4.34 KB
/
DefaultContextInteractionsSkill.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
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2016 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.UUID;
import io.sarl.core.DefaultContextInteractions;
import io.sarl.core.Lifecycle;
import io.sarl.lang.core.Address;
import io.sarl.lang.core.Agent;
import io.sarl.lang.core.AgentContext;
import io.sarl.lang.core.Event;
import io.sarl.lang.core.EventSpace;
import io.sarl.lang.core.Scope;
import io.sarl.lang.core.Skill;
import io.sarl.lang.core.Space;
import io.sarl.lang.core.SpaceID;
import io.sarl.util.Scopes;
/**
* Skill to access to the default interaction context.
*
* @author $Author: srodriguez$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public class DefaultContextInteractionsSkill extends Skill implements DefaultContextInteractions {
private AgentContext parentContext;
private EventSpace defaultSpace;
private Address addressInParentDefaultSpace;
/**
* Constructs a <code>DefaultContextInteractionsImpl</code>.
*
* @param agent - owner of the skill.
* @param parentContext - reference to the parent context.
*/
DefaultContextInteractionsSkill(Agent agent, AgentContext parentContext) {
super(agent);
this.parentContext = parentContext;
}
@Override
protected String attributesToString() {
return super.attributesToString() + ", parentContext = " + this.parentContext //$NON-NLS-1$
+ ", defaultSpace = " + this.defaultSpace //$NON-NLS-1$
+ ", addressInDefaultspace = " + this.addressInParentDefaultSpace; //$NON-NLS-1$
}
@Override
protected void install() {
this.defaultSpace = this.parentContext.getDefaultSpace();
}
@Override
public void emit(Event event) {
event.setSource(getDefaultAddress());
this.defaultSpace.emit(event);
}
@Override
public void emit(Event event, Scope<Address> scope) {
event.setSource(getDefaultAddress());
this.defaultSpace.emit(event, scope);
}
@Override
public Address getDefaultAddress() {
Address adr = this.addressInParentDefaultSpace;
if (adr == null) {
adr = this.defaultSpace.getAddress(getOwner().getID());
assert adr != null : "The agent has no address in the default space"; //$NON-NLS-1$
this.addressInParentDefaultSpace = adr;
}
return adr;
}
@Override
public AgentContext getDefaultContext() {
return this.parentContext;
}
@Override
public EventSpace getDefaultSpace() {
return this.defaultSpace;
}
/** {@inheritDoc}
* @deprecated see {@link #willReceive(UUID, Event)}
*/
@Deprecated
@Override
public void receive(UUID receiverID, Event event) {
willReceive(receiverID, event);
}
@Override
public void willReceive(UUID receiverID, Event event) {
final Address recAddr = this.defaultSpace.getAddress(receiverID);
this.emit(event, Scopes.addresses(recAddr));
}
@Override
public UUID spawn(Class<? extends Agent> agentType, Object... params) {
return getSkill(Lifecycle.class).spawnInContext(agentType, this.parentContext, params);
}
@Override
public boolean isDefaultSpace(Space space) {
return isDefaultSpace(space.getID());
}
@Override
public boolean isDefaultSpace(SpaceID space) {
return isDefaultSpace(space.getID());
}
@Override
public boolean isDefaultSpace(UUID space) {
return space.equals(this.defaultSpace.getID().getID());
}
@Override
public boolean isInDefaultSpace(Event event) {
if (event != null) {
final Address adr = event.getSource();
if (adr != null) {
return isDefaultSpace(adr.getSpaceId());
}
}
return false;
}
@Override
public boolean isDefaultContext(AgentContext context) {
return isDefaultContext(context.getID());
}
@Override
public boolean isDefaultContext(UUID contextID) {
return contextID.equals(this.defaultSpace.getID().getContextID());
}
}