use of org.nhindirect.policy.Opcode in project nhin-d by DirectProject.
the class StackMachine_evaluateTest method testEvaluate_notEqualsSameStringValues_assertFalse.
public void testEvaluate_notEqualsSameStringValues_assertFalse() throws Exception {
final Vector<Opcode> stuffToProcess = new Vector<Opcode>();
stuffToProcess.add(new StackMachineEntry(PolicyValueFactory.getInstance("12345")));
stuffToProcess.add(new StackMachineEntry(PolicyValueFactory.getInstance("12345")));
stuffToProcess.add(new StackMachineEntry(PolicyOperator.NOT_EQUALS));
final StackMachine stMachine = new StackMachine();
assertFalse(stMachine.evaluate(stuffToProcess));
}
use of org.nhindirect.policy.Opcode in project nhin-d by DirectProject.
the class StackMachine method evaluate.
/**
* {@inheritDoc}
*/
@Override
public Boolean evaluate(Vector<Opcode> opcodes) throws PolicyProcessException {
Boolean retVal;
for (Opcode opcode : opcodes) {
// the vector for this machine type should only use StackMachineEntry codes
final StackMachineEntry entry = StackMachineEntry.class.cast(opcode);
switch(entry.getEntryType()) {
case VALUE:
{
machineStack.push(entry.getValue());
break;
}
case OPERATOR:
{
PolicyOperatorExecutor<?, ?> executor = null;
switch(entry.getOperator().getParamsType()) {
case BINARY:
{
if (machineStack.size() < 2)
throw new IllegalStateException("Stack machine must have at least two pushed operands for " + entry.getOperator().getOperatorText() + " operator");
executor = createOperatorExecutor(entry.getOperator(), machineStack.pop(), machineStack.pop());
break;
}
case UNARY:
{
if (machineStack.size() < 1)
throw new IllegalStateException("Stack machine must have at least one pushed operand for " + entry.getOperator().getOperatorText() + " operator");
executor = createOperatorExecutor(entry.getOperator(), machineStack.pop());
break;
}
}
machineStack.push(executor.execute());
break;
}
}
}
if (machineStack.isEmpty() || machineStack.size() > 1)
throw new IllegalStateException("Stack machine is either empty or has remaining parameters to be processed." + "\r\n\tFinal stack size: " + machineStack.size());
final PolicyValue<?> finalValue = machineStack.pop();
try {
retVal = Boolean.class.cast(finalValue.getPolicyValue());
} catch (ClassCastException e) {
throw new IllegalStateException("Final machine value must be a boolean litteral" + "\r\n\tFinal value type: " + finalValue.getPolicyValue().getClass() + "\r\n\tFinal value value:" + finalValue.getPolicyValue().toString(), e);
}
return retVal;
}
Aggregations