package appl;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collection;

import org.drools.base.BaseEvaluator;
import org.drools.base.ValueType;
import org.drools.common.InternalWorkingMemory;
import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
import org.drools.rule.VariableRestriction.VariableContextEntry;
import org.drools.spi.Evaluator;
import org.drools.spi.FieldValue;
import org.drools.spi.InternalReadAccessor;

import org.drools.base.evaluators.EvaluatorCache;
import org.drools.base.evaluators.EvaluatorDefinition;
import org.drools.base.evaluators.Operator;


public class SubsetOfEvaluatorsDefinition implements EvaluatorDefinition  {
    public static final Operator  SUBSETOF     = Operator.addOperatorToRegistry( "subsetOf", false );
    public static final Operator  NOT_SUBSETOF = Operator.addOperatorToRegistry( "subsetOF", true );
    
    private static final String[] SUPPORTED_IDS = { SUBSETOF.getOperatorString() };
    
    private EvaluatorCache evaluators = new EvaluatorCache() {
        private static final long serialVersionUID  = 510l;
        {
            addEvaluator( ValueType.OBJECT_TYPE, SUBSETOF,     SubsetOfEvaluator.INSTANCE );
            addEvaluator( ValueType.OBJECT_TYPE, NOT_SUBSETOF, NotSubsetOfEvaluator.INSTANCE );
        }
    };
    
    public void readExternal(ObjectInput in) throws IOException,
    ClassNotFoundException {
    	evaluators = (EvaluatorCache) in.readObject();
    }

    public void writeExternal(ObjectOutput out) throws IOException {
    	out.writeObject( evaluators );
    }
 
    public Evaluator getEvaluator(ValueType type,
    		Operator operator) {
    	return this.evaluators.getEvaluator( type,
    			operator );
    }
   
    public Evaluator getEvaluator(ValueType type,
    		Operator operator,
    		String parameterText) {
    	return this.evaluators.getEvaluator( type,
    			operator );
    }
    public Evaluator getEvaluator(final ValueType type,
    		final String operatorId,
    		final boolean isNegated,
    		final String parameterText) {
    	return this.getEvaluator( type,
    			operatorId,
    			isNegated,
    			parameterText,
    			Target.FACT,
    			Target.FACT );
    }
   
    public Evaluator getEvaluator(final ValueType type,
                                  final String operatorId,
                                  final boolean isNegated,
                                  final String parameterText,
                                  final Target left,
                                  final Target right ) {
        return this.evaluators.getEvaluator( type, Operator.determineOperator( operatorId, isNegated ) );
    }

    public String[] getEvaluatorIds() {
        return SUPPORTED_IDS;
    }

    public boolean isNegatable() {
        return true;
    }

    public Target getTarget() {
        return Target.FACT;
    }

    public boolean supportsType(ValueType type) {
        return this.evaluators.supportsType( type );
    }

    public static class SubsetOfEvaluator extends BaseEvaluator {
     
        private static boolean isSubsetOf( Collection<?> a, Collection<?> b ){
        	if( a == null || a.size() == 0 ) return true;
        	if( b == null ) return false;
        	return b.containsAll( a );
        }

        private static final long     serialVersionUID = 510l;
        public final static Evaluator INSTANCE         = new SubsetOfEvaluator();

        public SubsetOfEvaluator() {
            super( ValueType.OBJECT_TYPE, SUBSETOF );
        }
        
        public boolean evaluate(InternalWorkingMemory workingMemory,
                                final InternalReadAccessor extractor,
                                final Object object1,
                                final FieldValue object2) {
            final Collection<?> setL = (Collection<?>) object2.getValue();
            final Collection<?> setR = (Collection<?>) extractor.getValue( workingMemory, object1 );
            return isSubsetOf( setL, setR );
        }

        public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
                                           final VariableContextEntry context,
                                           final Object left) {
            final Collection<?> setL = (Collection<?>) context.declaration.getExtractor().getValue( workingMemory, left );
            final Collection<?> setR = (Collection<?>) ((ObjectVariableContextEntry) context).right;
            return isSubsetOf( setL, setR );
        }

        public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
                                          final VariableContextEntry context,
                                          final Object right) {
            final Collection<?> setL = (Collection<?>)((ObjectVariableContextEntry) context).left;
            final Collection<?> setR = (Collection<?>) context.extractor.getValue( workingMemory, right );
            return isSubsetOf( setL, setR );
        }

        public boolean evaluate(InternalWorkingMemory workingMemory,
                                final InternalReadAccessor extractor1,
                                final Object object1,
                                final InternalReadAccessor extractor2,
                                final Object object2) {
            final Collection<?> setL = (Collection<?>)extractor2.getValue( workingMemory, object2 );
            final Collection<?> setR = (Collection<?>)extractor1.getValue( workingMemory, object1 );
            return isSubsetOf( setL, setR );
        }

        public String toString() {
            return "Set subset of";
        }
    }

    public static class NotSubsetOfEvaluator extends BaseEvaluator {
        
        private static boolean isNotSubsetOf( Collection<?> a, Collection<?> b ){
        	if( a == null || a.size() == 0 ) return false;
        	if( b == null ) return true;
        	return ! b.containsAll( a );
        }

        private static final long     serialVersionUID = 510l;
        public final static Evaluator INSTANCE         = new NotSubsetOfEvaluator();

        public NotSubsetOfEvaluator() {
            super( ValueType.OBJECT_TYPE, NOT_SUBSETOF );
        }

        public boolean evaluate(InternalWorkingMemory workingMemory,
                                final InternalReadAccessor extractor,
                                final Object object1,
                                final FieldValue object2) {
            final Collection<?> setL = (Collection<?>) object2.getValue();
            final Collection<?> setR = (Collection<?>) extractor.getValue( workingMemory, object1 );
            return isNotSubsetOf( setL, setR );
        }

        public boolean evaluateCachedRight(InternalWorkingMemory workingMemory,
                                           final VariableContextEntry context,
                                           final Object left) {
            final Collection<?> setL = (Collection<?>) context.declaration.getExtractor().getValue( workingMemory, left );
            final Collection<?> setR = (Collection<?>) ((ObjectVariableContextEntry) context).right;
            return isNotSubsetOf( setL, setR );
        }

        public boolean evaluateCachedLeft(InternalWorkingMemory workingMemory,
                                          final VariableContextEntry context,
                                          final Object right) {
            final Collection<?> setL = (Collection<?>)((ObjectVariableContextEntry) context).left;
            final Collection<?> setR = (Collection<?>) context.extractor.getValue( workingMemory, right );
            return isNotSubsetOf( setL, setR );
        }

        public boolean evaluate(InternalWorkingMemory workingMemory,
                                final InternalReadAccessor extractor1,
                                final Object object1,
                                final InternalReadAccessor extractor2,
                                final Object object2) {
            final Collection<?> setL = (Collection<?>)extractor2.getValue( workingMemory, object2 );
            final Collection<?> setR = (Collection<?>) extractor1.getValue( workingMemory, object1 );
            return isNotSubsetOf( setL, setR );
        }

        public String toString() {
            return "Set not subset of";
        }
    }
}
