]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
80faa484c076b742b172ec0d9a5b8a0e927299c6
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.representation.utils;\r
13 \r
14 import java.io.StringReader;\r
15 import java.util.Set;\r
16 \r
17 import org.simantics.Simantics;\r
18 import org.simantics.project.IProject;\r
19 import org.simantics.simulation.experiment.IExperiment;\r
20 import org.simantics.simulation.project.IExperimentManager;\r
21 import org.simantics.sysdyn.expressionParser.ExpressionParser;\r
22 import org.simantics.sysdyn.expressionParser.ParseException;\r
23 import org.simantics.sysdyn.manager.SysdynGameExperiment;\r
24 import org.simantics.sysdyn.representation.Configuration;\r
25 import org.simantics.sysdyn.representation.IElement;\r
26 import org.simantics.sysdyn.representation.IndependentVariable;\r
27 import org.simantics.sysdyn.representation.Module;\r
28 import org.simantics.sysdyn.representation.Stock;\r
29 import org.simantics.sysdyn.representation.Variability;\r
30 import org.simantics.sysdyn.representation.Variable;\r
31 \r
32 public class RepresentationUtils {\r
33 \r
34 \r
35         /**\r
36          * Returns true, if there is an active experiment and the active experiment\r
37          * is a game experiment\r
38          * @return true, if there is an active experiment and the active experiment\r
39          * is a game experiment\r
40          */\r
41         public static boolean isGameExperimentActive() {\r
42                 // Find active experiment\r
43                 IProject project = Simantics.peekProject();\r
44                 if (project == null)\r
45                         return false;\r
46 \r
47                 IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);\r
48 \r
49                 IExperiment active = manager.getActiveExperiment();\r
50                 if (active instanceof SysdynGameExperiment)\r
51                         return true;\r
52                 else\r
53                         return false;\r
54         }\r
55 \r
56         /**\r
57          * Find out if a variable is part of a game configuration. (This means that the variable is a \r
58          * part of a game experiment)\r
59          * @param variable\r
60          * @return\r
61          */\r
62         public static boolean isPartOfGameExperiment(Variable variable) {\r
63             Configuration conf = variable.getParentConfiguration();\r
64             if(conf != null)\r
65                 return conf.isGameConfiguration();\r
66             else\r
67                 return false;\r
68         }\r
69         \r
70         /**\r
71      * Find out if a module is part of a game configuration. (This means that the module is a \r
72      * part of a game experiment)\r
73      * @param variable\r
74      * @return\r
75      */\r
76     public static boolean isPartOfGameExperiment(Module module) {\r
77         Configuration conf = module.getParentConfiguration();\r
78         if(conf != null)\r
79             return conf.isGameConfiguration();\r
80         else\r
81             return false;\r
82     }\r
83 \r
84 \r
85         /**\r
86          * Retrieves the first reference to a game variable (parameter variable) in the given\r
87          * expression or null if there is no reference to a game variable.\r
88          * <p>\r
89          * Current requirements for a game variable: Variable contains only one parameter expression.\r
90          * @param variable Variable that has the expression\r
91          * @param expression Expression\r
92          * @return The name of the first referred game variable or null if there is no such reference\r
93          */\r
94         public static Variable getFirstGameVariableReference(Variable variable, String expression) {\r
95                 String equation = FormatUtils.formatExpressionForModelica(variable, expression, false);\r
96                 ExpressionParser parser = new ExpressionParser(new StringReader(equation));\r
97                 Variable result = null;\r
98                 try {\r
99                         parser.expr();\r
100                         if(!parser.getReferences().isEmpty()) {\r
101                                 Set<String> references = parser.getReferences().keySet();\r
102                                 // Go through each reference\r
103                                 for(String reference : references) {\r
104                                         if((result = getGameVariableReference(variable, variable.getParentConfiguration(), reference)) != null) {\r
105                                                 break;\r
106                                         }\r
107                                 }\r
108                         }\r
109                 } catch (ParseException e) {\r
110                 }\r
111                 return result;\r
112         }\r
113 \r
114         /**\r
115          * Test if a given reference is a game variable\r
116          * @param variable Variable that contains the reference\r
117          * @param configuration Configuration where the reference is searched in\r
118          * @param reference Reference variable name\r
119          * @return true if reference is a game variable, false otherwise\r
120          */\r
121         private static Variable getGameVariableReference(Variable variable, Configuration configuration, String reference) {\r
122 \r
123                 String r = reference.split("\\.")[0]; \r
124                 for(IElement element : configuration.getElements()) {\r
125                         if(element instanceof Module) {\r
126                                 Module m = (Module)element;\r
127                                 if(r.equals(m.getName())) {\r
128                                         if(!reference.contains(".")) {\r
129                                                 // The reference contains only modules, implementation feature for the expression parser\r
130                                                 return null;\r
131                                         }\r
132                                         Configuration moduleConfiguration = m.getType().getConfiguration();\r
133                                         return getGameVariableReference(variable, moduleConfiguration, reference.substring(reference.indexOf(".") + 1));\r
134                                 }\r
135                         } else if(element instanceof IndependentVariable && !(element instanceof Stock)) {\r
136                                 IndependentVariable v = (IndependentVariable)element;\r
137                                 if(r.equals(v.getName())) { \r
138                                         if(v.getName().equals(variable.getName())) {\r
139                                                 return null;// Reference to self.\r
140                                         } else {\r
141                                             if(Variability.PARAMETER.equals(Variability.getVariability(v, false, null))) {\r
142                                                 return v;\r
143                                             } else {\r
144                                                 return null;\r
145                                             }\r
146                                             \r
147                                             /*\r
148                                                 if(v.getExpressions() == null || v.getExpressions().getExpressions().size() > 1)\r
149                                                         return null;\r
150                                                 \r
151                                                 IExpression e = v.getExpressions().getExpressions().get(0);\r
152                                                 \r
153                                                 if(e instanceof ParameterExpression)\r
154                                                         return v;\r
155                                                 */\r
156                                         }\r
157                                 }\r
158                         }\r
159                 }\r
160                 return null;\r
161         }\r
162 \r
163 \r
164         public static Variable getVariable(Configuration configuration, String name) {\r
165             Configuration conf = configuration;\r
166             String[] components = name.split("\\.");\r
167             \r
168             for(String component : components) {\r
169                 \r
170                 Configuration newConf = null;\r
171                 \r
172                 for(IElement element : conf.getElements()) {\r
173                     if(element instanceof Variable) {\r
174                         Variable variable = (Variable) element;\r
175                         if(variable.getName().equals(component)) {\r
176                             return variable;\r
177                         }\r
178                     } else if(element instanceof Module) {\r
179                         Module m = (Module)element;\r
180                         if(m.getName().equals(component)) {\r
181                             newConf = m.getType().getConfiguration();\r
182                         }\r
183                     }\r
184                 }\r
185                 \r
186                 // If variable or configuration has not been found, return null\r
187                 if(newConf == null)\r
188                     return null;\r
189             }\r
190             \r
191             return null;\r
192         }\r
193 \r
194 }\r