View Javadoc
1   /*
2    * Copyright (C) 2021 INRAE AgroClim
3    *
4    * This file is part of Indicators.
5    *
6    * Indicators is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Indicators is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Indicators. If not, see <http://www.gnu.org/licenses/>.
18   */
19  package fr.inrae.agroclim.indicators.model.indicator;
20  
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Optional;
27  import java.util.Set;
28  
29  import fr.inrae.agroclim.indicators.exception.IndicatorsException;
30  import fr.inrae.agroclim.indicators.model.Knowledge;
31  import fr.inrae.agroclim.indicators.model.Parameter;
32  import fr.inrae.agroclim.indicators.model.data.DailyData;
33  import fr.inrae.agroclim.indicators.model.data.Resource;
34  import fr.inrae.agroclim.indicators.model.data.Variable;
35  import fr.inrae.agroclim.indicators.util.Doublet;
36  import jakarta.xml.bind.annotation.XmlAccessType;
37  import jakarta.xml.bind.annotation.XmlAccessorType;
38  import jakarta.xml.bind.annotation.XmlRootElement;
39  import jakarta.xml.bind.annotation.XmlType;
40  import lombok.Getter;
41  import lombok.Setter;
42  import lombok.extern.log4j.Log4j2;
43  
44  /**
45   * To inject a value into an indicator.
46   *
47   * Last changed : $Date$
48   *
49   * @author $Author$
50   * @version $Revision$
51   */
52  @XmlRootElement
53  @XmlAccessorType(XmlAccessType.FIELD)
54  @XmlType(propOrder = {"parameterId", "parameterValue"})
55  @Log4j2
56  public final class InjectedParameter extends SimpleIndicator {
57  
58      /**
59       * UUID for Serializable.
60       */
61      private static final long serialVersionUID = 6030595237342422008L;
62  
63      /**
64       * Id of variety parameter or cell parameter to use.
65       */
66      @Setter
67      @Getter
68      private String parameterId;
69  
70      /**
71       * Value of parameter for the variety or the cell.
72       */
73      @Setter
74      @Getter
75      private Double parameterValue;
76  
77      @Override
78      public double computeSingleValue(final Resource<? extends DailyData> data) throws IndicatorsException {
79          if (parameterValue == null) {
80              return 0;
81          }
82          return parameterValue;
83      }
84  
85      @Override
86      public List<Doublet<Parameter, Number>> getParameterDefaults() {
87          if (getParameters() == null) {
88              return List.of();
89          }
90          final Optional<Parameter> found = getParameters().stream() //
91                  .filter(a -> parameterId.equals(a.getAttribute())) //
92                  .findFirst();
93          if (found.isEmpty()) {
94              return List.of();
95          }
96          return List.of(Doublet.of(found.get(), parameterValue));
97      }
98  
99      @Override
100     public List<Parameter> getParameters() {
101         final Parameter param = new Parameter();
102         param.setAttribute(parameterId);
103         param.setId(parameterId);
104         return Arrays.asList(param);
105     }
106 
107     @Override
108     public Map<String, Double> getParametersValues() {
109         final Map<String, Double> params = new HashMap<>();
110         params.put(parameterId, parameterValue);
111         return params;
112     }
113 
114     @Override
115     public Set<Variable> getVariables() {
116         return new HashSet<>();
117     }
118 
119     @Override
120     public boolean isComputable(final Resource<? extends DailyData> res) {
121         // always good.
122         return true;
123     }
124 
125     @Override
126     public void setParametersFromKnowledge(final Knowledge knowledge) {
127         final InjectedParameter ind = (InjectedParameter) knowledge.getIndicator(getId());
128         setParameterId(ind.getParameterId());
129         parameterValue = ind.getValue();
130     }
131 
132     @Override
133     public void setParametersValues(final Map<String, Double> values) {
134         if (values != null && values.containsKey(parameterId)) {
135             parameterValue = values.get(parameterId);
136         }
137     }
138 
139     @Override
140     public String toStringTree(final String indent) {
141         final StringBuilder sb = new StringBuilder();
142         sb.append(toStringTreeBase(indent));
143         sb.append(indent).append("  parameterId: ").append(parameterId).append("\n");
144         sb.append(indent).append("  parameterValue: ").append(parameterValue).append("\n");
145         return sb.toString();
146     }
147 
148     @Override
149     public void removeParameter(final Parameter param) {
150         if (this.parameterId.equals(param.getId())) {
151             parameterId = null;
152             parameterValue = null;
153         }
154     }
155 }