Ejemplo n.º 1
0
        private void MkEvents()
        {
            var          evDeclarations           = new List <SyntaxNode>();
            var          eventInitializationStmts = new List <StatementSyntax>();
            const string eventsClassName          = "Events";

            foreach (string evName in exportedEvents)
            {
                //add declaration
                evDeclarations.Add(
                    CSharpHelper.MkCSharpFieldDeclaration(
                        IdentifierName("PrtEventValue"),
                        EventName(evName),
                        Token(SyntaxKind.PublicKeyword),
                        Token(SyntaxKind.StaticKeyword)));

                //add initialization
                SyntaxNode payloadType = typeContext.PTypeToCSharpExpr(allEvents[evName].payloadType);
                SyntaxNode maxInstances;
                if (allEvents[evName].maxInstances == -1)
                {
                    var s = "PrtEvent.DefaultMaxInstances";
                    maxInstances = IdentifierName(s);
                }
                else
                {
                    maxInstances = CSharpHelper.MkCSharpNumericLiteralExpression(allEvents[evName].maxInstances);
                }

                SyntaxNode doAssume = allEvents[evName].maxInstancesAssumed ? CSharpHelper.MkCSharpTrueLiteralExpression() : CSharpHelper.MkCSharpFalseLiteralExpression();
                SyntaxNode lhs      = IdentifierName(EventName(evName));
                SyntaxNode rhs      = CSharpHelper.MkCSharpObjectCreationExpression(
                    IdentifierName("PrtEventValue"),
                    CSharpHelper.MkCSharpObjectCreationExpression(
                        IdentifierName("PrtEvent"),
                        CSharpHelper.MkCSharpStringLiteralExpression(evName),
                        payloadType,
                        maxInstances,
                        doAssume));

                eventInitializationStmts.Add(CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(lhs, rhs));
            }

            string staticMethodName = "Events_" + Path.GetFileNameWithoutExtension(cSharpFileName.ToLower());
            MethodDeclarationSyntax staticInitializer =
                MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(staticMethodName))
                .WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword)))
                .WithBody(Block(eventInitializationStmts)).NormalizeWhitespace();

            evDeclarations.Add(staticInitializer);

            SyntaxNode eventsClass = generator.ClassDeclaration(
                eventsClassName,
                null,
                Accessibility.Public,
                DeclarationModifiers.Partial,
                members: evDeclarations);

            members.Add(eventsClass);
        }
Ejemplo n.º 2
0
        private SyntaxNode MkStaticAppConstructor(string testName)
        {
            var stmtList = new List <StatementSyntax>();

            //Initialize types and events
            var nodes = dependsOn.Keys.Select(s => s.ToLower()).ToList();

            foreach (var files in dependsOn.Values)
            {
                foreach (var f in files)
                {
                    if (!nodes.Contains(f))
                    {
                        nodes.Add(f);
                    }
                }
            }
            var edges = new List <Tuple <string, string> >();

            foreach (var file in dependsOn)
            {
                foreach (var dep in file.Value)
                {
                    if (file.Key != dep)
                    {
                        edges.Add(new Tuple <string, string>(dep.ToLower(), file.Key.ToLower()));
                    }
                }
            }
            List <string> topoOrder = TopologicalSortFiles <string>(nodes, edges);

            //make invocations to the initialization of all types
            foreach (var fName in topoOrder)
            {
                stmtList.Add(
                    SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression(
                                                          SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Types"), SyntaxFactory.IdentifierName("Types_" + fName))
                                                          )));
            }

            //make invocation to the initialization of all events
            foreach (var fName in topoOrder)
            {
                stmtList.Add(
                    SyntaxFactory.ExpressionStatement(CSharpHelper.MkCSharpInvocationExpression(
                                                          SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Events"), SyntaxFactory.IdentifierName("Events_" + fName))
                                                          )));
            }

            //Initialize all the maps

            //safe map
            foreach (var sm in allTests[testName].isSafeMap)
            {
                var safeadd = CSharpHelper.MkCSharpInvocationExpression(
                    CSharpHelper.MkCSharpDot("isSafeMap", "Add"),
                    CSharpHelper.MkCSharpStringLiteralExpression(sm.Key),
                    sm.Value ? CSharpHelper.MkCSharpTrueLiteralExpression() : CSharpHelper.MkCSharpFalseLiteralExpression()
                    );
                stmtList.Add(SyntaxFactory.ExpressionStatement(safeadd));
            }

            //rename map
            foreach (var rename in allTests[testName].renameMap)
            {
                var renameadd = CSharpHelper.MkCSharpInvocationExpression(
                    CSharpHelper.MkCSharpDot("renameMap", "Add"),
                    CSharpHelper.MkCSharpStringLiteralExpression(rename.Key),
                    CSharpHelper.MkCSharpStringLiteralExpression(rename.Value)
                    );
                stmtList.Add(SyntaxFactory.ExpressionStatement(renameadd));
            }
            //create map
            var listOfSpecMachineNames = allTests[testName].renameMap.Where(item => allTests[testName].specMachineMap.ContainsKey(item.Key)).Select(item => item.Value).Distinct();
            var listOfMachineNames     = allTests[testName].renameMap.Where(item => !allTests[testName].specMachineMap.ContainsKey(item.Key)).Select(item => item.Value).Distinct();

            foreach (var specMachine in listOfSpecMachineNames)
            {
                var createadd = CSharpHelper.MkCSharpInvocationExpression(
                    CSharpHelper.MkCSharpDot("createSpecMap", "Add"),
                    CSharpHelper.MkCSharpStringLiteralExpression(specMachine),
                    SyntaxFactory.IdentifierName(string.Format("CreateSpecMachine_{0}", specMachine)));
                stmtList.Add(SyntaxFactory.ExpressionStatement(createadd));
            }
            foreach (var machine in listOfMachineNames)
            {
                //its a machine
                var createadd = CSharpHelper.MkCSharpInvocationExpression(
                    CSharpHelper.MkCSharpDot("createMachineMap", "Add"),
                    CSharpHelper.MkCSharpStringLiteralExpression(machine),
                    SyntaxFactory.IdentifierName(string.Format("CreateMachine_{0}", machine))
                    );
                stmtList.Add(SyntaxFactory.ExpressionStatement(createadd));
            }
            //interface map
            foreach (var it in allTests[testName].interfaceMap)
            {
                var eventsParams = new SeparatedSyntaxList <ExpressionSyntax>();
                foreach (var ev in it.Value)
                {
                    eventsParams = eventsParams.Add(GetEventVar(ev));
                }
                var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                    SyntaxFactory.ElementAccessExpression(
                        SyntaxFactory.IdentifierName("interfaceMap"),
                        SyntaxFactory.BracketedArgumentList(SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(SyntaxFactory.Argument(CSharpHelper.MkCSharpStringLiteralExpression(it.Key))))
                        ),
                    SyntaxFactory.ObjectCreationExpression(
                        SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("PrtEventValue")))),
                        SyntaxFactory.ArgumentList(),
                        SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, eventsParams))
                    );
                stmtList.Add(initStmt);
            }

            //send actions
            {
                var eventsParams = new SeparatedSyntaxList <ExpressionSyntax>();
                foreach (var ev in allTests[testName].sendActions)
                {
                    eventsParams = eventsParams.Add(CSharpHelper.MkCSharpStringLiteralExpression(ev));
                }
                var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                    SyntaxFactory.IdentifierName("visibleEvents"),
                    SyntaxFactory.ObjectCreationExpression(
                        SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("string")))),
                        SyntaxFactory.ArgumentList(),
                        SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, eventsParams))
                    );
                stmtList.Add(initStmt);
            }

            //interface actions
            {
                var interfaceParams = new SeparatedSyntaxList <ExpressionSyntax>();
                foreach (var it in allTests[testName].createActions)
                {
                    interfaceParams = interfaceParams.Add(CSharpHelper.MkCSharpStringLiteralExpression(it));
                }
                var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                    SyntaxFactory.IdentifierName("visibleInterfaces"),
                    SyntaxFactory.ObjectCreationExpression(
                        SyntaxFactory.GenericName(SyntaxFactory.Identifier("List"), SyntaxFactory.TypeArgumentList(SyntaxFactory.SingletonSeparatedList <TypeSyntax>(SyntaxFactory.IdentifierName("string")))),
                        SyntaxFactory.ArgumentList(),
                        SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, interfaceParams))
                    );
                stmtList.Add(initStmt);
            }

            //initialize the interface types
            foreach (var it in allTests[testName].interfaceMap)
            {
                var initStmt = CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                    SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.ParenthesizedExpression(
                                                             CSharpHelper.MkCSharpCastExpression("PrtInterfaceType",
                                                                                                 SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("Types"), SyntaxFactory.IdentifierName(String.Format("type_{0}", it.Key))))),
                                                         SyntaxFactory.IdentifierName("permissions")),
                    SyntaxFactory.ElementAccessExpression(
                        SyntaxFactory.IdentifierName("interfaceMap"),
                        SyntaxFactory.BracketedArgumentList(SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(SyntaxFactory.Argument(CSharpHelper.MkCSharpStringLiteralExpression(it.Key))))
                        )
                    );
                stmtList.Add(initStmt);
            }

            //spec machine map
            foreach (var specMachine in allTests[testName].specMachineMap)
            {
                SeparatedSyntaxList <ExpressionSyntax> machines = new SeparatedSyntaxList <ExpressionSyntax>();
                machines = machines.AddRange(specMachine.Value.Select(x => CSharpHelper.MkCSharpStringLiteralExpression(x)));

                var addstmt =
                    CSharpHelper.MkCSharpInvocationExpression(
                        CSharpHelper.MkCSharpDot("specMachineMap", "Add"),
                        CSharpHelper.MkCSharpStringLiteralExpression(specMachine.Key),
                        SyntaxFactory.ObjectCreationExpression(
                            CSharpHelper.MkCSharpGenericListType(SyntaxFactory.IdentifierName("string")),
                            SyntaxFactory.ArgumentList(),
                            SyntaxFactory.InitializerExpression(SyntaxKind.CollectionInitializerExpression, machines))
                        );
                stmtList.Add(SyntaxFactory.ExpressionStatement(addstmt));
            }
            //link map
            //declare temp variable
            var dictStringString =
                SyntaxFactory.GenericName(
                    SyntaxFactory.Identifier("Dictionary"),
                    SyntaxFactory.TypeArgumentList(SyntaxFactory.SeparatedList <TypeSyntax>(
                                                       new SyntaxNodeOrToken[] {
                SyntaxFactory.PredefinedType(
                    SyntaxFactory.Token(SyntaxKind.StringKeyword)),
                SyntaxFactory.Token(SyntaxKind.CommaToken),
                SyntaxFactory.PredefinedType(
                    SyntaxFactory.Token(SyntaxKind.StringKeyword))
            }))
                    );

            stmtList.Add(
                SyntaxFactory.LocalDeclarationStatement(
                    SyntaxFactory.VariableDeclaration(
                        dictStringString,
                        SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                            SyntaxFactory.VariableDeclarator(
                                SyntaxFactory.Identifier("_temp"))
                            )
                        )
                    )
                );
            foreach (var creatorM in allTests[testName].linkMap)
            {
                var tempInit =
                    CSharpHelper.MkCSharpSimpleAssignmentExpressionStatement(
                        SyntaxFactory.IdentifierName("_temp"),
                        CSharpHelper.MkCSharpObjectCreationExpression(dictStringString)
                        );
                stmtList.Add(tempInit);
                foreach (var linkmapitem in creatorM.Value)
                {
                    stmtList.Add(
                        SyntaxFactory.ExpressionStatement(
                            CSharpHelper.MkCSharpInvocationExpression(
                                CSharpHelper.MkCSharpDot("_temp", "Add"),
                                CSharpHelper.MkCSharpStringLiteralExpression(linkmapitem.Key),
                                CSharpHelper.MkCSharpStringLiteralExpression(linkmapitem.Value)
                                ))
                        );
                }

                var addLinkItem = SyntaxFactory.ExpressionStatement(
                    CSharpHelper.MkCSharpInvocationExpression(
                        CSharpHelper.MkCSharpDot("linkMap", "Add"),
                        CSharpHelper.MkCSharpStringLiteralExpression(creatorM.Key),
                        SyntaxFactory.IdentifierName("_temp"))
                    );
                stmtList.Add(addLinkItem);
            }



            var staticConstrutor = SyntaxFactory.ConstructorDeclaration(
                SyntaxFactory.Identifier("Application"))
                                   .WithModifiers(
                SyntaxFactory.TokenList(
                    SyntaxFactory.Token(SyntaxKind.StaticKeyword)))
                                   .WithBody(SyntaxFactory.Block(stmtList))
                                   .NormalizeWhitespace();

            return(staticConstrutor);
        }