Search in sources :

Example 46 with Opcode

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));
}
Also used : StackMachine(org.nhindirect.policy.impl.machine.StackMachine) StackMachineEntry(org.nhindirect.policy.impl.machine.StackMachineEntry) Opcode(org.nhindirect.policy.Opcode) Vector(java.util.Vector)

Example 47 with Opcode

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

Aggregations

Opcode (org.nhindirect.policy.Opcode)47 Vector (java.util.Vector)43 StackMachine (org.nhindirect.policy.impl.machine.StackMachine)43 StackMachineEntry (org.nhindirect.policy.impl.machine.StackMachineEntry)40 PolicyExpression (org.nhindirect.policy.PolicyExpression)7 X509Certificate (java.security.cert.X509Certificate)6 LiteralPolicyExpression (org.nhindirect.policy.LiteralPolicyExpression)5 OperationPolicyExpression (org.nhindirect.policy.OperationPolicyExpression)5 StackMachineCompiler (org.nhindirect.policy.impl.machine.StackMachineCompiler)5 Compiler (org.nhindirect.policy.Compiler)3 ExecutionEngine (org.nhindirect.policy.ExecutionEngine)3 ExtendedKeyUsageExtensionField (org.nhindirect.policy.x509.ExtendedKeyUsageExtensionField)3 KeyUsageExtensionField (org.nhindirect.policy.x509.KeyUsageExtensionField)3 File (java.io.File)1 InputStream (java.io.InputStream)1 PolicyOperatorExecutor (org.nhindirect.policy.PolicyOperatorExecutor)1