use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testErrors.
@Test
public void testErrors() 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"));
pattern.addConstraint(new ExprConstraintDescr("price == x"));
// There is no m this should produce errors.
ruleDescr.setConsequence("update(m);");
builder.addPackage(packageDescr);
assertTrue(builder.getErrors().getErrors().length > 0);
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testAccumulate.
@Test
public void testAccumulate() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseAccumulate.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
Object patternobj = obj.getLhs().getDescrs().get(0);
assertTrue(patternobj instanceof PatternDescr);
final PatternDescr patterncheese = (PatternDescr) patternobj;
assertEquals(patterncheese.getIdentifier(), "cheese");
assertEquals(patterncheese.getObjectType(), "Cheese");
AccumulateDescr accumulatedescr = (AccumulateDescr) patterncheese.getSource();
assertEquals("total += $cheese.getPrice();", accumulatedescr.getActionCode());
assertEquals("int total = 0;", accumulatedescr.getInitCode());
assertEquals("new Integer( total ) );", accumulatedescr.getResultCode());
patternobj = obj.getLhs().getDescrs().get(1);
assertTrue(patternobj instanceof PatternDescr);
final PatternDescr patternmax = (PatternDescr) patternobj;
assertEquals(patternmax.getIdentifier(), "max");
assertEquals(patternmax.getObjectType(), "Number");
accumulatedescr = (AccumulateDescr) patternmax.getSource();
assertTrue(accumulatedescr.isExternalFunction());
assertEquals("max", accumulatedescr.getFunctions().get(0).getFunction());
assertNull(accumulatedescr.getInitCode());
assertNull(accumulatedescr.getActionCode());
assertNull(accumulatedescr.getResultCode());
assertNull(accumulatedescr.getReverseCode());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseForall.
@Test
public void testParseForall() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseForall.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
ForallDescr forall = (ForallDescr) obj.getLhs().getDescrs().get(0);
List forallPaterns = forall.getDescrs();
PatternDescr pattarnState = (PatternDescr) forallPaterns.get(0);
PatternDescr personState = (PatternDescr) forallPaterns.get(1);
PatternDescr cheeseState = (PatternDescr) forallPaterns.get(2);
assertEquals(pattarnState.getObjectType(), "State");
assertEquals(personState.getObjectType(), "Person");
assertEquals(cheeseState.getObjectType(), "Cheese");
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseQuery.
@Test
public void testParseQuery() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseQuery.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
assertEquals("com.sample", packageDescr.getName());
final List imports = packageDescr.getImports();
assertEquals(2, imports.size());
assertEquals("java.util.HashMap", ((ImportDescr) imports.get(0)).getTarget());
assertEquals("org.drools.mvel.compiler.*", ((ImportDescr) imports.get(1)).getTarget());
final List globals = packageDescr.getGlobals();
assertEquals(2, globals.size());
final GlobalDescr x = (GlobalDescr) globals.get(0);
final GlobalDescr yada = (GlobalDescr) globals.get(1);
assertEquals("com.sample.X", x.getType());
assertEquals("x", x.getIdentifier());
assertEquals("com.sample.Yada", yada.getType());
assertEquals("yada", yada.getIdentifier());
final FunctionDescr functionDescr = (FunctionDescr) packageDescr.getFunctions().get(0);
final List names = functionDescr.getParameterNames();
assertEquals("foo", names.get(0));
assertEquals("bada", names.get(1));
final List types = functionDescr.getParameterTypes();
assertEquals("Bar", types.get(0));
assertEquals("Bing", types.get(1));
assertEquals("System.out.println(\"hello world\");", functionDescr.getText().trim());
final QueryDescr queryDescr = (QueryDescr) packageDescr.getRules().get(0);
assertEquals("my query", queryDescr.getName());
final AndDescr lhs = queryDescr.getLhs();
assertEquals(1, lhs.getDescrs().size());
final PatternDescr patternDescr = (PatternDescr) lhs.getDescrs().get(0);
assertEquals("Foo", patternDescr.getObjectType());
}
use of org.drools.drl.ast.descr.PatternDescr in project drools by kiegroup.
the class PatternBuilderForQueryTest method testQuery.
@Test
public void testQuery() throws Exception {
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr = new PackageDescr("p1");
final QueryDescr queryDescr = new QueryDescr("query1");
queryDescr.addParameter("String", "$type");
packageDescr.addRule(queryDescr);
final AndDescr lhs = new AndDescr();
queryDescr.setLhs(lhs);
final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
lhs.addDescr(pattern);
pattern.addConstraint(new ExprConstraintDescr("type == $type"));
// Another query, no parameters
QueryDescr queryDescr2 = new QueryDescr("query2");
packageDescr.addRule(queryDescr2);
AndDescr lhs2 = new AndDescr();
queryDescr2.setLhs(lhs2);
PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
lhs2.addDescr(pattern2);
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 Cheese("stilton", 15));
QueryResults results = session.getQueryResults("query1", "stilton");
assertEquals(1, results.size());
Object object = results.iterator().next().get("stilton");
assertEquals(new Cheese("stilton", 15), object);
results = session.getQueryResults("query1", new Object[] { "cheddar" });
assertEquals(0, results.size());
session.insert(new Cheese("dolcelatte", 20));
results = session.getQueryResults("query2", new Object[] {});
assertEquals(2, results.size());
}
Aggregations