Search in sources :

Example 1 with Operation_Altguard

use of org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard in project titan.EclipsePlug-ins by eclipse.

the class Def_AltStatement_Writer method writeTestCaseAltStatement.

public void writeTestCaseAltStatement(Alt_Statement altStatement) {
    AltGuards currentAltGuards = altStatement.getAltGuards();
    String[] negativeConditions = new String[currentAltGuards.getNofAltguards()];
    String[] positiveConditions = new String[currentAltGuards.getNofAltguards()];
    // initialize values
    for (int i = 0; i < currentAltGuards.getNofAltguards(); i++) {
        AltGuard currentAltGuard = currentAltGuards.getAltguardByIndex(i);
        if (currentAltGuard instanceof Operation_Altguard) {
            Statement currentStatement = ((Operation_Altguard) currentAltGuard).getGuardStatement();
            if (currentStatement instanceof Receive_Port_Statement) {
                altReceiveCounter++;
                if (altGuardConditions.get(i) != null) {
                    // alt guard present && any port receive
                    if (altGuardPortReference.get(altReceiveCounter).equals("any port")) {
                        positiveConditions[i] = altGuardConditions.get(altReceiveCounter) + ".getValue()" + "&&(" + "anyPortReceive(true))";
                        negativeConditions[i] = altGuardConditions.get(altReceiveCounter) + ".getValue()" + "&&(" + "anyPortReceive(false))";
                    } else {
                        // alt guard present && normal port receive
                        positiveConditions[i] = altGuardConditions.get(altReceiveCounter) + ".getValue()" + "&&(" + altGuardPortReference.get(altReceiveCounter) + ".receive(" + altGuardReceiveValue.get(altReceiveCounter) + ",true)!=null)";
                        negativeConditions[i] = altGuardConditions.get(altReceiveCounter) + ".getValue()" + "&&(" + altGuardPortReference.get(altReceiveCounter) + ".receive(" + altGuardReceiveValue.get(altReceiveCounter) + ",false)!=null)";
                        if (altGuardReceiveValue.get(altReceiveCounter).startsWith("Templates")) {
                            altGuardReceiveType.set(altReceiveCounter, "Templates");
                        }
                    }
                } else {
                    // no alt guard && any port recieve
                    if (altGuardPortReference.get(altReceiveCounter).equals("any port")) {
                        positiveConditions[i] = "anyPortReceive(true)";
                        negativeConditions[i] = "anyPortReceive(false)";
                    } else {
                        // no alt guard && no receive parameter
                        if (altGuardReceiveType.get(altReceiveCounter).equals("noparam")) {
                            positiveConditions[i] = altGuardPortReference.get(altReceiveCounter) + ".receive(true)!=null";
                            negativeConditions[i] = altGuardPortReference.get(altReceiveCounter) + ".receive(false)!=null";
                        } else if (// no alt guard && typed port recieve
                        altGuardReceiveType.get(altReceiveCounter).equals("_TYPED_PARAM_")) {
                            negativeConditions[i] = altGuardPortReference.get(altReceiveCounter) + ".receive_" + altGuardReceiveValue.get(altReceiveCounter) + "(false)!=null";
                            if (altGuardReceiveAnyValValue.get(altReceiveCounter) != null) {
                                positiveConditions[i] = "(" + altGuardReceiveAnyValValue.get(altReceiveCounter) + "=" + altGuardPortReference.get(altReceiveCounter) + ".receive_" + altGuardReceiveValue.get(altReceiveCounter) + "(true))!=null";
                            } else {
                            }
                        } else {
                            // no alt guard && normal port recieve
                            positiveConditions[i] = altGuardPortReference.get(altReceiveCounter) + ".receive(" + altGuardReceiveValue.get(altReceiveCounter) + ",true)!=null";
                            negativeConditions[i] = altGuardPortReference.get(altReceiveCounter) + ".receive(" + altGuardReceiveValue.get(altReceiveCounter) + ",false)!=null";
                        }
                        if (altGuardReceiveValue.get(altReceiveCounter) != null && altGuardReceiveValue.get(altReceiveCounter).startsWith("Templates")) {
                            altGuardReceiveType.set(altReceiveCounter, "Templates");
                        }
                    }
                }
            }
            if (currentStatement instanceof Timeout_Statement) {
                timeOutCounter++;
                if (altGuardConditions.get(i) != null) {
                    positiveConditions[i] = altGuardConditions.get(i) + "&&(" + altGuardTimeout.get(timeOutCounter) + ".timeout()";
                    negativeConditions[i] = altGuardConditions.get(i) + "&&(!" + altGuardTimeout.get(i) + ".timeout()";
                } else {
                    positiveConditions[i] = altGuardTimeout.get(timeOutCounter) + ".timeout()";
                    negativeConditions[i] = "!" + altGuardTimeout.get(timeOutCounter) + ".timeout()";
                }
            }
        }
    }
    // write
    altString.append("rownum=" + altStatement.getLocation().getLine() + ";\r\n");
    altString.append("for(;;){" + "\r\n");
    altString.append("if(!(");
    for (int j = 0; j < negativeConditions.length; j++) {
        altString.append(negativeConditions[j]);
        if (j + 1 < negativeConditions.length) {
            altString.append("||");
        }
    }
    altString.append(")){" + "\r\n");
    // timer
    if (isThereAStartedTimer) {
        altString.append("long timeout = -1;" + "\r\n");
        altString.append("long newtimeout;" + "\r\n");
        altString.append("if(" + currentTimerName + ".running)if((newtimeout=(long)(" + currentTimerName + ".read().value*1000.0))<timeout || timeout == -1) timeout=newtimeout;" + "\r\n");
        altString.append("if(timeout>0) try{queue.poll(timeout,TimeUnit.MILLISECONDS);}catch(InterruptedException e){} " + "\r\n");
    } else {
        altString.append("try{queue.take();}catch(InterruptedException e){}" + "\r\n");
    }
    // endoftimer
    altString.append("}" + "\r\n");
    altString.append("this.lock();" + "\r\n");
    // ifwriter
    altReceiveCounter = -1;
    boolean isFirstIf = true;
    for (int i = 0; i < currentAltGuards.getNofAltguards(); i++) {
        AltGuard currentAltGuard = currentAltGuards.getAltguardByIndex(i);
        if (currentAltGuard instanceof Operation_Altguard) {
            Statement currentStatement = ((Operation_Altguard) currentAltGuard).getGuardStatement();
            StatementBlock currentStatementBlock = ((Operation_Altguard) currentAltGuard).getStatementBlock();
            if (isFirstIf) {
                altString.append("if(" + positiveConditions[i] + "){\r\n");
                isFirstIf = false;
            } else {
                altString.append("else if(" + positiveConditions[i] + "){\r\n");
            }
            if (currentStatement instanceof Receive_Port_Statement) {
                altReceiveCounter++;
                altString.append("rownum=" + currentStatement.getLocation().getLine() + ";\r\n");
                if (altGuardReceiveType.get(altReceiveCounter).equals("noparam")) {
                    altString.append("	TTCN3Logger.writeLog(compid, \"PORTEVENT\", sourcefilename, rownum, \"function\", \"" + nodeName + "\", \"RECEIVE event on port " + altGuardPortReference.get(altReceiveCounter) + "\", true);" + "\r\n");
                } else if (altGuardReceiveType.get(altReceiveCounter).equals("Templates")) {
                    String methodName = altGuardReceiveValue.get(altReceiveCounter);
                    if (methodName.endsWith("()") && methodName.startsWith("Templates.")) {
                        methodName = (String) methodName.subSequence(10, methodName.length() - 2);
                    }
                    altString.append("	TTCN3Logger.writeLog(compid, \"PORTEVENT\", sourcefilename, rownum, \"function\", \"" + nodeName + "\", \"RECEIVE event on port " + altGuardPortReference.get(altReceiveCounter) + ":\\n " + methodName + ":=\" + " + altGuardReceiveValue.get(altReceiveCounter) + ".toString(), true);" + "\r\n");
                } else if (altGuardReceiveType.get(altReceiveCounter).equals("any port")) {
                    altString.append("	TTCN3Logger.writeLog(compid, \"PORTEVENT\", sourcefilename, rownum, \"function\", \"" + nodeName + "\", \"RECEIVE event on port any port\", true);" + "\r\n");
                } else if (altGuardReceiveType.get(altReceiveCounter).equals("_TYPED_PARAM_")) {
                    altString.append("	TTCN3Logger.writeLog(compid, \"PORTEVENT\", sourcefilename, rownum, \"function\", \"" + nodeName + "\", \"RECEIVE event on port " + altGuardPortReference.get(altReceiveCounter) + ": type " + altGuardReceiveValue.get(altReceiveCounter) + "\", true);" + "\r\n");
                } else {
                    if (altGuardReceiveValue.get(altReceiveCounter) != null) {
                        altString.append("	TTCN3Logger.writeLog(compid, \"PORTEVENT\", sourcefilename, rownum, \"function\", \"" + nodeName + "\", \"RECEIVE event on port " + altGuardPortReference.get(altReceiveCounter) + ":\" + " + altGuardReceiveValue.get(altReceiveCounter) + ".toString(), true);" + "\r\n");
                    }
                }
            } else if (currentStatement instanceof Timeout_Statement) {
                altString.append("rownum=" + currentStatement.getLocation().getLine() + ";\r\n" + "TTCN3Logger.writeLog(compid, \"TIMEROP\", sourcefilename, rownum, \"function\", \"" + nodeName + "\", \"Timeout on timer " + currentTimerName + ".\", false);" + "\r\n");
            }
            boolean isThereARepeatStatement = false;
            for (int j = 0; j < currentStatementBlock.getSize(); j++) {
                Statement currentStatementBlockStatement = currentStatementBlock.getStatementByIndex(j);
                if (currentStatementBlockStatement instanceof Setverdict_Statement) {
                    Setverdict_Statement setVerdictStatement = (Setverdict_Statement) currentStatementBlockStatement;
                    String verdict = "";
                    if (setVerdictStatement.getVerdictValue() instanceof Verdict_Value) {
                        Verdict_Value verdictValue = (Verdict_Value) setVerdictStatement.getVerdictValue();
                        if (verdictValue.getValue().toString().equals("PASS")) {
                            verdict = "pass";
                        } else if (verdictValue.getValue().toString().equals("INCONC")) {
                            verdict = "inconc";
                        } else {
                            verdict = "fail";
                        }
                    }
                    altString.append("rownum=" + setVerdictStatement.getLocation().getLine() + ";\r\n");
                    altString.append("TTCN3Logger.writeLog(compid, \"VERDICTOP\", sourcefilename, rownum, \"function\", \"" + nodeName + "\", \"setverdict(" + verdict + "): \" + getVerdict() + \" -> " + verdict + "\", true);" + "\r\n");
                    altString.append("setVerdict(\"" + verdict + "\");" + "\r\n");
                }
                if (currentStatementBlockStatement instanceof Assignment_Statement) {
                    if (isFunction) {
                        functionParent.assignCounter++;
                        altString.append(functionParent.writeAssignmentStatement((Assignment_Statement) currentStatementBlockStatement));
                        String test = "";
                        test.toString();
                    } else if (isTestCase) {
                        testcaseParent.assignCounter++;
                        altString.append(testcaseParent.writeAssignmentStatement((Assignment_Statement) currentStatementBlockStatement));
                    }
                }
                if (currentStatementBlockStatement instanceof Send_Statement) {
                    if (isFunction) {
                        functionParent.sendCounter++;
                        altString.append(functionParent.writeSendStatement((Send_Statement) currentStatementBlockStatement));
                    } else if (isTestCase) {
                        testcaseParent.sendCounter++;
                        altString.append(testcaseParent.writeSendStatement((Send_Statement) currentStatementBlockStatement));
                    }
                }
                if (currentStatementBlockStatement instanceof Repeat_Statement) {
                    isThereARepeatStatement = true;
                }
            }
            if (isThereARepeatStatement) {
                isThereARepeatStatement = false;
            } else {
                altString.append("break;\r\n");
            }
            altString.append("}\r\n");
        }
    }
    altString.append("this.unlock();" + "\r\n");
    altString.append("}" + "\r\n");
}
Also used : AltGuard(org.eclipse.titan.designer.AST.TTCN3.statements.AltGuard) Receive_Port_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Receive_Port_Statement) Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Statement) Repeat_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Repeat_Statement) Timeout_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Timeout_Statement) Assignment_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Assignment_Statement) Setverdict_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Setverdict_Statement) Send_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Send_Statement) Alt_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Alt_Statement) Verdict_Value(org.eclipse.titan.designer.AST.TTCN3.values.Verdict_Value) AltGuards(org.eclipse.titan.designer.AST.TTCN3.statements.AltGuards) Operation_Altguard(org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard) Receive_Port_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Receive_Port_Statement) Assignment_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Assignment_Statement) Repeat_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Repeat_Statement) Send_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Send_Statement) Timeout_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Timeout_Statement) StatementBlock(org.eclipse.titan.designer.AST.TTCN3.statements.StatementBlock) Setverdict_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Setverdict_Statement)

Example 2 with Operation_Altguard

use of org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard in project titan.EclipsePlug-ins by eclipse.

the class TestCase_Function_Visit_Handler method leave.

public void leave(IVisitableNode node) {
    if (waitForTC || waitForFunction) {
        if (node instanceof Definition_Statement) {
            if (!waitForRecord) {
                nodeVarIsRecord.add(false);
            }
            waitForRecord = false;
            waitForValue = false;
            evaluateExpression();
        }
        if (node instanceof Undefined_LowerIdentifier_Value) {
            blockReferenceListing = false;
        }
        if ((node instanceof Assignment_Statement) || ((node instanceof If_Clause))) {
            waitForValue = false;
            // if assignment is a createExpression
            if (node instanceof Assignment_Statement) {
                if (((Assignment_Statement) node).getTemplate() instanceof SpecificValue_Template) {
                    SpecificValue_Template specValTemplate = (SpecificValue_Template) ((Assignment_Statement) node).getTemplate();
                    if (specValTemplate.getSpecificValue() instanceof ComponentCreateExpression) {
                        isCreate = true;
                    }
                }
            }
            evaluateExpression();
        }
        if (node instanceof SequenceOf_Value) {
            evaluateExpression();
        }
        if ((waitForFunction && (node instanceof Return_Statement)) || (waitForTC && (node instanceof ComponentCreateExpression)) || (node instanceof Send_Statement) || (node instanceof Receive_Port_Statement)) {
            evaluateExpression();
        }
        if (waitForTcStartValues && (node instanceof Identifier) && myASTVisitor.blockIdListing) {
            myASTVisitor.blockIdListing = false;
        }
        if (waitForTcStopValues && (node instanceof Identifier) && myASTVisitor.blockIdListing) {
            myASTVisitor.blockIdListing = false;
        }
        if (node instanceof Connect_Statement) {
            waitForTcConnectValues = false;
        }
        if (node instanceof Disconnect_Statement) {
            waitForTcDisconnectValues = false;
        }
        if (node instanceof Unknown_Start_Statement) {
            waitForTcStartValues = false;
        }
        if (node instanceof Unknown_Stop_Statement) {
            waitForTcStopValues = false;
        }
        if (node instanceof Definition_Statement) {
            waitForDefStatement = false;
            if (waitForValue) {
                nodeVarValues.add(null);
                nodeVarIsAValueReference.add(false);
                waitForValue = false;
            }
        }
        if (node instanceof Map_Statement) {
            waitForMapIdentifiers = false;
        }
        if (node instanceof ComponentCreateExpression) {
            // switch first two values
            testCaseCreateValues.add(testCaseCreateCharValues.get(0));
            testCaseCreateValues.add(testCaseCreateRefValues.get(0));
            for (int i = 1; i < testCaseCreateCharValues.size(); i++) {
                testCaseCreateValues.add(testCaseCreateCharValues.get(i));
            }
            testCaseCreateCharValues.clear();
            testCaseCreateRefValues.clear();
            waitForTcCreateValues = false;
        }
        if (node instanceof Assignment_Statement) {
            waitForAssignmentIdentifiers = false;
            waitForValue = true;
        }
        if (waitForTcIfCondition && (node instanceof If_Clause)) {
            waitForTcIfCondition = false;
        }
        if (waitForTcStartParameter && (node instanceof Unknown_Start_Statement)) {
            testCaseStartValueParameters.add(null);
        }
        if (waitForTcStopParameter && (node instanceof Unknown_Stop_Statement)) {
            testCaseStopValueParameters.add(null);
        }
        if (isAltGuards && (node instanceof Operation_Altguard)) {
            altGuardConditions.add(null);
        }
        if (node instanceof Operation_Altguard) {
            waitForAltStatements = false;
            isAltGuards = false;
            isReceiveValue = false;
        }
        handleAltCases(node);
        if (node instanceof Unknown_Start_Statement) {
            waitForUnknownStartStatement = false;
        }
        if (node instanceof Unknown_Stop_Statement) {
            waitForUnknownStopStatement = false;
        }
        if (node instanceof Receive_Port_Statement) {
            if (waitForReceiveParameter) {
                altGuardReceiveType.add("noparam");
            }
        // TODO checkAnyport = true;
        }
    }
    if (node instanceof Def_Testcase) {
        handleTestcase(node);
        waitForTC = false;
    }
    if (node instanceof Def_Function) {
        handleFunction(node);
        waitForTC = false;
    }
    if (waitForAssignmentIdentifiers && (node instanceof ArraySubReference)) {
        waitForAssignmentIdentifiers = false;
    }
}
Also used : Connect_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Connect_Statement) Definition_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Definition_Statement) Def_Testcase(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Testcase) SequenceOf_Value(org.eclipse.titan.designer.AST.TTCN3.values.SequenceOf_Value) ComponentCreateExpression(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ComponentCreateExpression) If_Clause(org.eclipse.titan.designer.AST.TTCN3.statements.If_Clause) Def_Function(org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Function) Operation_Altguard(org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard) ArraySubReference(org.eclipse.titan.designer.AST.ArraySubReference) Receive_Port_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Receive_Port_Statement) Assignment_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Assignment_Statement) SpecificValue_Template(org.eclipse.titan.designer.AST.TTCN3.templates.SpecificValue_Template) Identifier(org.eclipse.titan.designer.AST.Identifier) Unknown_Stop_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Unknown_Stop_Statement) Return_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Return_Statement) Send_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Send_Statement) Disconnect_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Disconnect_Statement) Unknown_Start_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Unknown_Start_Statement) Map_Statement(org.eclipse.titan.designer.AST.TTCN3.statements.Map_Statement) Undefined_LowerIdentifier_Value(org.eclipse.titan.designer.AST.TTCN3.values.Undefined_LowerIdentifier_Value)

Example 3 with Operation_Altguard

use of org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard in project titan.EclipsePlug-ins by eclipse.

the class AltGuards method generateCodeAlt.

/**
 * Generate code for an alt
 *
 * @param aData the structure to put imports into and get temporal variable names from.
 * @param source the source code generated
 */
public void generateCodeAlt(final JavaGenData aData, final StringBuilder source) {
    aData.addBuiltinTypeImport("TitanAlt_Status");
    boolean labelNeeded = hasRepeat;
    boolean hasElseBranch = false;
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        switch(altGuard.getType()) {
            case AG_OP:
                if (((Operation_Altguard) altGuard).getGuardStatement().canRepeat()) {
                    labelNeeded = true;
                }
                break;
            case AG_REF:
            case AG_INVOKE:
                labelNeeded = true;
                break;
            case AG_ELSE:
                hasElseBranch = true;
                break;
            default:
                ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
                return;
        }
        if (hasElseBranch) {
            break;
        }
    }
    // if there is no [else] branch the defaults may return ALT_REPEAT
    if (!hasElseBranch) {
        labelNeeded = true;
    }
    // opening bracket of the statement block
    label = aData.getTemporaryVariableName();
    if (labelNeeded) {
        source.append(label).append(":\n");
    }
    source.append("for ( ; ; ) {\n");
    // temporary variables used for caching of status codes
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        if (altGuard.getType().equals(altguard_type.AG_ELSE)) {
            break;
        }
        source.append(MessageFormat.format("TitanAlt_Status {0}_alt_flag_{1} = ", label, i));
        if (altGuard.getGuardExpression() == null) {
            source.append("TitanAlt_Status.ALT_MAYBE");
        } else {
            source.append("TitanAlt_Status.ALT_UNCHECKED");
        }
        source.append(";\n");
    }
    if (!hasElseBranch) {
        source.append(MessageFormat.format("TitanAlt_Status {0}_default_flag = TitanAlt_Status.ALT_MAYBE;\n", label));
    }
    // the first snapshot is taken in non-blocking mode
    aData.addCommonLibraryImport("TTCN_Snapshot");
    source.append("TTCN_Snapshot.takeNew(false);\n");
    // and opening infinite for() loop
    source.append("for ( ; ; ) {\n");
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        final altguard_type altGuardType = altGuard.getType();
        if (altGuardType.equals(altguard_type.AG_ELSE)) {
            source.append("TTCN_Snapshot.elseBranchReached();\n");
            StatementBlock block = altGuard.getStatementBlock();
            if (block.getSize() > 0) {
                source.append("{\n");
                // FIXME handle debugger
                block.generateCode(aData, source);
                source.append("}\n");
            }
            if (block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
                source.append("break;\n");
            }
            // do not generate code for further branches
            break;
        } else {
            final IValue guardExpression = altGuard.getGuardExpression();
            if (guardExpression != null) {
                source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_UNCHECKED) '{'\n", label, i));
                guardExpression.getLocation().update_location_object(aData, source);
                final ExpressionStruct expression = new ExpressionStruct();
                guardExpression.generateCodeExpression(aData, expression, true);
                source.append(expression.preamble);
                source.append(MessageFormat.format("if(TitanBoolean.getNative({0})) '{'\n", expression.expression));
                source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_MAYBE;\n", label, i));
                source.append("} else {\n");
                source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_NO;\n", label, i));
                source.append("}\n");
                source.append("}\n");
            }
            source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_MAYBE) '{'\n", label, i));
            boolean canRepeat = false;
            final ExpressionStruct expression = new ExpressionStruct();
            expression.expression.append(MessageFormat.format("{0}_alt_flag_{1} = ", label, i));
            switch(altGuardType) {
                case AG_OP:
                    {
                        // the guard operation is a receiving statement
                        final Statement statement = ((Operation_Altguard) altGuard).getGuardStatement();
                        altGuard.getLocation().update_location_object(aData, source);
                        statement.generateCodeExpression(aData, expression);
                        canRepeat = statement.canRepeat();
                    }
                    break;
                case AG_REF:
                    {
                        // the guard operation is an altstep instance
                        final Reference reference = ((Referenced_Altguard) altGuard).getGuardReference();
                        altGuard.getLocation().update_location_object(aData, source);
                        final Assignment altstep = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
                        expression.expression.append(MessageFormat.format("{0}_instance(", altstep.getGenNameFromScope(aData, source, myScope, "")));
                        final ISubReference subreference = reference.getSubreferences().get(0);
                        ((ParameterisedSubReference) subreference).getActualParameters().generateCodeAlias(aData, expression);
                        source.append(')');
                        canRepeat = true;
                    }
                    break;
                case AG_INVOKE:
                    {
                        // the guard operation is an altstep invocation
                        altGuard.getLocation().update_location_object(aData, source);
                        ((Invoke_Altguard) altGuard).generateCodeInvokeInstance(aData, expression);
                        canRepeat = true;
                    }
                    break;
                default:
                    ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
                    return;
            }
            expression.mergeExpression(source);
            if (canRepeat) {
                source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_REPEAT) continue {2};\n", label, i, label));
            }
            if (altGuardType.equals(altguard_type.AG_REF) || altGuardType.equals(altguard_type.AG_INVOKE)) {
                source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_BREAK) break;\n", label, i));
            }
            // execution of statement block if the guard was successful
            source.append(MessageFormat.format("if ({0}_alt_flag_{1} == TitanAlt_Status.ALT_YES) ", label, i));
            final StatementBlock block = altGuard.getStatementBlock();
            if (block != null && block.getSize() > 0) {
                source.append("{\n");
                // TODO handle debugger
                block.generateCode(aData, source);
                if (!ReturnStatus_type.RS_YES.equals(block.hasReturn(CompilationTimeStamp.getBaseTimestamp()))) {
                    source.append("break;\n");
                }
                source.append("}\n");
            } else {
                source.append("break;\n");
            }
            source.append("}\n");
        }
    }
    if (!hasElseBranch) {
        aData.addCommonLibraryImport("TTCN_Default");
        source.append(MessageFormat.format("if ({0}_default_flag == TitanAlt_Status.ALT_MAYBE) '{'\n", label));
        source.append(MessageFormat.format("{0}_default_flag = TTCN_Default.tryAltsteps();\n", label));
        source.append(MessageFormat.format("if ({0}_default_flag == TitanAlt_Status.ALT_YES || {0}_default_flag == TitanAlt_Status.ALT_BREAK) '{'\n", label));
        source.append("break;\n");
        source.append(MessageFormat.format("} else if({0}_default_flag == TitanAlt_Status.ALT_REPEAT) '{'\n", label));
        source.append(MessageFormat.format("continue {0};\n", label));
        source.append("}\n");
        source.append("}\n");
        getLocation().update_location_object(aData, source);
        // error handling and taking the next snapshot in blocking mode
        source.append("if ( ");
        for (int i = 0; i < altGuards.size(); i++) {
            source.append(MessageFormat.format("{0}_alt_flag_{1} == TitanAlt_Status.ALT_NO &&", label, i));
        }
        source.append(MessageFormat.format("{0}_default_flag == TitanAlt_Status.ALT_NO) '{'\n", label));
        source.append("throw new TtcnError(\"None of the branches can be chosen in the alt statement");
        // TODO translate_string
        if (location != null && location.getFile() != null) {
            source.append(MessageFormat.format("in file {0} at line {1}", location.getFile().getName(), location.getLine()));
        }
        source.append("\");\n");
        source.append("}\n");
        source.append("TTCN_Snapshot.takeNew(true);\n");
    }
    source.append("}\n");
    source.append("break;\n");
    source.append("}\n");
}
Also used : Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) IValue(org.eclipse.titan.designer.AST.IValue) AltGuard.altguard_type(org.eclipse.titan.designer.AST.TTCN3.statements.AltGuard.altguard_type) Reference(org.eclipse.titan.designer.AST.Reference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Example 4 with Operation_Altguard

use of org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard in project titan.EclipsePlug-ins by eclipse.

the class AltGuards method generateCodeCallBody.

/**
 * Generate code for an altstep (as the body of a call statement)
 *
 * @param aData the structure to put imports into and get temporal variable names from.
 * @param source the source code generated
 * @param tempId temporary id used as prefix of local variables
 * @param inInterleave is it used in interleave?
 */
public void generateCodeCallBody(final JavaGenData aData, final StringBuilder source, final String tempId, final boolean inInterleave) {
    if (hasRepeat) {
        source.append(MessageFormat.format("{0}:", tempId));
    }
    // temporary variables used for caching of status codes
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        source.append(MessageFormat.format("TitanAlt_Status {0}_alt_flag_{1} = ", tempId, i));
        if (altGuard.getGuardExpression() == null) {
            source.append("TitanAlt_Status.ALT_MAYBE");
        } else {
            source.append("TitanAlt_Status.ALT_UNCHECKED");
        }
        source.append(";\n");
    }
    getLocation().update_location_object(aData, source);
    // the first snapshot is taken in non-blocking mode
    // and opening infinite for() loop
    // the first snapshot is taken in non-blocking mode
    aData.addCommonLibraryImport("TTCN_Snapshot");
    source.append("TTCN_Snapshot.takeNew(false);\n");
    // and opening infinite for() loop
    source.append("for ( ; ; ) {\n");
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        if (!(altGuard instanceof Operation_Altguard)) {
            // FATAL ERROR
            continue;
        }
        final IValue guardExpression = altGuard.getGuardExpression();
        if (guardExpression != null) {
            source.append(MessageFormat.format("if ( {0}_alt_flag_{1} == TitanAlt_Status.ALT_UNCHECKED) '{'\n", tempId, i));
            guardExpression.getLocation().update_location_object(aData, source);
            final ExpressionStruct expression = new ExpressionStruct();
            guardExpression.generateCodeExpression(aData, expression, true);
            source.append(expression.preamble);
            source.append(MessageFormat.format("if (TitanBoolean.getNative({0})) '{'\n", expression.expression));
            source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_MAYBE;\n", tempId, i));
            source.append("} else {\n");
            source.append(MessageFormat.format("{0}_alt_flag_{1} = TitanAlt_Status.ALT_NO;\n", tempId, i));
            source.append("}\n");
            source.append(expression.postamble);
            source.append("}\n");
        }
        // evaluation of guard operation
        source.append(MessageFormat.format("if ( {0}_alt_flag_{1} == TitanAlt_Status.ALT_MAYBE) '{'\n", tempId, i));
        final ExpressionStruct expression = new ExpressionStruct();
        expression.expression.append(MessageFormat.format("{0}_alt_flag_{1} = ", tempId, i));
        final Statement statement = ((Operation_Altguard) altGuard).getGuardStatement();
        statement.getLocation().update_location_object(aData, source);
        statement.generateCodeExpression(aData, expression);
        expression.mergeExpression(source);
        // execution of statement block if the guard was successful
        source.append(MessageFormat.format("if ( {0}_alt_flag_{1} == TitanAlt_Status.ALT_YES) '{'\n", tempId, i));
        final StatementBlock block = ((Operation_Altguard) altGuard).getStatementBlock();
        if (inInterleave) {
            if (block != null && block.getSize() > 0) {
                if (block.hasReceivingStatement(0)) {
                    source.append(MessageFormat.format("continue {0}_branch{1};\n", tempId, i));
                } else {
                    source.append("{\n");
                    block.generateCode(aData, source);
                    source.append(MessageFormat.format("continue {0}_end;\n", tempId));
                    source.append("}\n");
                }
            } else {
                source.append(MessageFormat.format("continue {0}_end;\n", tempId));
            }
        } else {
            if (block != null && block.getSize() > 0) {
                source.append("{\n");
                block.generateCode(aData, source);
                if (block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != StatementBlock.ReturnStatus_type.RS_YES) {
                    source.append("break;\n");
                }
                source.append("}\n");
            }
        }
        source.append("}\n");
        source.append("}\n");
    }
    getLocation().update_location_object(aData, source);
    // error handling and taking the next snapshot in blocking mode
    source.append("if (");
    for (int i = 0; i < altGuards.size(); i++) {
        if (i > 0) {
            source.append(" && ");
        }
        source.append(MessageFormat.format("{0}_alt_flag_{1} == TitanAlt_Status.ALT_NO", tempId, i));
    }
    source.append(") {\n");
    source.append("throw new TtcnError(\"None of the branches can be chosen in the response and exception handling part of call statement in file");
    // TODO translate_string
    if (location != null && location.getFile() != null) {
        source.append(MessageFormat.format("in file {0} at line {1}", location.getFile().getName(), location.getLine()));
    }
    source.append("\");\n");
    source.append("}\n");
    source.append("TTCN_Snapshot.takeNew(true);\n");
    source.append("}\n");
}
Also used : IValue(org.eclipse.titan.designer.AST.IValue) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Example 5 with Operation_Altguard

use of org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard in project titan.EclipsePlug-ins by eclipse.

the class AltGuards method generateCodeAltstep.

/**
 * Generate code for an altstep
 *
 * @param aData the structure to put imports into and get temporal variable names from.
 * @param source the source code generated
 */
public void generateCodeAltstep(final JavaGenData aData, final StringBuilder source) {
    final boolean hasElse = hasElse();
    if (!hasElse) {
        source.append("TitanAlt_Status returnValue = TitanAlt_Status.ALT_NO;\n");
    }
    for (int i = 0; i < altGuards.size(); i++) {
        final AltGuard altGuard = altGuards.get(i);
        final altguard_type altGuardType = altGuard.getType();
        if (altGuardType.equals(altguard_type.AG_ELSE)) {
            source.append("TTCN_Snapshot.elseBranchReached();\n");
            final StatementBlock block = altGuard.getStatementBlock();
            if (block.getSize() > 0) {
                source.append("{\n");
                // TODO debugger
                block.generateCode(aData, source);
                source.append("}\n");
            }
            if (block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
                source.append("return TitanAlt_Status.ALT_YES;\n");
            }
            break;
        } else {
            final AtomicInteger blockCount = new AtomicInteger(0);
            final IValue guardExpression = altGuard.getGuardExpression();
            if (guardExpression != null) {
                guardExpression.getLocation().update_location_object(aData, source);
                guardExpression.generateCodeTmp(aData, source, "if (", blockCount);
                source.append(") {\n");
                blockCount.incrementAndGet();
            }
            boolean canRepeat = false;
            final ExpressionStruct expression = new ExpressionStruct();
            switch(altGuardType) {
                case AG_OP:
                    {
                        final Statement statement = ((Operation_Altguard) altGuard).getGuardStatement();
                        altGuard.getLocation().update_location_object(aData, source);
                        statement.generateCodeExpression(aData, expression);
                        canRepeat = statement.canRepeat();
                    }
                    break;
                case AG_REF:
                    {
                        // the guard operation is an altstep instance
                        final Reference reference = ((Referenced_Altguard) altGuard).getGuardReference();
                        altGuard.getLocation().update_location_object(aData, source);
                        final Assignment altstep = reference.getRefdAssignment(CompilationTimeStamp.getBaseTimestamp(), false);
                        expression.expression.append(MessageFormat.format("{0}_instance(", altstep.getGenNameFromScope(aData, source, myScope, "")));
                        final ISubReference subreference = reference.getSubreferences().get(0);
                        ((ParameterisedSubReference) subreference).getActualParameters().generateCodeAlias(aData, expression);
                        source.append(')');
                        canRepeat = true;
                    }
                    break;
                case AG_INVOKE:
                    {
                        // the guard operation is an altstep invocation
                        altGuard.getLocation().update_location_object(aData, source);
                        ((Invoke_Altguard) altGuard).generateCodeInvokeInstance(aData, expression);
                        canRepeat = true;
                    }
                    break;
                default:
                    ErrorReporter.INTERNAL_ERROR("unknown altguard type encountered `" + getFullName() + "''");
                    return;
            }
            if (expression.preamble.length() > 0 || expression.postamble.length() > 0) {
                if (blockCount.get() == 0) {
                    source.append("{\n");
                    blockCount.set(blockCount.get() + 1);
                }
                final String tempId = aData.getTemporaryVariableName();
                source.append(MessageFormat.format("TitanAlt_Status {0};\n", tempId));
                source.append("{\n");
                source.append(expression.preamble);
                source.append(MessageFormat.format("{0}.assign({1});\n", tempId, expression.expression));
                source.append(expression.postamble);
                source.append("}\n");
                source.append(MessageFormat.format("switch ({0}) '{'\n", tempId));
            } else {
                source.append(MessageFormat.format("switch ({0}) '{'\n", expression.expression));
            }
            source.append("case ALT_YES:\n");
            final StatementBlock block = altGuard.getStatementBlock();
            if (block != null && block.getSize() > 0) {
                source.append("{\n");
                // TODO handle debugger
                block.generateCode(aData, source);
                source.append("}\n");
            }
            if (block == null || block.hasReturn(CompilationTimeStamp.getBaseTimestamp()) != ReturnStatus_type.RS_YES) {
                source.append("return TitanAlt_Status.ALT_YES;\n");
            }
            if (canRepeat) {
                source.append("case ALT_REPEAT:\n");
                source.append("return TitanAlt_Status.ALT_REPEAT;\n");
            }
            if (altGuardType == altguard_type.AG_REF || altGuardType == altguard_type.AG_INVOKE) {
                source.append("case ALT_BREAK:\n");
                source.append("return TitanAlt_Status.ALT_BREAK;\n");
            }
            if (!hasElse) {
                source.append("case ALT_MAYBE:\n");
                source.append("returnValue = TitanAlt_Status.ALT_MAYBE;\n");
            }
            source.append("default:\n");
            source.append("break;\n");
            source.append("}\n");
            // closing statement blocks
            for (int j = 0; j < blockCount.get(); j++) {
                source.append("}\n");
            }
        }
    }
    if (!hasElse) {
        source.append("return returnValue;\n");
    }
}
Also used : AltGuard.altguard_type(org.eclipse.titan.designer.AST.TTCN3.statements.AltGuard.altguard_type) Reference(org.eclipse.titan.designer.AST.Reference) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) Assignment(org.eclipse.titan.designer.AST.Assignment) ISubReference(org.eclipse.titan.designer.AST.ISubReference) ParameterisedSubReference(org.eclipse.titan.designer.AST.ParameterisedSubReference) IValue(org.eclipse.titan.designer.AST.IValue) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpressionStruct(org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)

Aggregations

Operation_Altguard (org.eclipse.titan.designer.AST.TTCN3.statements.Operation_Altguard)4 Receive_Port_Statement (org.eclipse.titan.designer.AST.TTCN3.statements.Receive_Port_Statement)4 IValue (org.eclipse.titan.designer.AST.IValue)3 Reference (org.eclipse.titan.designer.AST.Reference)3 Statement (org.eclipse.titan.designer.AST.TTCN3.statements.Statement)3 ExpressionStruct (org.eclipse.titan.designer.AST.TTCN3.values.expressions.ExpressionStruct)3 Assignment (org.eclipse.titan.designer.AST.Assignment)2 ISubReference (org.eclipse.titan.designer.AST.ISubReference)2 ParameterisedSubReference (org.eclipse.titan.designer.AST.ParameterisedSubReference)2 AltGuard.altguard_type (org.eclipse.titan.designer.AST.TTCN3.statements.AltGuard.altguard_type)2 Assignment_Statement (org.eclipse.titan.designer.AST.TTCN3.statements.Assignment_Statement)2 Send_Statement (org.eclipse.titan.designer.AST.TTCN3.statements.Send_Statement)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ArraySubReference (org.eclipse.titan.designer.AST.ArraySubReference)1 IType (org.eclipse.titan.designer.AST.IType)1 Identifier (org.eclipse.titan.designer.AST.Identifier)1 Def_Function (org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Function)1 Def_Testcase (org.eclipse.titan.designer.AST.TTCN3.definitions.Def_Testcase)1 AltGuard (org.eclipse.titan.designer.AST.TTCN3.statements.AltGuard)1 AltGuards (org.eclipse.titan.designer.AST.TTCN3.statements.AltGuards)1