88 * The data structure for calculating and holding the levels of a planning graph.
99 *
1010 * @author samagra
11+ * @author Ruediger Lunde
1112 */
1213public class Level {
1314 List <Object > levelObjects ;
14- HashMap <Object , List <Object >> mutexLinks ;//can be planned alternatively
15+ HashMap <Object , List <Object >> mutexLinks ; //can be planned alternatively
1516 HashMap <Object , List <Object >> nextLinks ;
1617 HashMap <Object , List <Object >> prevLinks ;
17- Problem problem ;
1818 Level prevLevel ;
1919
2020 public Level (Level prevLevel , Problem problem ) {
21- this . prevLevel = prevLevel ;
22- this . problem = problem ;
21+ levelObjects = new ArrayList <>() ;
22+ prevLinks = new HashMap <>() ;
2323 if (prevLevel != null ) {
24+ this .prevLevel = prevLevel ;
2425 HashMap <Object , List <Object >> linksFromPreviousLevel = prevLevel .getNextLinks ();
25- this .problem = problem ;
26- levelObjects = new ArrayList <>();
27- prevLinks = new HashMap <>();
2826 for (Object node : linksFromPreviousLevel .keySet ()) {
2927 List <Object > thisLevelObjects = linksFromPreviousLevel .get (node );
30- for (Object nextNode :
31- thisLevelObjects ) {
28+ for (Object nextNode : thisLevelObjects ) {
3229 if (levelObjects .contains (nextNode )) {
3330 List <Object > tempPrevLink = prevLinks .get (nextNode );
3431 tempPrevLink .add (node );
@@ -37,38 +34,27 @@ public Level(Level prevLevel, Problem problem) {
3734 levelObjects .add (nextNode );
3835 prevLinks .put (nextNode , new ArrayList <>(Collections .singletonList (node )));
3936 }
40-
4137 }
42-
4338 }
44- addNoPrecondActions ();
45- calculateNextLinks ();
46- calculateMutexLinks (prevLevel );
4739 } else {
48- levelObjects = new ArrayList <>();
49- prevLinks = new HashMap <>();
5040 levelObjects .addAll (problem .getInitialState ().getFluents ());
51- for (Object obj : levelObjects ) {
41+ for (Object obj : levelObjects )
5242 prevLinks .put (obj , new ArrayList <>());
53- }
54- addNoPrecondActions ();
55- calculateNextLinks ();
56- calculateMutexLinks (null );
5743 }
44+ addNoPrecondActions (problem );
45+ calculateNextLinks (problem );
46+ calculateMutexLinks (prevLevel );
5847 addPersistenceActions ();
5948 }
6049
50+ // for testing only...
6151 public Level (Level prevLevel , Problem problem , String extraLiterals ) {
6252 this (prevLevel , problem );
63- this .addExtraLiterals (extraLiterals );
64- }
65-
66- public void addExtraLiterals (String s ) {
67- for (Literal literal : Utils .parse (s )) {
53+ for (Literal literal : Utils .parse (extraLiterals )) {
6854 if (!levelObjects .contains (literal ))
6955 levelObjects .add (literal );
7056 }
71- calculateNextLinks ();
57+ calculateNextLinks (problem );
7258 calculateMutexLinks (getPrevLevel ());
7359 addPersistenceActions ();
7460 }
@@ -89,35 +75,34 @@ public HashMap<Object, List<Object>> getPrevLinks() {
8975 return prevLinks ;
9076 }
9177
92- public Problem getProblem () {
93- return problem ;
94- }
95-
96- private void addPersistenceActions () {
97- if (getLevelObjects ().get (0 ) instanceof Literal ) {
98- for (Object literal : getLevelObjects ()) {
99- ActionSchema action = new ActionSchema (ActionSchema .NO_OP , null ,
100- Collections .singletonList ((Literal ) literal ),
101- Collections .singletonList ((Literal ) literal ));
102- addToHashMap (literal , action , nextLinks );
103- }
104- }
78+ public Level getPrevLevel () {
79+ return prevLevel ;
10580 }
10681
107- public void addNoPrecondActions () {
108- if (getLevelObjects () .get (0 ) instanceof ActionSchema ) {
82+ private void addNoPrecondActions (Problem problem ) {
83+ if (levelObjects .get (0 ) instanceof ActionSchema ) {
10984 for (ActionSchema action : problem .getPropositionalisedActions ()) {
11085 if (action .getPrecondition ().size ()==0 )
11186 levelObjects .add (action );
11287 }
11388 }
11489 }
11590
116-
91+ private void addPersistenceActions () {
92+ if (levelObjects .get (0 ) instanceof Literal ) {
93+ for (Object literal : getLevelObjects ()) {
94+ ActionSchema action = new ActionSchema (ActionSchema .NO_OP , null ,
95+ Collections .singletonList ((Literal ) literal ),
96+ Collections .singletonList ((Literal ) literal ));
97+ addToHashMap (literal , action , nextLinks );
98+ }
99+ }
100+ }
117101
118102 private void calculateMutexLinks (Level prevLevel ) {
119103 mutexLinks = new HashMap <>();
120- if (prevLevel == null ) return ;
104+ if (prevLevel == null )
105+ return ;
121106 if (levelObjects .get (0 ) instanceof Literal ) {
122107 Literal firstLiteral , secondLiteral ;
123108 List <Object > possibleActionsFirst , possibleActionsSecond ;
@@ -221,18 +206,7 @@ private boolean checkInterference(List<Literal> firstActionPreconditions, List<L
221206 return checkMutex ;
222207 }
223208
224- private void addToHashMap (Object firstObject , Object secondObject , HashMap <Object , List <Object >> map ) {
225- List <Object > tempList ;
226- if (map .containsKey (firstObject )) {
227- tempList = map .get (firstObject );
228- tempList .add (secondObject );
229- map .put (firstObject , tempList );
230- } else {
231- map .put (firstObject , new ArrayList <>(Collections .singletonList (secondObject )));
232- }
233- }
234-
235- private void calculateNextLinks () {
209+ private void calculateNextLinks (Problem problem ) {
236210 nextLinks = new HashMap <>();
237211 if (levelObjects .get (0 ) instanceof Literal ) {
238212 for (ActionSchema action : problem .getPropositionalisedActions ()) {
@@ -252,20 +226,20 @@ private void calculateNextLinks() {
252226 }
253227 } else if (levelObjects .get (0 ) instanceof ActionSchema ) {
254228 for (Object action : levelObjects ) {
255- Object [] effects = ((ActionSchema ) action ).getEffects (). toArray ();
256- nextLinks .put (action , new ArrayList <>(Arrays . asList (effects )));
229+ List < Literal > effects = ((ActionSchema ) action ).getEffects ();
230+ nextLinks .put (action , new ArrayList <>(new ArrayList < Object > (effects )));
257231 }
258232 }
259-
260233 }
261234
262- public Level getPrevLevel () {
263- return prevLevel ;
235+ private void addToHashMap (Object firstObject , Object secondObject , HashMap <Object , List <Object >> map ) {
236+ List <Object > list = map .computeIfAbsent (firstObject , k -> new ArrayList <>());
237+ list .add (secondObject );
264238 }
265239
266240 @ Override
267241 public boolean equals (Object obj ) {
268- if (!( obj instanceof Level ))
242+ if (obj == null || getClass () != obj . getClass ( ))
269243 return false ;
270244 return this .levelObjects .containsAll (((Level ) obj ).levelObjects )
271245 && ((Level ) obj ).levelObjects .containsAll (this .levelObjects )
0 commit comments