33import  aima .core .logic .fol .kb .data .Literal ;
44
55import  java .util .ArrayList ;
6+ import  java .util .Comparator ;
67import  java .util .List ;
78
89/** 
1516 * in a package delivery problem might be At(Truck1 , Melbourne) ∧ At(Truck2 , Sydney). 
1617 * 
1718 * @author samagra 
19+  * @author Ruediger Lunde 
1820 */ 
1921public  class  State  {
2022    List <Literal > fluents ;
@@ -38,16 +40,20 @@ public State(String fluents) {
3840     * that are positive literals in the action’s effects (what we call the add list or ADD(a)): 
3941     * RESULT(s, a) = (s − DEL(a)) ∪ ADD(a). 
4042     * 
41-      * @param a  The applicable action. 
43+      * @param action  The applicable action. 
4244     * @return The new state. 
4345     */ 
44-     public  State  result (ActionSchema  a ) {
45-         if  (this .isApplicable (a )) {
46-             for  (Literal  fluent  : a .getEffectsNegativeLiterals ()) {
47-                 Literal  tempFluent  = new  Literal (fluent .getAtomicSentence ());
48-                 fluents .remove (tempFluent );
46+     public  State  result (ActionSchema  action ) {
47+         if  (isApplicable (action )) {
48+             List <Literal > result  = new  ArrayList <>(fluents );
49+             for  (Literal  literal  : action .getEffect ()) {
50+                 if  (literal .isNegativeLiteral ())
51+                     result .remove (literal .getComplementaryLiteral ());
52+                 else  if  (!result .contains (literal ))
53+                     result .add (literal );
4954            }
50-             fluents .addAll (a .getEffectsPositiveLiterals ());
55+             result .sort (Comparator .comparing (Literal ::toString ));
56+             return  new  State (result );
5157        }
5258        return  this ;
5359    }
@@ -56,16 +62,12 @@ public State result(ActionSchema a) {
5662     * Returns the state obtained by the application of a list of applicable actions to 
5763     * the current state. This method does not change the original state and in fact returns 
5864     * a new state representing the changed state. 
59-      * 
60-      * @param actions 
61-      * @return 
6265     */ 
6366    public  State  result (List <ActionSchema > actions ) {
64-         State  resultState  = new  State (new  ArrayList <>(this .getFluents ()));
65-         for  (ActionSchema  action  : actions ) {
66-             resultState  = resultState .result (action );
67-         }
68-         return  resultState ;
67+         State  state  = this ;
68+         for  (ActionSchema  action  : actions )
69+             state  = state .result (action );
70+         return  state ;
6971    }
7072
7173
@@ -74,11 +76,20 @@ public State result(List<ActionSchema> actions) {
7476     * <p> 
7577     * We say that action a is applicable in state s if the preconditions are satisfied by s. 
7678     * 
77-      * @param a  an action 
79+      * @param action  an action 
7880     * @return a boolean stating if the action is applicable. 
7981     */ 
80-     public  boolean  isApplicable (ActionSchema  a ) {
81-         return  this .getFluents ().containsAll (a .getPrecondition ());
82+     public  boolean  isApplicable (ActionSchema  action ) {
83+         boolean  result  = true ;
84+         for  (Literal  literal  : action .getPrecondition ()) {
85+             if  (literal .isPositiveLiteral ()
86+                     ? !fluents .contains (literal )
87+                     : fluents .contains (literal .getComplementaryLiteral ())) {
88+                 result  = false ;
89+                 break ;
90+             }
91+         }
92+         return  result ;
8293    }
8394
8495    public  List <Literal > getFluents () {
0 commit comments