Skip to content

Commit 5b4a660

Browse files
Merge pull request #46 from CST-Group/codelet_container
Codelet container
2 parents cf45244 + 52ba409 commit 5b4a660

4 files changed

Lines changed: 1016 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Note: This library is still under development, and some concepts or features mig
2828
```
2929
dependencies {
3030
...
31-
implementation 'com.github.CST-Group:cst:1.0.0'
31+
implementation 'com.github.CST-Group:cst:1.1.0'
3232
}
3333
```
3434

@@ -53,7 +53,7 @@ Sometimes, the version number (tag) in this README gets out of date, as maintain
5353
<dependency>
5454
<groupId>com.github.CST-Group</groupId>
5555
<artifactId>cst</artifactId>
56-
<version>1.0.0</version>
56+
<version>1.1.0</version>
5757
</dependency>
5858
```
5959

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description = "CST is the Cognitive Systems Toolkit, a toolkit for the construct
1010

1111
sourceCompatibility = 1.8
1212
targetCompatibility = 1.8
13-
version = '1.0.0'
13+
version = '1.1.0'
1414

1515
repositories {
1616
mavenCentral()
Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
1+
package br.unicamp.cst.core.entities;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class CodeletContainer implements Memory {
9+
10+
private HashMap<String, List<Memory>> mapInputs = new HashMap<String, List<Memory>>();
11+
12+
13+
private HashMap<String, List<Memory>> mapOutputs = new HashMap<String, List<Memory>>();
14+
15+
16+
private HashMap<String, List<Memory>> mapBroadcast = new HashMap<String, List<Memory>>();
17+
18+
/**
19+
* Output memories, the ones that are written.
20+
*/
21+
protected List<Memory> outputs = new ArrayList<Memory>();
22+
23+
/**
24+
* Input memories, the ones that were broadcasted.
25+
*/
26+
private List<Memory> broadcast = new ArrayList<Memory>();
27+
28+
/**
29+
* Input memories, the ones that are read.
30+
*/
31+
protected List<Memory> inputs = new ArrayList<Memory>();
32+
33+
private ArrayList<Codelet> codelets;
34+
35+
/**
36+
* Type of the codelet container
37+
*/
38+
private String name;
39+
40+
41+
public CodeletContainer() {
42+
super();
43+
this.codelets = new ArrayList<>();
44+
}
45+
46+
47+
public CodeletContainer(ArrayList<Codelet> codelets) {
48+
super();
49+
this.codelets = new ArrayList<Codelet>();
50+
codelets.forEach((codelet) -> {
51+
this.addCodelet(codelet);
52+
});
53+
}
54+
55+
/**
56+
* Gets this Codelet activation.
57+
*
58+
* @return the activation
59+
*/
60+
public synchronized double getActivation() {
61+
double maxActivation = 0.0d;
62+
63+
for (Codelet codelet : codelets) {
64+
65+
double codeletActivation = codelet.getActivation();
66+
67+
if (codeletActivation >= maxActivation) {
68+
69+
maxActivation = codeletActivation;
70+
}
71+
72+
}
73+
return maxActivation;
74+
}
75+
76+
/**
77+
* Gets the input memories list.
78+
*
79+
* @return the inputs.
80+
*/
81+
public synchronized List<Memory> getInputs() {
82+
return inputs;
83+
}
84+
85+
/**
86+
* Sets the input memories list.
87+
*
88+
* @param inputs
89+
* the inputs to set.
90+
*/
91+
public synchronized void setInputs(List<Memory> inputs) {
92+
for (Map.Entry<String, List<Memory>> set :
93+
this.mapInputs.entrySet()) {
94+
set.setValue(inputs);
95+
}
96+
this.inputs = inputs;
97+
}
98+
99+
/**
100+
* Gets the list of broadcast memories.
101+
*
102+
* @return the broadcast.
103+
*/
104+
public synchronized List<Memory> getBroadcast() {
105+
return broadcast;
106+
}
107+
108+
/**
109+
* Sets the list of broadcast memories.
110+
*
111+
* @param broadcast
112+
* the broadcast to set.
113+
*/
114+
public synchronized void setBroadcast(List<Memory> broadcast) {
115+
for (Map.Entry<String, List<Memory>> set :
116+
this.mapBroadcast.entrySet()) {
117+
set.setValue(broadcast);
118+
}
119+
this.broadcast = broadcast;
120+
}
121+
122+
public void addCodelet(Codelet codelet) {
123+
List<Memory> addedInputs = new ArrayList<Memory>(codelet.inputs);
124+
//add the inputs from added codelet to the list of inputs from container which all of its codelets share
125+
inputs.addAll(addedInputs);
126+
codelet.setInputs(inputs);
127+
//keep track of each codelets inputs, so it can removed when needed
128+
mapInputs.put(codelet.name, addedInputs);
129+
130+
//same logic from inputs
131+
List<Memory> addedBroadcasts = new ArrayList<Memory>(codelet.broadcast);
132+
broadcast.addAll(addedBroadcasts);
133+
codelet.setBroadcast(broadcast);
134+
mapBroadcast.put(codelet.name, addedBroadcasts);
135+
136+
//same logic from inputs
137+
List<Memory> addedOutputs = new ArrayList<Memory>(codelet.outputs);
138+
outputs.addAll(addedOutputs);
139+
codelet.setOutputs(outputs);
140+
mapOutputs.put(codelet.name, addedOutputs);
141+
142+
this.codelets.add(codelet);
143+
144+
}
145+
146+
public void removeCodelet(Codelet codelet) {
147+
this.codelets.remove(codelet);
148+
149+
List<Memory> inputsToRemoveFromEachCodelet = mapInputs.get(codelet.name);
150+
inputs.removeAll(inputsToRemoveFromEachCodelet);
151+
mapInputs.remove(codelet.name);
152+
153+
List<Memory> broadcastsToRemoveFromEachCodelet = mapBroadcast.get(codelet.name);
154+
broadcast.removeAll(broadcastsToRemoveFromEachCodelet);
155+
mapBroadcast.remove(codelet.name);
156+
157+
List<Memory> outputsToRemoveFromEachCodelet = mapOutputs.get(codelet.name);
158+
outputs.removeAll(outputsToRemoveFromEachCodelet);
159+
mapOutputs.remove(codelet.name);
160+
161+
this.codelets.remove(codelet);
162+
}
163+
164+
@Override
165+
public Object getI() {
166+
Object I = null;
167+
168+
double maxActivation = 0.0d;
169+
170+
for (Codelet codelet : codelets) {
171+
172+
double codeletActivation = codelet.getActivation();
173+
174+
if (codeletActivation >= maxActivation) {
175+
176+
maxActivation = codeletActivation;
177+
I = codelet;
178+
}
179+
180+
}
181+
return I;
182+
}
183+
184+
@Override
185+
public int setI(Object info) {
186+
for (Codelet codelet : codelets) {
187+
for (Memory memoryInput : codelet.inputs) {
188+
memoryInput.setI(info);
189+
}
190+
}
191+
return -1;
192+
}
193+
194+
/**
195+
* Gets the greatest evaluation of the codelet with the greatest activation
196+
*
197+
* @return the greatest evaluation of the memories in codelet with the greatest activation.
198+
*/
199+
@Override
200+
public synchronized Double getEvaluation() {
201+
202+
Double maxInputEvaluation = 0.0d;
203+
Double maxBroadcastEvaluation = 0.0d;
204+
double maxActivation = 0.0d;
205+
Codelet codeletMaxActivation = null;
206+
207+
for (Codelet codelet : codelets) {
208+
209+
double codeletActivation = codelet.getActivation();
210+
211+
if (codeletActivation >= maxActivation) {
212+
213+
maxActivation = codeletActivation;
214+
codeletMaxActivation = codelet;
215+
}
216+
217+
}
218+
219+
if (codeletMaxActivation != null) {
220+
for (Memory memory : codeletMaxActivation.inputs) {
221+
222+
double memoryEval = memory.getEvaluation();
223+
224+
if (memoryEval >= maxInputEvaluation)
225+
maxInputEvaluation = memoryEval;
226+
227+
}
228+
for (Memory memory : codeletMaxActivation.broadcast) {
229+
double memoryEval = memory.getEvaluation();
230+
231+
if (memoryEval >= maxBroadcastEvaluation)
232+
maxBroadcastEvaluation = memoryEval;
233+
}
234+
}
235+
236+
return maxInputEvaluation > maxBroadcastEvaluation ? maxInputEvaluation : maxBroadcastEvaluation;
237+
}
238+
239+
@Override
240+
public String getName() {
241+
return name;
242+
}
243+
244+
@Override
245+
public void setType(String type) {
246+
this.name = type;
247+
248+
}
249+
250+
@Override
251+
public void setName(String name) {
252+
this.name = name;
253+
254+
}
255+
256+
@Override
257+
public synchronized void setEvaluation(Double eval) {
258+
for (Memory memory : inputs) {
259+
memory.setEvaluation(eval);
260+
}
261+
262+
}
263+
264+
@Override
265+
public Long getTimestamp() {
266+
Double maxInputEvaluation = 0.0d;
267+
Double maxBroadcastEvaluation = 0.0d;
268+
double maxActivation = 0.0d;
269+
Codelet codeletMaxActivation = null;
270+
Long timestampInput = null;
271+
Long timestampBroadcast = null;
272+
273+
for (Codelet codelet : codelets) {
274+
275+
double codeletActivation = codelet.getActivation();
276+
277+
if (codeletActivation >= maxActivation) {
278+
279+
maxActivation = codeletActivation;
280+
codeletMaxActivation = codelet;
281+
}
282+
283+
}
284+
285+
if (codeletMaxActivation != null) {
286+
for (Memory memory : codeletMaxActivation.inputs) {
287+
288+
double memoryEval = memory.getEvaluation();
289+
290+
if (memoryEval >= maxInputEvaluation) {
291+
maxInputEvaluation = memoryEval;
292+
timestampInput = memory.getTimestamp();
293+
}
294+
295+
296+
}
297+
for (Memory memory : codeletMaxActivation.broadcast) {
298+
double memoryEval = memory.getEvaluation();
299+
300+
if (memoryEval >= maxBroadcastEvaluation) {
301+
maxBroadcastEvaluation = memoryEval;
302+
timestampBroadcast = memory.getTimestamp();
303+
}
304+
305+
}
306+
}
307+
return maxBroadcastEvaluation > maxInputEvaluation ? timestampBroadcast : timestampInput;
308+
}
309+
310+
@Override
311+
public void addMemoryObserver(MemoryObserver memoryObserver) {
312+
for (Codelet codelet : codelets) {
313+
for (Memory memory : codelet.inputs) {
314+
memory.addMemoryObserver(memoryObserver);
315+
}
316+
for (Memory memory : codelet.broadcast) {
317+
memory.addMemoryObserver(memoryObserver);
318+
}
319+
}
320+
}
321+
322+
public synchronized List<Memory> getOutputs() {
323+
return outputs;
324+
}
325+
326+
public List<Codelet> getAll() {
327+
return codelets;
328+
}
329+
330+
public Codelet getCodelet(String name) {
331+
Codelet selectedCodelet = null;
332+
for (Codelet codelet : codelets) {
333+
if (codelet.name.equals(name)) {
334+
selectedCodelet = codelet;
335+
break;
336+
}
337+
}
338+
return selectedCodelet;
339+
}
340+
341+
}

0 commit comments

Comments
 (0)