Skip to content

Commit

Permalink
Merge pull request #71 from rgdoliveira/sync_main
Browse files Browse the repository at this point in the history
Sync main branch with Apache main branch
  • Loading branch information
rgdoliveira authored Aug 7, 2024
2 parents 14907da + 626b5fd commit bc39b82
Show file tree
Hide file tree
Showing 42 changed files with 1,044 additions and 128 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,15 @@ The following two commands will execute tests on machine with locale different t
1. `make test -Ptest-en`
2. `make test -DTestEn`

Documenting tips
================

UML diagrams have been used for architectural and design documentation. Those diagrams are in ".puml" format and have been created using the [PlantUML](https://plantuml.com/https://plantuml.com/) tool.
Plugins exists to use it in different IDE:
* [IDEA](https://plugins.jetbrains.com/plugin/7017-plantuml-integration)
* [Eclipse](https://marketplace.eclipse.org/content/plantuml-plugin)
* [VisualStudio](https://marketplace.visualstudio.com/items?itemName=jebbs.plantuml)




39 changes: 39 additions & 0 deletions drools-base/src/main/java/org/drools/base/rule/EvalCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,18 @@
import org.drools.base.rule.accessor.CompiledInvoker;
import org.drools.base.rule.accessor.EvalExpression;
import org.drools.base.rule.accessor.Wireable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EvalCondition extends ConditionalElement
implements
Externalizable,
Wireable {

private static final Logger LOG = LoggerFactory.getLogger(EvalCondition.class);

private static long warnLogCounter = 0;

private static final long serialVersionUID = 510l;

protected EvalExpression expression;
Expand Down Expand Up @@ -223,4 +230,36 @@ public void setCloned(List<EvalCondition> cloned) {
public String toString() {
return this.expression.toString();
}

public static void logWarnIfImproperEval(EvalCondition evalCondition, String evalExpression) {
if (warnLogCounter == 10) {
warnLogCounter++;
LOG.warn("More eval warnings will be suppressed...");
return;
} else if (warnLogCounter > 10) {
return; // avoid flooding the logs
}

if (evalExpression == null || evalExpression.isEmpty()) {
return; // cannot provide a meaningful warning
}

StringBuilder sb = new StringBuilder();
for (Declaration declaration : evalCondition.getRequiredDeclarations()) {
if (declaration.getPattern() != null) {
sb.append("'");
sb.append(declaration.getIdentifier());
sb.append("' comes from previous pattern '");
String className = declaration.getPattern().getObjectType().getClassName();
sb.append(className.substring(className.lastIndexOf('.') + 1));
sb.append("'. ");
}
}
if (!sb.isEmpty()) {
warnLogCounter++;
LOG.warn("In an eval expression [{}] : {}" +
"Consider placing the constraint in the pattern and removing the eval if possible," +
" as eval is not performance-efficient.", evalExpression, sb);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ For groups of constraints, you can use a delimiting comma `,` to use implicit `a
.Example patterns with multiple constraints
[source]
----
// Person is at least 50 years old and weighs at least 80 kilograms:
// Person is more than 50 years old and weighs more than 80 kilograms:
Person( age > 50, weight > 80 )
// Person is at least 50 years old, weighs at least 80 kilograms, and is taller than 2 meters:
// Person is more than 50 years old, weighs more than 80 kilograms, and is taller than 2 meters:
Person( age > 50, weight > 80, height > 2 )
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ endif::[]
rule "All full-time employees have red ID badges"
when
forall( $emp : Employee( type == "fulltime" )
Employee( this == $emp, badgeColor = "red" ) )
Employee( this == $emp, badgeColor == "red" ) )
then
// True, all full-time employees have red ID badges.
end
Expand All @@ -174,11 +174,11 @@ To state that all facts of a given type in the working memory of the {RULE_ENGIN
.Example rule with `forall` and a single pattern
[source]
----
rule "All full-time employees have red ID badges"
rule "All employees have red ID badges"
when
forall( Employee( badgeColor = "red" ) )
forall( Employee( badgeColor == "red" ) )
then
// True, all full-time employees have red ID badges.
// True, all employees have red ID badges.
end
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1345,10 +1345,10 @@ For groups of constraints, you can use a delimiting comma `,` to use implicit `a
.Example patterns with multiple constraints
[source]
----
// Person is at least 50 years old and weighs at least 80 kilograms:
// Person is more than 50 years old and weighs more than 80 kilograms:
/persons[ age > 50, weight > 80 ]
// Person is at least 50 years old, weighs at least 80 kilograms, and is taller than 2 meters:
// Person is more than 50 years old, weighs more than 80 kilograms, and is taller than 2 meters:
/persons[ age > 50, weight > 80, height > 2 ]
----
Expand Down Expand Up @@ -2062,7 +2062,7 @@ image::language-reference/forall.png[align="center"]
rule "All full-time employees have red ID badges"
when
forall( $emp : /employees[ type == "fulltime" ]
/employees[ this == $emp, badgeColor = "red" ] )
/employees[ this == $emp, badgeColor == "red" ] )
then
// True, all full-time employees have red ID badges.
end
Expand All @@ -2075,11 +2075,11 @@ To state that all facts of a given type in the working memory of the {RULE_ENGIN
.Example rule with `forall` and a single pattern
[source]
----
rule "All full-time employees have red ID badges"
rule "All employees have red ID badges"
when
forall( /employees[ badgeColor = "red" ] )
forall( /employees[ badgeColor == "red" ] )
then
// True, all full-time employees have red ID badges.
// True, all employees have red ID badges.
end
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public Collection<String> getApplicationProperties() {
public void setApplicationProperty(String key, String value) {
properties.put(key, value);
}

@Override
public void removeApplicationProperty(String key) {
properties.remove(key);
}
};
}

Expand All @@ -48,4 +53,6 @@ public void setApplicationProperty(String key, String value) {
Collection<String> getApplicationProperties();

void setApplicationProperty(String key, String value);

void removeApplicationProperty(String key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public interface DroolsModelBuildContext {

void setApplicationProperty(String key, String value);

void removeApplicationProperty(String key);

String getPackageName();

ClassLoader getClassLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public void setApplicationProperty(String key, String value) {
applicationProperties.setApplicationProperty(key, value);
}

@Override
public void removeApplicationProperty(String key) {
applicationProperties.removeApplicationProperty(key);
}

@Override
public String getPackageName() {
return packageName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
import org.kie.internal.builder.conf.PropertySpecificOption;

import static java.util.stream.Collectors.toList;
import static org.drools.base.rule.EvalCondition.logWarnIfImproperEval;
import static org.drools.base.rule.GroupElement.AND;
import static org.drools.base.rule.GroupElement.OR;
import static org.drools.compiler.rule.builder.RuleBuilder.buildTimer;
Expand Down Expand Up @@ -669,7 +670,9 @@ private void recursivelyAddConditions(RuleContext ctx, GroupElement group, Group
private EvalCondition buildEval(RuleContext ctx, EvalImpl eval) {
Declaration[] declarations = Stream.of( eval.getExpr().getVariables() ).map( ctx::getDeclaration ).toArray( Declaration[]::new );
EvalExpression evalExpr = new LambdaEvalExpression(declarations, eval.getExpr());
return new EvalCondition(evalExpr, declarations);
EvalCondition evalCondition = new EvalCondition(evalExpr, declarations);
logWarnIfImproperEval(evalCondition, eval.getExpr().predicateInformation().getStringConstraint());
return evalCondition;
}

private ConditionalBranch buildConditionalConsequence(RuleContext ctx, ConditionalNamedConsequenceImpl consequence) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.drools.base.rule.RuleConditionElement;
import org.drools.base.rule.accessor.DeclarationScopeResolver;

import static org.drools.base.rule.EvalCondition.logWarnIfImproperEval;
import static org.drools.compiler.rule.builder.PatternBuilder.buildAnalysis;
import static org.drools.compiler.rule.builder.PatternBuilder.createImplicitBindings;
import static org.drools.mvel.java.JavaRuleBuilderHelper.createVariableContext;
Expand Down Expand Up @@ -90,6 +91,7 @@ private RuleConditionElement buildEval(RuleBuildContext context, EvalDescr evalD
Arrays.sort(declarations, SortDeclarations.instance);

EvalCondition eval = EvalConditionFactory.Factory.get().createEvalCondition(declarations);
logWarnIfImproperEval(eval, (String) evalDescr.getContent());

Map<String, Object> vars = createVariableContext( className,
(String)evalDescr.getContent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.drools.mvel.expr.MVELCompilationUnit;
import org.drools.mvel.expr.MVELEvalExpression;

import static org.drools.base.rule.EvalCondition.logWarnIfImproperEval;
import static org.drools.mvel.asm.AsmUtil.copyErrorLocation;

public class MVELEvalBuilder
Expand Down Expand Up @@ -99,6 +100,7 @@ public RuleConditionElement build(final RuleBuildContext context,
false,
MVELCompilationUnit.Scope.EXPRESSION );
final EvalCondition eval = EvalConditionFactory.Factory.get().createEvalCondition( previousDeclarations );
logWarnIfImproperEval(eval, (String) evalDescr.getContent());

MVELEvalExpression expr = new MVELEvalExpression( unit,
dialect.getId() );
Expand Down
Loading

0 comments on commit bc39b82

Please sign in to comment.