use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclaration.
@Test
public void testTypeDeclaration() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.compiler");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("StockTick");
typeDescr.addAnnotation(Role.class.getCanonicalName(), "Event");
typeDescr.addAnnotation(TypeSafe.class.getCanonicalName(), "true");
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
if (builder.hasErrors()) {
fail(builder.getErrors().toString());
}
InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
assertEquals(1, pkg.getTypeDeclarations().size());
TypeDeclaration type = pkg.getTypeDeclaration("StockTick");
assertTrue(type.isTypesafe());
assertEquals(Role.Type.EVENT, type.getRole());
assertEquals(StockTick.class, type.getTypeClass());
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationWithFieldMetadata.
@Test
public void testTypeDeclarationWithFieldMetadata() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("TypeWithFieldMeta");
TypeFieldDescr f1 = new TypeFieldDescr("field", new PatternDescr("String"));
f1.addAnnotation("custom", null);
typeDescr.addField(f1);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
final FactType factType = bp.getFactType("org.drools.compiler.test.TypeWithFieldMeta");
assertNotNull(factType);
final FactField field = factType.getField("field");
assertNotNull(field);
final Map<String, Object> fieldMetaData = field.getMetaData();
assertNotNull("No field-level custom metadata got compiled", fieldMetaData);
assertTrue("Field metadata does not include expected value", fieldMetaData.containsKey("custom"));
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testNull.
@Test
public void testNull() {
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);
final PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
final FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
patternDescr.addConstraint(literalDescr);
ruleDescr.setConsequence("");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testReload.
@Test
public void testReload() 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) );");
builder.addPackage(packageDescr);
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
RuleImpl rule = pkg.getRule("rule-1");
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addGlobal("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(), rule, rule.getLhs(), 0, new BuildContext(kBase)));
final Activation activation = new MockActivation(rule, 0, rule.getLhs(), tuple);
DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(1), map.get("value"));
ruleDescr.setConsequence("map.put(\"value\", new Integer(2) );");
pkg.removeRule(rule);
// Make sure the compiled classes are also removed
assertEquals(0, ((JavaDialectRuntimeData) pkg.getDialectRuntimeRegistry().getDialectData("java")).getStore().size());
builder.addPackage(packageDescr);
pkg = builder.getPackage(packageDescr.getName());
rule = pkg.getRule("rule-1");
knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
knowledgeHelper.setActivation(activation);
rule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
assertEquals(new Integer(2), map.get("value"));
}
use of org.drools.core.definitions.InternalKnowledgePackage in project drools by kiegroup.
the class KnowledgeBuilderTest method testEval.
@Test
public void testEval() 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);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
pattern.addConstraint(fieldBindingDescr);
fieldBindingDescr = new BindingDescr("y", "price");
pattern.addConstraint(fieldBindingDescr);
packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
final EvalDescr evalDescr = new EvalDescr("( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
lhs.addDescr(evalDescr);
ruleDescr.setConsequence("update(stilton);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
final RuleImpl rule = pkg.getRule("rule-1");
final EvalCondition eval = (EvalCondition) rule.getLhs().getChildren().get(1);
final CompiledInvoker invoker = (CompiledInvoker) eval.getEvalExpression();
final List list = invoker.getMethodBytecode();
}
Aggregations