Skip to content

Commit 8aa390a

Browse files
committed
Allow multiple class names to be associated as the key for an agent context
This allows O(1) lookup of contexts implementing superclasses or interfaces. Without this lookup, iteration over all contexts is required, which in a multi- threaded application runs the risk of concurrency problems
1 parent 05aa5ec commit 8aa390a

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ bin/
2020
# IntelliJ / Idea
2121
*.idea
2222
log/
23+
*.classpath
24+
*.project

src/org/uu/nl/net2apl/core/agent/AgentArguments.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.uu.nl.net2apl.core.agent;
22

33
import java.util.ArrayList;
4+
import java.util.HashMap;
45
import java.util.List;
56

67
import org.uu.nl.net2apl.core.defaults.deliberationsteps.ApplyExternalTriggerPlanSchemes;
@@ -19,6 +20,7 @@ public class AgentArguments {
1920

2021
private final List<PlanScheme> goalPlanSchemes, internalTriggerPlanSchemes, externalTriggerPlanSchemes, messagePlanSchemes;
2122
private final List<Context> contexts;
23+
private final HashMap<Context, Class<? extends Context>[]> explicitKeyContexts;
2224
private final List<Plan> initialPlans;
2325
private final List<Plan> downPlans;
2426

@@ -28,6 +30,7 @@ public AgentArguments(){
2830
this.externalTriggerPlanSchemes = new ArrayList<>();
2931
this.messagePlanSchemes = new ArrayList<>();
3032
this.contexts = new ArrayList<>();
33+
this.explicitKeyContexts = new HashMap<>();
3134
this.initialPlans = new ArrayList<>();
3235
this.downPlans = new ArrayList<>();
3336
}
@@ -42,6 +45,9 @@ final ContextContainer createContextContainer(){
4245
ContextContainer container = new ContextContainer();
4346
for(Context context : this.contexts)
4447
container.addContext(context);
48+
for(Context context : this.explicitKeyContexts.keySet()) {
49+
container.addImplementedContext(context, this.explicitKeyContexts.get(context));
50+
}
4551
return container;
4652
}
4753

@@ -88,6 +94,8 @@ final List<Plan> getShutdownPlans(){
8894
public final AgentArguments addGoalPlanScheme(final FunctionalPlanSchemeInterface planScheme){ this.goalPlanSchemes.add(new FunctionalPlanScheme(planScheme)); return this; }
8995
/** Add a context that is used for decision making and plan execution. */
9096
public final AgentArguments addContext(final Context context){ this.contexts.add(context); return this; }
97+
/** Add a context that is used for decision making and plan execution with one or more explicit lookup keys. */
98+
public final AgentArguments addContext(final Context context, Class<? extends Context> ... keys){ this.explicitKeyContexts.put(context, keys); return this; }
9199
/** Add a plan that will be executed in the first deliberation cycle. */
92100
public final AgentArguments addInitialPlan(final Plan plan){ this.initialPlans.add(plan); return this; }
93101
/** Add a plan that will be executed after the last deliberation cycle this agent will participate in. */
@@ -105,6 +113,7 @@ public final AgentArguments include(final AgentArguments builder){
105113
this.initialPlans.addAll(builder.initialPlans);
106114
this.downPlans.addAll(builder.downPlans);
107115
this.contexts.addAll(builder.contexts);
116+
this.explicitKeyContexts.putAll(builder.explicitKeyContexts);
108117
return this;
109118
}
110119
}

src/org/uu/nl/net2apl/core/agent/ContextContainer.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ public final void addContext(final Context context) {
3333
this.map.put(context.getClass(), context);
3434
}
3535

36+
/**
37+
* Store a new context with one or more explicitly provided classes as keys. Useful if one context extends or
38+
* implements multiple (context) classes or interfaces, which should be used for lookup
39+
*
40+
* Contexts for existing keys will be overwritten!
41+
*
42+
* @param context Context to store
43+
* @param keys List of keys this context should be associated with.
44+
*/
45+
public final void addImplementedContext(final Context context, Class<? extends Context> ... keys) {
46+
boolean mainClassGiven = false;
47+
for (Class<? extends Context> key : keys) {
48+
this.map.put(key, context);
49+
if(context.getClass().equals(key)) mainClassGiven = true;
50+
}
51+
if(!mainClassGiven) this.map.put(context.getClass(), context);
52+
}
53+
3654
/**
3755
* Suppressed unchecked warning because the add method ensures that the cast
3856
* will always be successful.

0 commit comments

Comments
 (0)