OneStopTesting.com - Testing EBooks, Tutorials, Articles, Jobs, Training Institutes etc.
OneStopGate.com - Gate EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopMBA.com - MBA EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopIAS.com - IAS EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopSAP.com - SAP EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
OneStopGRE.com - of GRE EBooks, Tutorials, Articles, FAQs, Jobs, Training Institutes etc.
Loading a Rule Resource Using Drools API | Articles | Recent Articles | News Article | Interesting Articles | Technology Articles | Articles On Education | Articles On Corporate | Company Articles | College Articles | Articles on Recession
Home » Articles » Loading a Rule Resource Using Drools API
Loading a Rule Resource Using Drools API
Article Posted On Date : Friday, May 22, 2009
Loading a Rule Resource Using Drools API
Advertisements
HTML clipboard Loading a Rule Resource Using Drools API With an understanding of the Drools API descriptions in the previous section, you are ready to follow a conventional set of steps to load a rule resource and make a test. For this exercise, you will load the vote.drl rule resource: Create a KnowledgeBuilder to add a resource (you will add the vote.drl , but in the same manner you can load a decision table): KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("vote.drl"), ResourceType.DRL); Check for potential errors: KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error: errors) { System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); } Create a KnowledgeBase that contains all the relevant process definitions and other knowledge types like rules: KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); You need to create a session to interact with the engine: StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); Obtain a logger for getting the generated events (you will use a file logger, but a console logger is also available): KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "log"); Provide a test case to the vote.drl . Here you will provide a Vote instance, but you can adjust this according to your DRL: ksession.insert(vote); Where, for example, vote is: Vote vote = new Vote(); vote.setAverage(8.4f); "Fire all rules," so that all the rules from your DRL will be checked against your test case: ksession.fireAllRules(); Clean up. logger.close(); ksession.dispose(); Now, you can put everything together in a complete functional application (DroolsTest.java ). You can execute this as you would any Java application, as long as the vote.drl and Drools libraries are available.
Amazon.in Widgets