private KnowledgeBase loadKnowledgeBase(String fileName) { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource(fileName, getClass()), ResourceType.DRL); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { logger.warn(error.toString()); } throw new IllegalArgumentException("Could not parse knowledge."); } assertFalse(kbuilder.hasErrors()); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); Collection<KnowledgePackage> knowledgePackages = kbuilder.getKnowledgePackages(); kbase.addKnowledgePackages(knowledgePackages); return kbase; }
private KnowledgeBase readKnowledgeBase(String resource) throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration(); config.setInputType(DecisionTableInputType.XLS); kbuilder.add( ResourceFactory.newClassPathResource(resource, getClass()), ResourceType.DTABLE, config); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); return kbase; }
@Test public void testCollectAfterOrCE() throws Exception { // Set up facts final Cheesery bonFromage = new Cheesery(); bonFromage.addCheese(new Cheese("cheddar")); bonFromage.addCheese(new Cheese("cheddar")); // Test in memory compile of DRL KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource("test_OrCEFollowedByCollect.drl", getClass()), ResourceType.DRL); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { logger.warn(error.toString()); } throw new IllegalArgumentException("Could not parse knowledge."); } assertFalse(kbuilder.hasErrors()); Collection<KnowledgePackage> knowledgePackages = kbuilder.getKnowledgePackages(); KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(knowledgePackages); StatefulKnowledgeSession session = createKnowledgeSession(kbase); session.insert(bonFromage); int rules = session.fireAllRules(); assertEquals(2, rules); // Serialize and test again knowledgePackages = SerializationHelper.serializeObject(knowledgePackages); kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(knowledgePackages); session = createKnowledgeSession(kbase); session.insert(bonFromage); rules = session.fireAllRules(); assertEquals(2, rules); }