Uploaded image for project: 'OptaPlanner'
  1. OptaPlanner
  2. PLANNER-582

DroolsScoreDirector should support generating a unit test reproducer

    XMLWordPrintable

Details

    • Task
    • Resolution: Done
    • Major
    • 7.0.0.Beta2
    • None
    • optaplanner-core
    • None

    Description

      When we detect score corruption as a bug in Drools, such as DROOLS-1174, we (Mario, JiriL and Geoffrey) need to end up with a Drools only unit test reproducer. For this purpose, what we do know is: first we isolate (JiriL) in OptaPlanner and the Mario trial-and-errors to reproduce in a pure drools unit test. We need to automate this more, as it happened 34 times already and will happen again.

      Automating the isolation itself is non-trivial and not in the scope of this issue. Here, we want to automate the isolated OptaPlanner reproducer into a Drools unit test, which can be done this way:

      public class DroolsScoreDirector ... { // class in OptaPlanner
      
          private final boolean generateDroolsUnitTest = false; // New flag
      
          protected KieSession kieSession;
          ...
      
             public Score calculateScore() {
              variableListenerSupport.assertNotificationQueuesAreEmpty();
              if (generateDroolsUnitTest) {
                  System.out.println("kieSession.fireAllRules();");
              }
              kieSession.fireAllRules();
              ...
          }
      
          private void update(Object entity) {
              if (generateDroolsUnitTest) {
                  System.out.println("FactHandle factHandle = kieSession.getFactHandle(" + entity + ");");
                  System.out.println("kieSession.update(factHandle, " + entity + ");");
              }
              FactHandle factHandle = kieSession.getFactHandle(entity);
              ...
              kieSession.update(factHandle, entity);
          }
      
          @Override
          public void afterVariableChanged(VariableDescriptor variableDescriptor, Object entity) {
              System.out.println(entity + ".set"+ variableDescriptor.getVariableName().makeFirstCharCapital() + "(" + variableDescriptor.getValue(entity) + ");");
              update(entity);
              super.afterVariableChanged(variableDescriptor, entity);
          }
      
      }
      

      If we then make all the domain entity's toString() return their variable name, we can use this to generate a Drools unit test.

      Pitfall: during FAST/FULL_ASSERT with no assertionScoreDirectorFactory (which for example overwrites the from scratch assertions to use an EasyScorecalculator), there will be multiple DroolScoreDirector instances and we 'll need to make sure only the real one System.out's. That's possible.

      Attachments

        Issue Links

          Activity

            People

              jlocker Jiří Locker
              gdesmet@redhat.com Geoffrey De Smet (Inactive)
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: