1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2012 Association for Decentralized Information Management in
\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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.representation.utils;
\r
14 import java.io.StringReader;
\r
15 import java.util.Set;
\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
32 public class RepresentationUtils {
\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
41 public static boolean isGameExperimentActive() {
\r
42 // Find active experiment
\r
43 IProject project = Simantics.peekProject();
\r
44 if (project == null)
\r
47 IExperimentManager manager = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
\r
49 IExperiment active = manager.getActiveExperiment();
\r
50 if (active instanceof SysdynGameExperiment)
\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
62 public static boolean isPartOfGameExperiment(Variable variable) {
\r
63 Configuration conf = variable.getParentConfiguration();
\r
65 return conf.isGameConfiguration();
\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
76 public static boolean isPartOfGameExperiment(Module module) {
\r
77 Configuration conf = module.getParentConfiguration();
\r
79 return conf.isGameConfiguration();
\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
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
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
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
109 } catch (ParseException e) {
\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
121 private static Variable getGameVariableReference(Variable variable, Configuration configuration, String reference) {
\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
132 Configuration moduleConfiguration = m.getType().getConfiguration();
\r
133 return getGameVariableReference(variable, moduleConfiguration, reference.substring(reference.indexOf(".") + 1));
\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
141 if(Variability.PARAMETER.equals(Variability.getVariability(v, false, null))) {
\r
148 if(v.getExpressions() == null || v.getExpressions().getExpressions().size() > 1)
\r
151 IExpression e = v.getExpressions().getExpressions().get(0);
\r
153 if(e instanceof ParameterExpression)
\r
164 public static Variable getVariable(Configuration configuration, String name) {
\r
165 Configuration conf = configuration;
\r
166 String[] components = name.split("\\.");
\r
168 for(String component : components) {
\r
170 Configuration newConf = null;
\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
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
186 // If variable or configuration has not been found, return null
\r
187 if(newConf == null)
\r