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()); } }
@Override public Rule getRule( String s, String s1 ) { return kieBase.getRule( s, s1 ); }
@Override public Rule getRule( String s, String s1 ) { return kieBase.getRule( s, s1 ); }
@Override public Rule getRule( String packageName, String ruleName ) { return getKieBase().getRule( packageName, ruleName ); }
@Override public Rule getRule( String packageName, String ruleName ) { return getKieBase().getRule( packageName, ruleName ); }
@Test public void test() { // RULE CheeseWorld_11 has salience "=3+2" RuleImpl cheeseWorld11 = (RuleImpl) kieBase.getRule( "test", "CheeseWorld_11" ); assertEquals( 5, cheeseWorld11.getSalience().getValue() ); // RULE CheeseWorld_12 has salience "=ROW()" RuleImpl cheeseWorld12 = (RuleImpl) kieBase.getRule( "test", "CheeseWorld_12" ); assertEquals( 12, cheeseWorld12.getSalience().getValue() ); } }
kieBase.getRule("org.drools.test", "Row 1 test1"));
kieBase.getRule("org.drools.test", "Row 1 test"));