/*
 * 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: 12.10.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.SubPattern;
import org.drools.verifier.components.SubRule;
import org.drools.verifier.data.VerifierReport;
import org.drools.verifier.report.components.Redundancy;
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;


#
# Dependencies:
#
#    Needs at least one rule from following rules:
#        package org.drools.verifier.redundancy.restrictions
#            rule "Find redundant VariableRestriction"
#            rule "Find redundant LiteralRestriction"
#
rule "Find redundant restrictions from pattern possibilities"
    salience -100
    when
        $r :Redundancy()

        $pp :SubPattern(
            items contains $r.items[0],
            items contains $r.items[1]
        )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $pp.getRulePath(), $pp.getRuleName());

        Collection causes = new ArrayList();
        causes.addAll( $r.getItems() );

        result.add(new VerifierMessage(
                        impactedRules,
                        Severity.NOTE,
                        MessageType.REDUNDANCY,
                        null,
                        "Same restriction exists twice.",
                        causes ) );
end

#
# Dependencies:
#
#        package org.drools.verifier.redundancy.possibilities
#            rule "Find pattern possibility redundancy"
#
rule "Find redundant pattern possibilities from rule possibilities"
    salience -100
    when
        $r :Redundancy()

        $sr :SubRule(
            items contains $r.items[0],
            items contains $r.items[1]
        )
    then
        Map impactedRules = new HashMap();
        impactedRules.put( $sr.getRulePath(), $sr.getRuleName());

        Collection causes = new ArrayList();
        causes.addAll( $r.getItems() );

        result.add(new VerifierMessage(
                        impactedRules,
                        Severity.NOTE,
                        MessageType.REDUNDANCY,
                        null,
                        "Two pattern possibilities with redundant restrictions exists twice in the same rule.",
                        causes ) );
end

