
package com.uwetrottmann.shopr.algorithm;

import com.uwetrottmann.shopr.algorithm.model.Attributes;
import com.uwetrottmann.shopr.algorithm.model.ClothingType;
import com.uwetrottmann.shopr.algorithm.model.Color;
import com.uwetrottmann.shopr.algorithm.model.Item;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Utils {

    /**
     * Returns a subset of the overall case base filtered by hard-limits like
     * location, availability and opening hours.
     */
    public static List<Item> getLimitedCaseBase() {
        ArrayList<Item> cases = new ArrayList<Item>();
        // dresses
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.RED))
                .type(new ClothingType(ClothingType.Value.DRESS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLUE))
                .type(new ClothingType(ClothingType.Value.DRESS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.GREEN))
                .type(new ClothingType(ClothingType.Value.DRESS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLACK))
                .type(new ClothingType(ClothingType.Value.DRESS))));

        // Jeans
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.RED))
                .type(new ClothingType(ClothingType.Value.JEANS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLUE))
                .type(new ClothingType(ClothingType.Value.JEANS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.GREEN))
                .type(new ClothingType(ClothingType.Value.JEANS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLACK))
                .type(new ClothingType(ClothingType.Value.JEANS))));

        // Shirts
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.RED))
                .type(new ClothingType(ClothingType.Value.SHIRT))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLUE))
                .type(new ClothingType(ClothingType.Value.SHIRT))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.GREEN))
                .type(new ClothingType(ClothingType.Value.SHIRT))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLACK))
                .type(new ClothingType(ClothingType.Value.SHIRT))));

        // Shorts
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.RED))
                .type(new ClothingType(ClothingType.Value.SHORTS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLUE))
                .type(new ClothingType(ClothingType.Value.SHORTS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.GREEN))
                .type(new ClothingType(ClothingType.Value.SHORTS))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLACK))
                .type(new ClothingType(ClothingType.Value.SHORTS))));

        // Trousers
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.RED))
                .type(new ClothingType(ClothingType.Value.TROUSER))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLUE))
                .type(new ClothingType(ClothingType.Value.TROUSER))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.GREEN))
                .type(new ClothingType(ClothingType.Value.TROUSER))));
        cases.add(new Item().attributes(new Attributes()
                .color(new Color(Color.Value.BLACK))
                .type(new ClothingType(ClothingType.Value.TROUSER))));

        return cases;
    }

    /**
     * Sorts items by similarity to query using the Sim function: sim(query,
     * item of caseBase).
     */
    public static void sortBySimilarityToQuery(Query query, List<Item> caseBase) {
        // calculate similarity value for each item
        for (Item item : caseBase) {
            item.querySimilarity(Similarity.similarity(query.attributes(), item.attributes()));
        }

        // sort highest similarity first
        Collections.sort(caseBase, new Comparator<Item>() {
            @Override
            public int compare(Item o1, Item o2) {
                // is o2 smaller?
                if (o1.querySimilarity() > o2.querySimilarity()) {
                    return -1;
                }
                // is o2 bigger?
                if (o1.querySimilarity() < o2.querySimilarity()) {
                    return 1;
                }
                // they are equal!
                return 0;
            }
        });
    }

    public static void dumpToConsole(List<Item> cases, Query query) {
        System.out.println("Query " + query.attributes().color());
        for (int i = 0; i < cases.size(); i++) {
            System.out.println("[" + i + "] Item " + cases.get(i).attributes().color() + " "
                    + cases.get(i).attributes().type()
                    + " querySimilarity: " + cases.get(i).querySimilarity());
        }
    }

}

@@ -5377,24 +5377,70 @@
 ry query) %7B%0A
+        if (query.attributes() != null) %7B%0A    
         Syst
@@ -5491,18 +5491,146 @@
 .color()
-);
+ + %22 %22%0A                    + query.attributes().type());%0A        %7D else %7B%0A            System.out.println(%22Query EMPTY%22);%0A        %7D
 %0A       

Fix for null case.