Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Jan 11 2007 09:03:50 CET
file stats: LOC: 208   Methods: 10
NCLOC: 103   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Term.java 32,1% 45,5% 60% 43%
coverage coverage
 1    /* $Id: Term.java,v 1.4 2006/10/20 20:23:05 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2006, Michael Meyling <mime@qedeq.org>.
 6    *
 7    * "Hilbert II" is free software; you can redistribute
 8    * it and/or modify it under the terms of the GNU General Public
 9    * License as published by the Free Software Foundation; either
 10    * version 2 of the License, or (at your option) any later version.
 11    *
 12    * This program is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15    * GNU General Public License for more details.
 16    */
 17   
 18    package org.qedeq.kernel.parser;
 19   
 20    import java.util.ArrayList;
 21    import java.util.List;
 22   
 23    import org.qedeq.kernel.utility.IoUtility;
 24   
 25    /**
 26    * Parsed term.
 27    *
 28    * @version $Revision: 1.4 $
 29    * @author Michael Meyling
 30    */
 31    public final class Term {
 32   
 33    /** Operator, can be <code>null</code>. */
 34    private final Operator operator;
 35   
 36    /** Arguments, can be <code>null</code>. */
 37    private final List arguments;
 38   
 39    /** Atom, can be <code>null</code>. */
 40    private final Atom atom;
 41   
 42    /**
 43    * Constructor.
 44    *
 45    * @param atom Term atom.
 46    */
 47  898 public Term(final Atom atom) {
 48  898 this.operator = null;
 49  898 this.arguments = null;
 50  898 this.atom = atom;
 51    }
 52   
 53   
 54    /**
 55    * Constructor.
 56    *
 57    * @param operator Construct new term for this operator.
 58    */
 59  334 public Term(final Operator operator) {
 60  334 this.operator = operator;
 61  334 this.arguments = new ArrayList();
 62  334 this.atom = null;
 63    }
 64   
 65    /**
 66    * Constructor.
 67    *
 68    * @param operator Construct new term for this operator.
 69    * @param firstArgument First argument of operator.
 70    */
 71  533 public Term(final Operator operator, final Term firstArgument) {
 72  533 this.operator = operator;
 73  533 this.arguments = new ArrayList();
 74  533 this.atom = null;
 75  533 addArgument(firstArgument);
 76    }
 77   
 78    /**
 79    * Is this term an atom?
 80    *
 81    * @return Is this term an atom?
 82    */
 83  21095 public final boolean isAtom() {
 84  21095 return atom != null;
 85    }
 86   
 87    /**
 88    * Add next argument term to operator. Overall number of arguments must
 89    * not exceed {@link Operator#getMax()} (if <code>>= 0</code>). Addition is only possible if
 90    * this is no atom term (see {@link #Term(Atom)}).
 91    *
 92    * @param term Add this argument at last position.
 93    * @throws IllegalArgumentException This is an atom term or argument
 94    * maximum exceeded.
 95    */
 96  1564 public final void addArgument(final Term term) {
 97  1564 if (isAtom()) {
 98  0 throw new IllegalArgumentException(
 99    "this is an atom, no arguments could be added to " + atom.getValue());
 100    }
 101  1564 if (operator.getMax() >= 0 && operator.getMax() < arguments.size() + 1) {
 102  0 throw new IllegalArgumentException("operator could have maximal "
 103    + operator.getMax() + " arguments");
 104    }
 105  1564 arguments.add(term);
 106    }
 107   
 108    /**
 109    * Get operator of term. Can be <code>null</code> if this is an atom term.
 110    *
 111    * @return Term operator.
 112    */
 113  0 public final Operator getOperator() {
 114  0 return operator;
 115    }
 116   
 117    /**
 118    * Get number of arguments of this operator.
 119    *
 120    * @return Argument number.
 121    */
 122  0 public final int size() {
 123  0 if (arguments == null) {
 124  0 return 0;
 125    }
 126  0 return arguments.size();
 127    }
 128   
 129    /**
 130    * Get QEDEQ representation of this term.
 131    *
 132    * @return QEDEQ representation.
 133    */
 134  19531 public final String getQedeq() {
 135  19531 if (isAtom()) {
 136  10962 return atom.getValue();
 137    } else {
 138  8569 final StringBuffer buffer = new StringBuffer();
 139  8569 buffer.append(operator.getQedeq()).append('(');
 140  8569 if (operator.getQedeqArgument() != null) {
 141  0 buffer.append(IoUtility.quote(operator.getQedeqArgument()));
 142    }
 143  8569 for (int i = 0; i < arguments.size(); i++) {
 144  14720 if (i > 0 || operator.getQedeqArgument() != null) {
 145  6160 buffer.append(", ");
 146    }
 147  14720 buffer.append(((Term)
 148    arguments.get(i)).getQedeq());
 149    }
 150  8569 buffer.append(')');
 151  8569 return buffer.toString();
 152    }
 153    }
 154   
 155    /**
 156    * Get QEDEQ XML representation of this term.
 157    *
 158    * @return QEDEQ XML representation.
 159    */
 160  0 public final String getQedeqXml() {
 161  0 return getQedeqXml(0);
 162    }
 163   
 164    /**
 165    * Get QEDEQ XML representation of this term.
 166    *
 167    * @param level Tabulation level.
 168    * @return QEDEQ XML representation.
 169    */
 170  0 private final String getQedeqXml(final int level) {
 171  0 if (isAtom()) {
 172  0 return IoUtility.getSpaces(level * 2) + atom.getValue() + "\n";
 173    } else {
 174  0 final StringBuffer buffer = new StringBuffer();
 175  0 buffer.append(IoUtility.getSpaces(level * 2));
 176  0 buffer.append("<").append(operator.getQedeq());
 177  0 if (operator.getQedeq().endsWith("VAR")) { // TODO mime 20060612: ok for all QEDEQ?
 178    // TODO mime 20060612: quote text
 179  0 buffer.append(" id=\"" + operator.getQedeqArgument() + "\"");
 180  0 if (arguments == null || arguments.size() == 0) {
 181  0 buffer.append(" />" + "\n");
 182  0 return buffer.toString();
 183    }
 184  0 } else if (operator.getQedeq().endsWith("CON")) {
 185    // TODO mime 20060612: quote text
 186  0 buffer.append(" ref=\"" + operator.getQedeqArgument() + "\"");
 187  0 if (arguments == null || arguments.size() == 0) {
 188  0 buffer.append(" />" + "\n");
 189  0 return buffer.toString();
 190    }
 191    }
 192   
 193  0 buffer.append(">\n");
 194  0 if (operator.getQedeqArgument() != null) {
 195    // TODO error message
 196    }
 197  0 for (int i = 0; i < arguments.size(); i++) {
 198  0 buffer.append(((Term)
 199    arguments.get(i)).getQedeqXml(level + 1));
 200    }
 201  0 buffer.append(IoUtility.getSpaces(level * 2));
 202  0 buffer.append("</").append(operator.getQedeq()).append(">\n");
 203  0 return buffer.toString();
 204    }
 205    }
 206   
 207    // TODO override toString
 208    }