Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../../img/srcFileCovDistChart5.png 74% of files have more coverage
57   222   31   5,18
28   111   0,54   11
11     2,82  
1    
 
  Term       Line # 32 57 31 41,7% 0.41666666
 
  (92)
 
1    /* $Id: Term.java,v 1.1 2008/07/26 07:58:30 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.bo.parser;
19   
20    import java.util.ArrayList;
21    import java.util.List;
22   
23    import org.qedeq.base.trace.Trace;
24    import org.qedeq.base.utility.StringUtility;
25   
26    /**
27    * Parsed term.
28    *
29    * @version $Revision: 1.1 $
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  898 toggle public Term(final TermAtom atom) {
52  898 this.operator = null;
53  898 this.arguments = null;
54  898 this.atom = atom;
55    }
56   
57   
58    /**
59    * Constructor.
60    *
61    * @param operator Construct new term for this operator.
62    */
 
63  436 toggle public Term(final Operator operator) {
64  436 this.operator = operator;
65  436 this.arguments = new ArrayList();
66  436 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  585 toggle public Term(final Operator operator, final Term firstArgument) {
76  585 this.operator = operator;
77  585 this.arguments = new ArrayList();
78  585 this.atom = null;
79  585 addArgument(firstArgument);
80    }
81   
82    /**
83    * Is this term an atom?
84    *
85    * @return Is this term an atom?
86    */
 
87  21671 toggle public final boolean isAtom() {
88  21671 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  1698 toggle public final void addArgument(final Term term) {
101  1698 if (isAtom()) {
102  0 throw new IllegalArgumentException(
103    "this is an atom, no arguments could be added to " + atom.getValue());
104    }
105  1698 if (operator.getMax() >= 0 && operator.getMax() < arguments.size() + 1) {
106  0 throw new IllegalArgumentException("operator could have maximal "
107    + operator.getMax() + " arguments");
108    }
109  1698 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 toggle 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 toggle 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  19973 toggle public final String getQedeq() {
139  19973 if (isAtom()) {
140  10532 return atom.getValue();
141    } else {
142  9441 final StringBuffer buffer = new StringBuffer();
143  9441 buffer.append(operator.getQedeq()).append('(');
144  9441 if (operator.getQedeqArgument() != null) {
145  0 buffer.append(StringUtility.quote(operator.getQedeqArgument()));
146    }
147  24243 for (int i = 0; i < arguments.size(); i++) {
148  14802 if (i > 0 || operator.getQedeqArgument() != null) {
149  6147 buffer.append(", ");
150    }
151  14802 buffer.append(((Term)
152    arguments.get(i)).getQedeq());
153    }
154  9441 buffer.append(')');
155  9441 return buffer.toString();
156    }
157    }
158   
159    /**
160    * Get QEDEQ XML representation of this term.
161    *
162    * @return QEDEQ XML representation.
163    */
 
164  0 toggle public final String getQedeqXml() {
165  0 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  0 toggle private final String getQedeqXml(final int level) {
175  0 if (isAtom()) {
176  0 return StringUtility.getSpaces(level * 2) + atom.getValue() + "\n";
177    } else {
178  0 final StringBuffer buffer = new StringBuffer();
179  0 buffer.append(StringUtility.getSpaces(level * 2));
180  0 buffer.append("<").append(operator.getQedeq());
181  0 if (operator.getQedeq().endsWith("VAR")) { // TODO mime 20060612: ok for all QEDEQ?
182  0 buffer.append(" id=" + quote(operator.getQedeqArgument()));
183  0 if (arguments == null || arguments.size() == 0) {
184  0 buffer.append(" />" + "\n");
185  0 return buffer.toString();
186    }
187  0 } else if (operator.getQedeq().endsWith("CON")) {
188  0 buffer.append(" ref=" + quote(operator.getQedeqArgument()));
189  0 if (arguments == null || arguments.size() == 0) {
190  0 buffer.append(" />" + "\n");
191  0 return buffer.toString();
192    }
193    }
194   
195  0 buffer.append(">\n");
196  0 if (operator.getQedeqArgument() != null && !operator.getQedeq().endsWith("VAR")
197    && !operator.getQedeq().endsWith("CON")) {
198    // no arguments expected!
199  0 Trace.fatal(CLASS, this, "getQedeqXml(int)", "operator argument is not null but: "
200    + operator.getQedeqArgument(), new IllegalArgumentException());
201    }
202  0 for (int i = 0; i < arguments.size(); i++) {
203  0 buffer.append(((Term)
204    arguments.get(i)).getQedeqXml(level + 1));
205    }
206  0 buffer.append(StringUtility.getSpaces(level * 2));
207  0 buffer.append("</").append(operator.getQedeq()).append(">\n");
208  0 return buffer.toString();
209    }
210    }
211   
212    /**
213    * Quote attribute value.
214    *
215    * @param text Attribute text.
216    * @return Quoted attribute.
217    */
 
218  0 toggle private String quote(final String text) {
219  0 return "\"" + StringUtility.replace(text, "\"", "&quot;") + "\"";
220    }
221   
222    }