Package: IdentifierPatternCollector
IdentifierPatternCollector
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
IdentifierPatternCollector() |
|
|
|
|
|
||||||||||||||||||||
caseAIdentifierPatternIR(AIdentifierPatternIR) |
|
|
|
|
|
||||||||||||||||||||
findOccurences() |
|
|
|
|
|
||||||||||||||||||||
proceed(INode) |
|
|
|
|
|
||||||||||||||||||||
setTopNode(INode) |
|
|
|
|
|
Coverage
1: package org.overture.codegen.traces;
2:
3: import java.util.HashSet;
4: import java.util.LinkedList;
5: import java.util.List;
6: import java.util.Set;
7:
8: import org.apache.log4j.Logger;
9: import org.overture.codegen.ir.INode;
10: import org.overture.codegen.ir.analysis.AnalysisException;
11: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
12: import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
13:
14: public class IdentifierPatternCollector extends DepthFirstAnalysisAdaptor
15: {
16:         private INode topNode;
17:         private List<AIdentifierPatternIR> idOccurences;
18:
19:         private Logger log = Logger.getLogger(this.getClass().getName());
20:
21:         public IdentifierPatternCollector()
22:         {
23:                 this.topNode = null;
24:                 this.idOccurences = null;
25:         }
26:
27:         public void setTopNode(INode topNode)
28:         {
29:                 this.topNode = topNode;
30:         }
31:
32:         public List<AIdentifierPatternIR> findOccurences()
33:         {
34:                 idOccurences = new LinkedList<AIdentifierPatternIR>();
35:
36:                 try
37:                 {
38:                         topNode.apply(this);
39:                 } catch (AnalysisException e)
40:                 {
41:                         log.error("Could not find identifier pattern occurences for node: "
42:                                         + topNode);
43:                         e.printStackTrace();
44:                 }
45:
46:                 return idOccurences;
47:         }
48:
49:         @Override
50:         public void caseAIdentifierPatternIR(AIdentifierPatternIR node)
51:                         throws AnalysisException
52:         {
53:•                if (proceed(node))
54:                 {
55:                         idOccurences.add(node);
56:                 }
57:         }
58:
59:         private boolean proceed(INode node)
60:         {
61:•                if (topNode == null)
62:                 {
63:                         return false;
64:                 }
65:
66:•                if (node == topNode)
67:                 {
68:                         return true;
69:                 }
70:
71:                 INode parent = node.parent();
72:
73:                 Set<INode> visited = new HashSet<INode>();
74:
75:•                while (parent != null && !visited.contains(parent)
76:                                 && this.topNode != parent)
77:                 {
78:                         parent = parent.parent();
79:                         visited.add(parent);
80:                 }
81:
82:•                return this.topNode == parent;
83:         }
84: }