use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class QueryBuilderTest 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());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class AnnotationsTest method testCollectAnnotationsParsingAndBuilding.
@Test
public void testCollectAnnotationsParsingAndBuilding() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
kBuilder.addPackage(new PackageDescr("org.drools"));
String ruleDrl = "package org.drools.compiler; " + " " + "dialect 'mvel' " + " " + "import java.util.Collection; " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + " when " + " Collection() from collect ( " + " String() @Annot " + " ) " + " then " + "end " + "";
final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
// just checking there is no parsing errors
assertFalse(parser.getErrors().toString(), parser.hasErrors());
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
final RuleImpl rule = kBuilder.getPackage(pkgDescr.getName()).getRule("test collect with annotation");
List<? extends RuleConditionElement> nested = ((Pattern) rule.getLhs().getChildren().get(0)).getSource().getNestedElements();
assertEquals(1, nested.size());
Map<String, AnnotationDefinition> annotations = ((Pattern) nested.get(0)).getAnnotations();
assertEquals(1, annotations.size());
assertNotNull(annotations.keySet().iterator().next());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class AnnotationsTest method testAnnotationOnLHSAndMerging.
@Test
public void testAnnotationOnLHSAndMerging() throws Exception {
final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
String ruleDrl = "package org.drools.compiler; " + " " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + " when " + " ( and @Annot " + " String() " + " Integer() ) " + " then " + "end " + "";
final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
kBuilder.addPackage(pkgDescr);
assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseFrom.
@Test
public void testParseFrom() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseFrom.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
PatternDescr patterndescr = (PatternDescr) obj.getLhs().getDescrs().get(0);
FromDescr from = (FromDescr) patterndescr.getSource();
MVELExprDescr accessordescriptor = (MVELExprDescr) from.getDataSource();
assertEquals("cheesery.getCheeses(i+4)", accessordescriptor.getExpression());
assertEquals(patterndescr.getObjectType(), "Cheese");
assertEquals(patterndescr.getIdentifier(), "cheese");
}
use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.
the class XmlPackageReaderTest method testParseExists.
@Test
public void testParseExists() throws Exception {
final XmlPackageReader xmlPackageReader = getXmReader();
xmlPackageReader.read(new InputStreamReader(getClass().getResourceAsStream("test_ParseExists.xml")));
final PackageDescr packageDescr = xmlPackageReader.getPackageDescr();
assertNotNull(packageDescr);
RuleDescr obj = (RuleDescr) packageDescr.getRules().get(0);
Object existdescr = obj.getLhs().getDescrs().get(0);
assertTrue(existdescr instanceof ExistsDescr);
Object patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
assertTrue(patternDescriptor instanceof PatternDescr);
assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Person");
Object notDescr = obj.getLhs().getDescrs().get(1);
assertEquals(notDescr.getClass().getName(), NotDescr.class.getName());
existdescr = ((NotDescr) notDescr).getDescrs().get(0);
patternDescriptor = ((ExistsDescr) existdescr).getDescrs().get(0);
assertTrue(patternDescriptor instanceof PatternDescr);
assertEquals(((PatternDescr) patternDescriptor).getObjectType(), "Cheese");
}
Aggregations