Search in sources :

Example 1 with ExistsDescr

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

the class RuleParserTest method testNotExistWithBrackets.

@Test
public void testNotExistWithBrackets() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_exist_with_brackets.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertEquals(2, lhs.getDescrs().size());
    final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
    assertEquals(1, not.getDescrs().size());
    final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Cheese", pattern.getObjectType());
    final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
    assertEquals(1, ex.getDescrs().size());
    final PatternDescr exPattern = (PatternDescr) ex.getDescrs().get(0);
    assertEquals("Foo", exPattern.getObjectType());
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 2 with ExistsDescr

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

the class CEDescrBuilderImpl method exists.

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

Example 3 with ExistsDescr

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

the class KnowledgeBuilderTest method testExists.

@Test
public void testExists() throws Exception {
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    // Make sure we can't accessa  variable bound inside the not node
    RuleImpl rule = createRule(new ExistsDescr(), builder, "update(stilton);");
    assertTrue(builder.hasErrors());
    builder = new KnowledgeBuilderImpl();
    rule = createRule(new ExistsDescr(), builder, "");
    assertEquals(0, builder.getErrors().getErrors().length);
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement exists = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, exists.getChildren());
    final Pattern pattern = (Pattern) exists.getChildren().get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) 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 4 with ExistsDescr

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

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

the class RuleModelDRLPersistenceImpl method parseExistentialElementDescr.

private CompositeFactPattern parseExistentialElementDescr(final RuleModel m, final ConditionalElementDescr conditionalDescr, final boolean isJavaDialect, final Map<String, String> boundParams, final PackageDataModelOracle dmo) {
    CompositeFactPattern comp;
    if (conditionalDescr instanceof NotDescr) {
        comp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_NOT);
    } else if (conditionalDescr instanceof OrDescr) {
        comp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
    } else if (conditionalDescr instanceof ExistsDescr) {
        comp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_EXISTS);
    } else {
        throw new IllegalArgumentException("Unknown conditional descr type: " + conditionalDescr);
    }
    addPatternToComposite(m, conditionalDescr, comp, isJavaDialect, boundParams, dmo);
    IFactPattern[] patterns = comp.getPatterns();
    return patterns != null && patterns.length > 0 ? comp : null;
}
Also used : NotDescr(org.drools.compiler.lang.descr.NotDescr) ExistsDescr(org.drools.compiler.lang.descr.ExistsDescr) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) OrDescr(org.drools.compiler.lang.descr.OrDescr) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern)

Aggregations

ExistsDescr (org.drools.compiler.lang.descr.ExistsDescr)8 Test (org.junit.Test)4 NotDescr (org.drools.compiler.lang.descr.NotDescr)3 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)3 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 InputStreamReader (java.io.InputStreamReader)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 CEDescrBuilder (org.drools.compiler.lang.api.CEDescrBuilder)1 AndDescr (org.drools.compiler.lang.descr.AndDescr)1 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)1 OrDescr (org.drools.compiler.lang.descr.OrDescr)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 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)1