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

org.drools.verifier.missingEquality.MissingEquality.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: 31.10.2007
package org.drools.verifier.missingEquality

//list any import classes here.
import org.drools.core.base.evaluators.Operator;
import org.drools.core.base.evaluators.MatchesEvaluatorsDefinition;

import org.drools.verifier.components.VariableRestriction;
import org.drools.verifier.components.LiteralRestriction;
import org.drools.verifier.components.NumberRestriction;
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.Map;
import java.util.HashMap;



//declare any global variables here
global VerifierReport result;

//
// Informs that there are inequalities, but not equality being catered for.
//
// Type: Warning
// Dependencies: None
// Example: val != 10 when val == 10 is missing.
//
rule "Missing restriction in LiteralRestrictions"
    @verifying_scopes(["knowledge-package", "decision-table"])
    when
        //
        // == and !=
        //
        (
                $r :LiteralRestriction(
                    operator == Operator.EQUAL
                )
            and
                not LiteralRestriction(
                    fieldPath == $r.fieldPath,
                    operator == Operator.NOT_EQUAL,
                    patternIsNot == $r.patternIsNot,
                    valueAsString == $r.valueAsString
                )
        ) or (
                $r :LiteralRestriction(
                    operator == Operator.NOT_EQUAL
                )
            and
                not LiteralRestriction(
                    fieldPath == $r.fieldPath,
                    operator == Operator.EQUAL,
                    patternIsNot == $r.patternIsNot,
                    valueAsString == $r.valueAsString
                )
        //
        // matches and not matches
        //
        ) or (
                $r :LiteralRestriction(
                    operator == MatchesEvaluatorsDefinition.MATCHES,
                    valueAsString == $r.valueAsString
                )
            and
                not LiteralRestriction(
                    fieldPath == $r.fieldPath,
                    operator == MatchesEvaluatorsDefinition.NOT_MATCHES,
                    patternIsNot == $r.patternIsNot,
                    valueAsString == $r.valueAsString
                )
        ) or (
                $r :LiteralRestriction(
                    operator == MatchesEvaluatorsDefinition.NOT_MATCHES,
                    valueAsString == $r.valueAsString
                )
            and
                not LiteralRestriction(
                    fieldPath == $r.fieldPath,
                    operator == MatchesEvaluatorsDefinition.MATCHES,
                    patternIsNot == $r.patternIsNot,
                    valueAsString == $r.valueAsString
                )
        )
    then
        result.add( new VerifierMessage(
                                new HashMap(),
                                Severity.WARNING,
                                MessageType.MISSING_EQUALITY,
                                $r,
                                "Rule base covers " +
                                $r.getOperator().getOperatorString() + " " + $r.getValueAsString() +
                                ", but it is missing " + MissingRange.getReversedOperator( $r.getOperator() ).getOperatorString() +
                                " " + $r.getValueAsString()
                                ) );
end

rule "Missing restriction in VariableRestrictions, equal operator"
    @verifying_scopes(["knowledge-package", "decision-table"])
    when
        $r :VariableRestriction(
            operator == Operator.EQUAL
        )
        not VariableRestriction(
            fieldPath == $r.fieldPath,
            operator == Operator.NOT_EQUAL,
            patternIsNot == $r.patternIsNot,
            variable.parentPath == $r.variable.parentPath,
            variable.parentType == $r.variable.parentType
        )
    then
        result.add( new VerifierMessage(
                                new HashMap(),
                                Severity.WARNING,
                                MessageType.MISSING_EQUALITY,
                                $r,
                                "Rule base covers " +
                                $r.getOperator().getOperatorString() + " " + $r.getVariable().getName() +
                                ", but it is missing " + MissingRange.getReversedOperator( $r.getOperator() ).getOperatorString() +
                                " " + $r.getVariable().getName()
                                ) );
end

rule "Missing restriction in VariableRestrictions, unequal operator"
    @verifying_scopes(["knowledge-package", "decision-table"])
    when
        $r :VariableRestriction(
            operator == Operator.NOT_EQUAL
        )
        not VariableRestriction(
            fieldPath == $r.fieldPath,
            operator == Operator.EQUAL,
            patternIsNot == $r.patternIsNot,
            variable.parentPath == $r.variable.parentPath,
            variable.parentType == $r.variable.parentType
        )
    then
        result.add( new VerifierMessage(
                                new HashMap(),
                                Severity.WARNING,
                                MessageType.MISSING_EQUALITY,
                                $r,
                                "Rule base covers " +
                                $r.getOperator().getOperatorString() + " " + $r.getVariable().getName() +
                                ", but it is missing " + MissingRange.getReversedOperator( $r.getOperator() ).getOperatorString() +
                                " " + $r.getVariable().getName()
                                ) );
end

rule "Missing restriction in VariableRestrictions, custom operator"
    @verifying_scopes(["knowledge-package", "decision-table"])
    when
        $r :VariableRestriction(
            operator != Operator.EQUAL,
            operator != Operator.NOT_EQUAL,
            operator != Operator.LESS,
            operator != Operator.LESS_OR_EQUAL,
            operator != Operator.GREATER,
            operator != Operator.GREATER_OR_EQUAL
        )
        not VariableRestriction(
            fieldPath == $r.fieldPath,
            operator.operatorString == operator.operatorString,
            operator.negated != $r.operator.negated,
            patternIsNot == $r.patternIsNot
        )
    then
        result.add( new VerifierMessage(
                                new HashMap(),
                                Severity.WARNING,
                                MessageType.MISSING_EQUALITY,
                                $r,
                                "Rule base covers " +
                                $r.getOperator().getOperatorString() + " " + $r.getVariable().getName() +
                                ", but it is missing " + MissingRange.getReversedOperator( $r.getOperator() ).getOperatorString() +
                                " " + $r.getVariable().getName()
                                ) );
end




© 2015 - 2025 Weber Informatics LLC | Privacy Policy