package flintstones.entity.ahp; import java.util.LinkedHashMap; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamWriter; import flintstones.entity.problemelement.entities.ProblemElement; import flintstones.helper.DoubleHelper; import flintstones.helper.MatrixHelper; import flintstones.helper.StringHelper; import flintstones.helper.ahp.AHPHelper; import flintstones.valuation.ahp.AHPValuation; /** * The Class AHPMatrix. */ public class AHPMatrix { /** The content. */ private LinkedHashMap> content = new LinkedHashMap<>(); /* * El cuestionario: * * A2 -> 8 <- A1 A2 -> 1/6 <- A3 A1 -> 1/8 <- A2 (Repe) A1 -> 1 <- A3 A3 -> 6 <- * A2 (Repe) A3 -> 1 <- A1 (Repe) * * Genera la matrix: A2 A1 A3 A2 1 8 1/6 A1 1/8 1 1 A3 6 1 1 * * Genera la matrix de índices: A2 A1 A3 A2 8 15 3 A1 1 8 8 A3 13 8 8 * * */ /** * Instantiates a new AHP matrix. * * @param domain the domain * @param alternatives the alternatives */ public AHPMatrix(ProblemElement[] alternatives) { for (int i = 0; i < alternatives.length; i++) { ProblemElement alternative = alternatives[i]; LinkedHashMap map = new LinkedHashMap<>(); for (int j = i + 1; j < alternatives.length; j++) { ProblemElement b = alternatives[j]; AHPValuation v = new AHPValuation().buildAHPValuation(alternative, b); map.put(b, v); } content.put(alternative, map); } } /** * Sets the cell to a value by it index. * * @param a the first PE * @param b the second PE * @param index the index of the value in the domain */ public void set(ProblemElement a, ProblemElement b, int index) { if (a.equals(b)) return; LinkedHashMap map = content.get(a); AHPValuation val = map.get(b); if (val == null) { map = content.get(b); val = map.get(a); int reverseIndex = AHPHelper.getInverseIndex(index); val.setIndex(reverseIndex); } else { val.setIndex(index); } } // API for debbugging only public void setValue(ProblemElement a, ProblemElement b, String valueLabel) { String[] labels = AHPHelper.getLabels(); int index = StringHelper.IndexOf(labels, valueLabel); set(a, b, index); } /** * Gets the index. * * @param a the first PE * @param b the second PE * @return the index of the value in the domain */ public int getIndex(ProblemElement a, ProblemElement b) { if (a.equals(b)) return (int) AHPHelper.getMidPoint(); LinkedHashMap map = content.get(a); AHPValuation val = map.get(b); int index; if (val == null) { map = content.get(b); val = map.get(a); index = AHPHelper.getInverseIndex(val.getIndex()); } else { index = val.getIndex(); } return index; } /** * Gets the label. * * @param a the first PE * @param b the second PE * @return the human readable label */ public String getLabel(ProblemElement a, ProblemElement b) { int index = this.getIndex(a, b); return AHPHelper.getLabels()[index]; } /** * Gets the value. * * @param a the first PE * @param b the second PE * @return the value */ public Double getValue(ProblemElement a, ProblemElement b) { int index = this.getIndex(a, b); Double val = DoubleHelper.ParseFraction(AHPHelper.getLabels()[index]); return val; } /** * Gets the values. * * @return the values */ public double[][] getValues() { int size = content.size(); double[][] result = new double[size][size]; ProblemElement[] arr = content.keySet().stream().toArray(ProblemElement[]::new); for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) result[i][j] = this.getValue(arr[i], arr[j]); return result; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { boolean toStringIndex = true; String[] indexValueMapping = AHPHelper.getLabels(); StringBuilder sb = new StringBuilder(); String[] headers = content.keySet().stream().map(k -> k.getName()).toArray(String[]::new); String[][] m = new String[headers.length][headers.length]; ProblemElement[] pes = content.keySet().toArray(new ProblemElement[0]); int i = 0; for (ProblemElement pe1 : pes) { int j = 0; for (ProblemElement pe2 : pes) { m[i][j] = toStringIndex ? indexValueMapping[getIndex(pe1, pe2)] : this.getIndex(pe1, pe2) + ""; j++; } i++; } // for(Entry> entry1 : content.entrySet()) { // int j = 0; // for( Entry entry2 : entry1.getValue().entrySet()) { // m[i][j] = entry2.getValue().getIndex()+""; // j++; // } // i++; // } String[][] m2 = MatrixHelper.addHeaders(m, headers, headers); sb.append(StringHelper.PrintTable(m2)); sb.append("\n"); sb.append("RAW: \n"); sb.append(content.toString()); sb.append("\n"); return sb.toString(); } /** * Gets the cols. * * @return the cols */ public ProblemElement[] getCols() { return this.content.keySet().toArray(new ProblemElement[0]); } /** * Gets the rows. * * @return the rows */ public ProblemElement[] getRows() { return this.content.keySet().toArray(new ProblemElement[0]); } /** * Write. * * @param writer the writer * @throws XMLStreamException the XML stream exception */ public void write(XMLStreamWriter writer) throws XMLStreamException { ProblemElement[] arr = content.keySet().toArray(new ProblemElement[0]); for (int i = 0; i < arr.length; i++) { ProblemElement pe1 = arr[i]; for (int j = i; j < arr.length; j++) { ProblemElement pe2 = arr[j]; if (!pe1.equals(pe2)) { writer.writeStartElement("AHPValuation"); content.get(pe1).get(pe2).write(writer); writer.writeEndElement(); } } } } }