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

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

There is a newer version: 9.44.0.Final
Show newest version
/*
 * Copyright 2010 JBoss Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      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.
 */

//created on: 13.11.2007
package org.drools.verifier.incoherence

//list any import classes here.
import org.drools.verifier.components.LiteralRestriction;
import org.drools.verifier.components.NumberRestriction;
import org.drools.verifier.components.SubPattern;
import org.drools.verifier.components.Pattern;
import org.drools.verifier.components.VariableRestriction;
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.data.VerifierReport;

import java.util.Collection;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

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

//declare any global variables here
global VerifierReport result;

//
// If two LiteralRestrictions are in conflict.
//
// Type: Error
// Dependencies: None
// Example: IncorencePattern1( a == "b", a != "b" )
//
rule "Incoherent LiteralRestrictions in pattern possibility"
    when
        $r1 :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 )
        )

        $r2 :LiteralRestriction(
            patternPath == $r1.patternPath,
            restrictionType == $r1.restrictionType,
            fieldPath == $r1.fieldPath,
            valueType == $r1.valueType,
            path != $r1.path,
            // Operator needs to be reversed to what the other one has.
            eval( operator == MissingRange.getReversedOperator( $r1.getOperator() )),
            valueAsString == $r1.valueAsString
        )

        // There is a problem if both of these are in the same SubPattern.
        $pp :SubPattern(
             items contains $r1,
             items contains $r2
        )

        $p :Pattern( path == $pp.patternPath )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $p.getRulePath(), $p.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );

        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.ERROR,
                                MessageType.INCOHERENCE,
                                $p,
                                "Restriction " + $r1 + " and " + $r2 +
                                "are in conflict. Because of this, pattern that contains them can never be satisfied.",
                                list
                                ) );
end

//
// If two LiteralRestrictions are in conflict.
//
// Type: Error
// Dependencies: None
// Example: IncorencePattern( a > 10 && a < -10 )
//
rule "Incoherent LiteralRestrictions with ranges in pattern possibility, impossible ranges"
    when
        $r1 :NumberRestriction(
            ( operator == Operator.GREATER_OR_EQUAL || == Operator.GREATER )
        )

        $r2 :NumberRestriction(
            patternPath == $r1.patternPath,
            restrictionType == $r1.restrictionType,
            fieldPath == $r1.fieldPath,
            valueType == $r1.valueType,
            ( operator == Operator.LESS_OR_EQUAL || == Operator.LESS),
            path != $r1.path,
            value < $r1.value
        )

        // There is a problem if both of these are in the same SubPattern.
        $pp :SubPattern(
             items contains $r1,
             items contains $r2
        )

        $p :Pattern( path == $pp.patternPath )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $p.getRulePath(), $p.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );

        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.ERROR,
                                MessageType.INCOHERENCE,
                                $p,
                                "Restriction " + $r1 + " and " + $r2 +
                                "are in conflict. Because of this, pattern that contains them can never be satisfied.",
                                list
                                ) );
end

//
// If two LiteralRestrictions are in conflict.
//
// Type: Error
// Dependencies: None
// Example: IncorencePattern( a < 1 && a == 10 )
//
rule "Incoherent LiteralRestrictions with ranges in pattern possibility, impossible equality less or equal"
    when
        $r1 :NumberRestriction(
            operator == Operator.EQUAL
        )

        $r2 :NumberRestriction(
            patternPath == $r1.patternPath,
            restrictionType == $r1.restrictionType,
            fieldPath == $r1.fieldPath,
            valueType == $r1.valueType,
            ( operator == Operator.LESS_OR_EQUAL || == Operator.LESS || == Operator.EQUAL ),
            path != $r1.path,
            value < $r1.value
        )

        // There is a problem if both of these are in the same SubPattern.
        $pp :SubPattern(
             items contains $r1,
             items contains $r2
        )

        $p :Pattern( path == $pp.patternPath )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $p.getRulePath(), $p.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );

        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.ERROR,
                                MessageType.INCOHERENCE,
                                $p,
                                "Restriction " + $r1 + " and " + $r2 +
                                "are in conflict. Because of this, pattern that contains them can never be satisfied.",
                                list
                                ) );
end

//
// If two LiteralRestrictions are in conflict.
//
// Type: Error
// Dependencies: None
// Example: IncorencePattern( a > 10 && a == 1 )
//
rule "Incoherent LiteralRestrictions with ranges in pattern possibility, impossible equality greater"
    when
        $r1 :NumberRestriction(
            ( operator == Operator.GREATER || == Operator.GREATER_OR_EQUAL )
        )

        $r2 :NumberRestriction(
            patternPath == $r1.patternPath,
            restrictionType == $r1.restrictionType,
            fieldPath == $r1.fieldPath,
            valueType == $r1.valueType,
            operator == Operator.EQUAL,
            path != $r1.path,
            value < $r1.value
        )

        // There is a problem if both of these are in the same SubPattern.
        $pp :SubPattern(
             items contains $r1,
             items contains $r2
        )

        $p :Pattern( path == $pp.patternPath )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $p.getRulePath(), $p.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );

        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.ERROR,
                                MessageType.INCOHERENCE,
                                $p,
                                "Restriction " + $r1 + " and " + $r2 +
                                "are in conflict. Because of this, pattern that contains them can never be satisfied.",
                                list
                                ) );
end

//
// If two LiteralRestrictions are in conflict.
//
// Type: Error
// Dependencies: None
// Example: IncorencePattern( a < "12-Dec-2007", a > "12-Dec-2007" )
//
rule "Incoherent LiteralRestrictions with ranges in pattern possibility, impossible range"
    when
        $r1 :LiteralRestriction(
            operator == Operator.LESS
        )

        $r2 :LiteralRestriction(
            patternPath == $r1.patternPath,
            restrictionType == $r1.restrictionType,
            fieldPath == $r1.fieldPath,
            valueType == $r1.valueType,
            operator == Operator.GREATER,
            path != $r1.path,
            valueAsString == $r1.valueAsString
        )

        // There is a problem if both of these are in the same SubPattern.
        $pp :SubPattern(
             items contains $r1,
             items contains $r2
        )

        $p :Pattern( path == $pp.patternPath )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $p.getRulePath(), $p.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );

        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.ERROR,
                                MessageType.INCOHERENCE,
                                $p,
                                "Restriction " + $r1 + " and " + $r2 +
                                "are in conflict. Because of this, pattern that contains them can never be satisfied.",
                                list
                                ) );
end

//
// If two VariableRestrictions are in conflict.
//
// Type: Error
// Dependencies: None
// Example: IncorencePattern( a contains $o, a not contains $o )
//
rule "Incoherent VariableRestrictions in pattern possibility"
    when
        $r1 :VariableRestriction(
            // Not >= and <=, because ( a <=, a >= ) works.
            // And not < or > because they are handled in a separete rule.
            ( operator != Operator.GREATER_OR_EQUAL || != Operator.LESS_OR_EQUAL || != Operator.LESS || != Operator.GREATER )
        )

        $r2 :VariableRestriction(
            patternPath == $r1.patternPath,
            fieldPath == $r1.fieldPath,
            path != $r1.path,
            variable.parentPath == $r1.variable.parentPath,
            variable.parentType == $r1.variable.parentType,
            // Operator needs to be reversed to what the other one has.
            eval( operator == MissingRange.getReversedOperator( $r1.getOperator() ))
        )

        // There is a problem if both of these are in the same SubPattern.
        $pp :SubPattern(
             items contains $r1,
             items contains $r2
        )

        $p :Pattern( path == $pp.patternPath )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $p.getRulePath(), $p.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );

        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.ERROR,
                                MessageType.INCOHERENCE,
                                $p,
                                "Restriction " + $r1 + " and " + $r2 +
                                "are in conflict. Because of this, pattern that contains them can never be satisfied.",
                                list
                                ) );
end


//
// If two VariableRestrictions are in conflict.
//
// Type: Error
// Dependencies: None
// Example: IncorencePattern( a > $var, a < $var )
//
rule "Incoherent VariableRestrictions in pattern possibility, impossible range"
    when
        $r1 :VariableRestriction(
            operator == Operator.LESS
        )

        $r2 :VariableRestriction(
            patternPath == $r1.patternPath,
            fieldPath == $r1.fieldPath,
            operator == Operator.GREATER,
            variable.parentPath == $r1.variable.parentPath,
            variable.parentType == $r1.variable.parentType,
            path != $r1.path
        )

        // There is a problem if both of these are in the same SubPattern.
        $pp :SubPattern(
             items contains $r1,
             items contains $r2
        )

        $p :Pattern( path == $pp.patternPath )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $p.getRulePath(), $p.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );

        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.ERROR,
                                MessageType.INCOHERENCE,
                                $p,
                                "Restriction " + $r1 + " and " + $r2 +
                                "are in conflict. Because of this, pattern that contains them can never be satisfied.",
                                list
                                ) );
end





© 2015 - 2025 Weber Informatics LLC | Privacy Policy