Uploaded image for project: 'Drools'
  1. Drools
  2. DROOLS-795

Timers are reset during the serialization process

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 6.3.0.CR1
    • None
    • None
    • None

      When the marshall serializes a timer it doesn't take count of how much time it is already passed, so when the timer is deserialized it restart from it start time. The following failing test case reproduces the problem.

          @Test
          public void testMarshallWithTimedRule() {
      
              String drl = "rule \"Rule A Timeout\"\n" +
                           "when\n" +
                           "    String( this == \"ATrigger\" )\n" +
                           "then\n" +
                           "   insert (new String( \"A-Timer\") );\n" +
                           "System.out.println(\"+++++++Got ATrigger, started A-Timer with 5s timeout\");\n" +
                           "end\n" +
                           "\n" +
                           "rule \"Timer For rule A Timeout\"\n" +
                           "    timer ( int: 5s )\n" +
                           "when\n" +
                           "   String( this == \"A-Timer\")\n" +
                           "then\n" +
                           "   delete ( \"A-Timer\" );\n" +
                           "   delete ( \"ATrigger\" );\n" +
                           "System.out.println(\"******* reset rule A based on timer\");\n" +
                           "end\n";
      
              KieBase kbase = new KieHelper().addContent( drl, ResourceType.DRL )
                                             .build( EqualityBehaviorOption.EQUALITY,
                                                     DeclarativeAgendaOption.ENABLED,
                                                     EventProcessingOption.STREAM );
      
              KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
              sessionConfig.setOption( ClockTypeOption.get( "pseudo" ) );
              KieSession ksession = kbase.newKieSession(sessionConfig, null);
      
              ksession.insert( new String( "ATrigger" ) );
      
              assertEquals( 1, ksession.getFactCount() );
              ksession.fireAllRules();
              assertEquals( 2, ksession.getFactCount() );
      
              SessionPseudoClock clock = ksession.getSessionClock();
              clock.advanceTime( 4, TimeUnit.SECONDS );
      
              assertEquals( 2, ksession.getFactCount() );
              ksession.fireAllRules();
              assertEquals( 2, ksession.getFactCount() );
      
              ksession = marshallAndUnmarshall( kbase, ksession, sessionConfig);
              clock = ksession.getSessionClock();
      
              clock.advanceTime( 4, TimeUnit.SECONDS );
      
              assertEquals( 2, ksession.getFactCount() );
              ksession.fireAllRules();
              assertEquals( 0, ksession.getFactCount() );
          }
      
          public static KieSession marshallAndUnmarshall(KieBase kbase, KieSession ksession, KieSessionConfiguration sessionConfig) {
              // Serialize and Deserialize
              try {
                  Marshaller marshaller = KieServices.Factory.get().getMarshallers().newMarshaller(kbase);
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  marshaller.marshall(baos, ksession);
                  marshaller = MarshallerFactory.newMarshaller( kbase );
                  ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                  baos.close();
                  ksession = marshaller.unmarshall(bais, sessionConfig, null);
                  bais.close();
              } catch (Exception e) {
                  e.printStackTrace();
                  fail("unexpected exception :" + e.getMessage());
              }
              return ksession;
          }
      

              mfusco@redhat.com Mario Fusco
              mfusco@redhat.com Mario Fusco
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: