All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.drools.verifier.optimisation.RestrictionOrder.drl Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
#created on: 25.10.2007
package org.drools.verifier.optimisation.drl

#list any import classes here.
import java.util.Collection;
import java.util.ArrayList;

import org.drools.base.evaluators.Operator;
import org.drools.base.evaluators.MatchesEvaluatorsDefinition;

import org.drools.verifier.components.Restriction;
import org.drools.verifier.components.Pattern;
import org.drools.verifier.components.VerifierRule;
import org.drools.verifier.components.OperatorDescr;
import org.drools.verifier.components.VerifierPredicateDescr;
import org.drools.verifier.components.VerifierComponent;
import org.drools.verifier.components.VerifierComponentType;
import org.drools.verifier.report.components.VerifierMessage;
import org.drools.verifier.report.components.Severity;
import org.drools.verifier.report.components.MessageType;
import org.drools.verifier.dao.VerifierResult;

#declare any global variables here
global VerifierResult result;


# Informs that rule would be more effective if the less expensive
# component was on the left side.
#
# Type: Note
# Dependencies: None
# Example: { a:bar } < { baz == 1 } < { baz > 1 || < 1 } < { baz != 1 }
#
rule "Optimise restrictions inside operator"
	when
		$operator :OperatorDescr()

		(
				$r1 :Restriction(
					parent.parent == $operator,
					( operator == Operator.EQUAL || == MatchesEvaluatorsDefinition.MATCHES )
				)
			and
				# There is no reason to check if there is a less expensive constraint
				# if this one has a restriction that is more expensive.
				not Restriction(
					parent == $r1.parent,
					( operator == Operator.GREATER || == Operator.LESS || == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL || == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
				)
			and
				$r2 :Restriction(
					parent.parent == $operator,
					parent.orderNumber < $r1.parent.orderNumber,
					( operator == Operator.GREATER || == Operator.LESS || == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL || == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
				)
			and
				# The other restriction should not have any less expensive restrictions.
				not Restriction(
					parent == $r2.parent,
					( operator == Operator.EQUAL || == MatchesEvaluatorsDefinition.MATCHES )
				)
		) or (
				$r1 :Restriction(
					parent.parent == $operator,
					( operator == Operator.GREATER || == Operator.LESS )
				)
			and
				# There is no reason to check if there is a less expensive constraint
				# if this one has a restriction that is more expensive.
				not Restriction(
					parent == $r1.parent,
					( operator == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL || == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
				)
			and
				$r2 :Restriction(
					parent.parent == $operator,
					parent.orderNumber < $r1.parent.orderNumber,
					( operator == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL || == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
				)
			and
				# The other restriction should not have any less expensive restrictions.
				not Restriction(
					parent == $r2.parent,
					( operator == Operator.EQUAL || == MatchesEvaluatorsDefinition.MATCHES || == Operator.GREATER || == Operator.LESS )
				)
		) or (
				$r1 :Restriction(
					parent.parent == $operator,
					( operator == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL )
				)
			and
				# There is no reason to check if there is a less expensive constraint
				# if this one has a restriction that is more expensive.
				not Restriction(
					parent == $r1.parent,
					( operator == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
				)
			and
				$r2 :Restriction(
					parent.parent == $operator,
					parent.orderNumber < $r1.parent.orderNumber,
					( operator == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
				)
			and
				# The other restriction should not have any less expensive restrictions.
				not Restriction(
					parent == $r2.parent,
					( operator == Operator.EQUAL || == MatchesEvaluatorsDefinition.MATCHES || == Operator.GREATER || == Operator.LESS || == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL )
				)
		)

		$pattern :Pattern(
			id == $r1.patternId
		)
	then
		Collection list = new ArrayList();
		list.add( $r1 );
		list.add( $r2 );
		result.add( new VerifierMessage(
								Severity.NOTE,
								MessageType.OPTIMISATION,
								$pattern,
								"Rule \"" + $r1.getRuleName() +
								"\" would be more effective if the more restrictive component (" +
								$r2 +
								") could be before (" +
								$r1 + ").",
								list ) );
end

# Informs that rule would be more effective if the less expensive
# component was on the left side.
#
# Type: Note
# Dependencies: None
# Example: { a:bar } < { baz == 1 } < { baz > 1 || < 1 } < { baz != 1 }
#
rule "Optimise restrictions inside constraint"
	when
		(
				$r1 :Restriction(
					( operator == Operator.EQUAL || == MatchesEvaluatorsDefinition.MATCHES )
				)
			and
				$r2 :Restriction(
					parent == $r1.parent,
					orderNumber < $r1.orderNumber,
					( operator 	== Operator.GREATER || 
								== Operator.LESS || 
								== Operator.GREATER_OR_EQUAL || 
								== Operator.LESS_OR_EQUAL || 
								== MatchesEvaluatorsDefinition.NOT_MATCHES || 
								== Operator.NOT_EQUAL )
				)
		) or (
				$r1 :Restriction(
					( operator 	== Operator.GREATER || 
								== Operator.LESS )
				)
			and
				$r2 :Restriction(
					parent == $r1.parent,
					orderNumber < $r1.orderNumber,
					( operator 	== Operator.GREATER_OR_EQUAL || 
								== Operator.LESS_OR_EQUAL || 
								== MatchesEvaluatorsDefinition.NOT_MATCHES || 
								== Operator.NOT_EQUAL )
				)
		) 
		or (
				$r1 :Restriction(
					( operator == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL )
				)
			and
				$r2 :Restriction(
					parent == $r1.parent,
					orderNumber < $r1.orderNumber,
					( operator == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
				)
		)

		$pattern :Pattern(
			id == $r1.patternId
		)
	then
		Collection list = new ArrayList();
		list.add( $r1 );
		list.add( $r2 );
		result.add( new VerifierMessage(
								Severity.NOTE,
								MessageType.OPTIMISATION,
								$pattern,
								"Rule \"" + $r1.getRuleName() +
								"\" would be more effective if the more restrictive component (" +
								$r2 +
								") could be before (" +
								$r1 + ").",
								list ) );
end

# VerifierPredicateDescr should be last.
#
# Type: Note
# Dependencies: None
# Example: Foo( eval( bar == 1), bar == 1 )
#
rule "Optimise predicates inside operator"
	when
		$operator :OperatorDescr()

		$predicate :VerifierPredicateDescr(
			parent == $operator
		)

		# Any other VerifierComponent that is not predicate or operator
		$other :VerifierComponent(
			parent == $operator,
			orderNumber > $predicate.orderNumber,
			( componentType != VerifierComponentType.OPERATOR && != VerifierComponentType.PREDICATE )
		)

		$rule :VerifierRule(
			id == $predicate.ruleId
		)
	then
		Collection list = new ArrayList();
		list.add( $predicate );
		list.add( $other );
		result.add( new VerifierMessage(
								Severity.NOTE,
								MessageType.OPTIMISATION,
								$rule,
								"Rule \"" + $predicate.getRuleName() +
								"\" would be more effective if the less expensive component, (" +
								$other +
								"), could be before (" +
								$predicate + ").",
								list ) );
end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy