Skip to content

Commit

Permalink
Enhance test/grammar coverage. OR, inline map (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas authored and rgdoliveira committed Oct 24, 2024
1 parent acfa336 commit 4acd6db
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ lhsPattern : xpathPrimary (OVER patternFilter)? |
( QUESTION? qualifiedIdentifier LPAREN positionalConstraints? constraints? RPAREN (OVER patternFilter)? (FROM patternSource)? ) ;
*/

lhsPattern : QUESTION? objectType=drlQualifiedName LPAREN (positionalConstraints? constraints? ) RPAREN (DRL_FROM patternSource)? ;
lhsPattern : QUESTION? objectType=drlQualifiedName LPAREN positionalConstraints? constraints? RPAREN (DRL_FROM patternSource)? ;
positionalConstraints : constraint (COMMA constraint)* SEMI ;
constraints : constraint (COMMA constraint)* ;
constraint : label? ( nestedConstraint | conditionalOrExpression ) ;
Expand Down Expand Up @@ -198,6 +198,7 @@ drlPrimary
| typeTypeOrVoid DOT CLASS
| nonWildcardTypeArguments (explicitGenericInvocationSuffix | THIS arguments)
| inlineListExpression
| inlineMapExpression
;

/* extending JavaParser literal */
Expand All @@ -219,6 +220,18 @@ expressionList
: drlExpression (COMMA drlExpression)*
;

inlineMapExpression
: LBRACK mapExpressionList RBRACK
;

mapExpressionList
: mapEntry (COMMA mapEntry)*
;

mapEntry
: drlExpression COLON drlExpression
;

/*
patternSource := FROM
( fromAccumulate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public Object visitDrlIdentifier(DRLParser.DrlIdentifierContext ctx) {
return ctx.getText();
}


@Override
public Object visitDrlLiteral(DRLParser.DrlLiteralContext ctx) {
ParseTree node = ctx;
Expand Down Expand Up @@ -277,6 +276,22 @@ public Object visitLhsNot(DRLParser.LhsNotContext ctx) {
}
}

@Override
public Object visitLhsOr(DRLParser.LhsOrContext ctx) {
if (!ctx.DRL_OR().isEmpty()) {
OrDescr orDescr = new OrDescr();
currentConstructStack.peek().addDescr(orDescr);
currentConstructStack.push(orDescr);
try {
return super.visitLhsOr(ctx);
} finally {
currentConstructStack.pop();
}
} else {
return super.visitLhsOr(ctx);
}
}

public PackageDescr getPackageDescr() {
return packageDescr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.drools.drl.ast.descr.FunctionImportDescr;
import org.drools.drl.ast.descr.GlobalDescr;
import org.drools.drl.ast.descr.ImportDescr;
import org.drools.drl.ast.descr.MVELExprDescr;
import org.drools.drl.ast.descr.NotDescr;
import org.drools.drl.ast.descr.OrDescr;
import org.drools.drl.ast.descr.PackageDescr;
Expand Down Expand Up @@ -681,4 +682,139 @@ public void testConsequenceWithDeclaration() throws Exception {
assertTrue( ((String) rule.getConsequence()).indexOf( "i++" ) > 0 );
// note, need to assert that "i++" is preserved as is, no extra spaces.
}

@Test
public void testRuleParseLhs() throws Exception {
final String text = "rule X when Person(age < 42, location==\"atlanta\") \nor\nPerson(name==\"bob\") then end";
PackageDescr pkg = parser.parse(text);
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);

assertFalse(parser.getErrors().toString(),
parser.hasErrors());

assertNotNull(rule);

AndDescr lhs = rule.getLhs();
assertEquals(1,
lhs.getDescrs().size());
assertEquals(2,
((OrDescr) lhs.getDescrs().get(0)).getDescrs().size());
}

@Test
public void testRuleParseLhsWithStringQuotes() throws Exception {
final String text = "rule X when Person( location==\"atlanta\\\"\") then end\n";
PackageDescr pkg = parser.parse(text);
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);

assertFalse(parser.getErrors().toString(),
parser.hasErrors());

assertNotNull(rule);

AndDescr lhs = rule.getLhs();
ExprConstraintDescr constr = (ExprConstraintDescr) ((PatternDescr) lhs.getDescrs().get(0)).getDescrs().get(0);

assertThat(constr.getText()).isEqualToIgnoringWhitespace("location==\"atlanta\\\"\"");
}

@Test
public void testRuleParseLhsWithStringQuotes2() throws Exception {
final String text = "rule X when Cheese( $x: type, type == \"s\\tti\\\"lto\\nn\" ) then end\n";
PackageDescr pkg = parser.parse(text);
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
assertFalse(parser.getErrors().toString(),
parser.hasErrors());

assertNotNull(rule);

AndDescr lhs = rule.getLhs();
ExprConstraintDescr constr = (ExprConstraintDescr) ((PatternDescr) lhs.getDescrs().get(0)).getDescrs().get(1);

assertThat(constr.getText()).isEqualToIgnoringWhitespace("type == \"s\\tti\\\"lto\\nn\"");
}

@Test
public void testLiteralBoolAndNegativeNumbersRule() throws Exception {
String source = readResource("literal_bool_and_negative.drl");
PackageDescr pkg = parser.parse(source);
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);

assertFalse(parser.getErrors().toString(),
parser.hasErrors());

assertNotNull(rule);

assertEquals("simple_rule",
rule.getName());
assertNotNull(rule.getLhs());

assertThat((String) rule.getConsequence()).isEqualToIgnoringWhitespace("cons();");

final AndDescr lhs = rule.getLhs();
assertEquals(3,
lhs.getDescrs().size());

PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
assertEquals(1,
pattern.getConstraint().getDescrs().size());
AndDescr fieldAnd = (AndDescr) pattern.getConstraint();
ExprConstraintDescr fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);
assertThat(fld.getExpression()).isEqualToIgnoringWhitespace("bar == false");

pattern = (PatternDescr) lhs.getDescrs().get(1);
assertEquals(1,
pattern.getConstraint().getDescrs().size());

fieldAnd = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);

assertThat(fld.getText()).isEqualToIgnoringWhitespace("boo > -42");

pattern = (PatternDescr) lhs.getDescrs().get(2);
assertEquals(1,
pattern.getConstraint().getDescrs().size());

fieldAnd = (AndDescr) pattern.getConstraint();
fld = (ExprConstraintDescr) fieldAnd.getDescrs().get(0);

assertThat(fld.getText()).isEqualToIgnoringWhitespace("boo > -42.42");
}

@Test
public void testEmptyPattern() throws Exception {
String source = readResource("test_EmptyPattern.drl");
PackageDescr pkg = parser.parse(source);

assertFalse(parser.getErrors().toString(),
parser.hasErrors());

assertEquals(1,
pkg.getRules().size());
final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get(0);
assertEquals("simple rule",
ruleDescr.getName());
assertNotNull(ruleDescr.getLhs());
assertEquals(1,
ruleDescr.getLhs().getDescrs().size());
final PatternDescr patternDescr = (PatternDescr) ruleDescr.getLhs().getDescrs().get(0);
assertEquals(0,
patternDescr.getConstraint().getDescrs().size()); // this
assertEquals("Cheese",
patternDescr.getObjectType());
}

@Test
public void testSimpleMethodCallWithFrom() throws Exception {
String source = readResource("test_SimpleMethodCallWithFrom.drl");
PackageDescr pkg = parser.parse(source);
assertFalse(parser.getErrors().toString(),
parser.hasErrors());
RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
final FromDescr from = (FromDescr) pattern.getSource();
final MVELExprDescr method = (MVELExprDescr) from.getDataSource();

assertThat(method.getExpression()).isEqualToIgnoringWhitespace("something.doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

//check that it can handle true/false literals, and
//negative numbers
rule simple_rule
when
Foo(bar == false)
Foo(boo > -42)
Foo(boo > -42.42)
then
cons();
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.compiler.test;

import org.drools.compiler.Cheese;

rule "simple rule"
when
Cheese( )
then
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


rule blah

when

Col1() from something.doIt( foo,bar,42,"hello",[ a : "b", "something" : 42, "a" : foo, x : [x:y]],"end", [a, "b", 42] )
Col2()
then
partay();
end

0 comments on commit 4acd6db

Please sign in to comment.