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.ui.properties.widgets.modules;
\r
14 import java.util.List;
\r
16 import org.simantics.db.ReadGraph;
\r
17 import org.simantics.db.Resource;
\r
18 import org.simantics.db.common.request.ObjectsWithType;
\r
19 import org.simantics.db.common.utils.ListUtils;
\r
20 import org.simantics.db.common.utils.NameUtils;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.layer0.Layer0;
\r
23 import org.simantics.sysdyn.SysdynResource;
\r
24 import org.simantics.sysdyn.representation.IndependentVariable;
\r
27 * Utilities for module parameter override functionalities
\r
29 * @author Teemu Lempinen
\r
32 public class ModuleParameterOverrideUtils {
\r
35 * Get the parameter expression of a variable or null, if it does not contain a parameter expression,
\r
36 * or there are many parameter expressions
\r
37 * @param graph ReadGraph
\r
38 * @param variable IndependentVariable
\r
39 * @return parameter expression or null
\r
40 * @throws DatabaseException
\r
42 public static String getParameterExpression(IndependentVariable variable) throws DatabaseException {
\r
43 String result = null;
\r
45 result = variable.getExpressions().get(0).getExpression();
\r
46 if(result.contains("/* Actual value read from init file */"))
\r
47 result = result.substring(0, result.indexOf("/* Actual value read from init file */"));
\r
48 } catch (NullPointerException e) {
\r
54 * Get the expression, or the overriding expression, of a parameter
\r
55 * @param graph ReadGraph
\r
56 * @param parent Parent resource
\r
57 * @param variable IndependentVariable
\r
58 * @return Overriding expression, or the default parameter expression, or null
\r
59 * @throws DatabaseException
\r
61 public static String getParameterExpressionOrOverride(ReadGraph graph, Resource parent, IndependentVariable variable)
\r
62 throws DatabaseException {
\r
63 String result = null;
\r
64 SysdynResource sr = SysdynResource.getInstance(graph);
\r
65 Layer0 L0 = Layer0.getInstance(graph);
\r
67 // Get possible override
\r
68 for(Resource r : graph.syncRequest(new ObjectsWithType(parent, L0.ConsistsOf, sr.Module_ParameterOverride))) {
\r
69 Resource overridden = graph.getPossibleObject(r, sr.Module_ParameterOverride_overriddenParameter);
\r
70 if(variable.getName().equals(NameUtils.getSafeName(graph, overridden))) {
\r
71 result = graph.getPossibleRelatedValue(r, sr.Module_ParameterOverride_overrideExpression);
\r
75 if(result == null) {
\r
76 // Parameter is not overridden, find the actual expression
\r
77 result = getParameterExpression(variable);
\r
83 * Find out if a variable has a single parameter expression
\r
84 * @param graph ReadGraph
\r
85 * @param variable Variable
\r
86 * @return does the variable have a single parameter expression
\r
87 * @throws DatabaseException
\r
89 public static boolean hasParameterExpression(ReadGraph graph, Resource variable) throws DatabaseException {
\r
90 SysdynResource sr = SysdynResource.getInstance(graph);
\r
91 boolean result = false;
\r
92 Resource expressionsResource = graph.getPossibleObject(variable, sr.Variable_expressionList);
\r
93 if(expressionsResource != null) {
\r
94 List<Resource> expressions = ListUtils.toList(graph, expressionsResource);
\r
95 if(expressions.size() == 1 && graph.isInstanceOf(expressions.get(0), sr.ParameterExpression)) {
\r