Package org.drools.logger

Examples of org.drools.logger.KnowledgeRuntimeLogger


        final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

        final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/trouble_ticket");

        final Customer a = new Customer( "A",
                                         "Drools",
                                         "Gold" );
        final Customer b = new Customer( "B",
                                         "Drools",
                                         "Platinum" );
        final Customer c = new Customer( "C",
                                         "Drools",
                                         "Silver" );
        final Customer d = new Customer( "D",
                                         "Drools",
                                         "Silver" );

        final Ticket t1 = new Ticket( a );
        final Ticket t2 = new Ticket( b );
        final Ticket t3 = new Ticket( c );
        final Ticket t4 = new Ticket( d );

        ksession.insert( a );
        ksession.insert( b );
        ksession.insert( c );
        ksession.insert( d );

        ksession.insert( t1 );
        ksession.insert( t2 );
        final FactHandle ft3 = ksession.insert( t3 );
        ksession.insert( t4 );

        ksession.fireAllRules();

        t3.setStatus( "Done" );

        ksession.update( ft3,
                         t3 );

        try {
            System.err.println( "[[ Sleeping 5 seconds ]]" );
            Thread.sleep( 5000 );
        } catch ( final InterruptedException e ) {
            e.printStackTrace();
        }

        System.err.println( "[[ awake ]]" );

        ksession.dispose();

        logger.close();
    }
View Full Code Here


    ksession.addEventListener(new DebugAgendaEventListener());
    ksession.addEventListener(new DebugWorkingMemoryEventListener());

    // setup the audit logging
    KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory
        .newFileLogger(ksession, "log/helloworld");

    final Message message = new Message();
    message.setMessage("Hello World");
    message.setStatus(Message.HELLO);
    ksession.insert(message);

    ksession.fireAllRules();

    logger.close();

    ksession.dispose();
  }
View Full Code Here

            //load up the rulebase
            KnowledgeBase kbase = readRule();

            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/cdss");

            // set globals
            RecommendationService recommendationService = new RecommendationService();
            ksession.setGlobal( "recommendationService",
                                     recommendationService );
       
            // create patient
            Patient patient = new Patient();
            patient.setName( "John Doe" );
            patient.setAge( 20 );
            ksession.insert( patient );

            // Go!
            Diagnose diagnose = new Diagnose( Terminology.DIAGNOSE_X );
            ksession.insert( diagnose );
            ksession.fireAllRules();

            // Print out recommendations
            List<Recommendation> recommendations = recommendationService.getRecommendations();
            for ( Recommendation recommendation: recommendations ) {
                System.out.println( recommendation );
            }
            recommendations.clear();

            // Simulate a diagnose: incomplete results
            diagnose = new Diagnose( Terminology.DIAGNOSE_X_TYPE_UNKNOWN );
            ksession.insert( diagnose );
            ksession.fireAllRules();

            // Print out recommendations
            recommendations = recommendationService.getRecommendations();
            for ( Recommendation recommendation: recommendations ) {
                System.out.println( recommendation );
            }
            recommendations.clear();

            // Simulate a diagnose: type2
            diagnose = new Diagnose( Terminology.DIAGNOSE_X_TYPE2 );
            ksession.insert( diagnose );
            ksession.fireAllRules();

            logger.close();

        } catch ( Throwable t ) {
            t.printStackTrace();
        }
    }
View Full Code Here

        final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

        final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/numberguess");

        ksession.insert( new GameRules( 100,
                                        5 ) );
        ksession.insert( new RandomNumber() );
        ksession.insert( new Game() );

        ksession.startProcess( "Number Guess" );
        ksession.fireAllRules();

        logger.close();

        ksession.dispose();
    }
View Full Code Here

        final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );

        final StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log/fibonacci");

        ksession.insert( new Fibonacci( 10 ) );

        ksession.fireAllRules();

    logger.close();

        ksession.dispose(); // Stateful rule session must always be disposed when finished

    }
View Full Code Here

        {
          LOGGER.trace("adding default facts into fact list for stateless session [" + sid + "]");
        }
        facts.addAll(objectList);
      }
      KnowledgeRuntimeLogger statelessRuntimeLogger = getRuntimeLogger(ruleInfo, statelessSession);
      if (statelessRuntimeLogger != null && LOGGER.isDebugEnabled())
      {
        LOGGER.debug("created new runtime logger [" + getId(statelessRuntimeLogger) + "]");
      }
      if (LOGGER.isTraceEnabled())
      {
        LOGGER.trace("setting globals delegate for stateless session [" + sid + "]");
      }
      statelessSession.getGlobals().setDelegate(new StatelessGlobals(globals));
      try
      {
        if (LOGGER.isDebugEnabled())
        {
          LOGGER.debug("calling execute(Iterable) on stateless session [" + sid + "]");
        }
        statelessSession.execute(facts);
      }
      finally
      {
        statelessSession.getGlobals().setDelegate(null);
        if (statelessRuntimeLogger != null)
        {
          if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("calling close() on runtime logger [" + getId(statelessRuntimeLogger) + "]");
          }
          statelessRuntimeLogger.close();
        }
      }
    }
    finally
    {
View Full Code Here

        if (statefulSession != null && !continueState)
        {
          final String ssid = getId(statefulSession, statefulSessionCount);
          final StatefulKnowledgeSession disposedStatefulSession = statefulSession;
          final Thread haltedStatefulFireUntilHaltThread = statefulFireUntilHaltThread;
          final KnowledgeRuntimeLogger closedStatefulRuntimeLogger = statefulRuntimeLogger;
          statefulSession = null;
          statefulFireUntilHaltThread = null;
          statefulRuntimeLogger = null;
          // Maybe halt the session
          if (haltedStatefulFireUntilHaltThread != null)
          {
            if (LOGGER.isDebugEnabled())
            {
              LOGGER.debug("calling halt on stateful session [" + ssid + "] - no continue set");
            }
            disposedStatefulSession.halt();
            if (LOGGER.isDebugEnabled())
            {
              LOGGER.debug("joining thread [" + haltedStatefulFireUntilHaltThread.getName() + "] for stateful session [" + ssid + "]");
            }
            try
            {
              haltedStatefulFireUntilHaltThread.join();
            }
            catch (InterruptedException ie)
            {
              LOGGER.error("interrupted thread [" + haltedStatefulFireUntilHaltThread.getName() + "] for stateful session [" + ssid + "]", ie);
            }
          }
          // Always dispose the session
          if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("calling dispose() on stateful session [" + ssid + "] - no continue set");
          }
          disposedStatefulSession.dispose();
          // Maybe close the logger
          if (closedStatefulRuntimeLogger != null)
          {
            if (LOGGER.isDebugEnabled())
            {
              LOGGER.debug("calling close() on runtime logger [" + getId(closedStatefulRuntimeLogger, statefulSessionCount) + "] - no continue set");
            }
            closedStatefulRuntimeLogger.close();
          }
        }

        final String ssid;
        final boolean isStatefulSessionNew;
        if (statefulSession == null)
        {
          isStatefulSessionNew = true;
          KnowledgeSessionConfiguration statefulSessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
          setClockType(ruleInfo, statefulSessionConfiguration);
          statefulSession = ruleBase.newStatefulKnowledgeSession(statefulSessionConfiguration, EnvironmentFactory.newEnvironment());
          ssid = getId(statefulSession, statefulSessionCount);
          if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("created new stateful session [" + ssid + "]");
          }
          setChannels(ruleInfo, statefulSession);
          statefulRuntimeLogger = getRuntimeLogger(ruleInfo, statefulSession);
          if (statefulRuntimeLogger != null && LOGGER.isDebugEnabled())
          {
            LOGGER.debug("created new runtime logger [" + getId(statefulRuntimeLogger, statefulSessionCount) + "]");
          }
        }
        else
        {
          isStatefulSessionNew = false;
          ssid = getId(statefulSession, statefulSessionCount);
          if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("reusing old stateful session [" + ssid + "]");
          }
        }

        try
        {
          final Map<String, Object> globals = ruleInfo.getGlobals();
          if (globals != null)
          {
            if (LOGGER.isTraceEnabled())
            {
              LOGGER.trace("calling setGlobal(String,Object) on stateful session [" + ssid + "] for each global");
            }
            final Set<Entry<String, Object>> entrySet = globals.entrySet();
            for(Entry<String, Object> entry : entrySet)
            {
              statefulSession.setGlobal( entry.getKey(), entry.getValue() );
            }
          }

          if (LOGGER.isTraceEnabled())
          {
            LOGGER.trace("inserting Message on stateful session [" + ssid + "]");
          }
          // Always insert the ESB Message object.
          InternalFactHandle handle = (InternalFactHandle)statefulSession.insert(message);
          if (handle.isEvent() && LOGGER.isDebugEnabled())
          {
            EventFactHandle ef = (EventFactHandle)handle;
            LOGGER.debug("event [" + ef.getObject().getClass().getName() + "], startTimeStamp [" + ef.getStartTimestamp() + "]");
          }

          // Always insert the default facts (into the main WorkingMemory; no entry-point specified)
          final List<Object> defaultFacts = ruleInfo.getDefaultFacts();
          if (defaultFacts != null)
          {
            if (LOGGER.isTraceEnabled())
            {
              LOGGER.trace("calling insert(Object) on stateful session [" + ssid + "] for each default fact");
            }
            for(Object object : defaultFacts)
            {
              statefulSession.insert(object);
            }
          }

          // Maybe insert entry point facts (into a named WorkingMemoryEntryPoint)
          final Map<String,List<Object>> facts = ruleInfo.getFacts();
          if (facts != null)
          {
            if (LOGGER.isTraceEnabled())
            {
              LOGGER.trace("calling insert(Object) on stateful session [" + ssid + "] for each entry point fact");
            }
            for(Entry<String, List<Object>> entry : facts.entrySet())
            {
              String entryPointName = entry.getKey();
              // Insert objects that have explicitly specified an entry-point.
              WorkingMemoryEntryPoint wmep = statefulSession.getWorkingMemoryEntryPoint(entryPointName);
              if (wmep == null)
              {
                throw new RuleServiceException("The entry-point '" + entryPointName + "' was not found in the current stateful session. Please double check your rules source");
              }
              for(Object fact : entry.getValue())
              {
                wmep.insert(fact);
              }
            }
          }

          // Fire stateful rules.
          if (!fireUntilHalt)
          {
            if (LOGGER.isDebugEnabled())
            {
              LOGGER.debug("calling fireAllRules() on stateful session [" + ssid + "]");
            }
            statefulSession.fireAllRules();
          }
          else if (isStatefulSessionNew)
          {
            final String threadName = new StringBuilder()
              .append(getClass().getSimpleName())
              .append(":fireUntilHalt(")
              .append(ssid)
              .append(")")
              .toString();
            if (LOGGER.isDebugEnabled())
            {
              LOGGER.debug("spawning fireUntilHalt() on stateful session [" + ssid + "] in thread [" + threadName + "]");
            }
            final ClassLoader goodClassLoader = Thread.currentThread().getContextClassLoader();
            statefulFireUntilHaltThread = new Thread(new Runnable() {
              public void run() {
                Thread thread = Thread.currentThread();
                ClassLoader origClassLoader = thread.getContextClassLoader();
                thread.setContextClassLoader(goodClassLoader);
                try {
                  if (LOGGER.isDebugEnabled())
                  {
                    LOGGER.debug("calling fireUntilHalt() on stateful session [" + ssid + "] in thread [" + threadName + "]");
                  }
                  statefulSession.fireUntilHalt();
                } catch (NullPointerException npe) {
                  LOGGER.warn("fireUntilHalt() not called on stateful session [" + ssid + "] in thread [" + threadName + "] - already halt()ed and dispose()d: " + npe.getMessage());
                } finally {
                  thread.setContextClassLoader(origClassLoader);
                }
              }
            });
            statefulFireUntilHaltThread.setName(threadName);
            statefulFireUntilHaltThread.setDaemon(true);
            statefulFireUntilHaltThread.start();
          }
          else if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("rule firing unnecessary on stateful session [" + ssid + "] - was initially fireUntilHalt()");
          }
        }
        finally
        {
          if (dispose)
          {
            final StatefulKnowledgeSession disposedStatefulSession = statefulSession;
            final Thread haltedStatefulFireUntilHaltThread = statefulFireUntilHaltThread;
            final KnowledgeRuntimeLogger closedStatefulRuntimeLogger = statefulRuntimeLogger;
            statefulSession = null;
            statefulFireUntilHaltThread = null;
            statefulRuntimeLogger = null;
            // Maybe halt the session
            if (haltedStatefulFireUntilHaltThread != null)
            {
              if (LOGGER.isDebugEnabled())
              {
                LOGGER.debug("calling halt() on stateful session [" + ssid + "]");
              }
              disposedStatefulSession.halt();
              if (LOGGER.isDebugEnabled())
              {
                LOGGER.debug("joining thread [" + haltedStatefulFireUntilHaltThread.getName() + "] for stateful session [" + ssid + "]");
              }
              try
              {
                haltedStatefulFireUntilHaltThread.join();
              }
              catch (InterruptedException ie)
              {
                LOGGER.error("interrupted thread [" + haltedStatefulFireUntilHaltThread.getName() + "] for stateful session [" + ssid + "]", ie);
              }
            }
            // Always dispose the session
            if (LOGGER.isDebugEnabled())
            {
              LOGGER.debug("calling dispose() on stateful session [" + ssid + "]");
            }
            disposedStatefulSession.dispose();
            // Maybe close the logger
            if (closedStatefulRuntimeLogger != null)
            {
              if (LOGGER.isDebugEnabled())
              {
                LOGGER.debug("calling close() on runtime logger [" + getId(closedStatefulRuntimeLogger, statefulSessionCount) + "]");
              }
              closedStatefulRuntimeLogger.close();
            }
          }
        }
      }
      finally
View Full Code Here

      if ((statefulSessionCount == 0) && (statefulSession != null))
      {
        final String ssid = getId(statefulSession, statefulSessionCount);
        final StatefulKnowledgeSession disposedStatefulSession = statefulSession;
        final Thread haltedStatefulFireUntilHaltThread = statefulFireUntilHaltThread;
        final KnowledgeRuntimeLogger closedStatefulRuntimeLogger = statefulRuntimeLogger;
        statefulSession = null;
        statefulFireUntilHaltThread = null;
        statefulRuntimeLogger = null;
        // Maybe halt the session
        if (haltedStatefulFireUntilHaltThread != null)
        {
          if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("calling halt() on stateful session [" + ssid + "]");
          }
          disposedStatefulSession.halt();
          if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("joining thread [" + haltedStatefulFireUntilHaltThread.getName() + "] for stateful session [" + ssid + "]");
          }
          try
          {
            haltedStatefulFireUntilHaltThread.join();
          }
          catch (InterruptedException ie)
          {
            LOGGER.error("interrupted thread [" + haltedStatefulFireUntilHaltThread.getName() + "] for stateful session [" + ssid + "]", ie);
          }
        }
        // Always dispose the session
        if (LOGGER.isDebugEnabled())
        {
          LOGGER.debug("calling dispose() on stateful session [" + ssid + "]");
        }
        disposedStatefulSession.dispose();
        // Maybe close the logger
        if (closedStatefulRuntimeLogger != null)
        {
          if (LOGGER.isDebugEnabled())
          {
            LOGGER.debug("calling close() on runtime logger [" + getId(closedStatefulRuntimeLogger, statefulSessionCount) + "]");
          }
          closedStatefulRuntimeLogger.close();
        }
      }
      statelessSessions.clear();
    }
    finally
View Full Code Here

        {
            // TODO Add support for stateless sessions, for now we assume all sessions are stateful.
            throw new ConfigurationException(MessageFactory.createStaticMessage("Stateless sessions are not yet supported"));
        }
        StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
        KnowledgeRuntimeLogger droolsLogger = new WorkingMemorySLF4JLogger(session, logger);
       
        if (messageService == null)
        {
            throw new ConfigurationException(MessageFactory.createStaticMessage("MessageService is not available"));
        }
View Full Code Here

  public static final void main(String[] args) {
    try {
      // load up the knowledge base
      KnowledgeBase kbase = readKnowledgeBase();
      StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
      KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
      // go !
      Message message = new Message();
      message.setMessage("Hello World");
      message.setStatus(Message.HELLO);
      ksession.insert(message);
      ksession.fireAllRules();
      logger.close();
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
View Full Code Here

TOP

Related Classes of org.drools.logger.KnowledgeRuntimeLogger

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.