blob: 812b18626b2028c9f79f782cdf7a1b000dd4d44c [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Akos Horvath, Gergely Varro and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Akos Horvath, Gergely Varro - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal;
import org.eclipse.viatra2.gtasm.patternmatcher.exceptions.PatternMatcherRuntimeException;
import org.eclipse.viatra2.gtasm.patternmatcher.impl.patternmatcher.internal.operation.SearchPlanOperation;
import org.eclipse.viatra2.logger.Logger;
/**
* A SearchPlan is an unmodifiable array of
* SearchPlanOperations. PatternSignatures are supposed
* to be fixed
*/
public class SearchPlan {
private SearchPlanOperation[] operations;
private int currentOperation;
public SearchPlan(SearchPlanOperation[] operations){
this.operations = operations;
this.currentOperation = -1;
}
private void init(MatchingFrame frame)
throws PatternMatcherRuntimeException {
if (currentOperation == -1) {
currentOperation++;
operations[currentOperation].preprocess(frame);
} else if (currentOperation == operations.length - 1) {
currentOperation--;
} else {
String[] context = {frame.getPattern().getParent().getPattern().getName()};
throw new PatternMatcherRuntimeException(
PatternMatcherErrorStrings.INTERNAL_SEARCHPLAN_INIT_FAILED
,context
,frame.getPattern().getParent().getPattern());
}
}
/**
* Calculates the cost of the search plan.
*/
public double cost() {
/* default generated stub */;
return 0.0;
}
public boolean execute(MatchingFrame frame)
throws PatternMatcherRuntimeException {
int upperBound = operations.length - 1;
//try {
init(frame);
while (currentOperation >= 0 && currentOperation < upperBound) {
if (operations[currentOperation].execute(frame)) {
currentOperation++;
operations[currentOperation].preprocess(frame);
} else {
operations[currentOperation].postprocess(frame);
currentOperation--;
}
}
//TODO: additional stack trace information can be added if needed at this point (LS search plan execution)
//} catch (PatternMatcherRuntimeException e) {
//e.addNewStackElement(operations[currentOperation].getErrorfulElement(frame));
// throw e;
//}
return (currentOperation == upperBound);
}
public void printDebugInformation(Logger logger) {
for (int i = 0; i < operations.length; i++) {
logger.debug("[" + i + "]\t" + operations[i].toString());
}
}
}