Project

General

Profile

root / trunk / compiler / ooasCompiler / src / org / momut / ooas / visitors / analysis / OoaPrintActionSequencesVisitor.java @ 7

1
/**
2
  *
3
  *                      OOAS Compiler
4
  *
5
  *       Copyright 2015, AIT Austrian Institute of Technology.
6
  * This code is based on the C# Version of the OOAS Compiler, which is
7
  * copyright 2015 by the Institute of Software Technology, Graz University
8
  * of Technology with portions copyright by the AIT Austrian Institute of
9
  * Technology. All rights reserved.
10
  *
11
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
12
  *
13
  * If you modify the file please update the list of contributors below to in-
14
  * clude your name. Please also stick to the coding convention of using TABs
15
  * to do the basic (block-level) indentation and spaces for anything after
16
  * that. (Enable the display of special chars and it should be pretty obvious
17
  * what this means.) Also, remove all trailing whitespace.
18
  *
19
  * Contributors:
20
  *               Willibald Krenn (AIT)
21
  *               Stephan Zimmerer (AIT)
22
  *               Markus Demetz (AIT)
23
  *               Christoph Czurda (AIT)
24
  *
25
  */
26

    
27

    
28
package org.momut.ooas.visitors.analysis;
29

    
30
import java.util.ArrayList;
31
import java.util.Collections;
32
import java.util.IdentityHashMap;
33
import java.util.Map.Entry;
34

    
35
import org.momut.ooas.ast.IScope;
36
import org.momut.ooas.ast.expressions.IdentifierExpression;
37
import org.momut.ooas.ast.identifiers.FunctionIdentifier;
38
import org.momut.ooas.ast.identifiers.Identifier;
39
import org.momut.ooas.ast.identifiers.IdentifierKind;
40
import org.momut.ooas.ast.identifiers.MainModule;
41
import org.momut.ooas.ast.statements.Call;
42
import org.momut.ooas.ast.statements.SeqBlock;
43
import org.momut.ooas.ast.statements.Statement;
44
import org.momut.ooas.ast.statements.StatementKind;
45
import org.momut.ooas.ast.types.FunctionType;
46
import org.momut.ooas.ast.types.OoActionSystemType;
47
import org.momut.ooas.ast.types.FunctionType.FunctionTypeEnum;
48
import org.momut.ooas.parser.ParserMessage;
49
import org.momut.ooas.parser.ParserState;
50
import org.momut.ooas.parser.ParserWarning;
51
import org.momut.ooas.visitors.OoaCompleteAstTraversalVisitor;
52
import org.momut.ooas.visitors.OoaPrintVisitor;
53

    
54
public class OoaPrintActionSequencesVisitor extends OoaCompleteAstTraversalVisitor {
55

    
56
        private final IdentityHashMap<String, Integer> m_sequences = new IdentityHashMap<>();
57
        private boolean m_warned = false;
58

    
59
        @Override
60
        public void visit(SeqBlock seqBlock) {
61
                super.visit(seqBlock);
62

    
63
                boolean isFunction = false;
64
                IScope scope = seqBlock.GetParentScope();
65
                if (scope == null && !m_warned) {
66
                        m_ParserState.AddWarningMessage(new ParserWarning("", 0, 0, "Sequence analysis will be incomplete since the parent scope of a seq-block could not be resolved."));
67
                        m_warned = true;
68
                }
69
                while (!(scope instanceof OoActionSystemType) && !isFunction) {
70
                        isFunction = scope instanceof FunctionIdentifier;
71
                        scope = scope.GetParentScope();
72
                }
73
                if (isFunction)
74
                        return;
75

    
76
                int index = 0;
77
                int next = 0;
78
                final ArrayList<String> m_calls = new ArrayList<>();
79
                for (final Statement s: seqBlock.statements()) {
80
                        // get a list of call statements
81
                        if ( s.kind()== StatementKind.MethodCall
82
                             && index == next )
83
                        {
84
                                final OoaPrintVisitor customFormatter = new OoaPrintVisitor(null){
85
                                        @Override
86
                                        public void visit(IdentifierExpression identifierExpression) {
87
                                                final Identifier id = identifierExpression.identifier();
88
                                                if (id.kind() == IdentifierKind.NamedActionIdentifier) {
89
                                                        final FunctionType ft = (FunctionType) id.type();
90
                                                        if (ft.functionType() == FunctionTypeEnum.Observable)
91
                                                                output.Append("obs ");
92
                                                        else if (ft.functionType() == FunctionTypeEnum.Controllable)
93
                                                                output.Append("ctr ");
94
                                                }
95
                                                output.Append(identifierExpression.identifier().tokenText());
96
                                        }
97
                                };
98
                                final Call c = (Call) s;
99
                                m_calls.add( c.callExpression().toString(customFormatter));
100
                                next++;
101
                        }
102
                        index++;
103
                }
104
                if (m_calls.size() > 1) {
105
                        final String seq = m_calls.toString().intern();
106
                        if (!m_sequences.containsKey(seq))
107
                                m_sequences.put(seq, 0);
108
                        m_sequences.put(seq, m_sequences.get(seq) + 1);
109
                }
110
        }
111

    
112
        @Override
113
        public void visit(MainModule mainModule) {
114
                // we only care about the instantiated names...
115
                // if you want both, then just call super here..
116
                final boolean haveInstance = mainModule.instance() != null;
117
                if (haveInstance)
118
                        VisitSub(mainModule.instance(), mainModule);
119

    
120
                final StringBuilder b = new StringBuilder();
121
                b.append("List of sequential actions in model:");
122
                b.append(System.lineSeparator());
123
                final ArrayList<String> output = new ArrayList<>();
124
                for (final Entry<String, Integer>e: m_sequences.entrySet())
125
                        output.add(String.format("\t%s: %s", e.getValue(), e.getKey()));
126
                Collections.sort(output);
127
                for (final String s:output)
128
                        b.append(s + System.lineSeparator());
129
                m_ParserState.AddMessage(new ParserMessage("",0,0, b.toString()));
130
//                System.out.println(b.toString());
131
        }
132

    
133

    
134
        public OoaPrintActionSequencesVisitor(ParserState aState) {
135
                super(aState);
136
        }
137
}