Commits

petermr committed eaf6827

fixed ci inconsistent content

Comments (0)

Files changed (24)

src/main/java/org/xmlcml/mathml/APPLYElement.java

 	public APPLYElement() {
 		super(TAG);
 	}
-	public APPLYElement(String xml) {
-		super(xml);
-	}
 
 	public APPLYElement(Operator operator,
 			AbstractMathElement arg) {

src/main/java/org/xmlcml/mathml/BVARElement.java

 	public BVARElement() {
 		super(TAG);
 	}
-	public BVARElement(String xml) {
-		super(xml);
-	}
 	
 	@Override
 	protected void validate() {

src/main/java/org/xmlcml/mathml/CIElement.java

 	public CIElement() {
 		super(TAG);
 	}
-	public CIElement(String xml) {
-		super(xml);
-	}
 	
 	@Override
 	protected void validate() {

src/main/java/org/xmlcml/mathml/CNElement.java

 	public CNElement() {
 		super(TAG);
 	}
-	public CNElement(String xml) {
-		super(xml);
-	}
 	
 	/**
 	 * create an integer

src/main/java/org/xmlcml/mathml/CONDITIONElement.java

 	public CONDITIONElement() {
 		super(TAG);
 	}
-	public CONDITIONElement(String xml) {
-		super(xml);
-	}
+	
 	public CONDITIONElement(AbstractMathElement elem) {
 		this();
 		throw new RuntimeException("NYI");

src/main/java/org/xmlcml/mathml/COSElement.java

 	public COSElement() {
 		super(TAG);
 	}
-	public COSElement(String xml) {
-		super(xml);
-	}
+
 	public COSElement(AbstractMathElement elem) {
 		this();
 		throw new RuntimeException("NYI");

src/main/java/org/xmlcml/mathml/DIFFElement.java

 	public DIFFElement() {
 		super(TAG);
 	}
-	public DIFFElement(String xml) {
-		super(xml);
-	}
 	
 	public DIFFElement(CIElement ci) {
 		this();

src/main/java/org/xmlcml/mathml/DIVIDEElement.java

 	public DIVIDEElement() {
 		super(TAG);
 	}
-	public DIVIDEElement(String xml) {
-		super(xml);
-	}
 	
 	@Override
 	protected void validate() {

src/main/java/org/xmlcml/mathml/EQElement.java

 		super(TAG);
 	}
 	
-	public EQElement(String xml) {
-		super(xml);
-	}
-
 	@Override
 	protected void validate() {
 		assertNoElementChildren();

src/main/java/org/xmlcml/mathml/EXPElement.java

 	public EXPElement() {
 		super(TAG);
 	}
-	public EXPElement(String xml) {
-		super(xml);
-	}
+	
 	public Object eval() {
 		throw new RuntimeException("NYI");
 	}

src/main/java/org/xmlcml/mathml/LAMBDAElement.java

 	public LAMBDAElement() {
 		super(TAG);
 	}
-	public LAMBDAElement(String xml) {
-		super(xml);
-	}
 	
 	@Override
 	protected void validate() {

src/main/java/org/xmlcml/mathml/LOWLIMITElement.java

 
 import nu.xom.Elements;
 
-public class LOWLIMITElement extends AbstractMathElement  {
+public class LOWLIMITElement extends LimitElement {
 
-	public static String TAG = "condition";
+	public static String TAG = "lowlimit";
 	
 	public LOWLIMITElement() {
 		super(TAG);
 	}
-	public LOWLIMITElement(String xml) {
-		super(xml);
-	}
+
 	public LOWLIMITElement(AbstractMathElement elem) {
 		this();
 		throw new RuntimeException("NYI");

src/main/java/org/xmlcml/mathml/LimitElement.java

+package org.xmlcml.mathml;
+
+import nu.xom.Nodes;
+
+public abstract class LimitElement extends AbstractMathElement {
+
+	private String ciSymbol;
+	private String cnSymbol;
+	private Integer cnInteger;
+
+	public LimitElement(String tag) {
+		super(tag);
+	}
+	
+	@Override
+	public Object eval() {
+		return null;
+	}
+	
+	public Integer getInteger() {
+		return cnInteger;
+	}
+
+	@Override
+	protected void validate() {
+		assertElementChildren(1, 1);
+		assertNoStringContent();
+		calculateValue();
+	}
+
+	private void calculateValue() {
+		Nodes ciNodes = this.query("m:"+CIElement.TAG, MATHElement.XPATH_CONTEXT);
+		ciSymbol = (ciNodes.size() == 1) ? ciNodes.get(0).getValue() : null;
+		Nodes cnNodes = this.query("m:"+CNElement.TAG, MATHElement.XPATH_CONTEXT);
+		cnSymbol = (cnNodes.size() == 1) ? cnNodes.get(0).getValue() : null;
+		try {
+			cnInteger = new Integer(cnSymbol);
+		} catch (Exception e) {
+			// 
+		}
+	}
+
+}

src/main/java/org/xmlcml/mathml/MATHElement.java

 	public MATHElement() {
 		super(TAG);
 	}
-	public MATHElement(String xml) {
-		super(xml);
-	}
 	
 	@Override
 	protected void validate() {

src/main/java/org/xmlcml/mathml/MINUSElement.java

 	public MINUSElement() {
 		super(TAG);
 	}
-	public MINUSElement(String xml) {
-		super(xml);
-	}
+
 	@Override
 	protected void validate() {
 		assertNoElementChildren();

src/main/java/org/xmlcml/mathml/PLUSElement.java

 	public PLUSElement() {
 		super(TAG);
 	}
-	public PLUSElement(String xml) {
-		super(xml);
-	}
+
 	@Override
 	protected void validate() {
 		assertNoElementChildren();

src/main/java/org/xmlcml/mathml/POWERElement.java

 	public POWERElement() {
 		super(TAG);
 	}
-	public POWERElement(String xml) {
-		super(xml);
-	}
+
 	@Override
 	protected void validate() {
 		assertNoElementChildren();

src/main/java/org/xmlcml/mathml/ROOTElement.java

 	public ROOTElement() {
 		super(TAG);
 	}
-	public ROOTElement(String xml) {
-		super(xml);
-	}
+
 	@Override
 	protected void validate() {
 		assertNoElementChildren();

src/main/java/org/xmlcml/mathml/SINElement.java

 	public SINElement() {
 		super(TAG);
 	}
-	public SINElement(String xml) {
-		super(xml);
-	}
+
 	public SINElement(AbstractMathElement elem) {
 		this();
 		throw new RuntimeException("NYI");

src/main/java/org/xmlcml/mathml/SUMElement.java

 
 import nu.xom.Elements;
 
-public class SUMElement extends AbstractMathElement implements Operator {
+public class SUMElement extends SumProductElement {
 
 	public static String TAG = "sum";
-	private BVARElement bvar;
-	private APPLYElement apply;
-	
 	public SUMElement() {
 		super(TAG);
 	}
-	public SUMElement(String xml) {
-		super(xml);
-	}
+
 	public SUMElement(AbstractMathElement elem) {
 		this();
 		throw new RuntimeException("NYI");
 	}
 	
-	@Override
-	protected void validate() {
-		assertNoElementChildren();
-		assertNoStringContent();
+	public Object apply(Elements elements) {
+		return apply(elements, TAG);
 	}
-
-	public Object eval() {
-		throw new RuntimeException("NYI");
-	}
-
-	/**
-	 <apply xmlns:m='http://www.w3.org/1998/Math/MathML'>
-	   <sum/>
-	   <bvar>
-	     <ci> x </ci>
-	   </bvar>
-	   <lowlimit>
-	     <ci> a </ci>
-	   </lowlimit>
-	   <uplimit>
-	     <ci> b </ci>
-	   </uplimit>
-	   <apply>
-	     <ci type='fn'> f </ci>
-	     <ci> x </ci>
-	   </apply>
-	 </apply> 
-	    
-		<apply xmlns:m='http://www.w3.org/1998/Math/MathML'>
-	      <sum/>
-	      <bvar>
-	        <ci> x </ci>
-	      </bvar>
-	      <condition>
-	        <apply>
-	          <in/>
-	          <ci> x </ci>
-	          <ci type='set'> B </ci>
-	        </apply>
-	      </condition>
-	      <apply>
-	        <ci type='fn'> f </ci>
-	        <ci> x </ci>
-	      </apply>
-	    </apply> 
-	 * 
-	 */
-	public Object apply(Elements elements) {
-		if (elements.size() < 2) {
-			throw new RuntimeException("not enough siblings of <sum/>");
-		}
-		if (!(elements.get(0) instanceof BVARElement)) {
-			throw new RuntimeException("first element must be <bvar/>");
-		}
-		bvar = (BVARElement) elements.get(0);
-		if (!(elements.get(elements.size()-1) instanceof APPLYElement)) {
-			throw new RuntimeException("last element must be <apply/>");
-		}
-		apply = (APPLYElement) elements.get(0);
-		for (int i = 1; i < elements.size()-1; i++) {
-			if (elements.get(i) instanceof CONDITIONElement) {
-				CONDITIONElement condition = (CONDITIONElement) elements.get(i);
-			}
-		}
-		return null;
-	}
-	
 	public AbstractMathElement differentiate() {
 		throw new RuntimeException("NYI");
 	}

src/main/java/org/xmlcml/mathml/SumProductElement.java

+package org.xmlcml.mathml;
+
+import nu.xom.Elements;
+
+public abstract class SumProductElement extends AbstractMathElement implements Operator {
+
+
+	protected APPLYElement apply;
+	protected CONDITIONElement condition;
+	protected LOWLIMITElement lowlimit;
+	protected LimitElement uplimit;
+	BVARElement bvar;
+	private Integer uplimitI;
+	private Integer lowlimitI;
+
+	protected SumProductElement(String tag) {
+		super(tag);
+	}
+	
+	@Override
+	public Object eval() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	@Override
+	protected void validate() {
+		assertNoElementChildren();
+		assertNoStringContent();
+	}
+
+	protected void getLimits(Elements elements) {
+		for (int i = 1; i < elements.size()-1; i++) {
+			if (elements.get(i) instanceof CONDITIONElement) {
+				if (condition != null) {
+					throw new RuntimeException("multiple <condition/> not allowed");
+				}
+				condition = (CONDITIONElement) elements.get(i);
+				if (lowlimit != null) {
+					throw new RuntimeException("multiple <lowlimit/> not allowed");
+				}
+				lowlimit = (LOWLIMITElement) elements.get(i);
+				if (uplimit != null) {
+					throw new RuntimeException("multiple <uplimit/> not allowed");
+				}
+				uplimit = (LimitElement) elements.get(i);
+			}
+		}
+	}
+
+	protected void getApplyBvar(Elements elements) {
+		if (!(elements.get(0) instanceof BVARElement)) {
+			throw new RuntimeException("first element must be <bvar/>");
+		}
+		bvar = (BVARElement) elements.get(0);
+		if (!(elements.get(elements.size()-1) instanceof APPLYElement)) {
+			throw new RuntimeException("last element must be <apply/>");
+		}
+		apply = (APPLYElement) elements.get(0);
+	}
+
+	/**
+	 <apply xmlns:m='http://www.w3.org/1998/Math/MathML'>
+	   <sum/>
+	   <bvar>
+	     <ci> x </ci>
+	   </bvar>
+	   <lowlimit>
+	     <ci> a </ci>
+	   </lowlimit>
+	   <uplimit>
+	     <ci> b </ci>
+	   </uplimit>
+	   <apply>
+	     <ci type='fn'> f </ci>
+	     <ci> x </ci>
+	   </apply>
+	 </apply> 
+	    
+		<apply xmlns:m='http://www.w3.org/1998/Math/MathML'>
+	      <sum/>
+	      <bvar>
+	        <ci> x </ci>
+	      </bvar>
+	      <condition>
+	        <apply>
+	          <in/>
+	          <ci> x </ci>
+	          <ci type='set'> B </ci>
+	        </apply>
+	      </condition>
+	      <apply>
+	        <ci type='fn'> f </ci>
+	        <ci> x </ci>
+	      </apply>
+	    </apply> 
+	 * 
+	 */
+	public Object apply(Elements elements, String tag) {
+		if (elements.size() < 2) {
+			throw new RuntimeException("not enough siblings of <"+tag+"/>");
+		}
+		getApplyBvar(elements);
+		getLimits(elements);
+		if (condition != null) {
+			throw new RuntimeException("condition NYI");
+		} else if (uplimit != null && lowlimit != null) {
+			try {
+				uplimitI = uplimit.getInteger(); 
+				lowlimitI = lowlimit.getInteger(); 
+			} catch (Exception e) {
+				throw new RuntimeException("Cannot get integer up- and low-limits "+uplimit+"/"+lowlimit);
+			}
+			if (uplimitI != null && lowlimitI != null && lowlimitI < uplimitI) {
+				
+			}
+		} else {
+			throw new RuntimeException("limit(s) not given, cannot eval sum");
+		}
+		
+		return null;
+	}
+
+}

src/main/java/org/xmlcml/mathml/TIMESElement.java

 	public TIMESElement() {
 		super(TAG);
 	}
-	public TIMESElement(String xml) {
-		super(xml);
-	}
+
 	@Override
 	protected void validate() {
 		assertNoElementChildren();

src/main/java/org/xmlcml/mathml/UPLIMITElement.java

 
 import nu.xom.Elements;
 
-public class UPLIMITElement extends AbstractMathElement  {
+public class UPLIMITElement extends LimitElement  {
 
-	public static String TAG = "condition";
-	
+	public static String TAG = "uplimit";
 	public UPLIMITElement() {
 		super(TAG);
 	}
-	public UPLIMITElement(String xml) {
-		super(xml);
-	}
-	public UPLIMITElement(AbstractMathElement elem) {
-		this();
-		throw new RuntimeException("NYI");
-	}
-	
-	@Override
-	protected void validate() {
-		assertElementChildren(1, 99);
-		assertNoStringContent();
-	}
 
 	public Object eval() {
 		throw new RuntimeException("NYI");
 	}
 
 	public Object apply(Elements elements) {
-//		if (elements.size() == 2) {
-//			Object o1 = ((AbstractMathElement)elements.get(1)).eval();
-//			if (o1 instanceof Double) {
-//				return Math.cos((Double) o1);
-//			} else if (o1 instanceof Integer) {
-//				return Math.cos(new Double((Integer) o1));
-//			} else {
-//				return "(cos("+o1.toString()+"))";
-//			}
-//		} else {
-//			throw new RuntimeException("<cos/> takes one arguments");
-//		}
 		return null;
 	}
 	
-	public AbstractMathElement differentiate() {
-		List<AbstractMathElement> following = getFollowingElements(1);
-		APPLYElement result = new APPLYElement(
-			new TIMESElement(),
-			((Differentiable)following.get(0)).differentiate(),
-			new APPLYElement(new SINElement(), AbstractMathElement.copyElement(following.get(0)))
-			);
-		AbstractMathElement result1 = new APPLYElement(
-			new MINUSElement(),
-			result
-			);
-		return result;
-	}
-	
 }

src/test/java/org/xmlcml/mathml/SUMTest.java

 public class SUMTest {
 
 	@Test
-	@Ignore
 	public void testSum() {
 	    String ss = "" +
+	    "<apply>" +
 	    "<apply xmlns:m='http://www.w3.org/1998/Math/MathML'>" +
 	    "   <sum/>" +
 	    "   <bvar>" +
-	    "      <ci> x </ci>" +
+	    "      <ci>x</ci>" +
 	    "   </bvar>" +
 	    "   <lowlimit>" +
-	    "      <ci> a </ci>" +
+	    "      <ci>2</ci>" +
 	    "   </lowlimit>" +
 	    "   <uplimit>" +
-	    "      <ci> b </ci>" +
+	    "      <ci>10</ci>" +
 	    "   </uplimit>" +
 	    "   <apply>" +
-	    "      <ci type='fn'> f </ci>" +
-	    "      <ci> x </ci>" +
+	    "      <power/>" +
+	    "      <ci>x</ci>" +
+	    "      <cn>2</cn>" +
 	    "   </apply>" +
 	    "</apply> " +
 	    "";
 		APPLYElement apply = (APPLYElement) AbstractMathElement.createElementFromXML(ss);
 		Object o = apply.eval();
 		Assert.assertNotNull("object", o);
-		Assert.assertTrue("class", o instanceof Double);
-		Assert.assertEquals("object", 7.38905609893065, (double)(Double) o, 0.000001);
+		Assert.assertTrue("class", o instanceof Integer);
+		Assert.assertEquals("object", 44, (int)(Integer) o);
 		
 	}