Anonymous avatar Anonymous committed ae66c3e

Updated to use new parser

Comments (0)

Files changed (3)

src/main/java/org/xmlcml/declaratron/math/CalculationVisitable.java

 import nu.xom.Nodes;
 
 import org.xmlcml.cml.base.CMLElement;
-import org.xmlcml.mathml.Context;
-import org.xmlcml.mathml.MathMLDoubleElement;
-import org.xmlcml.mathml.MathMLParser;
+import org.xmlcml.mathml.*;
 import org.xmlcml.declaratron.core.AbstractExecutableElement;
 import org.xmlcml.declaratron.core.XMLContext;
 
 	
 	public class Calculator
 	{
-		Collection<MathMLDoubleElement> expressions = new ArrayList<MathMLDoubleElement>();
+		Collection<MathMLElement<Object>> expressions = new ArrayList<MathMLElement<Object>>();
 		CMLElement target;
 		
 		public Calculator( CMLElement target, Collection<Element> expressions )
 			this.target = target;
 			for( Element f : expressions )
 			{
-				MathMLParser p = new MathMLParser();
+				XomMathMLParser p = new XomMathMLParser();
 				Element math = getSingleElementNS( f, "m:math" );
-				this.expressions.add( p.buildDoubleExpressionFromString( math.toXML() ) );
+				this.expressions.add( p.getDoubleExpression( math ) );
 			}
 		}
 		
 		{
 			double val = 0;
 			Context context = getContext(target);
-			for( MathMLDoubleElement m : expressions ) {
+			for( MathMLElement<Object> m : expressions ) {
 				Double val0 =  (Double) m.eval(context); 
 				log.trace("Energy: " +val0 /*+"; "+m.print()*/);
 				val += val0; 

src/main/java/org/xmlcml/declaratron/math/MathVisitable.java

 
 import org.xmlcml.declaratron.core.AbstractExecutableElement;
 import org.xmlcml.declaratron.core.visitors.Executable;
-import org.xmlcml.mathml.MathMLDoubleElement;
-import org.xmlcml.mathml.MathMLParser;
+import org.xmlcml.mathml.*;
 
 public class MathVisitable extends AbstractExecutableElement implements Executable
 {	
 		return varNames;
 	}
 	
-	public static MathMLDoubleElement getEvaluatableFunction(AbstractExecutableElement context, Element function, String mathPath) {
-		MathMLParser p = new MathMLParser();
+	public static MathMLElement<Object> getEvaluatableFunction(AbstractExecutableElement context, Element function, String mathPath) {
+		//MathMLParser p = new MathMLParser();
 		Element math = context.getSingleElementNS( function, mathPath );
-		MathMLDoubleElement func = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		XomMathMLParser p = new XomMathMLParser();
+		MathMLElement<Object> func = p.getDoubleExpression( math ) ;
 		return func;
 	}
 

src/test/java/org/xmlcml/declaratron/math/SimpleTest.java

 import org.junit.Ignore;
 import org.junit.Test;
 import org.xmlcml.declaratron.core.NodeUtils;
-import org.xmlcml.mathml.Context;
-import org.xmlcml.mathml.MathMLDoubleElement;
-import org.xmlcml.mathml.MathMLParser;
+import org.xmlcml.mathml.*;
 
 public class SimpleTest {
 	
 
 	@Test
 	public void testTwoPlusTwo() {
-		MathMLParser parser = new MathMLParser();
+		XomMathMLParser parser = new XomMathMLParser();
 		String expression = "<math><apply><plus/><cn>2</cn><cn>2</cn></apply></math>";
-		MathMLDoubleElement tree = parser.buildDoubleExpressionFromString( expression );
+		MathMLElement<Object> tree = parser.getDoubleExpression( expression );
 		Assert.assertEquals("2+2", 4.0, (Double) tree.eval(new Context()), 0.001);
 	}
 
 	@Test
 	public void testExponential() {
-		MathMLParser parser = new MathMLParser();
+		XomMathMLParser parser = new XomMathMLParser();
 		String expression = "<math><apply><exp/><cn>2</cn></apply></math>";
-		MathMLDoubleElement tree = parser.buildDoubleExpressionFromString( expression );
+		MathMLElement<Object> tree = parser.getDoubleExpression( expression );
 		Assert.assertEquals("exp(2)", Math.exp(2.), (Double) tree.eval(new Context()), 0.001);
 	}
 
 	@Test
 	public void testSetVar() {
-		MathMLParser parser = new MathMLParser();
+		XomMathMLParser parser = new XomMathMLParser();
 		String expression = "<math><apply><times/><cn>2</cn><ci>x</ci></apply></math>";
-		MathMLDoubleElement tree = parser.buildDoubleExpressionFromString( expression );
+		MathMLElement<Object> tree = parser.getDoubleExpression( expression );
 		
 		Context c = new Context();
 		for( int i = 0; i < 5; i++ )
 		for (double d : dd) {
 			dList.add(d);
 		}
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"subscript.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		context.setVar("R", dList);
 		double val0 = (Double)mDouble.eval(context); 
 		List<Double> dList1 = Arrays.asList(new Double[]{3.0, 4.0, 5.0, 6.0, 7.0, 8.0});
 		List<Double> dList2 = Arrays.asList(new Double[]{7.0, 6.0, 5.0, 4.0, 3.0, 2.0});
 
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"subscript12.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		context.setVar("R1", dList1);
 		context.setVar("R2", dList2);
 	@Test
 	public void testAddConstants() {
 
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"add.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		double val0 = (Double)mDouble.eval(context); 
 		Assert.assertEquals("2+3 ", 5., val0, 0.001);
 	@Test
 	public void testAddVariables() {
 
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"addvars.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		context.setVar("a", new Double(6.));
 		double val0 = (Double)mDouble.eval(context); 
 	@Test
 	public void testQuadraticRoot() {
 
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"quadroot.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		context.setVar("a", new Double(1.));
 		context.setVar("b", new Double(5.));
 	
 	@Test
 	public void testTrig() {
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"cos2a.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		Double a = 1.0;
 		context.setVar("a", a);
 	
 	@Test
 	public void testSinDivideCos() {
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"sinDivideCos.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		Double a = 1.0;
 		context.setVar("a", a);
 	 */
 	@Ignore // parses and possibly computes but fails to give correct answer
 	public void testSinDivideCosGreek() {
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"sinDivideCosGreek.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		Double phi = 1.0;
 		context.setVar("&#981;", phi);
 	
 	@Test
 	public void testOppositeLength() {
-		MathMLParser p = new MathMLParser();
+		XomMathMLParser p = new XomMathMLParser();
 		Element math = NodeUtils.parseResource(Fixtures.MATH+"oppositeLength.xml");
-		MathMLDoubleElement mDouble = p.buildDoubleExpressionFromString( math.toXML() ) ;
+		MathMLElement<Object> mDouble = p.getDoubleExpression( math.toXML() ) ;
 		Context context = new Context();
 		Double a = 2.0;
 		context.setVar("a", a);
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.