1 | package org.expeditee.agents;
|
---|
2 |
|
---|
3 | import org.expeditee.gui.AttributeUtils;
|
---|
4 | import org.expeditee.gui.Frame;
|
---|
5 | import org.expeditee.gui.FrameGraphics;
|
---|
6 | import org.expeditee.gui.FrameIO;
|
---|
7 | import org.expeditee.items.Item;
|
---|
8 | import org.expeditee.items.Text;
|
---|
9 |
|
---|
10 | public class ComputeTree extends DefaultAgent {
|
---|
11 |
|
---|
12 | private Long startTime = 0L;
|
---|
13 |
|
---|
14 | private int frameCount = 0;
|
---|
15 |
|
---|
16 | private int itemCount = 0;
|
---|
17 |
|
---|
18 | @Override
|
---|
19 | protected void finalise(Frame frame) {
|
---|
20 | Long total = System.currentTimeMillis() - startTime;
|
---|
21 | FrameGraphics.DisplayMessage("Time: " + total + "ms, frames: "
|
---|
22 | + frameCount + ", items: " + itemCount);
|
---|
23 | }
|
---|
24 |
|
---|
25 | @Override
|
---|
26 | protected Frame process(Frame frame) {
|
---|
27 | frameCount = 0;
|
---|
28 | itemCount = 0;
|
---|
29 |
|
---|
30 | startTime = System.currentTimeMillis();
|
---|
31 |
|
---|
32 | if (computeFrame(frame) == null) {
|
---|
33 | message("Nothing to compute!");
|
---|
34 | }
|
---|
35 |
|
---|
36 | return null;
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Computes the value of a frame.
|
---|
41 | *
|
---|
42 | * @param frame
|
---|
43 | * @return the value of the frame or null if the frame does not have the
|
---|
44 | * compute tag or any values to compute;
|
---|
45 | */
|
---|
46 | private Double computeFrame(Frame frame) {
|
---|
47 | // Search for @Compute frame tag
|
---|
48 | String computeTag = null;
|
---|
49 | for (Item i : frame.getItems()) {
|
---|
50 | if (i.isAnnotation()) {
|
---|
51 | String s = ((Text) i).getFirstLine().toLowerCase().trim();
|
---|
52 | if (s.startsWith("@compute:")) {
|
---|
53 | computeTag = s;
|
---|
54 | break;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | // Check that the compute tag exists
|
---|
60 | if (computeTag == null)
|
---|
61 | return null;
|
---|
62 |
|
---|
63 | // Iterate through all valid items on the frame performing the correct
|
---|
64 | // operation
|
---|
65 | char operator = computeTag.charAt(computeTag.length() - 1);
|
---|
66 | // double defaultValue = (operator == '*' ? 1.0 : 0.0);
|
---|
67 | Double result = null;
|
---|
68 |
|
---|
69 | for (Item i : frame.getBodyTextItems()) {
|
---|
70 |
|
---|
71 | Double value = null;
|
---|
72 | // Process the frame that each item is linked to
|
---|
73 | if (i.getLink() != null) {
|
---|
74 | value = computeFrame(FrameIO.LoadFrame(i.getAbsoluteLink()));
|
---|
75 | // Set the items text
|
---|
76 | if (value != null)
|
---|
77 | AttributeUtils.setSingleValue((Text) i, value.toString());
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (value == null) {
|
---|
81 | value = AttributeUtils.getDoubleValue(((Text) i)
|
---|
82 | .getTextNoList());
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (value != null) {
|
---|
86 | itemCount++;
|
---|
87 |
|
---|
88 | // the first time we find an item init result
|
---|
89 | if (result == null)
|
---|
90 | result = (operator == '*' ? 1.0 : 0.0);
|
---|
91 |
|
---|
92 | result = ((operator == '*') ? (result * value)
|
---|
93 | : (result + value));
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (result != null)
|
---|
98 | AttributeUtils.setSingleValue(frame.getTitle(), result.toString());
|
---|
99 | frameCount++;
|
---|
100 |
|
---|
101 | return result;
|
---|
102 | }
|
---|
103 | }
|
---|