package fape.core.planning.search.strategies.flaws;

import fape.core.planning.planner.Planner;
import fape.core.planning.search.flaws.flaws.Flaw;
import fape.core.planning.search.flaws.flaws.UnmotivatedAction;
import fape.core.planning.search.flaws.flaws.UnrefinedTask;
import fape.core.planning.states.State;

public class HierarchicalFirstComp implements FlawComparator {

    public final State st;
    public final Planner planner;

    public HierarchicalFirstComp(State st, Planner planner) {
        this.st = st;
        this.planner = planner;
    }

    @Override
    public String shortName() {
        return "hf";
    }

    private int priority(Flaw flaw) {
        if(flaw instanceof UnrefinedTask)
            return 3;
        else if(flaw instanceof UnmotivatedAction)
            return 4;
        else
            return 5;
    }


    @Override
    public int compare(Flaw o1, Flaw o2) {
        return priority(o1) - priority(o2);
    }
}

@@ -140,16 +140,69 @@
 s.Flaw;%0A
+import fape.core.planning.search.flaws.flaws.Threat;%0A
 import f
@@ -482,16 +482,55 @@
 planner;
+%0A    public final boolean threatsFirst;
 %0A%0A    pu
@@ -635,24 +635,79 @@
  = planner;%0A
+        threatsFirst = st.pb.allActionsAreMotivated();%0A
     %7D%0A%0A    @
@@ -787,19 +787,22 @@
 private 
-int
+double
  priorit
@@ -831,59 +831,265 @@
  if(
-flaw instanceof UnrefinedTask)%0A            return 3
+threatsFirst && flaw instanceof Threat)%0A            return 0.;%0A        else if(flaw instanceof UnrefinedTask)%0A            return 1. + ((double) st.getEarliestStartTime(((UnrefinedTask) flaw).task.start())) / ((double) st.getEarliestStartTime(st.pb.end())+1)
 ;%0A  
@@ -1157,16 +1157,17 @@
 return 4
+.
 ;%0A      
@@ -1193,16 +1193,17 @@
 return 5
+.
 ;%0A    %7D%0A
@@ -1276,16 +1276,34 @@
  return 
+(int) Math.signum(
 priority
@@ -1321,16 +1321,17 @@
 rity(o2)
+)
 ;%0A    %7D%0A

Update default flaw selection strategy on hierarchical domains: threats first, then unrefined tasks.