1 | /**
|
---|
2 | * ComputeTree.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.agents;
|
---|
20 |
|
---|
21 | import java.text.NumberFormat;
|
---|
22 |
|
---|
23 | import org.expeditee.gui.AttributeUtils;
|
---|
24 | import org.expeditee.gui.AttributeValuePair;
|
---|
25 | import org.expeditee.gui.Frame;
|
---|
26 | import org.expeditee.gui.FrameIO;
|
---|
27 | import org.expeditee.items.Item;
|
---|
28 | import org.expeditee.items.Text;
|
---|
29 |
|
---|
30 | public class ComputeTree extends DefaultAgent {
|
---|
31 |
|
---|
32 | private static final String COMPUTE_TAG = "@compute:";
|
---|
33 |
|
---|
34 | private static final NumberFormat _format = NumberFormat.getInstance();
|
---|
35 |
|
---|
36 | private static final String DECIMAL_PLACES_TAG = "@dp:";
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | protected Frame process(Frame frame) {
|
---|
40 | _format.setMinimumFractionDigits(0);
|
---|
41 | if (computeFrame(frame) == null && !_stop) {
|
---|
42 | message("Nothing to compute!");
|
---|
43 | }
|
---|
44 |
|
---|
45 | return null;
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Computes the value of a frame.
|
---|
50 | *
|
---|
51 | * @param frame
|
---|
52 | * @return the value of the frame or null if the frame does not have the
|
---|
53 | * compute tag or any values to compute;
|
---|
54 | */
|
---|
55 | private Double computeFrame(Frame frame) {
|
---|
56 | // Search for @Compute frame tag
|
---|
57 | String computeTag = null;
|
---|
58 |
|
---|
59 | //TODO can speed this up by using frame.hasAnnotations
|
---|
60 | for (Item i : frame.getSortedItems()) {
|
---|
61 | if (_stop)
|
---|
62 | return null;
|
---|
63 | if (i.isAnnotation()) {
|
---|
64 | String s = ((Text) i).getFirstLine().toLowerCase().trim();
|
---|
65 | if (s.startsWith(COMPUTE_TAG)) {
|
---|
66 | computeTag = s.substring(COMPUTE_TAG.length()).trim();
|
---|
67 | // break;
|
---|
68 | } else if (s.startsWith(DECIMAL_PLACES_TAG)) {
|
---|
69 | try {
|
---|
70 | String[] values = s.substring(
|
---|
71 | DECIMAL_PLACES_TAG.length()).trim().split(
|
---|
72 | "\\s+");
|
---|
73 | int min = 0;
|
---|
74 | int max = 0;
|
---|
75 | if (values.length == 1) {
|
---|
76 | min = Integer.parseInt(values[0]);
|
---|
77 | max = min;
|
---|
78 | } else if (values.length == 2) {
|
---|
79 | min = Integer.parseInt(values[0]);
|
---|
80 | max = Integer.parseInt(values[1]);
|
---|
81 | }
|
---|
82 | _format.setMinimumFractionDigits(min);
|
---|
83 | _format.setMaximumFractionDigits(max);
|
---|
84 | } catch (Exception e) {
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | // Check that the compute tag exists
|
---|
91 | if (computeTag == null)
|
---|
92 | computeTag = "+";
|
---|
93 |
|
---|
94 | // check for text versions of the operators
|
---|
95 | if (computeTag.length() > 1) {
|
---|
96 | if (computeTag.equalsIgnoreCase("add"))
|
---|
97 | computeTag = "+";
|
---|
98 | else if (computeTag.equalsIgnoreCase("subtract"))
|
---|
99 | computeTag = "-";
|
---|
100 | else if (computeTag.equalsIgnoreCase("multiply"))
|
---|
101 | computeTag = "*";
|
---|
102 | else if (computeTag.equalsIgnoreCase("divide"))
|
---|
103 | computeTag = "/";
|
---|
104 | }
|
---|
105 | char operator = computeTag.charAt(computeTag.length() - 1);
|
---|
106 | // double defaultValue = (operator == '*' ? 1.0 : 0.0);
|
---|
107 | Double result = null;
|
---|
108 | // Iterate through all valid items on the frame performing the correct
|
---|
109 | // operation
|
---|
110 | for (Item i : frame.getBodyTextItems(false)) {
|
---|
111 | if (_stop)
|
---|
112 | return null;
|
---|
113 | Double value = null;
|
---|
114 | // Process the frame that each item is linked to
|
---|
115 | if (i.getLink() != null) {
|
---|
116 | value = computeFrame(FrameIO.LoadFrame(i.getAbsoluteLink()));
|
---|
117 | // Set the items text
|
---|
118 | if (value != null)
|
---|
119 | AttributeUtils
|
---|
120 | .replaceValue((Text) i, _format.format(value));
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (value == null) {
|
---|
124 | try {
|
---|
125 | value = new AttributeValuePair(i.getText())
|
---|
126 | .getDoubleValue();
|
---|
127 | } catch (NumberFormatException e) {
|
---|
128 | continue;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | if(value.equals(Double.NaN))
|
---|
133 | continue;
|
---|
134 |
|
---|
135 | if (value != null) {
|
---|
136 | _itemCount++;
|
---|
137 |
|
---|
138 | // the first time we find an item init result
|
---|
139 | if (result == null) {
|
---|
140 | result = value;
|
---|
141 | } else {
|
---|
142 | switch (operator) {
|
---|
143 | case '*':
|
---|
144 | result *= value;
|
---|
145 | break;
|
---|
146 | case '+':
|
---|
147 | result += value;
|
---|
148 | break;
|
---|
149 | case '-':
|
---|
150 | result -= value;
|
---|
151 | break;
|
---|
152 | case '/':
|
---|
153 | result /= value;
|
---|
154 | break;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (result != null) {
|
---|
161 | AttributeUtils.replaceValue(frame.getTitleItem(), _format
|
---|
162 | .format(result));
|
---|
163 | }
|
---|
164 | _frameCount++;
|
---|
165 | FrameIO.ForceSaveFrame(frame);
|
---|
166 |
|
---|
167 | return result;
|
---|
168 | }
|
---|
169 | }
|
---|