Skip to content

Commit

Permalink
[DO-NOT-MERGE] Experiment a special prefix for Custom Operator
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Nov 1, 2024
1 parent 6b2d92d commit 5c9d4f5
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
public class OperatorDescr extends BaseDescr {
private static final long serialVersionUID = 520l;

// prefix for custom operators which are not built-in
public static final String CUSTOM_OPERATOR_PREFIX = "##";

private String operator;
private boolean negated;
private List<String> parameters;
Expand Down Expand Up @@ -55,7 +58,15 @@ public String getOperator() {
}

public void setOperator( String operator ) {
this.operator = operator != null ? operator.trim() : null;
this.operator = operator != null ? trimOperator(operator) : null;
}

private String trimOperator(String operator) {
operator = operator.trim();
if (operator.startsWith(CUSTOM_OPERATOR_PREFIX)) {
operator = operator.substring(CUSTOM_OPERATOR_PREFIX.length());
}
return operator;
}

public boolean isNegated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ void noViableAlt() {
assertThat(exception.getColumn()).isEqualTo(2);
assertThat(exception.getOffset()).isEqualTo(2);
assertThat(exception.getMessage())
.isEqualToIgnoringCase("[ERR 101] Line 1:2 no viable alternative at input 'a'");
.isEqualToIgnoringCase("[ERR 101] Line 1:2 no viable alternative at input '~a'");
} else {
assertThat(parser.hasErrors()).isFalse();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
import org.drools.drl.parser.DroolsParserException;
import org.drools.drl.parser.impl.Operator;
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.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -95,6 +95,14 @@ 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 @@ -4411,8 +4419,9 @@ void traitExtendsMultiple() {
.containsExactlyInAnyOrder("com.sample.ParentTrait", "UncleTrait", "org.test.GrandParentTrait");
}

@EnabledIf("isOldParser")
@Test
void pluggableEvaluator() {
void pluggableEvaluatorOldParser() {
final String source = "package org.drools\n" +
"rule R\n" +
"when\n" +
Expand All @@ -4431,6 +4440,27 @@ void pluggableEvaluator() {
.containsExactly("$c : core", "this not isA t.x.E.class", "this isA t.x.D.class");
}

@EnabledIf("isNewParser")
@Test
void pluggableEvaluatorNewParser() {
final String source = "package org.drools\n" +
"rule R\n" +
"when\n" +
" $t : Thing( $c : core, this not ##isA t.x.E.class, this ##isA t.x.D.class )\n" +
"then\n" +
" list.add( \"E\" ); \n" +
" don( $t, E.class ); \n" +
"end\n";

Operator.addOperatorToRegistry("isA", false);
Operator.addOperatorToRegistry("isA", true);

PatternDescr pattern = (PatternDescr) parseAndGetFirstRuleDescr(source).getLhs().getDescrs().get(0);
assertThat(pattern.getConstraint().getDescrs())
.extracting(Object::toString)
.containsExactly("$c : core", "this not ##isA t.x.E.class", "this ##isA t.x.D.class");
}

@Test
void namedConsequenceDo() {
final String text =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ public static void enableOldParser() {
DrlParser.ANTLR4_PARSER_ENABLED = false;
}

/**
* Enables the new parser. Just for quick testing purposes.
*/
public static void enableNewParser() {
DrlParser.ANTLR4_PARSER_ENABLED = true;
}

public static List<String> javaKeywords() {
return javaKeywords;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,12 +1000,11 @@ in_key
;

operator_key
// IDENTIFIER is required to accept custom operators. We need to keep this semantic predicate for custom operators
: {(helper.isPluggableEvaluator(false))}? id=IDENTIFIER { helper.emit($id, DroolsEditorType.KEYWORD); }
: DRL_CUSTOM_OPERATOR_PREFIX {(helper.isPluggableEvaluator(false))}? id=IDENTIFIER { helper.emit($id, DroolsEditorType.KEYWORD); }
| op=builtInOperator { helper.emit($op.token, DroolsEditorType.KEYWORD); }
;

neg_operator_key
: {(helper.isPluggableEvaluator(true))}? id=IDENTIFIER { helper.emit($id, DroolsEditorType.KEYWORD); }
: DRL_CUSTOM_OPERATOR_PREFIX {(helper.isPluggableEvaluator(true))}? id=IDENTIFIER { helper.emit($id, DroolsEditorType.KEYWORD); }
| op=builtInOperator { helper.emit($op.token, DroolsEditorType.KEYWORD); }
;
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ DRL_CALENDARS : 'calendars';
DRL_TIMER : 'timer';
DRL_DURATION : 'duration';

DRL_CUSTOM_OPERATOR_PREFIX : '##' ;

/////////////////
// LEXER
/////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public static JavaToken.Category getCategory(int kind) {
case COMMA:
case DOT:
case AT:
case CUSTOM_OPERATOR_PREFIX:
return JavaToken.Category.SEPARATOR;
case MVEL_STARTS_WITH:
case MVEL_ENDS_WITH:
Expand Down
2 changes: 2 additions & 0 deletions drools-model/drools-mvel-parser/src/main/javacc/mvel.jj
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ TOKEN :
| < MVEL_ENDS_WITH: "str[endsWith]" >
| < MVEL_LENGTH: "str[length]" >
| < DOT_DOT_SLASH: "../" >
| < CUSTOM_OPERATOR_PREFIX: "##" >
| < HASHMARK: "#" >
| < EXCL_DOT: "!." >
| < PASSIVE_OOPATH: "?/" >
Expand Down Expand Up @@ -6419,6 +6420,7 @@ Expression PointFreeExpr():
{ negated = true; }
)*
(
[ <CUSTOM_OPERATOR_PREFIX> ]
operator = SimpleName()
temporalLiteralArguments = TemporalLiteralArguments()
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.drools.base.base.ValueResolver;
import org.drools.base.base.ValueType;
import org.drools.compiler.rule.builder.EvaluatorDefinition;
import org.drools.drl.parser.DrlParser;
import org.drools.drl.parser.impl.Operator;
import org.drools.base.rule.accessor.Evaluator;
import org.drools.base.rule.accessor.FieldValue;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void testCustomOperatorCombiningConstraints(KieBaseTestConfiguration kieB
"when\n" +
" gnId : GN()\n" +
" la : t547147( )\n" +
" v1717 : Tra48( gnId.gNo == gNo, name F_str[startsWith] la.c547148 || postCode F_str[contains] la.c547149 )\n" +
" v1717 : Tra48( gnId.gNo == gNo, name ##F_str[startsWith] la.c547148 || postCode ##F_str[contains] la.c547149 )\n" +
"then\n" +
"end\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.drools.base.base.ValueResolver;
import org.drools.base.base.ValueType;
import org.drools.compiler.rule.builder.EvaluatorDefinition;
import org.drools.core.reteoo.ReteDumper;
import org.drools.drl.parser.DrlParser;
import org.drools.drl.parser.impl.Operator;
import org.drools.base.rule.accessor.Evaluator;
import org.drools.base.rule.accessor.FieldValue;
Expand Down Expand Up @@ -58,7 +60,16 @@ public static Stream<KieBaseTestConfiguration> parameters() {
public void testCustomOperatorUsingCollections(KieBaseTestConfiguration kieBaseTestConfiguration) {
String constraints =
" $alice : Person(name == \"Alice\")\n" +
" $bob : Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n";
" $bob : Person(name == \"Bob\", addresses ##supersetOf $alice.addresses)\n";
customOperatorUsingCollections(kieBaseTestConfiguration, constraints);
}

@ParameterizedTest(name = "KieBase type={0}")
@MethodSource("parameters")
public void testCustomOperatorUsingCollectionsWithNot(KieBaseTestConfiguration kieBaseTestConfiguration) {
String constraints =
" $alice : Person(name == \"Alice\")\n" +
" $bob : Person(name == \"Bob\", $alice.addresses not ##supersetOf this.addresses)\n";
customOperatorUsingCollections(kieBaseTestConfiguration, constraints);
}

Expand All @@ -67,8 +78,8 @@ public void testCustomOperatorUsingCollections(KieBaseTestConfiguration kieBaseT
public void testNoOperatorInstancesCreatedAtRuntime(KieBaseTestConfiguration kieBaseTestConfiguration) {
String constraints =
" $alice : Person(name == \"Alice\")\n" +
" $bob : Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n" +
" Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n";
" $bob : Person(name == \"Bob\", addresses ##supersetOf $alice.addresses)\n" +
" Person(name == \"Bob\", addresses ##supersetOf $alice.addresses)\n";

customOperatorUsingCollections(kieBaseTestConfiguration, constraints);

Expand All @@ -81,7 +92,7 @@ public void testCustomOperatorUsingCollectionsInverted(KieBaseTestConfiguration
// DROOLS-6983
String constraints =
" $bob : Person(name == \"Bob\")\n" +
" $alice : Person(name == \"Alice\", $bob.addresses supersetOf this.addresses)\n";
" $alice : Person(name == \"Alice\", $bob.addresses ##supersetOf this.addresses)\n";
customOperatorUsingCollections(kieBaseTestConfiguration, constraints);
}

Expand Down Expand Up @@ -201,7 +212,7 @@ public boolean evaluateCachedRight(final ValueResolver reteEvaluator, final Vari
}

public boolean evaluateAll(final Collection leftCollection, final Collection rightCollection) {
return rightCollection.containsAll(leftCollection);
return getOperator().isNegated() ^ rightCollection.containsAll(leftCollection);
}
}

Expand All @@ -212,7 +223,7 @@ public void testCustomOperatorOnKieModule(KieBaseTestConfiguration kieBaseTestCo
"import " + Person.class.getCanonicalName() + ";\n" +
"rule R when\n" +
" $alice : Person(name == \"Alice\")\n" +
" $bob : Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n" +
" $bob : Person(name == \"Bob\", addresses ##supersetOf $alice.addresses)\n" +
"then\n" +
"end\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.drools.mvel.integrationtests;

import java.io.Serializable;
import java.io.StringReader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLClassLoader;
Expand All @@ -43,28 +42,24 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.drools.base.base.ValueResolver;
import org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl;
import org.drools.compiler.builder.impl.KnowledgeBuilderImpl;
import org.kie.api.runtime.ClassObjectFilter;
import org.drools.base.InitialFact;
import org.drools.base.base.ClassObjectType;
import org.drools.base.base.ValueResolver;
import org.drools.base.definitions.rule.impl.RuleImpl;
import org.drools.base.rule.accessor.Salience;
import org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl;
import org.drools.core.common.DefaultFactHandle;
import org.drools.core.common.InternalAgenda;
import org.drools.core.common.InternalFactHandle;
import org.drools.core.common.InternalWorkingMemory;
import org.drools.core.common.Memory;
import org.drools.core.common.NodeMemories;
import org.drools.base.definitions.InternalKnowledgePackage;
import org.drools.base.definitions.rule.impl.RuleImpl;
import org.drools.core.impl.InternalRuleBase;
import org.drools.core.reteoo.CoreComponentFactory;
import org.drools.core.reteoo.LeftInputAdapterNode;
import org.drools.core.reteoo.ObjectTypeNode;
import org.drools.core.reteoo.ObjectTypeNodeId;
import org.drools.core.reteoo.Rete;
import org.drools.core.reteoo.SegmentMemory;
import org.drools.base.rule.accessor.Salience;
import org.drools.core.reteoo.TupleImpl;
import org.drools.drl.ast.descr.PackageDescr;
import org.drools.drl.ast.descr.PatternDescr;
Expand Down Expand Up @@ -113,6 +108,7 @@
import org.kie.api.event.rule.MatchCancelledEvent;
import org.kie.api.event.rule.MatchCreatedEvent;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.ClassObjectFilter;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.KieSessionConfiguration;
Expand Down Expand Up @@ -8836,7 +8832,7 @@ public void testKieHelperKieModuleModel() throws Exception {
"import " + Person.class.getCanonicalName() + ";\n" +
"rule R when\n" +
" $alice : Person(name == \"Alice\")\n" +
" $bob : Person(name == \"Bob\", addresses supersetOf $alice.addresses)\n" +
" $bob : Person(name == \"Bob\", addresses ##supersetOf $alice.addresses)\n" +
"then\n" +
"end\n";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<!-- <logger name="org.drools.ancompiler" level="debug"/>-->
<!-- <logger name="org.drools.drl.parser.DrlParser" level="debug"/>-->

<!-- <logger name="org.drools.model" level="debug"/>-->

<root level="warn">
<appender-ref ref="consoleAppender" />
</root>
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
<profile>
<id>enableNewParser</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>enableNewParser</name>
</property>
Expand Down

0 comments on commit 5c9d4f5

Please sign in to comment.