Search in sources :

Example 1 with PolicyOperatorExecutor

use of org.nhindirect.policy.PolicyOperatorExecutor 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;
}
Also used : Opcode(org.nhindirect.policy.Opcode) PolicyOperatorExecutor(org.nhindirect.policy.PolicyOperatorExecutor)

Aggregations

Opcode (org.nhindirect.policy.Opcode)1 PolicyOperatorExecutor (org.nhindirect.policy.PolicyOperatorExecutor)1