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
/*
 * 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: 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.core.base.evaluators.Operator;
import org.drools.core.base.evaluators.MatchesEvaluatorsDefinition;

import org.drools.verifier.components.SubPattern;
import org.drools.verifier.components.Restriction;
import org.drools.verifier.components.Pattern;
import org.drools.verifier.components.VerifierRule;
import org.drools.verifier.components.PatternOperatorDescr;
import org.drools.verifier.components.Eval;
import org.drools.verifier.data.VerifierComponent;
import org.drools.verifier.components.VerifierComponentType;
import org.drools.verifier.components.RuleComponent;
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 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

        $sp : SubPattern()

        (
                $r1 :Restriction(
                    this memberOf $sp.items,
                    ( operator == Operator.EQUAL || == MatchesEvaluatorsDefinition.MATCHES )
                )
            and
                $r2 :Restriction(
                    this memberOf $sp.items,
                    path != $r1.path,
                    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(
                    this memberOf $sp.items,
                    ( operator == Operator.GREATER || == Operator.LESS )
                )
            and
                $r2 :Restriction(
                    this memberOf $sp.items,
                    path != $r1.path,
                    orderNumber < $r1.orderNumber,
                    ( operator == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL || == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
                )
        ) or (
                $r1 :Restriction(
                    this memberOf $sp.items,
                    ( operator == Operator.GREATER_OR_EQUAL || == Operator.LESS_OR_EQUAL )
                )
            and
                $r2 :Restriction(
                    this memberOf $sp.items,
                    path != $r1.path,
                    orderNumber < $r1.orderNumber,
                    ( operator == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
                )
        )

    then
        Map impactedRules = new HashMap();
        impactedRules.put( $sp.getRulePath(), $sp.getRuleName());

        Collection list = new ArrayList();
        list.add( $r1 );
        list.add( $r2 );
        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.NOTE,
                                MessageType.OPTIMISATION,
                                $sp.getPattern(),
                                "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(
                    parentPath == $r1.parentPath,
                    parentType == $r1.parentType,
                    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(
                    parentPath == $r1.parentPath,
                    parentType == $r1.parentType,
                    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(
                    parentPath == $r1.parentPath,
                    parentType == $r1.parentType,
                    orderNumber < $r1.orderNumber,
                    ( operator == MatchesEvaluatorsDefinition.NOT_MATCHES || == Operator.NOT_EQUAL )
                )
        )

        $pattern :Pattern(
            path == $r1.patternPath
        )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $pattern.getPath(), $pattern.getRuleName());

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

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

        $subPattern : SubPattern(
            $pattern :pattern
        )

        $eval :Eval(
            this memberOf $subPattern.items
        )

        $restriction :Restriction(
            this memberOf $subPattern.items,
            orderNumber > $eval.orderNumber
        )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $subPattern.getRulePath(), $subPattern.getRuleName());

        Collection list = new ArrayList();
        list.add( $eval );
        list.add( $restriction );
        result.add( new VerifierMessage(
                                impactedRules,
                                Severity.NOTE,
                                MessageType.OPTIMISATION,
                                $pattern,
                                "Rule \"" + $subPattern.getRuleName() +
                                "\" would be more effective if the less expensive component, (" +
                                $restriction +
                                "), could be before (" +
                                $eval + ").",
                                list ) );
end





© 2015 - 2025 Weber Informatics LLC | Privacy Policy