Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Mrz 27 2008 21:46:26 CET
file stats: LOC: 221   Methods: 11
NCLOC: 110   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Term.java 75% 84,2% 81,8% 81,2%
coverage coverage
 1    /* $Id: Term.java,v 1.7 2008/03/27 05:16:27 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2008, 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.trace.Trace;
 24    import org.qedeq.kernel.utility.StringUtility;
 25   
 26    /**
 27    * Parsed term.
 28    *
 29    * @version $Revision: 1.7 $
 30    * @author Michael Meyling
 31    */
 32    public class Term {
 33   
 34    /** This class. */
 35    private static final Class CLASS = Term.class;
 36   
 37    /** Operator, can be <code>null</code>. */
 38    private final Operator operator;
 39   
 40    /** Arguments, can be <code>null</code>. */
 41    private final List arguments;
 42   
 43    /** Atom, can be <code>null</code>. */
 44    private final TermAtom atom;
 45   
 46    /**
 47    * Constructor.
 48    *
 49    * @param atom Term atom.
 50    */
 51  946 public Term(final TermAtom atom) {
 52  946 this.operator = null;
 53  946 this.arguments = null;
 54  946 this.atom = atom;
 55    }
 56   
 57   
 58    /**
 59    * Constructor.
 60    *
 61    * @param operator Construct new term for this operator.
 62    */
 63  407 public Term(final Operator operator) {
 64  407 this.operator = operator;
 65  407 this.arguments = new ArrayList();
 66  407 this.atom = null;
 67    }
 68   
 69    /**
 70    * Constructor.
 71    *
 72    * @param operator Construct new term for this operator.
 73    * @param firstArgument First argument of operator.
 74    */
 75  594 public Term(final Operator operator, final Term firstArgument) {
 76  594 this.operator = operator;
 77  594 this.arguments = new ArrayList();
 78  594 this.atom = null;
 79  594 addArgument(firstArgument);
 80    }
 81   
 82    /**
 83    * Is this term an atom?
 84    *
 85    * @return Is this term an atom?
 86    */
 87  22830 public final boolean isAtom() {
 88  22830 return atom != null;
 89    }
 90   
 91    /**
 92    * Add next argument term to operator. Overall number of arguments must
 93    * not exceed {@link Operator#getMax()} (if <code>>= 0</code>). Addition is only possible if
 94    * this is no atom term (see {@link #Term(TermAtom)}).
 95    *
 96    * @param term Add this argument at last position.
 97    * @throws IllegalArgumentException This is an atom term or argument
 98    * maximum exceeded.
 99    */
 100  1722 public final void addArgument(final Term term) {
 101  1722 if (isAtom()) {
 102  0 throw new IllegalArgumentException(
 103    "this is an atom, no arguments could be added to " + atom.getValue());
 104    }
 105  1722 if (operator.getMax() >= 0 && operator.getMax() < arguments.size() + 1) {
 106  0 throw new IllegalArgumentException("operator could have maximal "
 107    + operator.getMax() + " arguments");
 108    }
 109  1722 arguments.add(term);
 110    }
 111   
 112    /**
 113    * Get operator of term. Can be <code>null</code> if this is an atom term.
 114    *
 115    * @return Term operator.
 116    */
 117  0 public final Operator getOperator() {
 118  0 return operator;
 119    }
 120   
 121    /**
 122    * Get number of arguments of this operator.
 123    *
 124    * @return Argument number.
 125    */
 126  0 public final int size() {
 127  0 if (arguments == null) {
 128  0 return 0;
 129    }
 130  0 return arguments.size();
 131    }
 132   
 133    /**
 134    * Get QEDEQ representation of this term.
 135    *
 136    * @return QEDEQ representation.
 137    */
 138  21060 public final String getQedeq() {
 139  21060 if (isAtom()) {
 140  11510 return atom.getValue();
 141    } else {
 142  9550 final StringBuffer buffer = new StringBuffer();
 143  9550 buffer.append(operator.getQedeq()).append('(');
 144  9550 if (operator.getQedeqArgument() != null) {
 145  127 buffer.append(StringUtility.quote(operator.getQedeqArgument()));
 146    }
 147  9550 for (int i = 0; i < arguments.size(); i++) {
 148  15739 if (i > 0 || operator.getQedeqArgument() != null) {
 149  6594 buffer.append(", ");
 150    }
 151  15739 buffer.append(((Term)
 152    arguments.get(i)).getQedeq());
 153    }
 154  9550 buffer.append(')');
 155  9550 return buffer.toString();
 156    }
 157    }
 158   
 159    /**
 160    * Get QEDEQ XML representation of this term.
 161    *
 162    * @return QEDEQ XML representation.
 163    */
 164  4 public final String getQedeqXml() {
 165  4 return getQedeqXml(0);
 166    }
 167   
 168    /**
 169    * Get QEDEQ XML representation of this term.
 170    *
 171    * @param level Tabulation level.
 172    * @return QEDEQ XML representation.
 173    */
 174  48 private final String getQedeqXml(final int level) {
 175  48 if (isAtom()) {
 176  0 return StringUtility.getSpaces(level * 2) + atom.getValue() + "\n";
 177    } else {
 178  48 final StringBuffer buffer = new StringBuffer();
 179  48 buffer.append(StringUtility.getSpaces(level * 2));
 180  48 buffer.append("<").append(operator.getQedeq());
 181  48 if (operator.getQedeq().endsWith("VAR")) { // TODO mime 20060612: ok for all QEDEQ?
 182  26 buffer.append(" id=" + quote(operator.getQedeqArgument()));
 183  26 if (arguments == null || arguments.size() == 0) {
 184  26 buffer.append(" />" + "\n");
 185  26 return buffer.toString();
 186    }
 187  22 } else if (operator.getQedeq().endsWith("CON")) {
 188  6 buffer.append(" ref=" + quote(operator.getQedeqArgument()));
 189  6 if (arguments == null || arguments.size() == 0) {
 190  0 buffer.append(" />" + "\n");
 191  0 return buffer.toString();
 192    }
 193    }
 194   
 195  22 buffer.append(">\n");
 196  22 if (operator.getQedeqArgument() != null) {
 197    // no arguments expected!
 198  6 Trace.fatal(CLASS, this, "getQedeqXml(int)", "operator argument is not null but:\n"
 199    + operator.getQedeqArgument(), new IllegalArgumentException());
 200    }
 201  22 for (int i = 0; i < arguments.size(); i++) {
 202  44 buffer.append(((Term)
 203    arguments.get(i)).getQedeqXml(level + 1));
 204    }
 205  22 buffer.append(StringUtility.getSpaces(level * 2));
 206  22 buffer.append("</").append(operator.getQedeq()).append(">\n");
 207  22 return buffer.toString();
 208    }
 209    }
 210   
 211    /**
 212    * Quote attribute value.
 213    *
 214    * @param text Attribute text.
 215    * @return Quoted attribute.
 216    */
 217  32 private String quote(final String text) {
 218  32 return "\"" + StringUtility.replace(text, "\"", "&quot;") + "\"";
 219    }
 220   
 221    }