Skip to content

Commit

Permalink
- drop half constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Jan 23, 2025
1 parent f9460d9 commit 0573080
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.ValueSource;
Expand Down Expand Up @@ -175,7 +176,7 @@ void bindingConstraint() {

@Test
void bindingWithRestrictions() {
String source = "$x : property > value && < 20";
String source = "$x : property > value && property < 20";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.drools.drl.parser.antlr4;

import org.drools.drl.parser.DrlParser;

public class DRLParserTestBase {

protected static boolean isNewParser() {
return DrlParser.ANTLR4_PARSER_ENABLED;
}

protected static boolean isOldParser() {
return !DrlParser.ANTLR4_PARSER_ENABLED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import org.kie.internal.builder.conf.LanguageLevelOption;

import static org.assertj.core.api.Assertions.assertThat;

public class DescrDumperTest {
public class DescrDumperTest extends DRLParserTestBase {

private DescrDumper dumper;

Expand All @@ -47,6 +48,7 @@ void setUp() {
dumper = new DescrDumper();
}

@EnabledIf("isOldParser")
@Test
void dump() {
String input = "price > 10 && < 20 || == $val || == 30";
Expand Down Expand Up @@ -147,6 +149,7 @@ void dumpWithDateAttr() {
assertThat(result).isEqualTo(expected);
}

@EnabledIf("isOldParser")
@Test
void dumpComplex() {
String input = "a ( > 60 && < 70 ) || ( > 50 && < 55 ) && a3 == \"black\" || a == 40 && a3 == \"pink\" || a == 12 && a3 == \"yellow\" || a3 == \"blue\"";
Expand Down Expand Up @@ -220,6 +223,7 @@ void dumpBindings4() {
assertThat(result).isEqualTo(expected);
}

@EnabledIf("isOldParser")
@Test
void dumpBindingsWithRestriction() {
String input = "$x : age > 10 && < 20 || > 30";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
/*
* This test class is ported from org.drools.mvel.compiler.lang.RuleParserTest
*/
class MiscDRLParserTest {
class MiscDRLParserTest extends DRLParserTestBase {

private DrlParser parser;

Expand All @@ -95,14 +95,6 @@ void setUp() {
parser = ParserTestUtils.getParser();
}

private static boolean isNewParser() {
return DrlParser.ANTLR4_PARSER_ENABLED;
}

private static boolean isOldParser() {
return !DrlParser.ANTLR4_PARSER_ENABLED;
}

private String readResource(final String filename) {
Path path;
try {
Expand Down Expand Up @@ -881,6 +873,7 @@ void simpleRuleWithBindings() {
assertThat((String) rule.getConsequence()).isEqualToIgnoringWhitespace("if ( a == b ) { " + " assert( foo3 );" + "} else {" + " retract( foo4 );" + "}" + " System.out.println( a4 );");
}

@EnabledIf("isOldParser")
@Test
void multipleRestrictionsConstraint() {
RuleDescr rule = parseAndGetFirstRuleDescrFromFile("restrictions_test.drl");
Expand Down Expand Up @@ -2452,7 +2445,7 @@ void inOperator() {
assertThat(pattern.getConstraint().getDescrs()).hasSize(1);

ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertThat(fld.getExpression()).isEqualTo("age > 30 && < 40");
assertThat(fld.getExpression()).isEqualTo("age > 30 && age < 40");

// the second col, with 2 fields, the first with 2 restrictions, the
// second field with one
Expand Down Expand Up @@ -2486,7 +2479,7 @@ void notInOperator() {
assertThat(pattern.getConstraint().getDescrs()).hasSize(1);

ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertThat(fld.getExpression()).isEqualTo("age > 30 && < 40");
assertThat(fld.getExpression()).isEqualTo("age > 30 && age < 40");

// the second col, with 2 fields, the first with 2 restrictions, the
// second field with one
Expand Down Expand Up @@ -2536,6 +2529,7 @@ void constraintOrConnective() {
assertThat(fcd.getExpression()).isEqualToIgnoringWhitespace("age < 42 || location==\"atlanta\"");
}

@EnabledIf("isOldParser")
@Test
void restrictions() {
final String text = "rule X when Foo( bar > 1 || == 1 ) then end\n";
Expand Down Expand Up @@ -2987,7 +2981,7 @@ void pluggableOperators() {
assertThat(eventE.getConstraint().getDescrs()).hasSize(1);

ExprConstraintDescr fcdE = (ExprConstraintDescr) eventE.getConstraint().getDescrs().get(0);
assertThat(fcdE.getExpression()).isEqualTo("this not before[1, 10] $b || after[1, 10] $c && this after[1, 5] $d");
assertThat(fcdE.getExpression()).isEqualTo("this not before[1, 10] $b || this after[1, 10] $c && this after[1, 5] $d");
}

@Test
Expand Down Expand Up @@ -3896,6 +3890,7 @@ void constraintOperators(String constraint) {
assertThat(exprConstraintDescr.getExpression()).isEqualToIgnoringWhitespace(constraint);
}

@EnabledIf("isOldParser")
@ParameterizedTest
@ValueSource(strings = {
"country matches \"[a-z]*\" || matches \"[A-Z]*\"",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
rule "AccumulateParserTest"
when
// below statement makes no sense, but is useful to test parsing recursiveness
$personList : ArrayList() from accumulate( Person( $age : age > 21 || < 10 ) from collect( People() from $town.getPeople() ),
$personList : ArrayList() from accumulate( Person( $age : age > 21 || age < 10 ) from collect( People() from $town.getPeople() ),
max( $age ) );
then
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

rule simple_rule
when
Person(age > 30 && < 40)
Person(age > 30 && age < 40)
Vehicle(type in ( "sedan", "wagon" ), age < 3)
then
consequence();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

rule simple_rule
when
Person(age > 30 && < 40)
Person(age > 30 && age < 40)
Vehicle(type not in ( "sedan", "wagon" ), age < 3)
then
consequence();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ when
$b : EventB( this after[1,10] $a || this not after[15,20] $a )
$c : EventC( this finishes $b )
$d : EventD( this not starts $a )
$e : EventE( this not before[1, 10] $b || after[1, 10] $c && this after[1, 5] $d )
$e : EventE( this not before[1, 10] $b || this after[1, 10] $c && this after[1, 5] $d )
then
end
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ locals [ BaseDescr lsd ]
$relationalExpression::lsd = $result;
}
}
( right=orRestriction
( right=singleRestriction
{ if( buildDescr ) {
$result = $right.result;
// TODO access lsd directly instead of through dynamic context here
Expand All @@ -581,36 +581,6 @@ locals [ BaseDescr lsd ]
)*
;

orRestriction returns [BaseDescr result]
: left=andRestriction { if( buildDescr ) { $result = $left.result; } }
( lop=OR args=fullAnnotation[null]? right=andRestriction
{ if( buildDescr ) {
ConstraintConnectiveDescr descr = ConstraintConnectiveDescr.newOr();
descr.addOrMerge( $result );
descr.addOrMerge( $right.result );
if ( $ctx.args != null ) { descr.addAnnotation( $args.result ); }
$result = descr;
}
}
)*? EOF?
;

andRestriction returns [BaseDescr result]
: left=singleRestriction { if( buildDescr ) { $result = $left.result; } }
( lop=AND
{ if ( isNotEOF() ) helper.emit( Location.LOCATION_LHS_INSIDE_CONDITION_OPERATOR ); }
args=fullAnnotation[null]?right=singleRestriction
{ if( buildDescr ) {
ConstraintConnectiveDescr descr = ConstraintConnectiveDescr.newAnd();
descr.addOrMerge( $result );
descr.addOrMerge( $right.result );
if ( $ctx.args != null ) { descr.addAnnotation( $args.result ); }
$result = descr;
}
}
)*?
;

singleRestriction returns [BaseDescr result]
: op=operator
{ helper.emit( Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT ); }
Expand All @@ -630,7 +600,6 @@ singleRestriction returns [BaseDescr result]
}
helper.emit( Location.LOCATION_LHS_INSIDE_CONDITION_END );
}
| LPAREN or=orRestriction RPAREN { $result = $or.result; }
;


Expand Down

0 comments on commit 0573080

Please sign in to comment.