Menu

Same goal is created redundantly despite unique flag

Help
2020-06-18
2020-06-19
  • Frederic Jacob

    Frederic Jacob - 2020-06-18

    Hello,

    I want a goal to be unique, so that only a single instance of the goal is active at a time. The unique flag should do the job, but it doesn't - which is a big issue in my case because the executed plan is repeated for an excessive amount of times. The goal does not use any parameters.
    Here is a small example agent which demonstrates this behavior - the unique flag in the @Goal-Annotation has no effect and two goals become active at the same time.

    @Agent
    public class UniqueGoalBDI {
        @AgentFeature
        IExecutionFeature executionFeature;
    
        @Belief
        private String mybelief = "";
    
        @Goal(unique = true)
        public static class AchievePrint {
            public AchievePrint() {
                System.out.println("Goal created.");
            }
            @GoalCreationCondition(beliefs = "mybelief")
            public static boolean shouldCreate(UniqueGoalBDI agent) {
                return agent.mybelief.equals("start");
            }
    
            @GoalTargetCondition(beliefs = "mybelief")
            public static boolean isFinished(UniqueGoalBDI agent) {
                return agent.mybelief.equals("end");
            }
            @GoalFinished
            public void onFinished() {
                System.out.println("Goal finished.");
            }
        }
    
        @AgentBody
        public void body() {
            System.out.println("Start UniqueGoalBDI...");
            mybelief = "start";
            mybelief = "";
            mybelief = "start";
            executionFeature.waitForDelay(100).get();
            mybelief = "end";
            executionFeature.waitForDelay(100).get();
        }
    
    }
    

    The output of execution is:

    Start UniqueGoalBDI...
    Goal created.
    Goal created.
    Goal finished.
    Goal finished.
    

    So a second instance of the goal is created although the first is still active.
    Is this a bug? Or am I missing something? I'm using Jadex 3.0.117.

     
  • Lars Braubach

    Lars Braubach - 2020-06-18

    Hi,

    the unique flag works based on equals test on the goal class/object. So in your case they are all different as no equals method has been implemented.

    Best,
    Lars

     
  • Frederic Jacob

    Frederic Jacob - 2020-06-19

    Ah, thank you, this makes sense. Implementing hashCode() and equals() for my unique goals fixed the problem for me.
    I noticed that the constructor is still called twice, but only goal finishes. I assume this is expected behavior - so a second instance is created despite the unique flag, but then Jadex compares it to the first one via equals(), which returns true, and the second goal is dropped?

    I really think this "contract" about the unique flag should be found in the documentation somewhere - the only place where I found some information on it was in the outdated "Goals User Guide", which says:

    By default two goal instances of the same type are equal, when all parameters and parameter sets have the same values.

    Obviously, this seems not to directly apply to BDIv3.
    So a description in the API documentation for the "unique" element of the Goal annotation which mentions the equals() and hashCode() methods would be very helpful.

    Again, thank you for your fast reply!

     

Log in to post a comment.

MongoDB Logo MongoDB