Skip to content

Commit f3a1604

Browse files
Merge pull request #21 from CST-Group/activityCodelet
Changes to ActivityCodelet and ActivityTrackingCodelet
2 parents de8e122 + 1079a8c commit f3a1604

File tree

14 files changed

+543
-32
lines changed

14 files changed

+543
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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:meca:0.4.2'
31+
implementation 'com.github.CST-Group:meca:0.4.3'
3232
}
3333
```
3434

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ description = "The Multipurpose Enhanced Cognitive Architecture (MECA)"
1010

1111
sourceCompatibility = 1.8
1212
targetCompatibility = 1.8
13-
version = '0.4.2'
13+
version = '0.4.3'
1414

1515
repositories {
1616
mavenCentral()

src/main/java/br/unicamp/meca/models/ActionSequencePlan.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,26 +44,40 @@ public ActionSequencePlan(ActionStep[] actionIdSequence) {
4444
}
4545

4646
/**
47-
* Returns the id of the current ActionFromPlanningCodelet to be undertaken in the plan.
47+
* Returns the ActionStep of the current ActionFromPlanningCodelet to be undertaken in the plan.
4848
*
49-
* @return currentActionId
49+
* @return the current ActionStep
5050
*/
5151
public ActionStep getCurrentActionStep() {
5252
return actionIdSequence[currentActionIdIndex];
5353
}
54+
55+
/**
56+
* Returns the ActionStep of the last ActionFromPlanningCodelet to be undertaken in the plan, or null if it is the first step.
57+
*
58+
* @return the last ActionStep
59+
*/
60+
public ActionStep getLastExecutedActionStep() {
61+
if (currentActionIdIndex == 0) return(actionIdSequence[actionIdSequence.length-1]);
62+
else return actionIdSequence[currentActionIdIndex-1];
63+
}
5464

5565
/**
66+
* get the array of ActionSteps which are related to the ActionSequencePlan
67+
*
5668
* @return the actionIdSequence
5769
*/
5870
public ActionStep[] getActionStepSequence() {
5971
return actionIdSequence;
6072
}
6173

6274
/**
63-
* @param actionIdSequence the actionIdSequence to set
75+
* Forces a new actionStepSequence
76+
*
77+
* @param actionStepSequence the actionIdSequence to set
6478
*/
65-
public void setActionStepSequence(ActionStep[] actionIdSequence) {
66-
this.actionIdSequence = actionIdSequence;
79+
public void setActionStepSequence(ActionStep[] actionStepSequence) {
80+
this.actionIdSequence = actionStepSequence;
6781
}
6882

6983
/**
@@ -80,13 +94,18 @@ public void setCurrentActionIdIndex(int currentActionIdIndex) {
8094
this.currentActionIdIndex = currentActionIdIndex;
8195
}
8296

97+
/**
98+
* Go to next action. If the action is the last in the plan, go to the first action
99+
*/
83100
public void gotoNextAction() {
101+
actionIdSequence[currentActionIdIndex].executed = true;
84102
if (currentActionIdIndex < actionIdSequence.length-1)
85103
currentActionIdIndex++;
104+
else currentActionIdIndex = 0;
86105
}
87106

88107
public String toString() {
89-
String output = "{ ";
108+
String output = "{";
90109
int i=0;
91110
for (ActionStep a : actionIdSequence) {
92111
output += a.toString();
@@ -97,4 +116,13 @@ public String toString() {
97116
output += "}";
98117
return(output);
99118
}
119+
120+
/**
121+
* Reset the plan. Set all the action steps executed flag to false.
122+
*/
123+
public void resetPlan() {
124+
for (ActionStep as : actionIdSequence) {
125+
as.executed = false;
126+
}
127+
}
100128
}

src/main/java/br/unicamp/meca/models/ActionStep.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
public abstract class ActionStep {
2525
String actionId;
2626
Map<String,Object> parameters;
27+
public boolean needsConclusion = false;
28+
public boolean executed = false;
2729

2830
public ActionStep() {
2931
actionId = "";

src/main/java/br/unicamp/meca/system1/codelets/ActivityCodelet.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public void accessMemoryObjects() {
9393

9494
for(String perceptualCodeletId : perceptualCodeletsIds) {
9595
Memory perceptualMemory = this.getInput(perceptualCodeletId, index);
96-
perceptualMemories.add(perceptualMemory);
96+
if (perceptualMemory != null)
97+
perceptualMemories.add(perceptualMemory);
9798
}
9899
}
99100
}
@@ -107,7 +108,8 @@ public void accessMemoryObjects() {
107108
for(String motivationalCodeletsId : motivationalCodeletsIds)
108109
{
109110
Memory inputDrive = this.getInput(motivationalCodeletsId + "Drive");
110-
driveMemories.add(inputDrive);
111+
if (inputDrive != null)
112+
driveMemories.add(inputDrive);
111113
}
112114
}
113115
}
@@ -168,11 +170,20 @@ public void calculateActivation() {
168170
public void proc() {
169171
if(actionSequencePlanMemoryContainer != null && actionSequencePlanMemoryContainer.getI() != null && actionSequencePlanMemoryContainer.getI() instanceof ActionSequencePlan) {
170172
ActionSequencePlan actionSequencePlan = (ActionSequencePlan) actionSequencePlanMemoryContainer.getI();
171-
String currentActionId = actionSequencePlan.getCurrentActionStep().getActionId();
173+
ActionStep currentAction = actionSequencePlan.getCurrentActionStep();
174+
String currentActionId = currentAction.getActionId();
172175

173-
if(currentActionId != null && currentActionId.equalsIgnoreCase(id)) {
176+
if(currentActionId != null && currentActionId.equalsIgnoreCase(id) && currentAction.executed == false) {
174177
proc(perceptualMemories, broadcastMemory, motorMemory);
175178
}
179+
else {
180+
ActionStep lastActionStep = actionSequencePlan.getLastExecutedActionStep();
181+
String lastActionId = lastActionStep.getActionId();
182+
if (lastActionStep != null && lastActionStep.needsConclusion && lastActionId.equalsIgnoreCase(id)) {
183+
doConclusion(perceptualMemories, broadcastMemory, motorMemory);
184+
lastActionStep.needsConclusion = false;
185+
}
186+
}
176187
}else {
177188
proc(perceptualMemories, broadcastMemory, motorMemory);
178189
}
@@ -189,6 +200,19 @@ public void proc() {
189200
* the output motor memory.
190201
*/
191202
public abstract void proc(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory);
203+
204+
/**
205+
* Method to be called to do the Conclusion of an ActionStep, after its activities are over.
206+
* Similar to the proc method, but to be called one last time to clean whatever needs to be cleaned at the motorMemory
207+
*
208+
* @param perceptualMemories
209+
* the input memories coming from perception.
210+
* @param broadcastMemory
211+
* the input memory coming from the conscious planner broadcast.
212+
* @param motorMemory
213+
* the output motor memory.
214+
*/
215+
public abstract void doConclusion(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory);
192216

193217
/**
194218
* Returns the id of the Soar Codelet whose outputs will be read by this
@@ -278,4 +302,24 @@ public void setMotorCodeletId(String motorCodeletId) {
278302
public ArrayList<String> getMotivationalCodeletsIds() {
279303
return motivationalCodeletsIds;
280304
}
305+
306+
public ArrayList<Memory> getPerceptionMemories() {
307+
return(perceptualMemories);
308+
}
309+
310+
public ArrayList<Memory> getDriveMemories() {
311+
return(driveMemories);
312+
}
313+
314+
public Memory getBroadcastMemory() {
315+
return(broadcastMemory);
316+
}
317+
318+
public Memory getActionSequencePlanMemoryContainer() {
319+
return(actionSequencePlanMemoryContainer);
320+
}
321+
322+
public Memory getMotorMemory() {
323+
return(motorMemory);
324+
}
281325
}

src/main/java/br/unicamp/meca/system1/codelets/ActivityTrackingCodelet.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,24 @@ public void trackActionSequencePlan(ArrayList<Memory> perceptualMemories, Action
100100
perceptualMemories != null &&
101101
perceptualMemories.size() > 0) {
102102
ActionStep currentActionStep = actionSequencePlan.getCurrentActionStep();
103-
if (currentActionStep.stopCondition(perceptualMemories)) {
104-
actionSequencePlan.gotoNextAction();
105-
}
103+
ActionStep lastActionStep = actionSequencePlan.getLastExecutedActionStep();
104+
if ((actionSequencePlan.getCurrentActionIdIndex() == 0
105+
|| lastActionStep.needsConclusion == false) // you can go to next step if it is the first step of the plan or if the last step is clear
106+
&& (currentActionStep != null // then just check if current ActionStep is not null to avoid breaking the next tests
107+
&& currentActionStep.executed == false // the ActionStep was not already executed
108+
&& currentActionStep.stopCondition(perceptualMemories)) ) { // and the ActionStep reached its final destination
109+
currentActionStep.needsConclusion = true;
110+
actionSequencePlan.gotoNextAction();
111+
}
106112
}
107113
}
108114

109115
@Override
110116
public void proc() {
111-
112-
actionSequencePlan = (ActionSequencePlan) actionSequencePlanMemoryContainer.getI();
113-
114-
trackActionSequencePlan(perceptualMemories, actionSequencePlan);
117+
if (actionSequencePlanMemoryContainer != null) {
118+
actionSequencePlan = (ActionSequencePlan) actionSequencePlanMemoryContainer.getI();
119+
trackActionSequencePlan(perceptualMemories, actionSequencePlan);
120+
}
115121
}
116122

117123
/**

src/test/java/br/unicamp/meca/mind/action/Test1ActivityCodelet.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,26 @@ public Test1ActivityCodelet(String id, ArrayList<String> perceptualCodeletsIds,
2626
ArrayList<String> motivationalCodeletsIds, String motorCodeletId, String soarCodeletId) {
2727
super(id, perceptualCodeletsIds, motivationalCodeletsIds, motorCodeletId, soarCodeletId);
2828
}
29+
30+
@Override
31+
public void doConclusion(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory) {
32+
System.out.println("Concluding "+id);
33+
((MemoryContainer) motorMemory).setI(id+" - concluded",getActivation(),id);
34+
}
2935

3036
@Override
3137
public void proc(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory) {
3238
if(perceptualMemories == null || perceptualMemories.size() == 0) {
3339
return;
3440
}
3541

36-
motorMemory.setI(null);
42+
((MemoryContainer) motorMemory).setI(null,0.0,id);
3743

3844
for(Memory memory: perceptualMemories) {
3945
if(memory.getI()!=null && memory.getI() instanceof String) {
4046
String perceptualContent = (String) memory.getI();
4147

42-
((MemoryContainer) motorMemory).setI("Test1Activity - "+perceptualContent,getActivation(),id);
48+
((MemoryContainer) motorMemory).setI(id+" - "+perceptualContent,getActivation(),id);
4349
}
4450
}
4551
}

src/test/java/br/unicamp/meca/mind/action/Test2ActivityCodelet.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ public Test2ActivityCodelet(String id, ArrayList<String> perceptualCodeletsIds,
2626
ArrayList<String> motivationalCodeletsIds, String motorCodeletId, String soarCodeletId) {
2727
super(id, perceptualCodeletsIds, motivationalCodeletsIds, motorCodeletId, soarCodeletId);
2828
}
29+
30+
@Override
31+
public void doConclusion(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory) {
32+
System.out.println("Concluding Test2Activity");
33+
}
2934

3035
@Override
3136
public void proc(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory) {
3237
if(perceptualMemories == null || perceptualMemories.size() == 0) {
3338
return;
3439
}
3540

36-
motorMemory.setI(null);
41+
((MemoryContainer) motorMemory).setI(null,0.0,id);
3742

3843
for(Memory memory: perceptualMemories) {
3944
if(memory.getI()!=null && memory.getI() instanceof String) {

src/test/java/br/unicamp/meca/mind/action/Test3ActivityCodelet.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ public Test3ActivityCodelet(String id, ArrayList<String> perceptualCodeletsIds,
2626
ArrayList<String> motivationalCodeletsIds, String motorCodeletId, String soarCodeletId) {
2727
super(id, perceptualCodeletsIds, motivationalCodeletsIds, motorCodeletId, soarCodeletId);
2828
}
29+
30+
@Override
31+
public void doConclusion(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory) {
32+
System.out.println("Concluding Test3Activity");
33+
}
2934

3035
@Override
3136
public void proc(ArrayList<Memory> perceptualMemories, Memory broadcastMemory, Memory motorMemory) {
3237
if(perceptualMemories == null || perceptualMemories.size() == 0) {
3338
return;
3439
}
3540

36-
motorMemory.setI(null);
41+
((MemoryContainer) motorMemory).setI(null,0.0,id);
3742

3843
for(Memory memory: perceptualMemories) {
3944
if(memory.getI()!=null && memory.getI() instanceof String) {

0 commit comments

Comments
 (0)