1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import java.text.NumberFormat;
|
---|
4 |
|
---|
5 | import org.expeditee.gui.AttributeUtils;
|
---|
6 | import org.expeditee.gui.Frame;
|
---|
7 | import org.expeditee.gui.FrameGraphics;
|
---|
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("\\s+");
|
---|
52 | int min = 0;
|
---|
53 | int max = 0;
|
---|
54 | if(values.length == 1){
|
---|
55 | min = Integer.parseInt(values[0]);
|
---|
56 | max = min;
|
---|
57 | }else if(values.length == 2){
|
---|
58 | min = Integer.parseInt(values[0]);
|
---|
59 | max = Integer.parseInt(values[1]);
|
---|
60 | }
|
---|
61 | _format.setMinimumFractionDigits(min);
|
---|
62 | _format.setMaximumFractionDigits(max);
|
---|
63 | } catch (Exception e) {
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | // Check that the compute tag exists
|
---|
70 | if (computeTag == null)
|
---|
71 | computeTag = "+";
|
---|
72 |
|
---|
73 | // check for text versions of the operators
|
---|
74 | if (computeTag.length() > 1) {
|
---|
75 | if (computeTag.equalsIgnoreCase("add"))
|
---|
76 | computeTag = "+";
|
---|
77 | else if (computeTag.equalsIgnoreCase("subtract"))
|
---|
78 | computeTag = "-";
|
---|
79 | else if (computeTag.equalsIgnoreCase("multiply"))
|
---|
80 | computeTag = "*";
|
---|
81 | else if (computeTag.equalsIgnoreCase("divide"))
|
---|
82 | computeTag = "/";
|
---|
83 | }
|
---|
84 | char operator = computeTag.charAt(computeTag.length() - 1);
|
---|
85 | // double defaultValue = (operator == '*' ? 1.0 : 0.0);
|
---|
86 | Double result = null;
|
---|
87 | // Iterate through all valid items on the frame performing the correct
|
---|
88 | // operation
|
---|
89 | for (Item i : frame.getBodyTextItems(false)) {
|
---|
90 | if(_stop)
|
---|
91 | return null;
|
---|
92 | Double value = null;
|
---|
93 | // Process the frame that each item is linked to
|
---|
94 | if (i.getLink() != null) {
|
---|
95 | value = computeFrame(FrameIO.LoadFrame(i.getAbsoluteLink()));
|
---|
96 | // Set the items text
|
---|
97 | if (value != null)
|
---|
98 | AttributeUtils.replaceValue((Text) i, _format
|
---|
99 | .format(value));
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (value == null) {
|
---|
103 | value = AttributeUtils.getDoubleValue(((Text) i).getFirstLine());
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (value != null) {
|
---|
107 | _itemCount++;
|
---|
108 |
|
---|
109 | // the first time we find an item init result
|
---|
110 | if (result == null) {
|
---|
111 | result = value;
|
---|
112 | } else {
|
---|
113 | switch (operator) {
|
---|
114 | case '*':
|
---|
115 | result *= value;
|
---|
116 | break;
|
---|
117 | case '+':
|
---|
118 | result += value;
|
---|
119 | break;
|
---|
120 | case '-':
|
---|
121 | result -= value;
|
---|
122 | break;
|
---|
123 | case '/':
|
---|
124 | result /= value;
|
---|
125 | break;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | if (result != null) {
|
---|
132 | AttributeUtils.replaceValue(frame.getTitleItem(), _format
|
---|
133 | .format(result));
|
---|
134 | }
|
---|
135 | _frameCount++;
|
---|
136 | FrameIO.ForceSaveFrame(frame);
|
---|
137 |
|
---|
138 | return result;
|
---|
139 | }
|
---|
140 | }
|
---|