protected void checkIfGlobalScoreHolderExists(KieBase kieBase) { boolean hasGlobalScoreHolder = false; for (KiePackage kiePackage : kieBase.getKiePackages()) { for (Global global : kiePackage.getGlobalVariables()) { if (DroolsScoreDirector.GLOBAL_SCORE_HOLDER_KEY.equals(global.getName())) { hasGlobalScoreHolder = true; // TODO Fail fast once global.getType() can be turned into a Class instead of a String // if (!ScoreHolder.class.isAssignableFrom(global.getType())) { // throw new IllegalStateException("The global with name (" + global.getName() // + ") has a type (" + global.getType() // + ") that does not implement " + ScoreHolder.class.getSimpleName() + "."); // } break; } } } if (!hasGlobalScoreHolder) { throw new IllegalArgumentException("The kieBase with kiePackages (" + kieBase.getKiePackages() + ") has no global field called " + DroolsScoreDirector.GLOBAL_SCORE_HOLDER_KEY + ".\n" + "Check if the rule files are found and if the global field is spelled correctly."); } }
protected void createRuleToConstraintWeightExtractorMap(KieBase kieBase) { ConstraintConfigurationDescriptor<Solution_> constraintConfigurationDescriptor = solutionDescriptor.getConstraintConfigurationDescriptor(); if (constraintConfigurationDescriptor == null) { ruleToConstraintWeightExtractorMap = new LinkedHashMap<>(0); return; } Collection<ConstraintWeightDescriptor<Solution_>> constraintWeightDescriptors = constraintConfigurationDescriptor.getConstraintWeightDescriptors(); ruleToConstraintWeightExtractorMap = new LinkedHashMap<>(constraintWeightDescriptors.size()); for (ConstraintWeightDescriptor<Solution_> constraintWeightDescriptor : constraintWeightDescriptors) { String constraintPackage = constraintWeightDescriptor.getConstraintPackage(); String constraintName = constraintWeightDescriptor.getConstraintName(); Rule rule = kieBase.getRule(constraintPackage, constraintName); if (rule == null) { Rule potentialRule = kieBase.getKiePackages().stream().flatMap(kiePackage -> kiePackage.getRules().stream()) .filter(selectedRule -> selectedRule.getName().equals(constraintName)).findFirst().orElse(null); throw new IllegalStateException("The constraintConfigurationClass (" + constraintConfigurationDescriptor.getConstraintConfigurationClass() + ") has a " + ConstraintWeight.class.getSimpleName() + " annotated member (" + constraintWeightDescriptor.getMemberAccessor() + ") with constraintPackage/rulePackage (" + constraintPackage + ") and constraintName/ruleName (" + constraintName + ") for which no Drools rule exist in the DRL.\n" + (potentialRule != null ? "Maybe the constraintPackage (" + constraintPackage + ") is wrong," + " because there is a rule with the same ruleName (" + constraintName + "), but in a different rulePackage (" + potentialRule.getPackageName() + ")." : "Maybe there is a typo in the constraintName (" + constraintName + ") so it not identical to the constraint's ruleName.")); } ruleToConstraintWeightExtractorMap.put(rule, constraintWeightDescriptor.createExtractionFunction()); } }
for (KiePackage pkg : kbase.getKiePackages() ) { packageFile = new File(System.getProperty("java.io.tmpdir") + File.separator + pkg.getName()+".pkg"); packageFile.deleteOnExit();
/** * Helper method which determines the number of rules in the {@link KieBase}. * * @param kieBase * the {@link KieBase} * @return the number of rules in the {@link KieBase} */ private static long getNumberOfRules(KieBase kieBase) { long nrOfRules = 0; Collection<KiePackage> kiePackages = kieBase.getKiePackages(); for (KiePackage nextKiePackage : kiePackages) { Collection<Rule> rules = nextKiePackage.getRules(); System.out.println(rules); nrOfRules += rules.size(); } return nrOfRules; } }
@Override public Collection<KiePackage> getKiePackages() { return kieBase.getKiePackages(); }
@Override public Collection<KiePackage> getKiePackages() { return kieBase.getKiePackages(); }
final Collection<KiePackage> kpkgs = kieBase.getKiePackages(); final Collection<KiePackage> newCollection = new ArrayList<>(); for (KiePackage kpkg : kpkgs) {
@Override public Collection<KiePackage> getKiePackages() { return getKieBase().getKiePackages(); }
@Test(timeout=10000) public void testRemovePackageSubNetwork() throws Exception { KieBase kbase = loadKnowledgeBase( "test_DynamicRulesWithSubnetwork.drl" ); String packageName = kbase.getKiePackages().iterator().next().getName();
@Override public Collection<KiePackage> getKiePackages() { return getKieBase().getKiePackages(); }
/** * * @return A String detailing the packages and rules in this knowledge base. */ public static String kbaseDetails(KieBase kbase) { StringBuilder sb = new StringBuilder(); for (KiePackage p : kbase.getKiePackages()) { sb.append("\n Package : " + p.getName()); for (Rule r : p.getRules()) { sb.append("\n Rule: " + r.getName()); } } return "Knowledge base built with packages: " + sb.toString(); }
/** * * @return A String detailing the packages and rules in this knowledge base. */ public static String kbaseDetails(KieBase kbase) { StringBuilder sb = new StringBuilder(); for (KiePackage p : kbase.getKiePackages()) { sb.append("\n Package : " + p.getName()); for (Rule r : p.getRules()) { sb.append("\n Rule: " + r.getName()); } } return "Knowledge base built with packages: " + sb.toString(); }
/** * Return a string containing the packages used to build the knowledge base. */ public static String knowledgeBaseDetails(KieBase kbase) { if (kbase == null) { return "Knowledge Base is null."; } else { StringBuilder sb = new StringBuilder( "Knowledge base built from the following packages:"); Collection<KiePackage> packages = kbase .getKiePackages(); for (KiePackage kp : packages) { sb.append("\n Package: [" + kp.getName() + "]"); for (Rule rule : kp.getRules()) { sb.append("\n Rule: [" + rule.getName() + "]"); } } return sb.toString(); } }
/** * Return a string containing the packages used to build the knowledge base. */ public static String knowledgeBaseDetails(KieBase kbase) { if (kbase == null) { return "Knowledge Base is null."; } else { StringBuilder sb = new StringBuilder( "Knowledge base built from the following packages:"); Collection<KiePackage> packages = kbase .getKiePackages(); for (KiePackage kp : packages) { sb.append("\n Package: [" + kp.getName() + "]"); for (Rule rule : kp.getRules()) { sb.append("\n Rule: [" + rule.getName() + "]"); } } return sb.toString(); } }
File file = new File(outputFolder, kbase.replace('.', '_').toLowerCase()); FileOutputStream out = new FileOutputStream(file); DroolsStreamUtils.streamOut(out, kb.getKiePackages()); out.close();
File file = new File(outputFolder, kbase.replace('.', '_').toLowerCase()); FileOutputStream out = new FileOutputStream(file); DroolsStreamUtils.streamOut(out, kb.getKiePackages()); out.close();
@Test public void testEncoding() throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("org/drools/container/spring/resourceWithEncoding.xml"); KieBase kbase = (KieBase) context.getBean("kbase"); assertNotNull( kbase ); for (KiePackage pkg : kbase.getKiePackages()) { assertEquals("sample acçéntèd rule", pkg.getRules().iterator().next().getName()); } }
/** * Tests injection of a KieBase defined in Spring application context. */ @Test public void testDRL() { KieBase kbase = getKieBase("kbaseDRL"); assertNotNull("KieBase should not be null", kbase); assertEquals("Unexpected number of packages in kbase", 1, kbase.getKiePackages().size()); }
private void testNonEmptyKieBase(final String kieBaseName) throws Exception { KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieBase kieBase = kContainer.getKieBase(kieBaseName); assertNotNull("KieBase not found", kieBase); assertFalse("Unexpected number of KiePackages in KieBase", kieBase.getKiePackages().isEmpty()); }
for (KiePackage pkg : kbase.getKiePackages() ) { packageFile = new File(System.getProperty("java.io.tmpdir") + File.separator + pkg.getName()+".pkg"); packageFile.deleteOnExit();