Search in sources :

Example 91 with PackageDescr

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());
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 92 with PackageDescr

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());
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 93 with PackageDescr

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());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 94 with PackageDescr

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");
}
Also used : InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FromDescr(org.drools.compiler.lang.descr.FromDescr) MVELExprDescr(org.drools.compiler.lang.descr.MVELExprDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 95 with PackageDescr

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");
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)239 Test (org.junit.Test)192 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)44 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InputStreamReader (java.io.InputStreamReader)25 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)22 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14