use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class PatternBuilder method buildQuery.
private RuleConditionElement buildQuery(RuleBuildContext context, PatternDescr descr, PatternDescr patternDescr) {
RuleConditionElement rce = null;
// it might be a recursive query, so check for same names
if (context.getRule().getName().equals(patternDescr.getObjectType())) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) context.getRule());
}
if (rce == null) {
// look up the query in the current package
RuleImpl rule = context.getPkg().getRule(patternDescr.getObjectType());
if (rule instanceof QueryImpl) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) rule);
}
}
if (rce == null) {
// the query may have been imported, so try package imports
for (String importName : context.getDialect().getTypeResolver().getImports()) {
importName = importName.trim();
int pos = importName.indexOf('*');
if (pos >= 0) {
String pkgName = importName.substring(0, pos - 1);
PackageRegistry pkgReg = context.getKnowledgeBuilder().getPackageRegistry(pkgName);
if (pkgReg != null) {
RuleImpl rule = pkgReg.getPackage().getRule(patternDescr.getObjectType());
if (rule instanceof QueryImpl) {
// it's a query so delegate to the QueryElementBuilder
rce = buildQueryElement(context, descr, (QueryImpl) rule);
break;
}
}
}
}
}
if (rce == null) {
// this isn't a query either, so log an error
registerDescrBuildError(context, patternDescr, "Unable to resolve ObjectType '" + patternDescr.getObjectType() + "'");
}
return rce;
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class RuleMetadataTest method testInsert2.
@Test
public void testInsert2() {
String rule1 = "insert( new B(1, $ab) );";
KieBase kbase = getKnowledgeBase(rule1);
RuleImpl rule = getRule(kbase, "R0");
ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
assertEquals(1, consequenceMetaData.getStatements().size());
ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
assertEquals(ConsequenceMetaData.Statement.Type.INSERT, statment.getType());
assertEquals(RuleMetadataTest.B.class.getName(), statment.getFactClassName());
}
use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.
the class RuleMetadataTest method testInsert.
@Test
public void testInsert() {
String rule1 = "insert( new A(1, $bb, \"3\") );";
KieBase kbase = getKnowledgeBase(rule1);
RuleImpl rule = getRule(kbase, "R0");
ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
assertEquals(1, consequenceMetaData.getStatements().size());
ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
assertEquals(ConsequenceMetaData.Statement.Type.INSERT, statment.getType());
assertEquals("org.drools.A", statment.getFactClassName());
assertEquals(3, statment.getFields().size());
ConsequenceMetaData.Field field1 = statment.getFields().get(0);
assertEquals("a", field1.getName());
assertEquals("1", field1.getValue());
assertTrue(field1.isLiteral());
ConsequenceMetaData.Field field2 = statment.getFields().get(1);
assertEquals("b", field2.getName());
assertEquals("$bb", field2.getValue());
assertFalse(field2.isLiteral());
ConsequenceMetaData.Field field3 = statment.getFields().get(2);
assertEquals("c", field3.getName());
assertEquals("\"3\"", field3.getValue());
assertTrue(field3.isLiteral());
}
use of org.drools.core.definitions.rule.impl.RuleImpl 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.rule.impl.RuleImpl 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