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

Timers are reset during the serialization process

    Details

    • Type: Bug
    • Status: Resolved (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 6.3.0.CR1
    • Component/s: None
    • Labels:
      None

      Description

      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;
          }
      

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                mfusco Mario Fusco
                Reporter:
                mfusco Mario Fusco
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: