source: trunk/src/org/expeditee/agents/ComputeTree.java@ 636

Last change on this file since 636 was 390, checked in by ra33, 16 years ago

Added auto colour wheel... when drawing rectangles...
Fixed computeTree bug
Added TDFC for rectangles.

File size: 3.7 KB
Line 
1package org.expeditee.agents;
2
3import java.text.NumberFormat;
4
5import org.expeditee.gui.AttributeUtils;
6import org.expeditee.gui.AttributeValuePair;
7import org.expeditee.gui.Frame;
8import org.expeditee.gui.FrameIO;
9import org.expeditee.items.Item;
10import org.expeditee.items.Text;
11
12public 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
41 //TODO can speed this up by using frame.hasAnnotations
42 for (Item i : frame.getItems()) {
43 if (_stop)
44 return null;
45 if (i.isAnnotation()) {
46 String s = ((Text) i).getFirstLine().toLowerCase().trim();
47 if (s.startsWith(COMPUTE_TAG)) {
48 computeTag = s.substring(COMPUTE_TAG.length()).trim();
49 // break;
50 } else if (s.startsWith(DECIMAL_PLACES_TAG)) {
51 try {
52 String[] values = s.substring(
53 DECIMAL_PLACES_TAG.length()).trim().split(
54 "\\s+");
55 int min = 0;
56 int max = 0;
57 if (values.length == 1) {
58 min = Integer.parseInt(values[0]);
59 max = min;
60 } else if (values.length == 2) {
61 min = Integer.parseInt(values[0]);
62 max = Integer.parseInt(values[1]);
63 }
64 _format.setMinimumFractionDigits(min);
65 _format.setMaximumFractionDigits(max);
66 } catch (Exception e) {
67 }
68 }
69 }
70 }
71
72 // Check that the compute tag exists
73 if (computeTag == null)
74 computeTag = "+";
75
76 // check for text versions of the operators
77 if (computeTag.length() > 1) {
78 if (computeTag.equalsIgnoreCase("add"))
79 computeTag = "+";
80 else if (computeTag.equalsIgnoreCase("subtract"))
81 computeTag = "-";
82 else if (computeTag.equalsIgnoreCase("multiply"))
83 computeTag = "*";
84 else if (computeTag.equalsIgnoreCase("divide"))
85 computeTag = "/";
86 }
87 char operator = computeTag.charAt(computeTag.length() - 1);
88 // double defaultValue = (operator == '*' ? 1.0 : 0.0);
89 Double result = null;
90 // Iterate through all valid items on the frame performing the correct
91 // operation
92 for (Item i : frame.getBodyTextItems(false)) {
93 if (_stop)
94 return null;
95 Double value = null;
96 // Process the frame that each item is linked to
97 if (i.getLink() != null) {
98 value = computeFrame(FrameIO.LoadFrame(i.getAbsoluteLink()));
99 // Set the items text
100 if (value != null)
101 AttributeUtils
102 .replaceValue((Text) i, _format.format(value));
103 }
104
105 if (value == null) {
106 try {
107 value = new AttributeValuePair(i.getText())
108 .getDoubleValue();
109 } catch (NumberFormatException e) {
110 continue;
111 }
112 }
113
114 if(value.equals(Double.NaN))
115 continue;
116
117 if (value != null) {
118 _itemCount++;
119
120 // the first time we find an item init result
121 if (result == null) {
122 result = value;
123 } else {
124 switch (operator) {
125 case '*':
126 result *= value;
127 break;
128 case '+':
129 result += value;
130 break;
131 case '-':
132 result -= value;
133 break;
134 case '/':
135 result /= value;
136 break;
137 }
138 }
139 }
140 }
141
142 if (result != null) {
143 AttributeUtils.replaceValue(frame.getTitleItem(), _format
144 .format(result));
145 }
146 _frameCount++;
147 FrameIO.ForceSaveFrame(frame);
148
149 return result;
150 }
151}
Note: See TracBrowser for help on using the repository browser.