1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/* TreeGenerator.java
* =========================================================================
* This file is part of the GrInvIn project - http://www.grinvin.org
*
* Copyright (C) 2005-2008 Universiteit Gent
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* A copy of the GNU General Public License can be found in the file
* LICENSE.txt provided with the source distribution of this program (see
* the META-INF directory in the source jar). This license can also be
* found on the GNU website at http://www.gnu.org/licenses/gpl.html.
*
* If you did not receive a copy of the GNU General Public License along
* with this program, contact the lead developer, or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.grinvin.conjecture.engine.apengine;
/**
*
* @author adpeeter
*/
public class TreeGenerator<T extends BinaryTree> {
//
protected T workingTree = null;
//
protected boolean completeWorkingTree = false;
//
protected int currentUnaryOperators;
//
protected int currentBinaryOperators;
//
protected TreeGeneratorStateStack stateStack;
//
protected TreeFactory<T> treeFactory;
public TreeGenerator(TreeFactory<T> treeFactory) {
this(0, 0, treeFactory);
}
//
public TreeGenerator(int unaryOperators, int binaryOperators, TreeFactory<T> treeFactory) {
stateStack = new TreeGeneratorStateStack();
stateStack.pushNewDepthState(unaryOperators, binaryOperators);
workingTree = null;
this.treeFactory = treeFactory;
}
/**
* Private class to represent the stack of the {@link TreeGenerator}. Handling this
* manually improves computation speed.
*/
protected static class TreeGeneratorStateStack extends StateStack<TreeGeneratorState> {
//
public TreeGeneratorState emptyState() {
return new TreeGeneratorState(null, 0, 0, 0);
}
//
public TreeGeneratorState[] createArray(int size) {
return new TreeGeneratorState[size];
}
//
private final void push(StateType type, int a, int b, int c) {
if(top >= size) {
super.extend();
}
TreeGeneratorState state = peek();
top++;
state.type = type;
state.a = a;
state.b = b;
state.c = c;
}
/**
* Push a new state that upon execution will remove the node with the given coordinates.
* @param depth depth in the tree
* @param leftrightpos position relative to the {@code parent}: {@code 0} for left, {@code 1} for right
* @param parent parent of the node to be removed
*/
public final void pushRemoveState(int depth, int leftrightpos, int parent) {
push(StateType.REMOVE, depth, leftrightpos, parent);
}
//
public void pushRecurseState(int depth, int pos, int endpos) {
push(StateType.RECURSE, depth, pos, endpos);
}
/**
* Push a new state that upon execution will start working with new unary and binary count.
*/
public void pushNewDepthState(int unaryCount, int binaryCount) {
push(StateType.NEWDEPTH, 0, unaryCount, binaryCount);
}
}
//
private enum StateType {
RECURSE,
REMOVE,
NEWDEPTH
}
//
private static class TreeGeneratorState {
private TreeGeneratorState(StateType type, int a, int b, int c) {
this.type = type;
this.a = a;
this.b = b;
this.c = c;
}
public StateType type;
public int a;
public int b;
public int c;
//
private int getRemoveDepth() {
assert StateType.REMOVE == this.type;
return this.a;
}
//
private int getRemovePos() {
assert StateType.REMOVE == this.type;
return this.b;
}
//
private int getRemoveParent() {
assert StateType.REMOVE == this.type;
return this.c;
}
//
private int getRecurseDepth() {
assert StateType.RECURSE == this.type;
return this.a;
}
//
private int getRecursePos() {
assert StateType.RECURSE == this.type;
return this.b;
}
//
private int getRecurseEndPos() {
assert StateType.RECURSE == this.type;
return this.c;
}
//
private int getDepthUnaryCount() {
assert StateType.NEWDEPTH == this.type;
return this.b;
}
//
private int getDepthBinaryCount() {
assert StateType.NEWDEPTH == this.type;
return this.c;
}
}
/**
* Return the next {@link LabeledBinaryTree}.
*/
public T nextTree() {
completeWorkingTree = false;
while(!completeWorkingTree) {
extend();
}
return workingTree;
}
/**
* Push a new depthstate on the stack with the next number of unary and binary operators.
* The operators are added as follows:
* <table>
* <tr><th>unary</th><th<binary</th></tr>
* <tr><td>0</td><td>0</td></tr>
* <tr><td>1</td><td>0</td></tr>
* <tr><td>0</td><td>1</td></tr>
* <tr><td>2</td><td>0</td></tr>
* <tr><td>1</td><td>1</td></tr>
* <tr><td>3</td><td>0</td></tr>
* <tr><td>0</td><td>2</td></tr>
* </table>
*/
protected void pushNextOperatorCount() {
if (currentBinaryOperators > 0) {
stateStack.pushNewDepthState(currentUnaryOperators + 2, currentBinaryOperators - 1);
} else {
if ((currentUnaryOperators % 2) > 0) {
stateStack.pushNewDepthState(0, (currentUnaryOperators + 1) / 2);
} else {
stateStack.pushNewDepthState(1, currentUnaryOperators / 2);
}
}
}
/**
* Try to extend the workingTree.
*/
protected void extend() {
TreeGeneratorState state = stateStack.pop();
switch (state.type) {
case RECURSE:
int depth = state.getRecurseDepth();
int pos = state.getRecursePos();
int endpos = state.getRecurseEndPos();
if(pos < endpos) {
stateStack.pushRecurseState(depth, pos + 1, endpos);
int parent = workingTree.extendOn(depth, pos);
if(parent != -1) {
if(correct(workingTree)) {
if(complete(workingTree)) {
stateStack.pushRemoveState(depth, pos % 2, parent);
// we have a finished working tree
completeWorkingTree = true;
} else {
stateStack.pushRemoveState(depth, pos % 2, parent);
stateStack.pushRecurseState(depth, pos + 1, endpos);
stateStack.pushRecurseState(depth + 1, 0, workingTree.nodesonlevel(depth) * 2);
}
} else {
workingTree.removeOn(depth, pos % 2, parent);
}
}
}
break;
case REMOVE:
workingTree.removeOn(state.getRemoveDepth(), state.getRemovePos(), state.getRemoveParent());
break;
case NEWDEPTH:
currentUnaryOperators = state.getDepthUnaryCount();
currentBinaryOperators = state.getDepthBinaryCount();
workingTree = treeFactory.createTree(currentUnaryOperators, currentBinaryOperators);
pushNextOperatorCount();
if (complete(workingTree)) {
// we have a finished working tree
completeWorkingTree = true;
} else {
stateStack.pushRecurseState(1,0,2);
}
break;
default:
assert false : "Unexpected case: " + state.type;
}
}
/**
* Is the given {@link LabeledBinaryTree} complete? A tree is complete if its
* amount of unary and binary operators matches the current unary and
* binary operator count respectively.
*/
public boolean complete(T tree) {
return (tree.getUnaryCount() == currentUnaryOperators ) && (tree.getBinaryCount() == currentBinaryOperators);
}
/**
* Is the given {@link LabeledBinaryTree} correct? A tree is correct if it is still
* possible to extend it to a complete tree.
*/
public boolean correct(T tree) {
int unaryCount = tree.getUnaryCount();
int binaryCount = tree.getBinaryCount();
return (unaryCount <= currentUnaryOperators + 1) && (binaryCount <= currentBinaryOperators) && ((unaryCount + binaryCount) <= (currentUnaryOperators + currentBinaryOperators));
}
}
|