Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package structuredoutputcbr.adaptation; import java.util.ArrayList; import java.util.Comparator; import structuredoutputcbr.StructuredOutputCBR; import structuredoutputcbr.casebase.CaseBlock; import structuredoutputcbr.ontology.OntologyElement; import org.apache.commons.collections4.CollectionUtils; /** * Class that implements a comparator between combination of exercises given the maximization union-intersection * @author Joan T. Matamalas <jtmatamalas@gmail.com> */ public class ComparatorBetweenCombinations implements Comparator { //the muscular group which is used to find the best combination private final OntologyElement muscular_group; /** * Constuctor of the class * @param muscular_group The muscular_group where the exercises are optimized */ public ComparatorBetweenCombinations(OntologyElement muscular_group) { this.muscular_group = muscular_group; } @Override /** * Comparator between two combinations of exercises */ public int compare(Object o1, Object o2) { //Cast the objects ArrayList<OntologyElement> cb1 = (ArrayList<OntologyElement>) o1; ArrayList<OntologyElement> cb2 = (ArrayList<OntologyElement>) o2; //Get all the muscles of the muscular_group ArrayList<OntologyElement> muscles_group = muscular_group.getRelationValues("muscles"); //Get the first ontology element for exercise for each object OntologyElement ex1 = cb1.get(0); OntologyElement ex2 = cb2.get(0); //Start the intersections and unions ArrayList<OntologyElement> inter1 = ex1.getRelationValues("muscles"); inter1 = (ArrayList<OntologyElement>) CollectionUtils.union(inter1, muscles_group); ArrayList<OntologyElement> inter2 = ex2.getRelationValues("muscles"); inter2 = (ArrayList<OntologyElement>) CollectionUtils.union(inter1, muscles_group); ArrayList<OntologyElement> union1 = ex1.getRelationValues("muscles"); union1 = (ArrayList<OntologyElement>) CollectionUtils.union(union1, muscles_group); ArrayList<OntologyElement> union2 = ex2.getRelationValues("muscles"); union2 = (ArrayList<OntologyElement>) CollectionUtils.union(union2, muscles_group); //Keep adding exercises to compute intersections and unions for (int i = 1; i < cb1.size(); i++) { ex1 = cb1.get(i); ex2 = cb2.get(i); ArrayList<OntologyElement> muscles_ex1 = ex1.getRelationValues("muscles"); muscles_ex1 = (ArrayList<OntologyElement>) CollectionUtils.union(inter1, muscles_group); ArrayList<OntologyElement> muscles_ex2 = ex2.getRelationValues("muscles"); muscles_ex2 = (ArrayList<OntologyElement>) CollectionUtils.union(inter1, muscles_group); inter1 = (ArrayList<OntologyElement>) CollectionUtils.intersection(inter1, muscles_ex1); inter2 = (ArrayList<OntologyElement>) CollectionUtils.intersection(inter2, muscles_ex2); union1 = (ArrayList<OntologyElement>) CollectionUtils.union(union1, muscles_ex1); union2 = (ArrayList<OntologyElement>) CollectionUtils.union(union2, muscles_ex2); } //Return the result of the comparation return (union1.size() - inter1.size()) - (union2.size() - inter2.size()); } }