Search in sources :

Example 6 with NotDescr

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

the class RuleParserTest method testRuleParseLhs3.

@Test
public void testRuleParseLhs3() throws Exception {
    final String text = "rule X when (or\nnot Person()\n(and Cheese()\nMeat()\nWine())) then end";
    AndDescr pattern = ((RuleDescr) parse("rule", text)).getLhs();
    assertEquals(1, pattern.getDescrs().size());
    OrDescr or = (OrDescr) pattern.getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    NotDescr not = (NotDescr) or.getDescrs().get(0);
    AndDescr and = (AndDescr) or.getDescrs().get(1);
    assertEquals(1, not.getDescrs().size());
    PatternDescr person = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals(3, and.getDescrs().size());
    PatternDescr cheese = (PatternDescr) and.getDescrs().get(0);
    assertEquals("Cheese", cheese.getObjectType());
    PatternDescr meat = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Meat", meat.getObjectType());
    PatternDescr wine = (PatternDescr) and.getDescrs().get(2);
    assertEquals("Wine", wine.getObjectType());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 7 with NotDescr

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

the class RuleParserTest method testRuleOldSyntax1.

@Test
public void testRuleOldSyntax1() throws Exception {
    final String source = "rule \"Test\" when ( not $r :LiteralRestriction( operator == Operator.EQUAL ) ) then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("Test", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    assertEquals(1, ((NotDescr) rule.getLhs().getDescrs().get(0)).getDescrs().size());
    NotDescr notDescr = (NotDescr) rule.getLhs().getDescrs().get(0);
    PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().get(0);
    assertEquals("$r", patternDescr.getIdentifier());
    assertEquals(1, patternDescr.getDescrs().size());
    ExprConstraintDescr fieldConstraintDescr = (ExprConstraintDescr) patternDescr.getDescrs().get(0);
    assertEquals("operator == Operator.EQUAL", fieldConstraintDescr.getExpression());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 8 with NotDescr

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

the class CEDescrBuilderImpl method not.

/**
 * {@inheritDoc}
 */
public CEDescrBuilder<CEDescrBuilder<P, T>, NotDescr> not() {
    CEDescrBuilder<CEDescrBuilder<P, T>, NotDescr> not = new CEDescrBuilderImpl<CEDescrBuilder<P, T>, NotDescr>(this, new NotDescr());
    ((ConditionalElementDescr) descr).addDescr(not.getDescr());
    return not;
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) CEDescrBuilder(org.drools.compiler.lang.api.CEDescrBuilder) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 9 with NotDescr

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

the class KnowledgeBuilderTest method testNot.

@Test
public void testNot() throws Exception {
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    // Make sure we can't accessa  variable bound inside the not node
    RuleImpl rule = createRule(new NotDescr(), builder, "update(stilton);");
    assertTrue(builder.hasErrors());
    builder = new KnowledgeBuilderImpl();
    rule = createRule(new NotDescr(), builder, "");
    assertEquals(0, builder.getErrors().getErrors().length);
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement not = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, not.getChildren());
    final Pattern pattern = (Pattern) not.getChildren().get(0);
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) Pattern(org.drools.core.rule.Pattern) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 10 with NotDescr

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

NotDescr (org.drools.compiler.lang.descr.NotDescr)16 Test (org.junit.Test)12 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)11 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)10 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)7 AndDescr (org.drools.compiler.lang.descr.AndDescr)6 OrDescr (org.drools.compiler.lang.descr.OrDescr)4 ExistsDescr (org.drools.compiler.lang.descr.ExistsDescr)3 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)3 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)2 FromDescr (org.drools.compiler.lang.descr.FromDescr)2 InputStreamReader (java.io.InputStreamReader)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 CEDescrBuilder (org.drools.compiler.lang.api.CEDescrBuilder)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)1 GroupElement (org.drools.core.rule.GroupElement)1 Pattern (org.drools.core.rule.Pattern)1 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)1 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)1