1 | package org.expeditee.math;
|
---|
2 |
|
---|
3 | import java.text.NumberFormat;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.Observable;
|
---|
7 | import java.util.Observer;
|
---|
8 |
|
---|
9 | import org.expeditee.gui.AttributeUtils;
|
---|
10 | import org.expeditee.gui.AttributeValuePair;
|
---|
11 | import org.expeditee.gui.Frame;
|
---|
12 | import org.expeditee.gui.FrameUtils;
|
---|
13 | import org.expeditee.items.Item;
|
---|
14 | import org.expeditee.items.Text;
|
---|
15 | import org.lsmp.djep.vectorJep.VectorJep;
|
---|
16 | import org.lsmp.djep.vectorJep.values.MVector;
|
---|
17 | import org.nfunk.jep.Node;
|
---|
18 | import org.nfunk.jep.ParseException;
|
---|
19 | import org.nfunk.jep.Variable;
|
---|
20 |
|
---|
21 | public class ExpediteeJEP extends VectorJep {
|
---|
22 | Observer observer = null;
|
---|
23 |
|
---|
24 | public ExpediteeJEP() {
|
---|
25 | super();
|
---|
26 | addStandardFunctions();
|
---|
27 | addStandardConstants();
|
---|
28 | setImplicitMul(true);
|
---|
29 | setAllowAssignment(true);
|
---|
30 | setAllowUndeclared(true);
|
---|
31 | resetObserver();
|
---|
32 | }
|
---|
33 |
|
---|
34 | public void resetObserver() {
|
---|
35 | observer = new Observer() {
|
---|
36 | private String _attribute = "";
|
---|
37 |
|
---|
38 | public void update(Observable ob, Object o) {
|
---|
39 | assert (o instanceof Variable);
|
---|
40 | _attribute = ((Variable) o).getName();
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public String toString() {
|
---|
45 | return _attribute;
|
---|
46 | }
|
---|
47 | };
|
---|
48 | getSymbolTable().addObserver(observer);
|
---|
49 | getSymbolTable().addObserverToExistingVariables(observer);
|
---|
50 | }
|
---|
51 |
|
---|
52 | public String evaluate(Node node) throws ParseException {
|
---|
53 | return evaluate(node, true);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public String evaluate(Node node, boolean prependVarName)
|
---|
57 | throws ParseException {
|
---|
58 | Object rawResult = super.evaluate(node);
|
---|
59 | if (rawResult instanceof Double) {
|
---|
60 | Double result = (Double) rawResult;
|
---|
61 | if (result.isNaN()) {
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 | NumberFormat nf = NumberFormat.getInstance();
|
---|
65 | // TODO see if the parser can handle commas if a flag is switched
|
---|
66 | nf.setGroupingUsed(false);
|
---|
67 | nf.setMinimumFractionDigits(0);
|
---|
68 | nf.setMaximumFractionDigits(15);
|
---|
69 | String varName = observer.toString();
|
---|
70 | if (varName.length() > 0)
|
---|
71 | return (prependVarName ? (varName + ": ") : "")
|
---|
72 | + nf.format(result);
|
---|
73 |
|
---|
74 | return nf.format(result);
|
---|
75 | }
|
---|
76 | return rawResult.toString();
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void addVariables(Frame frame) {
|
---|
80 | if (frame == null)
|
---|
81 | return;
|
---|
82 | // Check for variables
|
---|
83 | for (Text t : frame.getTextItems()) {
|
---|
84 | if (t.isAnnotation())
|
---|
85 | continue;
|
---|
86 |
|
---|
87 | AttributeValuePair avp = new AttributeValuePair(t.getText());
|
---|
88 | if (avp.hasPair()) {
|
---|
89 | try {
|
---|
90 | if (getVar(avp.getAttribute()) == null)
|
---|
91 | addVariable(avp.getAttribute(), avp.getDoubleValue());
|
---|
92 | } catch (NumberFormatException e) {
|
---|
93 | } catch (Exception e) {
|
---|
94 | e.printStackTrace();
|
---|
95 | }
|
---|
96 | } else if (t.isLineEnd()) {
|
---|
97 | Collection<Item> enclosed = FrameUtils.getItemsEnclosedBy(
|
---|
98 | frame, t.getEnclosedShape());
|
---|
99 | String variableName = t.getText();
|
---|
100 | addVectorVariable(enclosed, variableName);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * @param textItems
|
---|
107 | * @param variableName
|
---|
108 | */
|
---|
109 | public void addVectorVariable(Collection<Item> items, String variableName) {
|
---|
110 | Collection<Double> vector = new LinkedList<Double>();
|
---|
111 | for (Item i : items) {
|
---|
112 | if (i instanceof Text && !i.isAnnotation() && !i.isLineEnd()) {
|
---|
113 | try {
|
---|
114 | Double value = new AttributeValuePair(i.getText())
|
---|
115 | .getDoubleValue();
|
---|
116 | if (value != null) {
|
---|
117 | vector.add(value);
|
---|
118 | }
|
---|
119 | } catch (NumberFormatException e) {
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | if (vector.size() > 0)
|
---|
124 | addVariable(variableName, MVector.getInstance(vector.toArray()));
|
---|
125 | }
|
---|
126 |
|
---|
127 | public String getNewVariable() {
|
---|
128 | return observer.toString();
|
---|
129 | }
|
---|
130 | }
|
---|