示例#1
0
        private void MkCreateSpecMachineMethod(string machineName)
        {
            var fields = new List <SyntaxNode>();

            //create machine
            fields.Add(
                generator.LocalDeclarationStatement(
                    generator.IdentifierName("var"),
                    "machine",
                    generator.ObjectCreationExpression(
                        generator.IdentifierName(machineName),
                        new List <SyntaxNode> {
                generator.IdentifierName("application")
            })));

            //log
            fields.Add(CSharpHelper.MkCSharpTrace($"<CreateLog> Created spec Machine {machineName}"));

            fields.AddRange(allMachines[machineName].observesEvents.Select(x => CSharpHelper.MkCSharpInvocationExpression(CSharpHelper.MkCSharpDot("machine", "observes", "Add"), GetEventVar(x))));

            var prtvalueNull =
                (ExpressionSyntax)generator.MemberAccessExpression(IdentifierName("PrtEventValue"), IdentifierName("@null"));

            //execute the entry function of the machine
            fields.Add(
                CSharpHelper.MkCSharpInvocationExpression(
                    CSharpHelper.MkCSharpDot("machine", "PrtEnqueueEvent"),
                    prtvalueNull,
                    prtvalueNull,
                    LiteralExpression(SyntaxKind.NullLiteralExpression)));
            //return machine;
            fields.Add(generator.ReturnStatement(generator.IdentifierName("machine")));

            var        methodPars         = new[] { generator.ParameterDeclaration("application", generator.IdentifierName("StateImpl")) };
            SyntaxNode makeCreateSpecDecl = generator.MethodDeclaration(
                $"CreateSpecMachine_{machineName}",
                methodPars,
                null,
                IdentifierName("PrtSpecMachine"),
                Accessibility.Public,
                DeclarationModifiers.Static,
                fields);

            members.Add(makeCreateSpecDecl);
        }
示例#2
0
        private void MkCreateRealMachineMethod(string machineName)
        {
            var fields = new List <SyntaxNode>();

            //create machine
            MachineInfo machineInfo = allMachines[machineName];

            //There are three cases:
            //- default (no constraint on queue size): maxQueueSizeAssumed == false; maxQueueSize = default (10?)
            //- assume <maxQueueSize>: maxQueueSize > 0, maxQueueSizeAssumed == true;
            //- assert <maxQueueSize>: maxQueueSize > 0, maxQueueSizeAssumed == false;
            if (machineInfo.maxQueueSize > 0)
            {
                fields.Add(
                    generator.LocalDeclarationStatement(
                        generator.IdentifierName("var"),
                        "machine",
                        generator.ObjectCreationExpression(
                            generator.IdentifierName(machineName),
                            new List <SyntaxNode>
                {
                    generator.IdentifierName("application"),
                    generator.LiteralExpression(machineInfo.maxQueueSize),
                    generator.LiteralExpression(machineInfo.maxQueueSizeAssumed)
                })));
            }
            else
            {
                fields.Add(
                    generator.LocalDeclarationStatement(
                        generator.IdentifierName("var"),
                        "machine",
                        generator.ObjectCreationExpression(
                            generator.IdentifierName(machineName),
                            new List <SyntaxNode>
                {
                    generator.IdentifierName("application"),
                    MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        IdentifierName("PrtImplMachine"),
                        IdentifierName("DefaultMaxBufferSize")),
                    generator.LiteralExpression(machineInfo.maxQueueSizeAssumed)
                })));
            }

            // log
            fields.Add(
                CSharpHelper.MkCSharpTrace(
                    $"<CreateLog> Created Machine {machineName}-{{0}}",
                    CSharpHelper.MkCSharpDot("machine", "instanceNumber")));

            //initialize the permission set for self
            if (allMachines[machineName].receiveSet == null)
            {
                fields.Add(
                    CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                        CSharpHelper.MkCSharpDot(IdentifierName("machine"), "self", "permissions"),
                        LiteralExpression(SyntaxKind.NullLiteralExpression)));
            }
            else
            {
                fields.AddRange(
                    allMachines[machineName].receiveSet.Select(
                        ev => CSharpHelper.MkCSharpInvocationExpression(
                            CSharpHelper.MkCSharpDot(IdentifierName("machine"), "self", "permissions", "Add"),
                            GetEventVar(ev))));
            }

            //initialize the send set
            if (allMachines[machineName].sendsSet == null)
            {
                fields.Add(
                    CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                        CSharpHelper.MkCSharpDot(IdentifierName("machine"), "sends"),
                        LiteralExpression(SyntaxKind.NullLiteralExpression)));
            }
            else
            {
                fields.AddRange(
                    allMachines[machineName].sendsSet.Select(
                        ev => CSharpHelper.MkCSharpInvocationExpression(
                            CSharpHelper.MkCSharpDot(IdentifierName("machine"), "sends", "Add"),
                            GetEventVar(ev))));
            }

            //machine.currentPayload = payload;
            fields.Add(
                CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                    CSharpHelper.MkCSharpDot("machine", "currentPayload"),
                    IdentifierName("payload")));

            //return machine;
            fields.Add(generator.ReturnStatement(generator.IdentifierName("machine")));

            var methodPars = new[]
            {
                generator.ParameterDeclaration("application", generator.IdentifierName("StateImpl")),
                generator.ParameterDeclaration("payload", generator.IdentifierName("PrtValue"))
            };
            SyntaxNode makeCreateMachineDecl = generator.MethodDeclaration(
                $"CreateMachine_{machineName}",
                methodPars,
                null, // type parameters
                generator.IdentifierName("PrtImplMachine"),
                Accessibility.Public,
                DeclarationModifiers.Static,
                fields);

            members.Add(makeCreateMachineDecl);
        }