From c745f024b3473157bc07da99b74a60cf001475a7 Mon Sep 17 00:00:00 2001 From: miettinen Date: Tue, 25 Mar 2014 11:20:25 +0000 Subject: [PATCH] Separate color for Sysdyn dependency arrows which go to stocks where their tail variable is used only for initial equation (refs #4637). git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@29200 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../DependencyConnectionFactory.java | 77 ++++++++++++++++++- .../SysdynDiagramPreferencePage.java | 5 +- .../preferences/SysdynDiagramPreferences.java | 8 +- .../SysdynDiagramPreferencesInitializer.java | 7 +- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/DependencyConnectionFactory.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/DependencyConnectionFactory.java index 3f757a08..6316eec8 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/DependencyConnectionFactory.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/connections/DependencyConnectionFactory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010, 2012 Association for Decentralized Information Management in + * Copyright (c) 2010, 2012, 2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -13,7 +13,10 @@ package org.simantics.sysdyn.ui.elements.connections; import java.awt.Color; import java.awt.Font; +import java.io.StringReader; import java.util.HashMap; +import java.util.List; +import java.util.Set; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.atomic.AtomicInteger; @@ -26,6 +29,7 @@ import org.simantics.db.AsyncReadGraph; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Statement; +import org.simantics.db.common.utils.ListUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.procedure.AsyncMultiProcedure; import org.simantics.db.procedure.AsyncProcedure; @@ -42,6 +46,9 @@ import org.simantics.g2d.element.ElementHints; import org.simantics.g2d.element.IElement; import org.simantics.g2d.element.handler.impl.StaticObjectAdapter; import org.simantics.layer0.Layer0; +import org.simantics.modeling.ModelingResources; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.expressionParser.ExpressionParser; import org.simantics.sysdyn.ui.editor.routing.DependencyRouter; import org.simantics.sysdyn.ui.preferences.SysdynDiagramPreferences; import org.simantics.sysdyn.ui.preferences.SysdynDiagramPropertyExternalRead; @@ -52,6 +59,7 @@ import org.simantics.utils.datastructures.Pair; * consists of connection edge segments and branch points as its children. * * @author Tuukka Lehtonen + * @author Tuomas Miettinen */ public class DependencyConnectionFactory extends ElementFactoryAdapter { @@ -123,8 +131,13 @@ public class DependencyConnectionFactory extends ElementFactoryAdapter { if(result != null && !result.isAsserted(elementResource)) { element.setHint(ElementHints.KEY_TEXT_COLOR, G2DUtils.getColor(graph, result.getObject())); } else { - String color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair(elementResource, SysdynDiagramPreferences.ARROW_COLOR))); - if(color != null) { + String color; + if (isForStockInitialOnly(graph, elementResource)) { + color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair(elementResource, SysdynDiagramPreferences.ARROW_STOCK_INITIAL_COLOR))); + } else { + color = graph.syncRequest(new SysdynDiagramPropertyExternalRead(new Pair(elementResource, SysdynDiagramPreferences.ARROW_COLOR))); + } + if(color != null) { RGB rgb = StringConverter.asRGB(color, null); if(rgb != null) { Color c = new Color(rgb.red, rgb.green, rgb.blue); @@ -245,4 +258,62 @@ public class DependencyConnectionFactory extends ElementFactoryAdapter { } + protected static boolean isForStockInitialOnly(ReadGraph graph, + Resource elementResource) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + ModelingResources MO = ModelingResources.getInstance(graph); + Layer0 L0 = Layer0.getInstance(graph); + Resource connection = graph.getPossibleObject(elementResource, MO.DiagramConnectionToConnection); + Resource tail = graph.getPossibleObject(connection, SR.Variable_HasTail); + if (tail == null) + return false; + + if (graph.isInstanceOf(tail, SR.Shadow)) + tail = graph.getPossibleObject(tail, SR.Shadow_original); + + Resource head = graph.getPossibleObject(connection, SR.Variable_HasHead); + if (head == null || !graph.isInstanceOf(head, SR.Stock) || tail == null) + return false; + + Resource expressionListResource = graph.getPossibleObject(head, SR.Variable_expressionList); + List expressionList = ListUtils.toPossibleList(graph, expressionListResource); + + // Keep track on whether at least one initial equation contains the variable + boolean initialContainsVariable = false; + // Go through all expressions + for (Resource expression : expressionList) { + if (!graph.isInstanceOf(expression, SR.StockExpression)) + return false; + + String tailStr = graph.getPossibleRelatedValue(tail, L0.HasName, Bindings.STRING); + String integral = graph.getPossibleRelatedValue(expression, SR.StockExpression_integralEquation, Bindings.STRING); + if (equationContainsVariable(graph, integral, tailStr)) + // At least one integral equation contains the variable + return false; + String initial = graph.getPossibleRelatedValue(expression, SR.StockExpression_initialEquation, Bindings.STRING); + if (!initialContainsVariable && equationContainsVariable(graph, initial, tailStr)) + // At least one initial equation contains the variable + initialContainsVariable = true; + } + + // No integral equation contains the variable. + return initialContainsVariable; + } + + private static boolean equationContainsVariable(ReadGraph graph, + String equation, String variable) { + ExpressionParser parser = new ExpressionParser(new StringReader(equation)); + try { + parser.expr(); + } catch (Throwable t) { + return false; + } + + // Collect references + Set references = parser.getReferences().keySet(); + + // See if the equation contains variable + return references.contains(variable); + } + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencePage.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencePage.java index 4f9639da..a61ae6bd 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencePage.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013, 2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.ui.preferences; @@ -33,6 +34,7 @@ import org.simantics.sysdyn.ui.Activator; /** * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class SysdynDiagramPreferencePage extends FieldEditorPreferencePage implements @@ -82,6 +84,7 @@ IWorkbenchPreferencePage { GridDataFactory.fillDefaults().applyTo(label); addColorFieldEditor(SysdynDiagramPreferences.ARROW_COLOR, "&Dependency", SysdynDiagramPreferences.ARROW_USE_DEFAULT_COLOR); + addColorFieldEditor(SysdynDiagramPreferences.ARROW_STOCK_INITIAL_COLOR, "&Dependency to Stock Initial", SysdynDiagramPreferences.ARROW_STOCK_INITIAL_USE_DEFAULT_COLOR); addColorFieldEditor(SysdynDiagramPreferences.FLOW_COLOR, "&Flow", SysdynDiagramPreferences.FLOW_USE_DEFAULT_COLOR); addColorFieldEditor(SysdynDiagramPreferences.AUXILIARY_COLOR, "&Auxiliary", SysdynDiagramPreferences.AUXILIARY_USE_DEFAULT_COLOR); addColorFieldEditor(SysdynDiagramPreferences.STOCK_COLOR, "&Stock", SysdynDiagramPreferences.STOCK_USE_DEFAULT_COLOR); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferences.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferences.java index 94c4bcab..1a8255fa 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferences.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferences.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013, 2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.ui.preferences; @@ -24,6 +25,7 @@ import org.simantics.sysdyn.ui.Activator; /** * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class SysdynDiagramPreferences { @@ -32,6 +34,7 @@ public class SysdynDiagramPreferences { public static String DEFAULT_FONT = "Default font"; public static String ARROW_COLOR = "Arrow color"; + public static String ARROW_STOCK_INITIAL_COLOR = "Arrow stock initial color"; public static String FLOW_COLOR = "Flow color"; public static String AUXILIARY_COLOR = "Auxiliary color"; public static String CLOUD_COLOR = "Cloud color"; @@ -45,6 +48,7 @@ public class SysdynDiagramPreferences { static { colorPreferenceNames = new HashMap(); colorPreferenceNames.put(SysdynResource.URIs.DependencyConnection, ARROW_COLOR); + colorPreferenceNames.put(SysdynResource.URIs.DependencyConnection + "_stockInitial", ARROW_STOCK_INITIAL_COLOR); colorPreferenceNames.put(SysdynResource.URIs.FlowConnection, FLOW_COLOR); colorPreferenceNames.put(SysdynResource.URIs.AuxiliarySymbol, AUXILIARY_COLOR); colorPreferenceNames.put(SysdynResource.URIs.CloudSymbol, CLOUD_COLOR); @@ -61,6 +65,7 @@ public class SysdynDiagramPreferences { } public static String ARROW_USE_DEFAULT_COLOR = "Arrow use default color"; + public static String ARROW_STOCK_INITIAL_USE_DEFAULT_COLOR = "Arrow stock initial use default color"; public static String FLOW_USE_DEFAULT_COLOR = "Flow use default color"; public static String AUXILIARY_USE_DEFAULT_COLOR = "Auxiliary use default color"; public static String CLOUD_USE_DEFAULT_COLOR = "Cloud use default color"; @@ -74,6 +79,7 @@ public class SysdynDiagramPreferences { static { colorDefaults = new HashMap(); colorDefaults.put(ARROW_COLOR, ARROW_USE_DEFAULT_COLOR); + colorDefaults.put(ARROW_STOCK_INITIAL_COLOR, ARROW_STOCK_INITIAL_USE_DEFAULT_COLOR); colorDefaults.put(FLOW_COLOR, FLOW_USE_DEFAULT_COLOR); colorDefaults.put(AUXILIARY_COLOR, AUXILIARY_USE_DEFAULT_COLOR); colorDefaults.put(CLOUD_COLOR, CLOUD_USE_DEFAULT_COLOR); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencesInitializer.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencesInitializer.java index cd5c8f50..1b1c4556 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencesInitializer.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/preferences/SysdynDiagramPreferencesInitializer.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013, 2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.ui.preferences; @@ -23,6 +24,7 @@ import com.lowagie.text.Font; /** * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class SysdynDiagramPreferencesInitializer extends AbstractPreferenceInitializer { @@ -31,10 +33,12 @@ public class SysdynDiagramPreferencesInitializer extends AbstractPreferenceIniti public void initializeDefaultPreferences() { RGB black = new RGB(0, 0, 0); RGB arrow = new RGB(0,128,192); + RGB arrowToStockInitial = new RGB(128,128,128); IPreferenceStore store = Activator.getDefault().getPreferenceStore(); PreferenceConverter.setDefault(store, SysdynDiagramPreferences.DEFAULT_COLOR, black); + PreferenceConverter.setDefault(store, SysdynDiagramPreferences.ARROW_STOCK_INITIAL_COLOR, arrowToStockInitial); PreferenceConverter.setDefault(store, SysdynDiagramPreferences.ARROW_COLOR, arrow); PreferenceConverter.setDefault(store, SysdynDiagramPreferences.FLOW_COLOR, black); PreferenceConverter.setDefault(store, SysdynDiagramPreferences.AUXILIARY_COLOR, black); @@ -46,6 +50,7 @@ public class SysdynDiagramPreferencesInitializer extends AbstractPreferenceIniti store.setDefault(SysdynDiagramPreferences.ARROW_USE_DEFAULT_COLOR, false); + store.setDefault(SysdynDiagramPreferences.ARROW_STOCK_INITIAL_USE_DEFAULT_COLOR, false); store.setDefault(SysdynDiagramPreferences.FLOW_USE_DEFAULT_COLOR, true); store.setDefault(SysdynDiagramPreferences.AUXILIARY_USE_DEFAULT_COLOR, true); store.setDefault(SysdynDiagramPreferences.STOCK_USE_DEFAULT_COLOR, true); -- 2.47.1