/*******************************************************************************
* Copyright (c) 2010, 2014 Willink Transformations and others.
* 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:
* E.D.Willink - Initial API and implementation
*******************************************************************************/
package org.eclipse.ocl.pivot.library.logical;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.ocl.pivot.OCLExpression;
import org.eclipse.ocl.pivot.OperationCallExp;
import org.eclipse.ocl.pivot.evaluation.Executor;
import org.eclipse.ocl.pivot.ids.TypeId;
import org.eclipse.ocl.pivot.library.AbstractSimpleBinaryOperation;
import org.eclipse.ocl.pivot.messages.PivotMessages;
import org.eclipse.ocl.pivot.values.InvalidValueException;
/**
* OrOperation realises the or() library operation.
*/
public class BooleanOrOperation extends AbstractSimpleBinaryOperation
{
public static final @NonNull BooleanOrOperation INSTANCE = new BooleanOrOperation();
@Override
public @Nullable Object dispatch(@NonNull Executor executor, @NonNull OperationCallExp callExp, @Nullable Object sourceValue) {
if (sourceValue == Boolean.TRUE) {
return TRUE_VALUE;
}
List<? extends OCLExpression> arguments = callExp.getOwnedArguments();
OCLExpression argument0 = arguments.get(0);
assert argument0 != null;
Object firstArgument;
try {
firstArgument = executor.evaluate(argument0);
}
catch (InvalidValueException e) {
firstArgument = e; // FIXME ?? propagate part of environment
}
return evaluate(sourceValue, firstArgument);
}
@Override
public @Nullable Boolean evaluate(@Nullable Object left, @Nullable Object right) {
if ((left == Boolean.TRUE) || (right == Boolean.TRUE)) {
return TRUE_VALUE;
}
else if ((left == Boolean.FALSE) && (right == Boolean.FALSE)) {
return FALSE_VALUE;
}
if (left instanceof InvalidValueException) {
throw (InvalidValueException)left;
}
else if (right instanceof InvalidValueException) {
throw (InvalidValueException)right;
}
else if ((left == null) || (right == null)) {
return null;
}
else if (!(left instanceof Boolean)) {
throw new InvalidValueException(PivotMessages.TypedValueRequired, TypeId.BOOLEAN_NAME, getTypeName(left));
}
else {
throw new InvalidValueException(PivotMessages.TypedValueRequired, TypeId.BOOLEAN_NAME, getTypeName(right));
}
}
}