Search in sources :

Example 71 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class RuleBuilder method getLhsForRuleUnit.

private static AndDescr getLhsForRuleUnit(RuleImpl rule, AndDescr lhs) {
    if (rule.hasRuleUnit()) {
        PatternDescr unitPattern = new PatternDescr(rule.getRuleUnitClassName(), RULE_UNIT_DECLARATION);
        unitPattern.setSource(EntryPointDescr.RULE_UNIT_ENTRY_POINT_DESCR);
        unitPattern.setResource(rule.getResource());
        lhs.getDescrs().add(0, unitPattern);
    }
    return lhs;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr)

Example 72 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr 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 73 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr 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 74 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr 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)

Example 75 with PatternDescr

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

Aggregations

PatternDescr (org.drools.compiler.lang.descr.PatternDescr)150 Test (org.junit.Test)122 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)84 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)56 AndDescr (org.drools.compiler.lang.descr.AndDescr)52 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)19 Cheese (org.drools.compiler.Cheese)14 FromDescr (org.drools.compiler.lang.descr.FromDescr)14 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 NotDescr (org.drools.compiler.lang.descr.NotDescr)11 OrDescr (org.drools.compiler.lang.descr.OrDescr)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 Element (org.w3c.dom.Element)9 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 InputStreamReader (java.io.InputStreamReader)7 List (java.util.List)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7