Files
Flintstones/bundles/flintstones.application.debug/src/flintstones/application/debug/handler/FillValuationsDebugHandler.java
T
Francisco Jesús Martínez Mimbrera 759a8968a2 public code v1
2026-05-23 00:32:57 +02:00

88 lines
2.8 KiB
Java

package flintstones.application.debug.handler;
import java.util.Arrays;
import javax.inject.Inject;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.core.services.events.IEventBroker;
import flintstones.application.constants.FrameworkConstants;
import flintstones.entity.problemelement.ProblemElementKey;
import flintstones.entity.problemelement.entities.Alternative;
import flintstones.entity.problemelement.entities.Criterion;
import flintstones.entity.problemelement.entities.Expert;
import flintstones.entity.problemelement.entities.ProblemElement;
import flintstones.entity.valuation.Valuation;
import flintstones.model.method.phase.IPhaseMethodService;
import flintstones.model.problemelement.service.IProblemElementService;
import flintstones.model.valuation.service.IValuationService;
@SuppressWarnings("javadoc")
public class FillValuationsDebugHandler {
@Inject
IPhaseMethodService phaseService;
@Inject
IEventBroker broker;
@Inject
IProblemElementService problemService;
@Inject
IValuationService valuationService;
Valuation[] valuations;
@Execute
public void execute() {
fill();
sendEvents();
}
private void fill() {
valuations = Arrays.stream(valuationService.getAll()).filter( k -> k.isEvaluated()).toArray(Valuation[]::new);
ProblemElement[] criterions = problemService.getSubElements(Criterion.Type);
ProblemElement[] experts = problemService.getSubElements(Expert.Type);
ProblemElement[] alternatives = problemService.getAll(Alternative.Type);
fillValuations(criterions, experts, alternatives);
}
private void fillValuations(ProblemElement[] cs, ProblemElement[] es, ProblemElement[] as) {
for(ProblemElement c : cs )
for(ProblemElement e : es )
for(ProblemElement a : as ) {
ProblemElementKey pek = new ProblemElementKey((Expert)e, (Alternative)a, (Criterion)c);
if(valuationService.getValuationFor( pek ) == null || !valuationService.getValuationFor(pek).isEvaluated() )
fillWithRandomValuation(c,e,a);
}
}
private void fillWithRandomValuation(ProblemElement c, ProblemElement e, ProblemElement a) {
if(valuations.length == 0)
throw new RuntimeException("Es necesario tener al menos 1 evaluación puesta. Se elige al azar entre las disponibles");
int rnd = (int) (Math.random() * valuations.length) + 1;
Valuation v = valuations[rnd-1];
ProblemElementKey pek = new ProblemElementKey((Expert)e, (Alternative)a, (Criterion)c);
valuationService.addOrUpdate(pek, v);
}
private void sendEvents() {
this.broker.post(FrameworkConstants.TOPIC_FRAMEWORK_PROBLEMELEMENT_CREATED, FrameworkConstants.empty() );
this.broker.post(FrameworkConstants.TOPIC_FRAMEWORK_DOMAIN_CREATED, null);
this.broker.post(FrameworkConstants.TOPIC_VALUATION_CREATED, null);
}
}