Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mai 10 2007 03:16:40 CEST
file stats: LOC: 208   Methods: 10
NCLOC: 103   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Term.java 78,6% 85,5% 80% 82,8%
coverage coverage
 1    /* $Id: Term.java,v 1.6 2007/05/10 00:37:51 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2007, 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.6 $
 29    * @author Michael Meyling
 30    */
 31    public 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 TermAtom atom;
 41   
 42    /**
 43    * Constructor.
 44    *
 45    * @param atom Term atom.
 46    */
 47  1162 public Term(final TermAtom atom) {
 48  1162 this.operator = null;
 49  1162 this.arguments = null;
 50  1162 this.atom = atom;
 51    }
 52   
 53   
 54    /**
 55    * Constructor.
 56    *
 57    * @param operator Construct new term for this operator.
 58    */
 59  407 public Term(final Operator operator) {
 60  407 this.operator = operator;
 61  407 this.arguments = new ArrayList();
 62  407 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  558 public Term(final Operator operator, final Term firstArgument) {
 72  558 this.operator = operator;
 73  558 this.arguments = new ArrayList();
 74  558 this.atom = null;
 75  558 addArgument(firstArgument);
 76    }
 77   
 78    /**
 79    * Is this term an atom?
 80    *
 81    * @return Is this term an atom?
 82    */
 83  23051 public final boolean isAtom() {
 84  23051 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(TermAtom)}).
 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  1620 public final void addArgument(final Term term) {
 97  1620 if (isAtom()) {
 98  0 throw new IllegalArgumentException(
 99    "this is an atom, no arguments could be added to " + atom.getValue());
 100    }
 101  1620 if (operator.getMax() >= 0 && operator.getMax() < arguments.size() + 1) {
 102  0 throw new IllegalArgumentException("operator could have maximal "
 103    + operator.getMax() + " arguments");
 104    }
 105  1620 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  20715 public final String getQedeq() {
 135  20715 if (isAtom()) {
 136  11786 return atom.getValue();
 137    } else {
 138  8929 final StringBuffer buffer = new StringBuffer();
 139  8929 buffer.append(operator.getQedeq()).append('(');
 140  8929 if (operator.getQedeqArgument() != null) {
 141  321 buffer.append(IoUtility.quote(operator.getQedeqArgument()));
 142    }
 143  8929 for (int i = 0; i < arguments.size(); i++) {
 144  14890 if (i > 0 || operator.getQedeqArgument() != null) {
 145  6291 buffer.append(", ");
 146    }
 147  14890 buffer.append(((Term)
 148    arguments.get(i)).getQedeq());
 149    }
 150  8929 buffer.append(')');
 151  8929 return buffer.toString();
 152    }
 153    }
 154   
 155    /**
 156    * Get QEDEQ XML representation of this term.
 157    *
 158    * @return QEDEQ XML representation.
 159    */
 160  608 public final String getQedeqXml() {
 161  608 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  716 private final String getQedeqXml(final int level) {
 171  716 if (isAtom()) {
 172  528 return IoUtility.getSpaces(level * 2) + atom.getValue() + "\n";
 173    } else {
 174  188 final StringBuffer buffer = new StringBuffer();
 175  188 buffer.append(IoUtility.getSpaces(level * 2));
 176  188 buffer.append("<").append(operator.getQedeq());
 177  188 if (operator.getQedeq().endsWith("VAR")) { // TODO mime 20060612: ok for all QEDEQ?
 178    // TODO mime 20060612: quote text
 179  134 buffer.append(" id=\"" + operator.getQedeqArgument() + "\"");
 180  134 if (arguments == null || arguments.size() == 0) {
 181  134 buffer.append(" />" + "\n");
 182  134 return buffer.toString();
 183    }
 184  54 } else if (operator.getQedeq().endsWith("CON")) {
 185    // TODO mime 20060612: quote text
 186  38 buffer.append(" ref=\"" + operator.getQedeqArgument() + "\"");
 187  38 if (arguments == null || arguments.size() == 0) {
 188  0 buffer.append(" />" + "\n");
 189  0 return buffer.toString();
 190    }
 191    }
 192   
 193  54 buffer.append(">\n");
 194  54 if (operator.getQedeqArgument() != null) {
 195    // TODO error message
 196    }
 197  54 for (int i = 0; i < arguments.size(); i++) {
 198  108 buffer.append(((Term)
 199    arguments.get(i)).getQedeqXml(level + 1));
 200    }
 201  54 buffer.append(IoUtility.getSpaces(level * 2));
 202  54 buffer.append("</").append(operator.getQedeq()).append(">\n");
 203  54 return buffer.toString();
 204    }
 205    }
 206   
 207    // TODO override toString
 208    }