source: trunk/t3q/templates/AbstractVisitor.st @ 9

Last change on this file since 9 was 7, checked in by phdmakk, 15 years ago
File size: 4.2 KB
RevLine 
[7]1//This class has been generated automatically. Do not modify it by hand!
2package org.etsi.t3q.visitor;
3import java.util.Iterator;
4import java.util.LinkedList;
5import java.util.Queue;
6import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST;
7import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes;
8//import de.ugoe.cs.swe.t3simexec.exceptions.TTCN3BehaviorException;
9import org.etsi.t3q.exceptions.TTCN3BehaviorException;
10
11public abstract class AbstractVisitor {
12
13    public int visitedNodeCount;
14
15    $nodeType:VisitMethod(); separator="\n"$
16
17    public ContinueStatus visit(LocationAST node) throws TTCN3BehaviorException {
18        visitedNodeCount++;
19        ContinueStatus continueStatus;
20        switch(node.getType()) {
21            $nodeType:VisitCase(); separator="\n"$
22        }
23        return new ContinueStatus();
24    }
25
26    public abstract void init();
27    public abstract void finish();
28   
29    public void acceptDFS(LocationAST node) throws TTCN3BehaviorException {
30        init();
31        searchASTDepthFirst(node);
32        finish();
33    }
34
35    public void acceptBFS(LocationAST node) throws TTCN3BehaviorException {
36        init();
37        searchASTBreadthFirst(node);
38        finish();
39    }
40
41    protected ContinueStatus searchASTDepthFirst(LocationAST node) throws TTCN3BehaviorException {
42        if (node == null)
43            return ContinueStatus.getInstance(true, true);
44
45        LocationAST next = node;
46        while (next != null) {
47            ContinueStatus continueStatus = visit(next);
48            if (!continueStatus.continueSearch)
49                return ContinueStatus.getInstance(false, false);
50            if (continueStatus.goDeeper)
51                searchASTDepthFirst(next.getFirstChild());
52            next = next.getNextSibling();
53        }
54        return ContinueStatus.getInstance(true, true);
55    }
56
57    protected ContinueStatus searchASTBreadthFirst(LocationAST node) throws TTCN3BehaviorException {
58        if (node == null)
59            return ContinueStatus.getInstance(true, true);
60       
61        Queue<LocationAST> open = new LinkedList<LocationAST>();
62        open.add(node);
63        searchASTBreadthFirst(open);
64        return ContinueStatus.getInstance(true, true);
65    }
66
67    private ContinueStatus searchASTBreadthFirst(Queue<LocationAST> nodes) throws TTCN3BehaviorException {
68        Queue<LocationAST> open = new LinkedList<LocationAST>();
69        Iterator<LocationAST> it = nodes.iterator();
70        while (it.hasNext()) {
71            LocationAST item = it.next();
72            while (item != null) {
73                ContinueStatus continueStatus = visit(item);
74                if (!continueStatus.continueSearch)
75                    return ContinueStatus.getInstance(false, false);
76                if ( (item.getFirstChild() != null) && (continueStatus.goDeeper) )
77                    open.add(item.getFirstChild());
78                item = item.getNextSibling();
79            }
80        }
81        if (open.size() > 0)
82            searchASTBreadthFirst(open);
83        return ContinueStatus.getInstance(true, true);
84    }
85
86    static class ContinueStatus {
87        public boolean continueSearch = true;
88        public boolean goDeeper = true;
89        private static ContinueStatus truetrue = new ContinueStatus(true, true);
90        private static ContinueStatus truefalse = new ContinueStatus(true, false);
91        private static ContinueStatus falsetrue = new ContinueStatus(false, true);
92        private static ContinueStatus falsefalse = new ContinueStatus(false, false);
93
94        private ContinueStatus() {
95            this.continueSearch = true;
96            this.goDeeper = true;
97        }
98
99        private ContinueStatus(boolean continueSearch, boolean goDeeper) {
100            this.continueSearch = continueSearch;
101            this.goDeeper = goDeeper;
102        }
103
104        public static ContinueStatus getInstance(boolean continueSearch, boolean goDeeper) {
105            if (continueSearch && goDeeper) {
106                return truetrue;
107            } else if (!continueSearch && goDeeper) {
108                return falsetrue;
109            } else if (continueSearch && !goDeeper) {
110                return truefalse;
111            } else {
112                return falsefalse;
113            }
114        }
115    }
116   
117
118}
Note: See TracBrowser for help on using the repository browser.