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

org.drools.verifier.opposites.Restrictions.drl Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
#created on: 23.02.2008
package org.drools.verifier.opposites

#list any import classes here.
import org.drools.verifier.components.LiteralRestriction;
import org.drools.verifier.components.VariableRestriction;
import org.drools.verifier.components.Field.FieldType;

import org.drools.verifier.report.components.Cause;
import org.drools.verifier.report.components.MissingRange;
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.report.components.Opposites;
import org.drools.verifier.report.components.MissingRange;

import org.drools.verifier.dao.VerifierResult;

import org.drools.base.evaluators.Operator;

#declare any global variables here
global VerifierResult result;

#
# If two LiteralRestrictions are in conflict.
#
# Dependencies: None
# Example: Pattern( a == "b", a != "b" )
#
rule "Opposite LiteralRestrictions"
	when
		$left :LiteralRestriction(
			# Not >= and <=, because ( a <=, a >= ) works.
			# And not < or > because they are handled in a separate rule.
			( operator != Operator.GREATER_OR_EQUAL || != Operator.LESS_OR_EQUAL || != Operator.LESS || != Operator.GREATER )
		)

		$right :LiteralRestriction(
			fieldId == $left.fieldId,
			valueType == $left.valueType,
			id != $left.id,
			# Operator needs to be reversed to what the other one has.
			eval( operator == MissingRange.getReversedOperator( $left.getOperator() ))
		)

		eval( $left.compareValues( $right ) == 0 )

		# Check that there is not already a pair with these values.
		not Opposites( 
			left == $left, 
			right == $right 
		)
		not Opposites( 
			right == $left, 
			left == $right 
		)
	then
		insert( new Opposites( $left, $right ));
end

#
# If two LiteralRestrictions are in conflict.
#
# Dependencies: None
# Example: Pattern( a >= 10 && a < 10 )
#
rule "Opposite LiteralRestrictions with ranges, greater or equal - less"
	when
		$left :LiteralRestriction(
			operator == Operator.GREATER_OR_EQUAL 
		)

		$right :LiteralRestriction(
			restrictionType == $left.restrictionType,
			fieldId == $left.fieldId,
			valueType == $left.valueType,
			operator == Operator.LESS,
			id != $left.id
		)
		
		eval( $left.compareValues( $right ) == 0 )

		# Check that there is not already a pair with these values.
		not Opposites( 
			left == $left, 
			right == $right 
		)
		not Opposites( 
			right == $left, 
			left == $right 
		)
	then
		insert( new Opposites( $left, $right ));
end

#
# If two LiteralRestrictions are in conflict.
#
# Dependencies: None
# Example: Pattern( a > 10 && a <= 10 )
#
rule "Opposite LiteralRestrictions with ranges, greater - less or equal"
	when
		$left :LiteralRestriction(
			operator == Operator.GREATER
		)

		$right :LiteralRestriction(
			restrictionType == $left.restrictionType,
			fieldId == $left.fieldId,
			valueType == $left.valueType,
			operator == Operator.LESS_OR_EQUAL,
			id != $left.id
		)
		
		eval( $left.compareValues( $right ) == 0 )

		# Check that there is not already a pair with these values.
		not Opposites( 
			left == $left, 
			right == $right 
		)
		not Opposites( 
			right == $left, 
			left == $right 
		)
	then
		insert( new Opposites( $left, $right ));
end


#
# If two LiteralRestrictions are in conflict.
#
# Dependencies: None
# Example: Pattern( a < 10 && a > 9 )
#
rule "Opposite LiteralRestrictions with ranges, less - greater for ints and dates"
	when
		$left :LiteralRestriction(
			operator == Operator.LESS,
			( valueType == Field.FieldType.INT || == Field.FieldType.DATE )
		)

		$right :LiteralRestriction(
			restrictionType == $left.restrictionType,
			fieldId == $left.fieldId,
			valueType == $left.valueType,
			operator == Operator.GREATER,
			id != $left.id
		)
		
		eval( $left.compareValues( $right ) == 1 )
		
		# Check that there is not already a pair with these values.
		not Opposites( 
			left == $left, 
			right == $right 
		)
		not Opposites( 
			right == $left, 
			left == $right 
		)
	then
		insert( new Opposites( $left, $right ));
end

#
# If two LiteralRestrictions are in conflict.
#
# Dependencies: None
# Example: Pattern( a >= 10 && a <= 9 )
#
rule "Opposite LiteralRestrictions with ranges, less or equal - greater or equal for ints and dates"
	when
		$left :LiteralRestriction(
			operator == Operator.GREATER_OR_EQUAL,
			( valueType == Field.FieldType.INT || == Field.FieldType.DATE )
		)

		$right :LiteralRestriction(
			restrictionType == $left.restrictionType,
			fieldId == $left.fieldId,
			valueType == $left.valueType,
			operator == Operator.LESS_OR_EQUAL,
			id != $left.id
		)
		
		eval( $left.compareValues( $right ) == 1 )
		
		# Check that there is not already a pair with these values.
		not Opposites( 
			left == $left, 
			right == $right 
		)
		not Opposites( 
			right == $left, 
			left == $right 
		)
	then
		insert( new Opposites( $left, $right ));
end

#
# If two VariableRestrictions are in conflict.
#
# Dependencies: None
# Example: Pattern( a contains $o, a not contains $o )
#
rule "Opposite VariableRestrictions"
	when
		$r1 :VariableRestriction()

		$r2 :VariableRestriction(
			fieldId == $r1.fieldId,
			id != $r1.id,
			variable.objectId == $r1.variable.objectId,
			variable.objectType == $r1.variable.objectType,
			# Operator needs to be reverse to what the other one has.
			eval( operator == MissingRange.getReversedOperator( $r1.getOperator() ))
		)
		
		# Check that there is not already a pair with these values.
		not Opposites( 
			left == $r1, 
			right == $r2 
		)
		not Opposites( 
			right == $r1, 
			left == $r2 
		)
	then
		insert( new Opposites( $r1, $r2 ) );
end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy