public KieSession newKieSession(
      String kSessionName, Environment environment, KieSessionConfiguration conf) {
    KieSessionModelImpl kSessionModel = (KieSessionModelImpl) getKieSessionModel(kSessionName);
    if (kSessionModel == null) {
      log.error("Unknown KieSession name: " + kSessionName);
      return null;
    }
    if (kSessionModel.getType() == KieSessionModel.KieSessionType.STATELESS) {
      throw new RuntimeException(
          "Trying to create a stateful KieSession from a stateless KieSessionModel: "
              + kSessionName);
    }
    KieBase kBase = getKieBase(kSessionModel.getKieBaseModel().getName());
    if (kBase == null) {
      log.error("Unknown KieBase name: " + kSessionModel.getKieBaseModel().getName());
      return null;
    }

    KieSession kSession =
        kBase.newKieSession(
            conf != null ? conf : getKnowledgeSessionConfiguration(kSessionModel), environment);
    wireListnersAndWIHs(kSessionModel, kSession);
    registerLoggers(kSessionModel, kSession);
    kSessions.put(kSessionName, kSession);
    return kSession;
  }
  /**
   * This method creates a stateful knowledge session per Drools event processor.
   *
   * @return The stateful knowledge session
   */
  private KieSession createSession() {
    KieSession ret = null;

    synchronized (SESSIONS) {
      ret = SESSIONS.get(getRuleName());

      if (ret == null) {
        KieBase kbase = loadRuleBase();

        if (kbase != null) {
          ret = kbase.newKieSession();

          if (ret != null) {
            ret.setGlobal("epc", _context);
            ret.fireAllRules();

            SESSIONS.put(getRuleName(), ret);
          } else {
            System.out.println("The kieSession is null!!!");
          }
        }
      }
    }

    return (ret);
  }
  protected KieSession createKnowledgeSession(String processFile) throws Exception {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    if (processFile != null) {
      Resource process = ResourceFactory.newClassPathResource(processFile);
      kfs.write(process);
    }

    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();

    if (kb.getResults().hasMessages(Level.ERROR)) {
      throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }

    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    KieBase kbase = kContainer.getKieBase();

    Environment env = EnvironmentFactory.newEnvironment();

    Properties defaultProps = new Properties();
    defaultProps.setProperty(
        "drools.processSignalManagerFactory", DefaultSignalManagerFactory.class.getName());
    defaultProps.setProperty(
        "drools.processInstanceManagerFactory",
        DefaultProcessInstanceManagerFactory.class.getName());
    SessionConfiguration conf = new SessionConfiguration(defaultProps);

    KieSession ksession = (StatefulKnowledgeSession) kbase.newKieSession(conf, env);
    return ksession;
  }
  @Override
  public void start(BundleContext context) throws Exception {

    KieServices ks = KieServices.Factory.get();
    ks.newKieClasspathContainer(getClass().getClassLoader());
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
    KieBase kbase = createKieBase();

    ksession = kbase.newKieSession();
    System.out.println("KieSession created.");

    for (int i = 0; i < 10; i++) {
      // Create a Cheese
      Cheese aCheese = EntityHelper.createCheese();
      ksession.insert(aCheese);

      // Fire the rules
      ksession.fireAllRules();

      // Check Cheese Price
      EntityHelper.cheesePrice(aCheese);
    }

    System.out.println("Cheese added and rules fired.");
  }
 private static KieSession createKieSession() {
   KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
   kbuilder.add(
       ResourceFactory.newClassPathResource("exceptions/ExceptionHandlingWithSignal.bpmn2"),
       ResourceType.BPMN2);
   KieBase kbase = kbuilder.newKnowledgeBase();
   return kbase.newKieSession();
 }
  protected void checkForValue(int value) {
    List<Integer> list = new ArrayList<Integer>();
    KieBase kieBase = (KieBase) container.getComponentInstance("KBase1");
    KieSession ksession = kieBase.newKieSession();

    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.dispose();
    assertTrue("Expected:<" + value + "> but was:<" + list.get(0) + ">", list.get(0) == value);
  }
Exemple #7
0
 @Test
 public void testMVELUsingGlobalsInDebugMode() throws Exception {
   MVELDebugHandler.setDebugMode(true);
   try {
     KieBase kbase = loadKnowledgeBase("test_MVELGlobalDebug.drl");
     KieSession ksession = kbase.newKieSession();
     ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
     ksession.dispose();
     MVELDebugHandler.setDebugMode(false);
   } catch (Exception e) {
     MVELDebugHandler.setDebugMode(false);
     e.printStackTrace();
     fail("Should not raise exceptions");
   }
 }
 /**
  * 执行规则
  *
  * @param order
  * @param userRule
  */
 public void executeRule(Order order, final UserRule userRule) {
   System.out.println("Thread name =" + Thread.currentThread().getId());
   // initKBase();
   loadPackage(userRule);
   KieSession kieStatefulSession = (KieSession) kBase.newKieSession();
   //        KieSession kieStatefulSession = kBase.newStatefulKnowledgeSession();
   kieStatefulSession.insert(order);
   kieStatefulSession.insert(userRule);
   //        kieStatefulSession.startProcess(userRule.getUserCode());
   kieStatefulSession.fireAllRules(
       new AgendaFilter() {
         @Override
         public boolean accept(Match match) {
           return match.getRule().getPackageName().endsWith(userRule.getUserCode());
         }
       });
   kieStatefulSession.dispose();
   clearKiePackages();
 }
Exemple #9
0
  @Test
  public void testLocalVariableMVELConsequence() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_LocalVariableMVELConsequence.drl");
    KieSession ksession = kbase.newKieSession();

    final List list = new ArrayList();
    ksession.setGlobal("results", list);

    ksession.insert(new Person("bob", "stilton"));
    ksession.insert(new Person("mark", "brie"));

    try {
      ksession.fireAllRules();

      assertEquals("should have fired twice", 2, list.size());

    } catch (Exception e) {
      e.printStackTrace();
      fail("Should not raise any exception");
    }
  }
Exemple #10
0
  @Test
  public void testHelloWorld() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_mvel.drl");
    KieSession ksession = kbase.newKieSession();

    final List list = new ArrayList();
    ksession.setGlobal("list", list);

    final List list2 = new ArrayList();
    ksession.setGlobal("list2", list2);

    Cheese c = new Cheese("stilton", 10);
    ksession.insert(c);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(BigInteger.valueOf(30), list.get(0));
    assertEquals(Integer.valueOf(22), list.get(1));

    assertEquals("hello world", list2.get(0));

    Date dt = DateUtils.parseDate("10-Jul-1974");
    assertEquals(dt, c.getUsedBy());
  }
 public KieSession createSession() {
   return droolsKnowledgeBase.newKieSession();
 }