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

org.drools.verifier.redundancy.Warnings.drl Maven / Gradle / Ivy

There is a newer version: 9.44.0.Final
Show newest version
/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * 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: 9.12.2007
package org.drools.verifier.redundancy

//list any import classes here.
global VerifierReport result;



//declare any global variables here
import java.util.Collection;
import java.util.ArrayList;

import org.drools.verifier.components.SubRule;
import org.drools.verifier.components.VerifierRule;
import org.drools.verifier.components.Consequence;

import org.drools.verifier.data.VerifierReport;

import org.drools.verifier.components.RuleComponent;
import org.drools.verifier.report.components.Redundancy;
import org.drools.verifier.report.components.Equivalence;
import org.drools.verifier.report.components.Subsumption;
import org.drools.verifier.report.components.VerifierMessage;
import org.drools.verifier.report.components.Cause;
import org.drools.verifier.report.components.Severity;
import org.drools.verifier.report.components.MessageType;

import java.util.Map;
import java.util.HashMap;



rule "Find redundant LHS"
    @verifying_scopes(["knowledge-package", "decision-table"])
    when
        // Check that VerifierRule $left and $right have redundant fields.
        $left :VerifierRule()

        $right :VerifierRule(
            path != $left.path
        )

        $leftSubRules : ArrayList()
            from collect(
                SubRule(
                    rulePath == $left.path
                )
            )

        $rightSubRules : ArrayList()
            from collect(
                SubRule(
                    rulePath == $right.path
                )
            )

        forall (
            $subRule :SubRule(
            ) from $leftSubRules

            Subsumption(
                left == $subRule,
                right memberOf $rightSubRules
            )
        )
        forall (
            $subRule :SubRule(
            ) from $rightSubRules

            Subsumption(
                left == $subRule,
                right memberOf $leftSubRules
            )
        )
    then
end

rule "Find equivalent Rules" extends "Find redundant LHS"
    // This rule needs to fire after "Find redundant rules"
    // or the not Reduncancy( ... ) does not check correctly.
    salience -1
    when

        not Equivalence(
            items contains $left,
            items contains $right
        )

        // RHS should not be redundant
        $leftConsequence : Consequence(
            path == $left.consequencePath,
            consequenceType == $left.consequenceType
        )
        $rightConsequence : Consequence(
            path == $right.consequencePath,
            consequenceType == $right.consequenceType
        )
        not Redundancy(
            items contains $leftConsequence,
            items contains $rightConsequence
        )

        $causes : ArrayList()
            from collect(
                  Subsumption(
                      left memberOf $rightSubRules,
                      right memberOf $leftSubRules
                  )
            )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $leftConsequence.getRulePath(), $leftConsequence.getRuleName());
        impactedRules.put( $rightConsequence.getRulePath(), $rightConsequence.getRuleName());

        Equivalence equivalence = new Equivalence( $left, $right, $causes );
        insert( equivalence );

        result.add(new VerifierMessage(
                        impactedRules,
                        Severity.WARNING,
                        MessageType.EQUIVALANCE,
                        null,
                        "Rule is equivalent to rule " + $right.getName() + ".",
                        equivalence ) );
end

//
// Dependencies:
//
//    Needs at least one rule from these packages:
//        package org.drools.verifier.equivalency.rules
//            rule "Find equivalent Rules"
//        package org.drools.verifier.redundancy.rules
//            rule "Find redundant Rules, strong redundancy"
//
//    And this rule:
//        package org.drools.verifier.redundancy.possibilities
//            rule "Find rule possibility redundancy"
//
rule "Find redundant rules" extends "Find redundant LHS"
    when

        not Redundancy(
            items contains $left,
            items contains $right
        )

        // RHS needs to be redundant
        $leftConsequence : Consequence(
            path == $left.consequencePath,
            consequenceType == $left.consequenceType
        )
        $rightConsequence : Consequence(
            path == $right.consequencePath,
            consequenceType == $right.consequenceType
        )
        exists Redundancy(
            items contains $leftConsequence,
            items contains $rightConsequence
        )

//        $causes : ArrayList()
//            from collect(
//                  Subsumption(
//                      (
//                          left memberOf $rightSubRules
//                          &&
//                          right memberOf $leftSubRules
//                      ) || (
//                          right memberOf $rightSubRules
//                          &&
//                          left memberOf $leftSubRules
//                      )
//                  )
//            )
    then
//        Map impactedRules = new HashMap();
//        impactedRules.put( $leftConsequence.getRulePath(), $leftConsequence.getRuleName());
//        impactedRules.put( $rightConsequence.getRulePath(), $rightConsequence.getRuleName());

//        Redundancy redundancy = new Redundancy( $left, $right, $causes );
//        insert( redundancy );

//        result.add(new VerifierMessage(
//                        impactedRules,
//                        Severity.WARNING,
//                        MessageType.REDUNDANCY,
//                        null,
//                        "Rules " + $right.getName() + " and " + $left.getName() + " are redundant.",
//                        redundancy ) );
end

/*

//
// Dependencies:
//
//    Needs at least one rule from these packages:
//        package org.drools.verifier.equivalency.rules
//            rule "Find equivalent Rules"
//        package org.drools.verifier.redundancy.rules
//            rule "Find redundant Rules, strong redundancy"
//
//    And this rule:
//        package org.drools.verifier.redundancy.possibilities
//            rule "Find rule possibility redundancy"
//
rule "Find subsumptant rule possibilities from different rules"
    @verifying_scopes(["knowledge-package", "decision-table"])
    salience -100
    when
        $left :VerifierRule()
        $right :VerifierRule(
            path != $left.path
        )

        $subsumption :Subsumption(
            left == $left,
            right == $right
        )

        // No redundacy
        not Subsumption(
            left == $subsumption.right,
            right == $subsumption.left
        )

    then
        Map impactedRules = new HashMap();
        impactedRules.put( $left.getPath(), $left.getName());
        impactedRules.put( $right.getPath(), $right.getName());

        Collection causes = new ArrayList();
        causes.add( $subsumption );

        result.add(new VerifierMessage(
                        impactedRules,
                        Severity.WARNING,
                        MessageType.SUBSUMPTION,
                        null,
                        "Rules are subsumptant.",
                        causes ) );
end
*/




© 2015 - 2025 Weber Informatics LLC | Privacy Policy