AssertionStatement.java

package edu.udel.cis.vsl.tass.model.impl.statement;

import java.util.Collection;

import edu.udel.cis.vsl.tass.model.IF.ModelFactoryIF;
import edu.udel.cis.vsl.tass.model.IF.SyntaxException;
import edu.udel.cis.vsl.tass.model.IF.expression.ExpressionIF;
import edu.udel.cis.vsl.tass.model.IF.location.AssertionLocationIF;
import edu.udel.cis.vsl.tass.model.IF.statement.AssertionStatementIF;
import edu.udel.cis.vsl.tass.model.IF.type.TypeIF.TypeKind;
import edu.udel.cis.vsl.tass.model.IF.variable.SharedVariableIF;
import edu.udel.cis.vsl.tass.model.IF.variable.VariableIF;

public class AssertionStatement extends Statement implements
		AssertionStatementIF {

	private ExpressionIF assertion;
	private boolean accuracyFlag;
	private String message = "";

	public AssertionStatement(ModelFactoryIF factory,
			AssertionLocationIF sourceLocation, ExpressionIF assertion)
			throws SyntaxException {
		super(factory, sourceLocation, StatementKind.ASSERTION);
		if (assertion == null)
			throw new NullPointerException("Null assertion");
		if (assertion.type().kind() != TypeKind.BOOLEAN)
			throw new SyntaxException(assertion,
					"Type of assertion expression is not boolean");
		factory.checkScope(assertion, sourceLocation.scope());
		this.assertion = assertion;
	}

	public AssertionStatement(ModelFactoryIF factory,
			AssertionLocationIF sourceLocation, ExpressionIF assertion,
			String message) throws SyntaxException {
		super(factory, sourceLocation, StatementKind.ASSERTION);
		if (assertion == null)
			throw new NullPointerException("Null assertion");
		if (assertion.type().kind() != TypeKind.BOOLEAN)
			throw new SyntaxException(assertion,
					"Type of assertion expression is not boolean");
		factory.checkScope(assertion, sourceLocation.scope());
		this.message = message;
		this.assertion = assertion;
	}

	public ExpressionIF assertion() {
		return assertion;
	}

	public String toString() {
		return "assert " + assertion + "; " + message;
	}

	public String message() {
		return message;
	}

	public void complete() throws SyntaxException {
		Collection<SharedVariableIF> shared = process().model().scope()
				.properSharedVariables();

		super.complete();
		isLocal = true;
		for (VariableIF variable : assertion.freeVariables()) {
			if (variable instanceof SharedVariableIF
					&& shared.contains((SharedVariableIF) variable)) {
				isLocal = false;
				break;
			}
		}
	}

	public void setAccuracyFlag(boolean value) {
		accuracyFlag = value;
	}

	public boolean involvesAccuracy() {
		return accuracyFlag;
	}
}