public code v1

This commit is contained in:
Francisco Jesús Martínez Mimbrera
2026-05-23 00:32:57 +02:00
commit 759a8968a2
4357 changed files with 163763 additions and 0 deletions
@@ -0,0 +1,309 @@
package flintstones.entity.ahppreferences;
import java.util.LinkedList;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import flintstones.entity.ahp.AHPMatrix;
import flintstones.entity.extensionenum.ExtensionEnum;
import flintstones.entity.problemelement.entities.FakeProblemElement;
import flintstones.entity.problemelement.entities.ProblemElement;
import flintstones.model.problemelement.service.IProblemElementService;
/**
* The Class PreferenceBuild.
* Almacena una instancia completa de una comparación.
* Para el Experto e1 con el Criterio c1, tenemos 2 listas de elementos (leftArr y rightArr) y sus comparaciones en "matrix".
*/
public class PreferenceBuild {
/** The problem service. */
@Inject
IProblemElementService problemService;
/** The main. */
ProblemElement main;
/** The other. */
ProblemElement other;
/** The main type. */
String mainType;
/** The other type. */
String otherType;
/** The left type. */
String leftType;
/** The right type. */
String rightType;
/** The matrix. */
// Preferences
PreferenceMatrix matrix;
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** The Preference build. */
PreferenceBuild,
/** The main problem element. */
mainProblemElement,
/** The other problem element. */
otherProblemElement,
/** The left elements. */
leftElements,
/** The right elements. */
rightElements,
/** The type. */
type,
/** The right problem element. */
rightProblemElement,
/** The left problem element. */
leftProblemElement,
/** The Preference. */
Preference
};
/**
* Instantiates a new preference build.
*
* @param domain the domain
* @param main the main
* @param other the other
* @param leftArr the left arr
* @param rightArr the right arr
*/
public PreferenceBuild(ProblemElement main, ProblemElement other, ProblemElement[] leftArr,
ProblemElement[] rightArr) {
this.main = main;
this.other = other;
mainType = main != null ? main.getType() : FakeProblemElement.Type;
otherType = other != null ? other.getType() : FakeProblemElement.Type;
leftType = leftArr[0].getType();
rightType = rightArr[0].getType();
matrix = new PreferenceMatrix(leftArr, rightArr);
}
/**
* Post construct.
*/
@PostConstruct
private void postConstruct() {
if (main == null)
main = getFakeElementArr();
if (other == null)
other = getFakeElementArr();
}
/**
* Gets the main type.
*
* @return the main type
*/
public String getMainType() {
return mainType;
}
/**
* Gets the other type.
*
* @return the other type
*/
public String getOtherType() {
return otherType;
}
/**
* Gets the left type.
*
* @return the left type
*/
public String getLeftType() {
return leftType;
}
/**
* Gets the right type.
*
* @return the right type
*/
public String getRightType() {
return rightType;
}
/**
* Gets the main.
*
* @return the main
*/
public ProblemElement getMain() {
return main;
}
/**
* Gets the other.
*
* @return the other
*/
public ProblemElement getOther() {
return other;
}
/**
* Gets the left elements.
*
* @return the left elements
*/
public LinkedList<ProblemElement> getLeftElements() {
return matrix.getRowHeaders();
}
/**
* Gets the right elements.
*
* @return the right elements
*/
public LinkedList<ProblemElement> getRightElements() {
return matrix.getColumnHeaders();
}
/**
* Gets the fake element arr.
*
* @return the fake element arr
*/
private ProblemElement getFakeElementArr() {
return problemService.getGeneric();
}
/**
* Checks if is main generic.
*
* @return true, if is main generic
*/
public boolean isMainGeneric() {
return main.getType().equals(FakeProblemElement.Type);
}
/**
* Checks if is other generic.
*
* @return true, if is other generic
*/
public boolean isOtherGeneric() {
return other.getType().equals(FakeProblemElement.Type);
}
/**
* Gets the matrix.
*
* @return the matrix
*/
public PreferenceMatrix getMatrix() {
return this.matrix;
}
/**
* Gets the key.
*
* @return the key
*/
public PreferenceCollectionKey getKey() {
return new PreferenceCollectionKey(mainType, otherType, leftType, rightType);
}
/**
* Gets the AHP matrix.
*
* @return the AHP matrix
*/
// Matrix operations
public AHPMatrix getAHPMatrix() {
if (!isAHP())
throw new RuntimeException(
"AHPMatrix can not be generated if you are not comparing element with the same type ");
return matrix.getAHPMatrix(/*main, other*/);
}
/**
* Sets the.
*
* @param left the left
* @param right the right
* @param index the index
*/
public void set(ProblemElement left, ProblemElement right, int index) {
matrix.set(left, right, index);
}
/**
* Sets the diagonal.
*
* @param left the left
* @param right the right
* @param index the index
*/
public void setDiagonal(ProblemElement left, ProblemElement right, int index) {
matrix.setDiagonal(left, right, index);
}
/**
* Gets the.
*
* @return the int[][]
*/
public int[][] get() {
return matrix.getMatrix();
}
/**
* Gets the.
*
* @param left the left
* @param right the right
* @return the int
*/
public int get(ProblemElement left, ProblemElement right) {
return matrix.get(left, right);
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(matrix.toString());
return sb.toString();
}
/**
* Checks if is ahp.
*
* @return true, if is ahp
*/
public boolean isAHP() {
return leftType.equals(rightType);
}
}
@@ -0,0 +1,133 @@
package flintstones.entity.ahppreferences;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import flintstones.entity.problemelement.entities.FakeProblemElement;
/**
* The Class PreferenceCollectionKey.
*/
public class PreferenceCollectionKey {
/** The main type. */
private String mainType;
/** The other type. */
private String otherType;
/** The left type. */
private String leftType;
/** The right type. */
private String rightType;
/**
* Instantiates a new preference collection key.
*
* @param mainType the main type
* @param otherType the other type
* @param leftType the left type
* @param rightType the right type
*/
public PreferenceCollectionKey(String mainType, String otherType, String leftType, String rightType) {
this.mainType = mainType != null ? mainType : FakeProblemElement.Type;
this.otherType = otherType != null ? otherType : FakeProblemElement.Type;
this.leftType = leftType;
this.rightType = rightType;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
HashCodeBuilder builder = new HashCodeBuilder();
builder.append(mainType);
builder.append(otherType);
builder.append(leftType);
builder.append(rightType);
return builder.toHashCode();
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
PreferenceCollectionKey pc = (PreferenceCollectionKey)obj;
EqualsBuilder builder = new EqualsBuilder();
builder.append(mainType, pc.mainType);
builder.append(otherType, pc.otherType);
builder.append(leftType, pc.leftType);
builder.append(rightType, pc.rightType);
return builder.isEquals();
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append(mainType);
builder.append(", ");
builder.append(otherType);
builder.append(", ");
builder.append(leftType);
builder.append(", ");
builder.append(rightType);
builder.append("\n ");
return builder.toString();
}
/**
* Gets the main type.
*
* @return the main type
*/
public String getMainType() {
return mainType;
}
/**
* Gets the other type.
*
* @return the other type
*/
public String getOtherType() {
return otherType;
}
/**
* Gets the left type.
*
* @return the left type
*/
public String getLeftType() {
return leftType;
}
/**
* Gets the right type.
*
* @return the right type
*/
public String getRightType() {
return rightType;
}
}
@@ -0,0 +1,157 @@
package flintstones.entity.ahppreferences;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map.Entry;
import flintstones.entity.ahp.AHPMatrix;
import flintstones.entity.extensionenum.ExtensionEnum;
import flintstones.entity.problemelement.entities.ProblemElement;
import flintstones.helper.ahp.AHPHelper;
import flintstones.helper.data.LinkedHashMatrix;
/**
* The Class PreferenceMatrix store the preferences of two lists of elements and
* provide tools to convert it to an AHP Matrix or to múltiple AHP Matrix.
*
* It also respect the properties of an AHP matrix if it used with the right
* API.
*
* Almacena preferencias.
*/
public class PreferenceMatrix {
/** The matrix. */
LinkedHashMatrix<ProblemElement, ProblemElement, Integer> matrix = new LinkedHashMatrix<>();
/**
* The Enum Fields.
*/
public static enum Fields implements ExtensionEnum {
/** The Preference matrix. */
PreferenceMatrix,
/** The preference index. */
preferenceIndex
}
/**
* Instantiates a new preference matrix.
*
* @param ahpDomain the ahp domain
* @param leftItems the left items
* @param rightItems the right items
*/
public PreferenceMatrix(ProblemElement[] leftItems, ProblemElement[] rightItems) {
for (ProblemElement left : leftItems)
for (ProblemElement right : rightItems)
matrix.put(left, right, (int) AHPHelper.getMidPoint() );
}
/**
* Gets the matrix.
*
* @return the matrix
*/
public int[][] getMatrix() {
int[][] basicMatrix = new int[matrix.size()][matrix.innerSize()];
int i = 0;
for (Entry<ProblemElement, LinkedHashMap<ProblemElement, Integer>> entry1 : matrix.entrySet()) {
int j = 0;
for (Entry<ProblemElement, Integer> entry2 : entry1.getValue().entrySet()) {
basicMatrix[i][j] = get(entry1.getKey(), entry2.getKey());
j++;
}
i++;
}
return basicMatrix;
}
/**
* Sets the.
*
* @param left the left
* @param right the right
* @param index the index
*/
public void set(ProblemElement left, ProblemElement right, int index) {
matrix.put(left, right, index);
}
/**
* Sets the diagonal.
*
* @param left the left
* @param right the right
* @param index the index
*/
public void setDiagonal(ProblemElement left, ProblemElement right, int index) {
if (left.equals(right) || matrix.size() != matrix.innerSize())
throw new RuntimeException("Use set(...) instead of setDiagonal(...)");
matrix.put(left, right, index);
matrix.put(right, left, AHPHelper.getInverseIndex(index));
}
/**
* Gets the.
*
* @param left the left
* @param right the right
* @return the int
*/
public Integer get(ProblemElement left, ProblemElement right) {
return matrix.get(left, right);
}
/**
* Gets the AHP matrix.
*
* @param main the main
* @param other the other
* @return the AHP matrix
*/
public AHPMatrix getAHPMatrix() {
LinkedList<ProblemElement> leftItems = getRowHeaders();
AHPMatrix ahpmatrix = new AHPMatrix(leftItems.toArray(new ProblemElement[0]));
for (ProblemElement pe1 : leftItems) {
for (ProblemElement pe2 : leftItems) {
int index = get(pe1, pe2);
ahpmatrix.set(pe1, pe2, index);
}
}
return ahpmatrix;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return matrix.toString();
}
public LinkedList<ProblemElement> getRowHeaders() {
return new LinkedList<>(matrix.keySet());
}
public LinkedList<ProblemElement> getColumnHeaders() {
return new LinkedList<>(matrix.iterator().next().getValue().keySet());
}
}