use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderImpl method compileRete.
protected void compileRete(PackageDescr packageDescr) {
if (!hasErrors() && this.kBase != null) {
Collection<RuleImpl> rulesToBeAdded = new ArrayList<>();
for (RuleDescr ruleDescr : packageDescr.getRules()) {
if (filterAccepts(ResourceChange.Type.RULE, ruleDescr.getNamespace(), ruleDescr.getName())) {
InternalKnowledgePackage pkg = pkgRegistryMap.get(ruleDescr.getNamespace()).getPackage();
rulesToBeAdded.add(pkg.getRule(ruleDescr.getName()));
}
}
if (!rulesToBeAdded.isEmpty()) {
this.kBase.addRules(rulesToBeAdded);
}
}
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class DefaultTemplateRuleBaseTest method testSimpleTemplate.
@SuppressWarnings("unchecked")
@Test
public void testSimpleTemplate() throws Exception {
TemplateContainer tc = new TemplateContainer() {
private Column[] columns = new Column[] { new LongColumn("column1"), new LongColumn("column2"), new StringColumn("column3") };
public Column[] getColumns() {
return columns;
}
public String getHeader() {
return null;
}
public Map<String, RuleTemplate> getTemplates() {
Map<String, RuleTemplate> templates = new HashMap<String, RuleTemplate>();
RuleTemplate ruleTemplate = new RuleTemplate("template1", this);
ruleTemplate.addColumn("column1 == 10");
ruleTemplate.addColumn("column2 < 5 || > 20");
ruleTemplate.addColumn("column3 == \"xyz\"");
templates.put("template1", ruleTemplate);
return templates;
}
public Column getColumn(String name) {
return columns[Integer.parseInt(name.substring(6)) - 1];
}
};
DefaultTemplateRuleBase ruleBase = new DefaultTemplateRuleBase(tc);
InternalKnowledgePackage[] packages = ((KnowledgeBaseImpl) ruleBase.newStatefulSession().getKieBase()).getPackages();
assertEquals(1, packages.length);
Map<String, String> globals = packages[0].getGlobals();
assertEquals(DefaultGenerator.class.getName(), globals.get("generator"));
Collection<org.kie.api.definition.rule.Rule> rules = packages[0].getRules();
assertEquals(1, rules.size());
assertEquals("template1", rules.iterator().next().getName());
GroupElement lhs = ((RuleImpl) rules.iterator().next()).getLhs();
// when
// r : Row()
// column1 : Column(name == "column1")
// exists LongCell(row == r, column == column1, value == 10)
// column2 : Column(name == "column2")
// exists LongCell(row == r, column == column2, value < 5 | > 20)
// column3 : Column(name == "column3")
// exists StringCell(row == r, column == column3, value == "xyz")
assertEquals(7, lhs.getChildren().size());
org.drools.core.rule.Pattern pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(1);
assertEquals(1, pattern.getConstraints().size());
Constraint constraint = pattern.getConstraints().get(0);
GroupElement exists = (GroupElement) lhs.getChildren().get(2);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
IndexableConstraint vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
assertEquals("column1", vconstraint.getRequiredDeclarations()[0].getIdentifier());
pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(3);
assertEquals(1, pattern.getConstraints().size());
constraint = pattern.getConstraints().get(0);
exists = (GroupElement) lhs.getChildren().get(4);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
assertEquals("column2", vconstraint.getRequiredDeclarations()[0].getIdentifier());
pattern = (org.drools.core.rule.Pattern) lhs.getChildren().get(5);
assertEquals(1, pattern.getConstraints().size());
constraint = pattern.getConstraints().get(0);
exists = (GroupElement) lhs.getChildren().get(6);
pattern = (org.drools.core.rule.Pattern) exists.getChildren().get(0);
assertEquals(3, pattern.getConstraints().size());
vconstraint = (IndexableConstraint) pattern.getConstraints().get(1);
assertEquals(Column.class, vconstraint.getFieldIndex().getExtractor().getExtractToClass());
assertEquals("column3", vconstraint.getRequiredDeclarations()[0].getIdentifier());
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class PatternBuilder method getTypeDeclaration.
private TypeDeclaration getTypeDeclaration(RuleBuildContext context, Class<?> userProvidedClass) {
String packageName = ClassUtils.getPackage(userProvidedClass);
KnowledgeBuilderImpl kbuilder = context.getKnowledgeBuilder();
PackageRegistry pkgr = kbuilder.getPackageRegistry(packageName);
TypeDeclaration typeDeclaration = pkgr != null ? pkgr.getPackage().getTypeDeclaration(userProvidedClass) : null;
if (typeDeclaration == null && kbuilder.getKnowledgeBase() != null) {
// check if the type declaration is contained only in the already existing kbase (possible during incremental compilation)
InternalKnowledgePackage pkg = kbuilder.getKnowledgeBase().getPackage(packageName);
typeDeclaration = pkg != null ? pkg.getTypeDeclaration(userProvidedClass) : null;
}
if (typeDeclaration == null) {
typeDeclaration = context.getPkg().getTypeDeclaration(userProvidedClass);
}
return typeDeclaration;
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testSerializable.
@Test
public void testSerializable() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
// check that packageDescr is serializable
final PackageDescr back = (PackageDescr) SerializationHelper.serializeObject(packageDescr);
assertNotNull(back);
assertEquals("p1", back.getName());
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage newPkg = SerializationHelper.serializeObject(pkg);
final RuleImpl newRule = newPkg.getRule("rule-1");
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
// It's been serialised so we have to simulate the re-wiring process
newPkg.getDialectRuntimeRegistry().onAdd(kBase.getRootClassLoader());
newPkg.getDialectRuntimeRegistry().onBeforeExecute();
kBase.getGlobals().put("map", Map.class);
final KieSession workingMemory = kBase.newKieSession();
final HashMap map = new HashMap();
workingMemory.setGlobal("map", map);
final LeftTupleImpl tuple = new MockTuple(new HashMap());
tuple.setLeftTupleSink(new RuleTerminalNode(1, new CompositeObjectSinkAdapterTest.MockBetaNode(), newRule, newRule.getLhs(), 0, new BuildContext(kBase)));
final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
final DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testTimeWindowBehavior.
@Test
public void testTimeWindowBehavior() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
typeDeclDescr.addAnnotation("role", "event");
packageDescr.addTypeDeclaration(typeDeclDescr);
final RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
final AndDescr lhs = new AndDescr();
ruleDescr.setLhs(lhs);
final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
final BehaviorDescr windowDescr = new BehaviorDescr("window");
windowDescr.setSubType("time");
windowDescr.setParameters(Collections.singletonList("60000"));
patternDescr.addBehavior(windowDescr);
lhs.addDescr(patternDescr);
ruleDescr.setConsequence("System.out.println( $tick );");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
final RuleImpl rule = pkg.getRule("rule-1");
assertNotNull(rule);
final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
final Behavior window = pattern.getBehaviors().get(0);
assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
Aggregations