1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import java.text.NumberFormat;
|
---|
4 |
|
---|
5 | import org.expeditee.gui.AttributeUtils;
|
---|
6 | import org.expeditee.gui.AttributeValuePair;
|
---|
7 | import org.expeditee.gui.Frame;
|
---|
8 | import org.expeditee.gui.FrameIO;
|
---|
9 | import org.expeditee.items.Item;
|
---|
10 | import org.expeditee.items.Text;
|
---|
11 |
|
---|
12 | public class ComputeTree extends DefaultAgent {
|
---|
13 |
|
---|
14 | private static final String COMPUTE_TAG = "@compute:";
|
---|
15 |
|
---|
16 | private static final NumberFormat _format = NumberFormat.getInstance();
|
---|
17 |
|
---|
18 | private static final String DECIMAL_PLACES_TAG = "@dp:";
|
---|
19 |
|
---|
20 | @Override
|
---|
21 | protected Frame process(Frame frame) {
|
---|
22 | _format.setMinimumFractionDigits(0);
|
---|
23 | if (computeFrame(frame) == null && !_stop) {
|
---|
24 | message("Nothing to compute!");
|
---|
25 | }
|
---|
26 |
|
---|
27 | return null;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Computes the value of a frame.
|
---|
32 | *
|
---|
33 | * @param frame
|
---|
34 | * @return the value of the frame or null if the frame does not have the
|
---|
35 | * compute tag or any values to compute;
|
---|
36 | */
|
---|
37 | private Double computeFrame(Frame frame) {
|
---|
38 | // Search for @Compute frame tag
|
---|
39 | String computeTag = null;
|
---|
40 | for (Item i : frame.getItems()) {
|
---|
41 | if (_stop)
|
---|
42 | return null;
|
---|
43 | if (i.isAnnotation()) {
|
---|
44 | String s = ((Text) i).getFirstLine().toLowerCase().trim();
|
---|
45 | if (s.startsWith(COMPUTE_TAG)) {
|
---|
46 | computeTag = s.substring(COMPUTE_TAG.length()).trim();
|
---|
47 | // break;
|
---|
48 | } else if (s.startsWith(DECIMAL_PLACES_TAG)) {
|
---|
49 | try {
|
---|
50 | String[] values = s.substring(
|
---|
51 | DECIMAL_PLACES_TAG.length()).trim().split(
|
---|
52 | "\\s+");
|
---|
53 | int min = 0;
|
---|
54 | int max = 0;
|
---|
55 | if (values.length == 1) {
|
---|
56 | min = Integer.parseInt(values[0]);
|
---|
57 | max = min;
|
---|
58 | } else if (values.length == 2) {
|
---|
59 | min = Integer.parseInt(values[0]);
|
---|
60 | max = Integer.parseInt(values[1]);
|
---|
61 | }
|
---|
62 | _format.setMinimumFractionDigits(min);
|
---|
63 | _format.setMaximumFractionDigits(max);
|
---|
64 | } catch (Exception e) {
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Check that the compute tag exists
|
---|
71 | if (computeTag == null)
|
---|
72 | computeTag = "+";
|
---|
73 |
|
---|
74 | // check for text versions of the operators
|
---|
75 | if (computeTag.length() > 1) {
|
---|
76 | if (computeTag.equalsIgnoreCase("add"))
|
---|
77 | computeTag = "+";
|
---|
78 | else if (computeTag.equalsIgnoreCase("subtract"))
|
---|
79 | computeTag = "-";
|
---|
80 | else if (computeTag.equalsIgnoreCase("multiply"))
|
---|
81 | computeTag = "*";
|
---|
82 | else if (computeTag.equalsIgnoreCase("divide"))
|
---|
83 | computeTag = "/";
|
---|
84 | }
|
---|
85 | char operator = computeTag.charAt(computeTag.length() - 1);
|
---|
86 | // double defaultValue = (operator == '*' ? 1.0 : 0.0);
|
---|
87 | Double result = null;
|
---|
88 | // Iterate through all valid items on the frame performing the correct
|
---|
89 | // operation
|
---|
90 | for (Item i : frame.getBodyTextItems(false)) {
|
---|
91 | if (_stop)
|
---|
92 | return null;
|
---|
93 | Double value = null;
|
---|
94 | // Process the frame that each item is linked to
|
---|
95 | if (i.getLink() != null) {
|
---|
96 | value = computeFrame(FrameIO.LoadFrame(i.getAbsoluteLink()));
|
---|
97 | // Set the items text
|
---|
98 | if (value != null)
|
---|
99 | AttributeUtils
|
---|
100 | .replaceValue((Text) i, _format.format(value));
|
---|
101 | }
|
---|
102 |
|
---|
103 | if (value == null) {
|
---|
104 | try {
|
---|
105 | value = new AttributeValuePair(i.getText())
|
---|
106 | .getDoubleValue();
|
---|
107 | } catch (NumberFormatException e) {
|
---|
108 | continue;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (value != null) {
|
---|
113 | _itemCount++;
|
---|
114 |
|
---|
115 | // the first time we find an item init result
|
---|
116 | if (result == null) {
|
---|
117 | result = value;
|
---|
118 | } else {
|
---|
119 | switch (operator) {
|
---|
120 | case '*':
|
---|
121 | result *= value;
|
---|
122 | break;
|
---|
123 | case '+':
|
---|
124 | result += value;
|
---|
125 | break;
|
---|
126 | case '-':
|
---|
127 | result -= value;
|
---|
128 | break;
|
---|
129 | case '/':
|
---|
130 | result /= value;
|
---|
131 | break;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (result != null) {
|
---|
138 | AttributeUtils.replaceValue(frame.getTitleItem(), _format
|
---|
139 | .format(result));
|
---|
140 | }
|
---|
141 | _frameCount++;
|
---|
142 | FrameIO.ForceSaveFrame(frame);
|
---|
143 |
|
---|
144 | return result;
|
---|
145 | }
|
---|
146 | }
|
---|