Becoming a D2 developer on the job

Step 4: Create the appropriate computation service

  • Create a new package, named org.decisiondeck.mytest.computation.
  • Create a new class in this package, and named it WeightedSum. It should extend AlternativeDependentComputation and implement IComputation.
  • Implement the weighted sum method in the compute function, and update the name and discriminant functions.
    public class WeightedSum extends AlternativeDependentComputation implements
                    IComputation {
    
            static public final String NAME = "WeightedSum";
            static public final String LOGICAL_NAME = "NewWeightedSum";
            
            public Object compute(User user) {
                    AbstractAlternative alt = getAlternative1();
    
                    List<AbstractEntity> entities = MyBeanService.getInstance().getAll(user);
                    
                    float weightedSum = 0;
                    float totalWeight = 0;
                    
                    // Browse the weights given by the relevant user
                    // each weight has a float value and is related to a criterion
                    for (AbstractEntity entity:entities)
                    {
                            if (entity instanceof MyBean)
                            {
                                    MyBean bean = (MyBean) entity;
                                    
                                    Float weight = bean.getWeight();
                                    ICriteria criterion = bean.getCriterion();
                                    
                                    float localSum = 0;
                                    float numEvaluator = 0;
    
                                    // Browse all the evaluators to have a mean value (among evaluators)
                                    // of the valuation of alt wrt criterion
                                    for (Iterator iter = EvaluatorService.getEvaluators().iterator(); iter.hasNext();)
                                    {
                                            Evaluator evaluator = (Evaluator) iter.next();
                                            Float valuation = evaluator.getEvaluations().getAnswer(alt, criterion);
                                            
                                            if (valuation != null)
                                            {
                                                    localSum += valuation;
                                                    numEvaluator++;
                                            }
                                    }
                                    
                                    if (numEvaluator!=0 && weight!=null)
                                    {
                                            float contribution = localSum/numEvaluator;
                                            weightedSum += weight.floatValue()*contribution;
                                            totalWeight += weight.floatValue();
                                    }
                            }
                    }
                    if (totalWeight !=0)
                    {
                            return weightedSum/totalWeight;
                    }
                    return 0;
            }
    
            public String getDiscriminant() {
                    return WeightedSum.NAME;
            }
    
            public String getName() {
                    return WeightedSum.NAME;
            }
    
            public String getSecondaryDiscriminant() {
                    return WeightedSum.LOGICAL_NAME;
            }
    
    }
    

    It is necessary to add the following imports

    import org.decisiondeck.model.alternative.AbstractAlternative;
    import org.decisiondeck.model.AbstractEntity;
    import java.util.List;
    import java.util.Iterator;
    import org.decisiondeck.mytest.model.MyBean;
    import org.decisiondeck.mytest.service.MyBeanService;
    import org.decisiondeck.model.evaluation.ICriteria;
    import org.decisiondeck.model.evaluation.Evaluator;
    import org.decisiondeck.service.EvaluatorService;