root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / nonSpatial / calculator / CalculatorAlgorithm.java @ 59
History | View | Annotate | Download (2.89 KB)
1 | 59 | nbrodin | |
---|---|---|---|
2 | |||
3 | package es.unex.sextante.nonSpatial.calculator; |
||
4 | |||
5 | import java.util.HashMap; |
||
6 | import java.util.Iterator; |
||
7 | import java.util.Set; |
||
8 | |||
9 | import org.nfunk.jep.JEP; |
||
10 | |||
11 | import es.unex.sextante.core.GeoAlgorithm; |
||
12 | import es.unex.sextante.core.Sextante; |
||
13 | import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
||
14 | import es.unex.sextante.exceptions.RepeatedParameterNameException; |
||
15 | import es.unex.sextante.gui.modeler.ModelAlgorithm; |
||
16 | import es.unex.sextante.modeler.elements.ModelElementNumericalValue; |
||
17 | |||
18 | |||
19 | public class CalculatorAlgorithm |
||
20 | extends
|
||
21 | GeoAlgorithm { |
||
22 | |||
23 | public static final String FORMULA = "FORMULA"; |
||
24 | public static final String RESULT = "RESULT"; |
||
25 | |||
26 | |||
27 | @Override
|
||
28 | public void defineCharacteristics() { |
||
29 | |||
30 | setName(Sextante.getText("Calculator"));
|
||
31 | setGroup(Sextante.getText("nonSpatial"));
|
||
32 | setUserCanDefineAnalysisExtent(false);
|
||
33 | |||
34 | try {
|
||
35 | m_Parameters.addString(FORMULA, Sextante.getText("Formula"));
|
||
36 | addOutputNumericalValue(RESULT, Sextante.getText("Result"));
|
||
37 | } |
||
38 | catch (final RepeatedParameterNameException e) { |
||
39 | } |
||
40 | |||
41 | } |
||
42 | |||
43 | |||
44 | @Override
|
||
45 | public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
||
46 | |||
47 | final String sFormula = m_Parameters.getParameterValueAsString(FORMULA); |
||
48 | |||
49 | final JEP jep = new JEP(); |
||
50 | |||
51 | jep.addStandardConstants(); |
||
52 | jep.addStandardFunctions(); |
||
53 | |||
54 | jep.parseExpression(sFormula); |
||
55 | |||
56 | if (jep.hasError()) {
|
||
57 | Sextante.addErrorToLog(jep.getErrorInfo()); |
||
58 | throw new GeoAlgorithmExecutionException(Sextante.getText("Syntax_error")); |
||
59 | } |
||
60 | |||
61 | addOutputNumericalValue(RESULT, jep.getValue()); |
||
62 | |||
63 | return true; |
||
64 | |||
65 | } |
||
66 | |||
67 | |||
68 | @Override
|
||
69 | public boolean preprocessForModeller(final Object obj) throws GeoAlgorithmExecutionException { |
||
70 | |||
71 | final ModelAlgorithm model = (ModelAlgorithm) obj;
|
||
72 | |||
73 | try {
|
||
74 | String sFormula = m_Parameters.getParameterValueAsString(FORMULA);
|
||
75 | final HashMap inputs = model.getInputs(); |
||
76 | final Set set = inputs.keySet(); |
||
77 | final Iterator iter = set.iterator(); |
||
78 | while (iter.hasNext()) {
|
||
79 | final Object key = iter.next(); |
||
80 | final Object input = inputs.get(key); |
||
81 | if (input instanceof Double) { |
||
82 | if (sFormula.contains((String) key)) { |
||
83 | sFormula = sFormula.replace(key.toString(), input.toString()); |
||
84 | } |
||
85 | } |
||
86 | if (input instanceof ModelElementNumericalValue) { |
||
87 | if (sFormula.contains(((String) key).toLowerCase())) { |
||
88 | return false; |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | m_Parameters.getParameter(FORMULA).setParameterValue(sFormula); |
||
93 | } |
||
94 | catch (final Exception e) { |
||
95 | throw new GeoAlgorithmExecutionException(Sextante.getText("Syntax_error")); |
||
96 | } |
||
97 | |||
98 | return true; |
||
99 | |||
100 | } |
||
101 | |||
102 | |||
103 | } |