Search in sources :

Example 1 with ExistsDescr

use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareConstraintsXor.

@Test
public void declareConstraintsXor() {
    String temperatureField = "TEMPERATURE";
    final List<KiePMMLFieldOperatorValue> xorConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsXor(xorConstraints);
    assertNotNull(lhsBuilder.getDescr());
    assertNotNull(lhsBuilder.getDescr().getDescrs());
    assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
    assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr);
    AndDescr rootAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0);
    assertEquals(2, rootAndDescr.getDescrs().size());
    assertTrue(rootAndDescr.getDescrs().get(0) instanceof NotDescr);
    assertTrue(rootAndDescr.getDescrs().get(1) instanceof ExistsDescr);
    // "Not" construct
    NotDescr notDescr = (NotDescr) rootAndDescr.getDescrs().get(0);
    assertEquals(1, notDescr.getDescrs().size());
    assertTrue(notDescr.getDescrs().get(0) instanceof AndDescr);
    AndDescr notAndDescr = (AndDescr) notDescr.getDescrs().get(0);
    assertTrue(notAndDescr.getDescrs().get(0) instanceof PatternDescr);
    assertTrue(notAndDescr.getDescrs().get(1) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) notAndDescr.getDescrs().get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    String expected = "value < 35";
    assertEquals(expected, exprConstraintDescr.getExpression());
    patternDescr = (PatternDescr) notAndDescr.getDescrs().get(1);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value > 85";
    assertEquals(expected, exprConstraintDescr.getExpression());
    // "Exists" construct
    ExistsDescr existsDescr = (ExistsDescr) rootAndDescr.getDescrs().get(1);
    assertEquals(1, existsDescr.getDescrs().size());
    assertTrue(existsDescr.getDescrs().get(0) instanceof OrDescr);
    OrDescr existsOrDescr = (OrDescr) existsDescr.getDescrs().get(0);
    assertEquals(2, existsOrDescr.getDescrs().size());
    assertTrue(existsOrDescr.getDescrs().get(0) instanceof PatternDescr);
    assertTrue(existsOrDescr.getDescrs().get(1) instanceof OrDescr);
    patternDescr = (PatternDescr) existsOrDescr.getDescrs().get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value < 35";
    assertEquals(expected, exprConstraintDescr.getExpression());
    OrDescr nestedOrDescr = (OrDescr) existsOrDescr.getDescrs().get(1);
    assertEquals(1, nestedOrDescr.getDescrs().size());
    assertTrue(nestedOrDescr.getDescrs().get(0) instanceof PatternDescr);
    patternDescr = (PatternDescr) nestedOrDescr.getDescrs().get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value > 85";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) OrDescr(org.drools.drl.ast.descr.OrDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 2 with ExistsDescr

use of org.drools.drl.ast.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.drl.ast.descr.NotDescr) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) InputStreamReader(java.io.InputStreamReader) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Test(org.junit.Test)

Example 3 with ExistsDescr

use of org.drools.drl.ast.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.drl.ast.dsl.CEDescrBuilder) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 4 with ExistsDescr

use of org.drools.drl.ast.descr.ExistsDescr in project drools by kiegroup.

the class RuleParserTest method testPluggableOperators2.

@Test
public void testPluggableOperators2() throws Exception {
    final String text = "rule \"tt\"\n" + "    dialect \"mvel\"\n" + "when\n" + "    exists (TelephoneCall( this finishes [1m] \"25-May-2011\" ))\n" + "then\n" + "end";
    PatternDescr pattern = (PatternDescr) ((ExistsDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0)).getDescrs().get(0);
    assertEquals("TelephoneCall", pattern.getObjectType());
    ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("this finishes [1m] \"25-May-2011\"", constr.getText());
}
Also used : ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 5 with ExistsDescr

use of org.drools.drl.ast.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.drl.ast.descr.ExistsDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Aggregations

ExistsDescr (org.drools.drl.ast.descr.ExistsDescr)10 PatternDescr (org.drools.drl.ast.descr.PatternDescr)6 Test (org.junit.Test)6 NotDescr (org.drools.drl.ast.descr.NotDescr)4 AndDescr (org.drools.drl.ast.descr.AndDescr)3 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)3 GroupElement (org.drools.core.rule.GroupElement)2 Pattern (org.drools.core.rule.Pattern)2 BaseDescr (org.drools.drl.ast.descr.BaseDescr)2 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)2 PackageDescr (org.drools.drl.ast.descr.PackageDescr)2 RuleDescr (org.drools.drl.ast.descr.RuleDescr)2 CEDescrBuilder (org.drools.drl.ast.dsl.CEDescrBuilder)2 InputStreamReader (java.io.InputStreamReader)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 Forall (org.drools.core.rule.Forall)1 ForallDescr (org.drools.drl.ast.descr.ForallDescr)1 OrDescr (org.drools.drl.ast.descr.OrDescr)1