1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.qedeq.kernel.bo.parser;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.qedeq.base.trace.Trace;
22 import org.qedeq.base.utility.StringUtility;
23
24
25
26
27
28
29
30 public class Term {
31
32
33 private static final Class CLASS = Term.class;
34
35
36 private final Operator operator;
37
38
39 private final List arguments;
40
41
42 private final TermAtom atom;
43
44
45
46
47
48
49 public Term(final TermAtom atom) {
50 this.operator = null;
51 this.arguments = null;
52 this.atom = atom;
53 }
54
55
56
57
58
59
60
61 public Term(final Operator operator) {
62 this.operator = operator;
63 this.arguments = new ArrayList();
64 this.atom = null;
65 }
66
67
68
69
70
71
72
73 public Term(final Operator operator, final Term firstArgument) {
74 this.operator = operator;
75 this.arguments = new ArrayList();
76 this.atom = null;
77 addArgument(firstArgument);
78 }
79
80
81
82
83
84
85 public final boolean isAtom() {
86 return atom != null;
87 }
88
89
90
91
92
93
94
95
96
97
98 public final void addArgument(final Term term) {
99 if (isAtom()) {
100 throw new IllegalArgumentException(
101 "this is an atom, no arguments could be added to " + atom.getValue());
102 }
103 if (operator.getMax() >= 0 && operator.getMax() < arguments.size() + 1) {
104 throw new IllegalArgumentException("operator could have maximal "
105 + operator.getMax() + " arguments");
106 }
107 arguments.add(term);
108 }
109
110
111
112
113
114
115 public final Operator getOperator() {
116 return operator;
117 }
118
119
120
121
122
123
124 public final int size() {
125 if (arguments == null) {
126 return 0;
127 }
128 return arguments.size();
129 }
130
131
132
133
134
135
136 public final String getQedeq() {
137 if (isAtom()) {
138 return atom.getValue();
139 }
140 final StringBuffer buffer = new StringBuffer();
141 buffer.append(operator.getQedeq()).append('(');
142 if (operator.getQedeqArgument() != null) {
143 buffer.append(StringUtility.quote(operator.getQedeqArgument()));
144 }
145 for (int i = 0; i < arguments.size(); i++) {
146 if (i > 0 || operator.getQedeqArgument() != null) {
147 buffer.append(", ");
148 }
149 buffer.append(((Term)
150 arguments.get(i)).getQedeq());
151 }
152 buffer.append(')');
153 return buffer.toString();
154 }
155
156
157
158
159
160
161 public final String getQedeqXml() {
162 return getQedeqXml(0);
163 }
164
165
166
167
168
169
170
171 private final String getQedeqXml(final int level) {
172 if (isAtom()) {
173 return StringUtility.getSpaces(level * 2) + atom.getValue() + "\n";
174 }
175 final StringBuffer buffer = new StringBuffer();
176 buffer.append(StringUtility.getSpaces(level * 2));
177 buffer.append("<").append(operator.getQedeq());
178 if (operator.getQedeq().endsWith("VAR")) {
179 buffer.append(" id=" + quote(operator.getQedeqArgument()));
180 if (arguments == null || arguments.size() == 0) {
181 buffer.append(" />" + "\n");
182 return buffer.toString();
183 }
184 } else if (operator.getQedeq().endsWith("CON")) {
185 buffer.append(" ref=" + quote(operator.getQedeqArgument()));
186 if (arguments == null || arguments.size() == 0) {
187 buffer.append(" />" + "\n");
188 return buffer.toString();
189 }
190 }
191
192 buffer.append(">\n");
193 if (operator.getQedeqArgument() != null && !operator.getQedeq().endsWith("VAR")
194 && !operator.getQedeq().endsWith("CON")) {
195
196 Trace.fatal(CLASS, this, "getQedeqXml(int)", "operator argument is not null but: "
197 + operator.getQedeqArgument(), new IllegalArgumentException());
198 }
199 for (int i = 0; i < arguments.size(); i++) {
200 buffer.append(((Term)
201 arguments.get(i)).getQedeqXml(level + 1));
202 }
203 buffer.append(StringUtility.getSpaces(level * 2));
204 buffer.append("</").append(operator.getQedeq()).append(">\n");
205 return buffer.toString();
206 }
207
208
209
210
211
212
213
214 private String quote(final String text) {
215 return "\"" + StringUtility.replace(text, "\"", """) + "\"";
216 }
217
218 }