Este contenido no está disponible en el idioma seleccionado.
Chapter 22. Hello World Example
22.1. HelloWorld Example: Creating the KnowledgeBase and Session Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- A
KnowledgeBuilderis used to turn a DRL source file intoPackageobjects which the Knowledge Base can consume. - The add method takes a
Resourceinterface and a Resource Type as parameters. TheResourcecan be used to retrieve a DRL source file from various locations; in this case the DRL file is being retrieved from the classpath using aResourceFactory, but it could come from a disk file or a URL. - Multiple packages of different namespaces can be added to the same Knowledge Base.
- While the Knowledge Base will validate the package, it will only have access to the error information as a String, so if you wish to debug the error information you should do it on the
KnowledgeBuilderinstance. - Once the builder is error free, get the
Packagecollection, instantiate aKnowledgeBasefrom theKnowledgeBaseFactoryand add the package collection.
22.2. HelloWorld Example: Event Logging and Auditing Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- Two default debug listeners are supplied:
DebugAgendaEventListenerandDebugWorkingMemoryEventListener. These print out debug event information to theSystem.errstream displayed in the Console window. - The
KnowledgeRuntimeLoggerprovides execution auditing which can be viewed in a graphical viewer. This logger is a specialised implementation built on the Agenda and Working Memory listeners. - When the engine has finished executing,
logger.close()must be called.
22.3. HelloWorld Example: Message Class Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The single class used in this example has two fields: the message, which is a String, and the status which can be one of the two integers
HELLOorGOODBYE.
22.4. HelloWorld Example: Execution Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- A single
Messageobject is created with the message text "Hello World" and the statusHELLOand then inserted into the engine, at which pointfireAllRules()is executed. - All network evaluation is done during the insert time. By the time the program execution reaches the
fireAllRules()method call the engine already knows which rules are fully matches and able to fire.
Note
To execute the example as a Java application:
- Open the class
org.drools.examples.helloworld.HelloWorldExamplein your Eclipse IDE. - Right-click the class and select
Run as...and thenJava application
22.5. HelloWorld Example: System.out in the Console Window Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Hello Goodbye
Hello
Goodbye
- By putting a breakpoint on the
fireAllRules()method and select theksessionvariable, you can see that the "Hello World" rule is already activated and on the Agenda, confirming that all the pattern matching work was already done during the insert. - The application print outs go to
System.outwhile the debug listener print outs go toSystem.err.
22.6. HelloWorld Example: Rule "Hello World" Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The LHS (after
when) section of the rule states that it will be activated for eachMessageobject inserted into the Working Memory whose status isMessage.HELLO. - Two variable bindings are created: the variable
messageis bound to themessageattribute and the variablemis bound to the matchedMessageobject itself. - The RHS (after
then) or consequence part of the rule is written using the MVEL expression language, as declared by the rule's attributedialect. - After printing the content of the bound variable
messagetoSystem.out, the rule changes the values of themessageandstatusattributes of theMessageobject bound tom. - MVEL's
modifystatement allows you to apply a block of assignments in one statement, with the engine being automatically notified of the changes at the end of the block.
22.7. HelloWorld Example: Using the "Debug as..." Option Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Procedure 22.1. Task
- To access this debugging option, open the class
org.drools.examples.HelloWorldin your Eclipse IDE. - Right-click the class and select "Debug as..." and then "Drools application". The rule will be shown along with information about where it is.
22.8. HelloWorld Example: Rule "Good Bye" Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The "Good Bye" rule, which specifies the "java" dialect, is similar to the "Hello World" rule except that it matches
Messageobjects whose status isMessage.GOODBYE