use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method createLiteralRule.
private void createLiteralRule(final BaseDescr descr) {
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(Primitives.class.getName());
lhs.addDescr(pattern);
pattern.addConstraint(descr);
ruleDescr.setConsequence("");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
}
use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class PackageBuilderConfigurationTest method testMockDialect.
@Test
public void testMockDialect() {
InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.pkg1");
KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
MockDialectConfiguration mockConf = new MockDialectConfiguration();
// cfg1.buildDialectRegistry().addDialect( "mock",
// mockConf.getDialect() );
cfg1.addDialect("mock", mockConf);
cfg1.setDefaultDialect("mock");
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
assertSame(pkg, mockDialect2.getPkg());
assertNull(mockDialect2.getRuleDescr());
RuleDescr ruleDescr = new RuleDescr("test rule");
ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
ruleDescr.setLhs(new AndDescr());
EvalDescr evalDescr = new EvalDescr();
ruleDescr.getLhs().addDescr(evalDescr);
PackageDescr pkgDescr = new PackageDescr("org.pkg1");
pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
FunctionImportDescr functionImportDescr = new FunctionImportDescr();
functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
pkgDescr.addFunctionImport(functionImportDescr);
pkgDescr.addRule(ruleDescr);
builder.addPackage(pkgDescr);
assertSame(ruleDescr, mockDialect2.getRuleDescr());
assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
assertEquals("eval was built", evalDescr.getContent());
assertEquals("consequence was built", ruleDescr.getConsequence());
assertTrue(mockDialect2.isCompileAll());
assertNotNull(pkg.getRule("test rule"));
// make sure there were no other general errors.
assertFalse(builder.hasErrors());
}
use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class QueryBuilderTest method testRuleWithQuery.
@Test
public void testRuleWithQuery() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
QueryDescr queryDescr = new QueryDescr("query1");
queryDescr.addParameter("String", "$name");
queryDescr.addParameter("int", "$age");
queryDescr.addParameter("String", "$likes");
packageDescr.addRule(queryDescr);
AndDescr lhs = new AndDescr();
queryDescr.setLhs(lhs);
PatternDescr pattern = new PatternDescr(Person.class.getName());
lhs.addDescr(pattern);
pattern.addConstraint(new BindingDescr("$name", "name", true));
pattern.addConstraint(new BindingDescr("$age", "age", true));
pattern.addConstraint(new BindingDescr("$likes", "likes", true));
RuleDescr ruleDescr = new RuleDescr("rule-1");
packageDescr.addRule(ruleDescr);
lhs = new AndDescr();
ruleDescr.setLhs(lhs);
pattern = new PatternDescr(Cheese.class.getName());
lhs.addDescr(pattern);
pattern.addConstraint(new BindingDescr("$type", "type"));
pattern = new PatternDescr("query1");
pattern.setQuery(true);
lhs.addDescr(pattern);
ExprConstraintDescr expr = new ExprConstraintDescr("'bobba'");
expr.setPosition(0);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
expr = new ExprConstraintDescr("$age");
expr.setPosition(1);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
expr = new ExprConstraintDescr("$type");
expr.setPosition(2);
expr.setType(Type.POSITIONAL);
pattern.addConstraint(expr);
ruleDescr.setConsequence("System.out.println(\"age: \" + $age);");
builder.addPackage(packageDescr);
assertLength(0, builder.getErrors().getErrors());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
final KieSession session = kbase.newKieSession();
session.insert(new Person("bobba", "stilton", 90));
session.insert(new Person("bobba", "brie", 80));
session.insert(new Person("bobba", "brie", 75));
session.insert(new Person("darth", "brie", 100));
session.insert(new Person("luke", "brie", 25));
session.insert(new Cheese("brie", 25));
session.fireAllRules();
}
use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class NamedConsequenceVisitor method visit.
public void visit(ConditionalBranchDescr desc) {
PatternDescr patternRelated = (PatternDescr) getReferringPatternDescr(desc, (AndDescr) context.parentDesc);
Class<?> patternRelatedClass = getClassFromContext(context.getTypeResolver(), patternRelated.getObjectType());
MethodCallExpr whenBlock = whenThenDSL(desc, patternRelated, patternRelatedClass, WHEN_CALL, null);
recurseAmongElseBranch(patternRelatedClass, patternRelated, whenBlock, desc.getElseBranch());
}
use of org.drools.compiler.lang.descr.AndDescr in project drools by kiegroup.
the class AndHandler method end.
public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
final Element element = parser.endElementBuilder();
final AndDescr andDescr = (AndDescr) parser.getCurrent();
final Object parent = parser.getParent();
if (!andDescr.getDescrs().isEmpty()) {
if (parent instanceof RuleDescr || parent instanceof QueryDescr) {
final RuleDescr ruleDescr = (RuleDescr) parent;
ruleDescr.setLhs(andDescr);
} else if (parent instanceof MultiPatternDestinationDescr) {
final MultiPatternDestinationDescr mpDescr = (MultiPatternDestinationDescr) parent;
mpDescr.setInput(andDescr);
} else if (parent instanceof ConditionalElementDescr) {
final ConditionalElementDescr ceDescr = (ConditionalElementDescr) parent;
ceDescr.addDescr(andDescr);
}
}
return andDescr;
}
Aggregations