
package com.uwetrottmann.shopr.algorithm.model;

import com.uwetrottmann.shopr.algorithm.model.Attributes.Attribute;
import com.uwetrottmann.shopr.algorithm.model.Attributes.AttributeValue;

public abstract class GenericAttribute implements Attribute {

    private AttributeValue currentValue;

    double[] mValueWeights;

    public AttributeValue currentValue() {
        return currentValue;
    }

    public GenericAttribute currentValue(AttributeValue currentValue) {
        this.currentValue = currentValue;
        return this;
    }

    public double[] getValueWeights() {
        return mValueWeights;
    }

    public abstract AttributeValue[] getValueSymbols();

    @Override
    public String getValueWeightsString() {
        StringBuilder builder = new StringBuilder();
        AttributeValue[] values = getValueSymbols();

        builder.append("[");
        for (int i = 0; i < mValueWeights.length; i++) {
            if (mValueWeights[i] == 1) {
                return "[" + values[i].descriptor() + ":" + mValueWeights[i] + "]";
            }
            builder.append(values[i].descriptor()).append(":").append(mValueWeights[i]).append(" ");
        }
        builder.append("]");

        return builder.toString();
    }

    @Override
    public String getReasonString() {
        StringBuilder reason = new StringBuilder();
        AttributeValue[] values = getValueSymbols();

        int maxIndex = 0; // assuming there is always a maximum
        for (int i = 0; i < mValueWeights.length; i++) {
            if (mValueWeights[i] == 1) {
                // e.g. "only Red"
                return "only " + values[i].descriptor();
            }
            if (mValueWeights[i] == 0) {
                // e.g. ", no Red"
                if (reason.length() != 0) {
                    reason.append(", ");
                }
                reason.append("no ").append(values[i].descriptor());
            }
            if (mValueWeights[maxIndex] > mValueWeights[i]) {
                maxIndex = i;
            }
        }

        // e.g. ", mainly Red"
        if (reason.length() != 0) {
            reason.append(", ");
        }
        reason.append("mainly ").append(values[maxIndex].descriptor());

        return reason.toString();
    }

}

@@ -1938,99 +1938,452 @@
    %7D
-%0A            if (mValueWeights%5BmaxIndex%5D %3E mValueWeights%5Bi%5D) %7B%0A                maxIndex = i
+ else if (mValueWeights%5Bi%5D %3E mValueWeights%5BmaxIndex%5D) %7B%0A                maxIndex = i;%0A            %7D%0A        %7D%0A%0A        // check for global maximum%0A        boolean hasGlobalMaximum = true;%0A        for (int i = 0; i %3C mValueWeights.length; i++) %7B%0A            if (i == maxIndex) %7B%0A                continue;%0A            %7D%0A            if (mValueWeights%5Bi%5D == mValueWeights%5BmaxIndex%5D) %7B%0A                hasGlobalMaximum = false;%0A                break
 ;%0A  
@@ -2428,32 +2428,68 @@
  %22, mainly Red%22%0A
+        if (hasGlobalMaximum) %7B%0A    
         if (reas
@@ -2500,32 +2500,36 @@
 ength() != 0) %7B%0A
+    
             reas
@@ -2545,32 +2545,36 @@
 (%22, %22);%0A        
+    
 %7D%0A        reason
@@ -2559,32 +2559,36 @@
       %7D%0A        
+ 
+   
 reason.append(%22m
@@ -2634,16 +2634,26 @@
 ptor());
+%0A        %7D
 %0A%0A      

Fix reason string to handle cases with no global maximum.