Ce contenu n'est pas disponible dans la langue sélectionnée.
6.2. Building and Deploying
6.2.1. KnowledgePackage Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
A Knowledge Package is a collection of Knowledge Definitions, such as rules and processes. It is created by the Knowledge Builder. Knowledge Packages are self-contained and serializable, and they currently form the basic deployment unit.
Figure 6.1. KnowledgePackage interface
Note
A Knowledge Package instance cannot be reused once it's added to the Knowledge Base. If you need to add it to another Knowledge Base, serialize it and use the "cloned" result.
6.2.2. Creating a new KnowledgeBase Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Procedure 6.2. Task
- Use this default configuration to create a new KnowledgeBase:
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - If a custom class loader was used with the
KnowledgeBuilder
to resolve types not in the default class loader, then that must also be set on theKnowledgeBase
. The technique for this is the same as with theKnowledgeBuilder
and is shown below:KnowledgeBaseConfiguration kbaseConf = KnowledgeBaseFactory.createKnowledgeBaseConfiguration( null, cl ); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase( kbaseConf );
KnowledgeBaseConfiguration kbaseConf = KnowledgeBaseFactory.createKnowledgeBaseConfiguration( null, cl ); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase( kbaseConf );
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
6.2.3. Add KnowledgePackages to a KnowledgeBase Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
In-process building is the simplest form of deployment. It compiles the knowledge definitions and adds them to the Knowledge Base in the same JVM. This approach requires
drools-core.jar
and drools-compiler.jar
to be on the classpath.
Procedure 6.3. Task
- To add KnowledgePackages to a KnowledgeBase, use this code:
Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages(); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages( kpkgs );
Collection<KnowledgePackage> kpkgs = kbuilder.getKnowledgePackages(); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages( kpkgs );
Copy to Clipboard Copied! Toggle word wrap Toggle overflow TheaddKnowledgePackages(kpkgs)
method can be called iteratively to add additional knowledge.
6.2.4. Building and Deployment in Separate Processes Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Both the
KnowledgeBase
and the KnowledgePackage
are units of deployment and serializable. This means you can have one machine do any necessary building, requiring drools-compiler.jar
, and have another machine deploy and execute everything, needing only drools-core.jar
.
6.2.5. Writing the KnowledgePackage to an OutputStream Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
This is the code for writing the KnowledgePackage to an OutputStream:
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( fileName ) ); out.writeObject( kpkgs ); out.close();
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( fileName ) );
out.writeObject( kpkgs );
out.close();
6.2.6. Reading the KnowledgePackage from an InputStream Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
Use this code for reading the KnowledgePackage from an InputStream:
6.2.7. StatefulknowledgeSessions and KnowledgeBase Modifications Copier lienLien copié sur presse-papiers!
Copier lienLien copié sur presse-papiers!
The
KnowledgeBase
creates and returns StatefulKnowledgeSession
objects and can optionally keep references to them.
When
KnowledgeBase
modifications occur, they are applied against the data in the sessions. This reference is a weak reference and it is also optional. It is controlled by a boolean flag.