Method: TraceAstUtility()

1: /*
2: * #%~
3: * Combinatorial Testing
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.plugins.combinatorialtesting.views;
23:
24: import java.util.HashMap;
25: import java.util.HashSet;
26: import java.util.LinkedList;
27: import java.util.List;
28: import java.util.Map;
29: import java.util.Set;
30:
31: import org.eclipse.core.resources.IProject;
32: import org.eclipse.core.resources.IWorkspaceRoot;
33: import org.eclipse.core.resources.ResourcesPlugin;
34: import org.overture.ast.definitions.ANamedTraceDefinition;
35: import org.overture.ast.definitions.PDefinition;
36: import org.overture.ast.definitions.SClassDefinition;
37: import org.overture.ast.modules.AModuleModules;
38: import org.overture.ast.node.INode;
39: import org.overture.ide.core.resources.IVdmProject;
40: import org.overture.ide.plugins.combinatorialtesting.views.ViewContentProvider.TraceContainerSearch;
41: import org.overture.ide.plugins.combinatorialtesting.views.ViewContentProvider.TraceSearch;
42:
43: public class TraceAstUtility
44: {
45:         static Map<INode, IVdmProject> cache = new HashMap<INode, IVdmProject>();
46:
47:         static public Set<IVdmProject> getProjects()
48:         {
49:                 Set<IVdmProject> projects = new HashSet<IVdmProject>();
50:                 IWorkspaceRoot iworkspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
51:                 IProject[] iprojects = iworkspaceRoot.getProjects();
52:                 for (IProject iProject : iprojects)
53:                 {
54:                         IVdmProject vdmProject = (IVdmProject) iProject.getAdapter(IVdmProject.class);
55:
56:                         if (vdmProject != null)
57:                         {
58:                                 projects.add(vdmProject);
59:                         }
60:                 }
61:
62:                 return projects;
63:         }
64:
65:         static public IVdmProject getProject(INode node)
66:         {
67:                 if (cache.containsKey(node))
68:                 {
69:                         return cache.get(node);
70:                 }
71:                 for (IVdmProject project : getProjects())
72:                 {
73:                         for (INode projectNode : project.getModel().getRootElementList())
74:                         {
75:                                 if (node instanceof SClassDefinition
76:                                                 || node instanceof AModuleModules)
77:                                 {
78:                                         if (node == projectNode)
79:                                         {
80:                                                 cache.put(node, project);
81:                                                 return project;
82:                                         }
83:                                 } else if (node instanceof ANamedTraceDefinition)
84:                                 {
85:                                         LinkedList<PDefinition> definitions = null;
86:                                         if (projectNode instanceof SClassDefinition)
87:                                         {
88:                                                 definitions = ((SClassDefinition) projectNode).getDefinitions();
89:                                         } else if (projectNode instanceof AModuleModules)
90:                                         {
91:                                                 definitions = ((AModuleModules) projectNode).getDefs();
92:                                         }
93:
94:                                         for (PDefinition pDefinition : definitions)
95:                                         {
96:                                                 if (node == pDefinition)
97:                                                 {
98:                                                         cache.put(node, project);
99:                                                         return project;
100:                                                 }
101:                                         }
102:                                 } else
103:                                 {
104:                                         // not supported
105:                                 }
106:                         }
107:                 }
108:                 return null;
109:         }
110:
111:         public static Set<INode> getTraceContainers(IVdmProject project)
112:         {
113:                 TraceContainerSearch analysis = new TraceContainerSearch();
114:                 Set<INode> containers = new HashSet<INode>();
115:                 for (INode node : project.getModel().getRootElementList())
116:                 {
117:                         containers.addAll(analysis.getTraceContainers(node));
118:                 }
119:
120:                 for (INode iNode : containers)
121:                 {
122:                         cache.put(iNode, project);
123:                 }
124:                 return containers;
125:
126:         }
127:
128:         public static String getContainerName(INode node)
129:         {
130:                 if (node instanceof SClassDefinition)
131:                 {
132:                         return ((SClassDefinition) node).getName().getName();
133:                 } else if (node instanceof AModuleModules)
134:                 {
135:                         return ((AModuleModules) node).getName().getName();
136:                 }
137:                 return null;
138:         }
139:
140:         public static List<ANamedTraceDefinition> getTraceDefinitions(INode node)
141:         {
142:                 return new TraceSearch().getTraces(node);
143:         }
144:
145:         public static INode getTraceDefinitionContainer(ANamedTraceDefinition node)
146:         {
147:                 INode parent = null;
148:                 parent = node.getAncestor(SClassDefinition.class);
149:                 if (parent != null)
150:                 {
151:                         return parent;
152:                 }
153:                 parent = node.getAncestor(AModuleModules.class);
154:                 return parent;
155:         }
156: }