private List<Object> applyRules(
     List<Object> treeObjects, Element newChildElementData, Element scopeElement)
     throws Exception {
   List<Rule> rules =
       ruleRepository.getRuleBywatchObjectAndWatchCategoryAndWatchSubType(
           Element.class.toString(), scopeElement.getCategory(), scopeElement.getSubType());
   for (Object o : treeObjects) {
     if (o instanceof Element && ((Element) o).getId().equals(scopeElement.getId())) {
       rootElement = (Element) o;
     }
   }
   for (Rule rule : rules) {
     System.out.println("Applying Rule :" + rule.getRuleName());
     KnowledgeBase kbase = createKnowledgeBase(rule.getRuleFile(), rule.getRuleFileType());
     StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
     StringTokenizer st = new StringTokenizer(rule.getRuleTriggerFact(), "|");
     String triggerFactPackage = st.nextToken();
     String triggerFactName = st.nextToken();
     FactType triggerFact = kbase.getFactType(triggerFactPackage, triggerFactName);
     Object triggerFactInstance = triggerFact.newInstance();
     if (rule.getRuleTriggerFactParams() != null) {
       triggerFactInstance =
           setTriggerFactInstanceParams(
               triggerFact, triggerFactInstance, rule.getRuleTriggerFactParams());
     }
     if (rule.getRuleParams() != null) {
       ksession = setSessionVariables(ksession, rule.getRuleParams());
     }
     ksession.insert(triggerFactInstance);
     ksession.insert(rootElement);
     ksession.fireAllRules();
     ksession.dispose();
   }
   return treeObjects;
 }
  @Test
  public void testEventExpires() throws Exception {
    String timerRule =
        "package org.drools.test\n"
            + "declare TestEvent \n"
            + "    @role( event )\n"
            + "    @expires( 10s )\n"
            + "end\n"
            + ""
            + "rule TimerRule \n"
            + "    when \n"
            + "        TestEvent( ) from entry-point \"Test\"\n"
            + "    then \n"
            + "end";
    KnowledgeBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KnowledgePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addKnowledgePackages(kpackages);
    StatefulKnowledgeSession ksession = createSession(kbase);

    FactType type = kbase.getFactType("org.drools.test", "TestEvent");
    Assert.assertNotNull("could not get type", type);

    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.getWorkingMemoryEntryPoint("Test").insert(type.newInstance());
    ksession.fireAllRules();
    ksession = disposeAndReloadSession(ksession, kbase);

    ksession = disposeAndReloadSession(ksession, kbase);
  }
  @Override
  public void prepare(TestCase testCase) {

    facts = new ArrayList<Object>(maxStep);

    FactType inputObject = ksession.getKnowledgeBase().getFactType("opencds.test", "InputObject");

    try {
      Object obj = null;
      obj = inputObject.newInstance();
      inputObject.set(obj, "id", "00A001");

      facts.add(obj);
      ksession.insert(obj);
    } catch (InstantiationException e) {
      e.printStackTrace(); // To change body of catch statement use File | Settings | File
      // Templates.
    } catch (IllegalAccessException e) {
      e.printStackTrace(); // To change body of catch statement use File | Settings | File
      // Templates.
    }
    wCounter = 0;
    tCounter++;
  }
 private Object setTriggerFactInstanceParams(
     FactType triggerFact, Object triggerFactInstance, String triggerFactInstanceParams)
     throws Exception {
   StringTokenizer st = new StringTokenizer(triggerFactInstanceParams, "|");
   while (st.hasMoreTokens()) {
     StringTokenizer st1 = new StringTokenizer(st.nextToken(), ",");
     String paramName = st1.nextToken();
     String paramValue = st1.nextToken();
     Object o = null;
     if (paramValue.equalsIgnoreCase("EXIST")) {
       Field f = getClass().getDeclaredField(paramName);
       System.out.println(f.get(this));
       o = f.get(this);
     }
     triggerFact.set(triggerFactInstance, paramName, o);
   }
   return triggerFactInstance;
 }
  @Override
  public void prepare(TestCase testCase) {

    facts = new ArrayList<Object>(maxStep);

    FactType observationResult =
        ksession.getKnowledgeBase().getFactType("opencds.test", "ObservationResult2");
    try {
      for (int j = 0; j < maxStep; j++) {

        Object obs = null;
        obs = observationResult.newInstance();
        CD cdFocus = new CD();
        cdFocus.setCodeSystem("AHRQ v4.3");
        cdFocus.setCode("10220");
        observationResult.set(
            obs, "id", "001" + Integer.toString(j)); // UUID.randomUUID().toString());
        observationResult.set(obs, "observationFocus", cdFocus);
        observationResult.set(obs, "subjectIsFocalPerson", true);

        CD cdCoded = new CD();
        cdCoded.setCodeSystem("AHRQ v4.3");
        cdCoded.setCode("34254");
        ObservationValue obsValue = new ObservationValue();
        obsValue.setConcept(cdCoded);
        obsValue.setIdentifier(UUID.randomUUID().toString());
        observationResult.set(obs, "observationValue", obsValue);

        facts.add(obs);
        ksession.insert(obs);
      }
    } catch (InstantiationException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    } catch (IllegalAccessException e) {
      e
          .printStackTrace(); // To change body of catch statement use File | Settings | File
                              // Templates.
    }
  }