use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintsOr.
@Test
public void declareConstraintsOr() {
String temperatureField = "TEMPERATURE";
String humidityField = "HUMIDITY";
List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.or());
assertNotNull(lhsBuilder.getDescr());
assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof OrDescr);
OrDescr baseOrDescr = (OrDescr) lhsBuilder.getDescr().getDescrs().get(0);
final List<BaseDescr> descrs = baseOrDescr.getDescrs();
assertNotNull(descrs);
assertEquals(2, descrs.size());
// First KiePMMLFieldOperatorValue
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.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());
// Second KiePMMLFieldOperatorValue
assertTrue(descrs.get(1) instanceof PatternDescr);
patternDescr = (PatternDescr) descrs.get(1);
assertEquals(humidityField, 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());
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class KiePMMLDescrLhsFactoryTest method declareConstraintIn.
@Test
public void declareConstraintIn() {
List<Object> values = Arrays.asList("-5", "0.5", "1", "10");
String patternType = "INPUT1";
KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintIn(patternType, values);
final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
assertNotNull(descrs);
assertEquals(1, descrs.size());
assertTrue(descrs.get(0) instanceof PatternDescr);
PatternDescr patternDescr = (PatternDescr) descrs.get(0);
assertEquals(patternType, 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 in (-5, 0.5, 1, 10)";
assertEquals(expected, exprConstraintDescr.getExpression());
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class TypeDeclarationConfigurator method wireDurationAccessor.
private static void wireDurationAccessor(KnowledgeBuilderImpl kbuilder, Annotated annotated, TypeDeclaration type, PackageRegistry pkgRegistry) {
Duration duration = annotated.getTypedAnnotation(Duration.class);
if (duration != null) {
BaseDescr typeDescr = annotated instanceof BaseDescr ? ((BaseDescr) annotated) : new BaseDescr();
String durationField;
try {
durationField = duration.value();
} catch (Exception e) {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, e.getMessage()));
return;
}
type.setDurationAttribute(durationField);
InternalKnowledgePackage pkg = pkgRegistry.getPackage();
AnalysisResult results = getMvelAnalysisResult(kbuilder, typeDescr, type, pkgRegistry, durationField, pkg);
if (results != null) {
type.setDurationExtractor(pkg.getFieldExtractor(type, durationField, results.getReturnType()));
} else {
kbuilder.addBuilderResult(new TypeDeclarationError(typeDescr, "Error processing @duration for TypeDeclaration '" + type.getFullName() + "': cannot access the field '" + durationField + "'"));
}
}
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6Expressions method expression.
// $ANTLR start "expression"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:163:1: expression returns [BaseDescr result] : left= conditionalExpression ( ( assignmentOperator )=>op= assignmentOperator right= expression )? ;
public final DRL6Expressions.expression_return expression() throws RecognitionException {
DRL6Expressions.expression_return retval = new DRL6Expressions.expression_return();
retval.start = input.LT(1);
BaseDescr left = null;
ParserRuleReturnScope right = null;
try {
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:164:5: (left= conditionalExpression ( ( assignmentOperator )=>op= assignmentOperator right= expression )? )
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:164:7: left= conditionalExpression ( ( assignmentOperator )=>op= assignmentOperator right= expression )?
{
pushFollow(FOLLOW_conditionalExpression_in_expression768);
left = conditionalExpression();
state._fsp--;
if (state.failed)
return retval;
if (state.backtracking == 0) {
if (buildDescr) {
retval.result = left;
}
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:165:9: ( ( assignmentOperator )=>op= assignmentOperator right= expression )?
int alt16 = 2;
switch(input.LA(1)) {
case EQUALS_ASSIGN:
{
int LA16_1 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case PLUS_ASSIGN:
{
int LA16_2 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case MINUS_ASSIGN:
{
int LA16_3 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case MULT_ASSIGN:
{
int LA16_4 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case DIV_ASSIGN:
{
int LA16_5 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case AND_ASSIGN:
{
int LA16_6 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case OR_ASSIGN:
{
int LA16_7 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case XOR_ASSIGN:
{
int LA16_8 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case MOD_ASSIGN:
{
int LA16_9 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case LESS:
{
int LA16_10 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
case GREATER:
{
int LA16_11 = input.LA(2);
if ((synpred6_DRL6Expressions())) {
alt16 = 1;
}
}
break;
}
switch(alt16) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:165:10: ( assignmentOperator )=>op= assignmentOperator right= expression
{
pushFollow(FOLLOW_assignmentOperator_in_expression789);
assignmentOperator();
state._fsp--;
if (state.failed)
return retval;
pushFollow(FOLLOW_expression_in_expression793);
right = expression();
state._fsp--;
if (state.failed)
return retval;
}
break;
}
}
retval.stop = input.LT(-1);
} catch (RecognitionException re) {
throw re;
} finally {
// do for sure before leaving
}
return retval;
}
use of org.drools.drl.ast.descr.BaseDescr in project drools by kiegroup.
the class DRL6Expressions method exclusiveOrExpression.
// $ANTLR end "inclusiveOrExpression"
// $ANTLR start "exclusiveOrExpression"
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:273:1: exclusiveOrExpression returns [BaseDescr result] : left= andExpression ( XOR right= andExpression )* ;
public final BaseDescr exclusiveOrExpression() throws RecognitionException {
BaseDescr result = null;
BaseDescr left = null;
BaseDescr right = null;
try {
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:274:3: (left= andExpression ( XOR right= andExpression )* )
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:274:5: left= andExpression ( XOR right= andExpression )*
{
pushFollow(FOLLOW_andExpression_in_exclusiveOrExpression1383);
left = andExpression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
if (buildDescr) {
result = left;
}
}
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:275:3: ( XOR right= andExpression )*
loop29: while (true) {
int alt29 = 2;
int LA29_0 = input.LA(1);
if ((LA29_0 == XOR)) {
alt29 = 1;
}
switch(alt29) {
case 1:
// src/main/resources/org/drools/compiler/lang/DRL6Expressions.g:275:5: XOR right= andExpression
{
match(input, XOR, FOLLOW_XOR_in_exclusiveOrExpression1391);
if (state.failed)
return result;
pushFollow(FOLLOW_andExpression_in_exclusiveOrExpression1395);
right = andExpression();
state._fsp--;
if (state.failed)
return result;
if (state.backtracking == 0) {
if (buildDescr) {
ConstraintConnectiveDescr descr = ConstraintConnectiveDescr.newXor();
descr.addOrMerge(result);
descr.addOrMerge(right);
result = descr;
}
}
}
break;
default:
break loop29;
}
}
}
} catch (RecognitionException re) {
throw re;
} finally {
// do for sure before leaving
}
return result;
}
Aggregations