source: trunk/src/org/expeditee/math/ExpediteeJEP.java@ 1477

Last change on this file since 1477 was 919, checked in by jts21, 10 years ago

Added license headers to all files, added full GPL3 license file, moved license header generator script to dev/bin/scripts

File size: 4.2 KB
Line 
1/**
2 * ExpediteeJEP.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
19package org.expeditee.math;
20
21import java.text.NumberFormat;
22import java.util.Collection;
23import java.util.LinkedList;
24import java.util.Observable;
25import java.util.Observer;
26
27import org.expeditee.gui.AttributeValuePair;
28import org.expeditee.gui.Frame;
29import org.expeditee.gui.FrameUtils;
30import org.expeditee.items.Item;
31import org.expeditee.items.Text;
32import org.lsmp.djep.vectorJep.VectorJep;
33import org.lsmp.djep.vectorJep.values.MVector;
34import org.nfunk.jep.Node;
35import org.nfunk.jep.ParseException;
36import org.nfunk.jep.Variable;
37
38public class ExpediteeJEP extends VectorJep {
39 Observer observer = null;
40
41 public ExpediteeJEP() {
42 super();
43 addStandardFunctions();
44 addStandardConstants();
45 setImplicitMul(true);
46 setAllowAssignment(true);
47 setAllowUndeclared(true);
48 resetObserver();
49 }
50
51 public void resetObserver() {
52 observer = new Observer() {
53 private String _attribute = "";
54
55 public void update(Observable ob, Object o) {
56 assert (o instanceof Variable);
57 _attribute = ((Variable) o).getName();
58 }
59
60 @Override
61 public String toString() {
62 return _attribute;
63 }
64 };
65 getSymbolTable().addObserver(observer);
66 getSymbolTable().addObserverToExistingVariables(observer);
67 }
68
69 public String evaluate(Node node) throws ParseException {
70 return evaluate(node, true);
71 }
72
73 public String evaluate(Node node, boolean prependVarName)
74 throws ParseException {
75 Object rawResult = rawResult = super.evaluate(node);
76
77 if (rawResult instanceof Double) {
78 Double result = (Double) rawResult;
79 if (result.isNaN()) {
80 return null;
81 }
82 NumberFormat nf = NumberFormat.getInstance();
83 // TODO see if the parser can handle commas if a flag is switched
84 nf.setGroupingUsed(false);
85 nf.setMinimumFractionDigits(0);
86 nf.setMaximumFractionDigits(15);
87 String varName = observer.toString();
88 if (varName.length() > 0)
89 return (prependVarName ? (varName + ": ") : "")
90 + nf.format(result);
91
92 return nf.format(result);
93 }
94 return rawResult.toString();
95 }
96
97 public void addVariables(Frame frame) {
98 if (frame == null)
99 return;
100 // Check for variables
101 for (Text t : frame.getTextItems()) {
102 if (t.isAnnotation())
103 continue;
104
105 AttributeValuePair avp = t.getAttributeValuePair();
106 if (avp.hasPair()) {
107 try {
108 if (getVar(avp.getAttribute()) == null) {
109 Double d = avp.getDoubleValue();
110 if (!d.equals(Double.NaN))
111 addVariable(avp.getAttribute(), avp
112 .getDoubleValue());
113 }
114 } catch (Exception e) {
115 e.printStackTrace();
116 }
117 } else if (t.isLineEnd()) {
118 Collection<Item> enclosed = FrameUtils.getItemsEnclosedBy(
119 frame, t.getEnclosedShape());
120 String variableName = t.getText();
121 if (!variableName.contains(" "))
122 addVectorVariable(enclosed, variableName);
123 }
124 }
125 }
126
127 /**
128 * @param textItems
129 * @param variableName
130 */
131 public void addVectorVariable(Collection<Item> items, String variableName) {
132 Collection<Double> vector = new LinkedList<Double>();
133 for (Item i : items) {
134 if (i instanceof Text && !i.isAnnotation() && !i.isLineEnd()) {
135 try {
136 Double value = i.getAttributeValuePair().getDoubleValue();
137 if (!value.equals(Double.NaN)) {
138 vector.add(value);
139 }
140 } catch (Exception e) {
141 e.printStackTrace();
142 }
143 }
144 }
145 // At the moment the VSum method will not work with empty vectors.
146 if (vector.size() > 0)
147 addVariable(variableName, MVector.getInstance(vector.toArray()));
148 }
149
150 public String getNewVariable() {
151 return observer.toString();
152 }
153}
Note: See TracBrowser for help on using the repository browser.