From: lempinen Date: Wed, 26 Oct 2011 12:13:58 +0000 (+0000) Subject: Gradient options for playback experiments & polarity location selection X-Git-Tag: simantics-1.5~1 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=95253679ae1737eb9356308abe0f453217c613ea;p=simantics%2Fsysdyn.git Gradient options for playback experiments & polarity location selection git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@22963 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index f0c4675e..156c62e5 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index 397a99be..1e2942e3 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -350,8 +350,17 @@ SYSDYN.GameExperiment properties = e.getHint(DiagramHints.PROPERTIES); Pair polarityPair = (Pair)properties.get("Polarity"); + Pair polarityLocationPair = (Pair)properties.get("PolarityLocation"); + + String location; + if(polarityLocationPair == null) + location = DependencyNode.INSIDE; + else + location = (String) polarityLocationPair.second; + if(polarityPair != null) - node.init((String) polarityPair.second, font, color, 0, 0, 0.235); + node.init((String) polarityPair.second, location, font, color, 0, 0, 0.235); update(e); } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java index 780d8f1a..529ca158 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/connections/DependencyNode.java @@ -13,6 +13,7 @@ package org.simantics.sysdyn.ui.elements2.connections; import java.awt.BasicStroke; import java.awt.Color; +import java.awt.Font; import java.awt.Graphics2D; import java.awt.Stroke; import java.awt.event.MouseEvent; @@ -34,6 +35,9 @@ import org.simantics.utils.datastructures.Pair; public class DependencyNode extends TextNode implements ISelectionPainterNode, MouseListener, MouseMotionListener { + public static final String INSIDE = "Inside"; + public static final String OUTSIDE = "Outside"; + private static final long serialVersionUID = 1294351381209071074L; private static final BasicStroke STROKE = new BasicStroke(1.0f); @@ -43,6 +47,7 @@ public class DependencyNode extends TextNode implements ISelectionPainterNode, M private Rectangle2D beginBounds; private Rectangle2D endBounds; private double angle = 0.1; + private String side; private transient Pair shapes = new Pair(new Arc2D.Double(), new Path2D.Double()); transient public boolean hover = false; @@ -55,6 +60,12 @@ public class DependencyNode extends TextNode implements ISelectionPainterNode, M NodeUtil.getEventDelegator(this).addMouseListener(this); NodeUtil.getEventDelegator(this).addMouseMotionListener(this); } + + + public void init(String text, String side, Font font, Color color, double x, double y, double scale) { + super.init(text, font, color, x, y, scale); + this.side = side; + } @Override public void cleanup() { @@ -177,7 +188,13 @@ public class DependencyNode extends TextNode implements ISelectionPainterNode, M Math.toRadians(shapes.first.getAngleStart()); Point2D point = angle > 0 ? shapes.first.getEndPoint() : shapes.first.getStartPoint(); - double a = Math.toRadians(angle < 0 ? 255 : -75); + int angle1 = 225; + int angle2 = -75; + if(OUTSIDE.equals(side)) { + angle1 *= -1; + angle2 *= -1; + } + double a = Math.toRadians(angle < 0 ? angle1 : angle2); double s = Math.sin(a) * 2; double c = Math.cos(a) * 3; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java index 8a68153a..44816700 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements2/profiles/SimulationPlaybackStyle.java @@ -11,13 +11,15 @@ *******************************************************************************/ package org.simantics.sysdyn.ui.elements2.profiles; -import java.awt.Color; +//import java.awt.Color; import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; +import java.util.ArrayList; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.diagram.elements.TextNode; @@ -40,12 +42,19 @@ import org.simantics.simulation.project.IExperimentManager; import org.simantics.sysdyn.adapter.SysdynVariableProperties; import org.simantics.sysdyn.manager.SysdynPlaybackExperiment; import org.simantics.ui.SimanticsUI; -import org.simantics.utils.datastructures.Pair; +import org.simantics.utils.datastructures.Triple; +import org.simantics.utils.ui.color.Color; +import org.simantics.utils.ui.color.ColorGradient; +import org.simantics.utils.ui.color.ColorValue; -public class SimulationPlaybackStyle extends StyleBase> { +public class SimulationPlaybackStyle extends StyleBase> { + + Resource gradientResource; + ColorGradient cg; + byte[] gradient; @Override - public Pair calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException { + public Triple calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) throws DatabaseException { IProject project = SimanticsUI.getProject(); IExperimentManager em = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER); @@ -56,6 +65,8 @@ public class SimulationPlaybackStyle extends StyleBase(at, multiplier); + G2DResource g2d = G2DResource.getInstance(graph); + Resource gradient = graph.getPossibleObject(experiment.getResource(), g2d.HasColorGradient); + if(this.gradientResource == null || !this.gradientResource.equals(gradient)) { + ArrayList colorValues = new ArrayList(); + for(Resource placement : graph.syncRequest(new ObjectsWithType(gradient, g2d.HasColorPlacement, g2d.ColorPlacement))) { + Double position = graph.getPossibleRelatedValue(placement, g2d.HasGradientPosition, Bindings.DOUBLE); + float[] rgba = graph.getPossibleRelatedValue(placement, g2d.HasColor, Bindings.FLOAT_ARRAY); + colorValues.add(new ColorValue(new Color(new java.awt.Color(rgba[0], rgba[1], rgba[2])), position)); + } + cg = new ColorGradient(colorValues, ColorGradient.HSV); + } else { + cg = this.cg; + } + + return new Triple(at, multiplier, cg); } catch(Exception ignore) { ignore.printStackTrace(); @@ -122,7 +147,7 @@ public class SimulationPlaybackStyle extends StyleBase result) { + public void styleResultChanged(Observer observer, Resource element, Triple result) { if (result != null) values.put(element, result); else @@ -131,7 +156,7 @@ public class SimulationPlaybackStyle extends StyleBase result) { + public void applyStyleForNode(EvaluationContext observer, INode _node, Triple result) { Double multiplier; if (result != null && (multiplier = result.second) != null && !multiplier.isNaN()) { @@ -141,7 +166,18 @@ public class SimulationPlaybackStyle extends StyleBase { + private String location; + + public PolarityLocationSelectionListener(ISessionContext context, String location) { + super(context); + this.location = location; + } + + @Override + public void apply(WriteGraph graph, Resource connectionElement) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + graph.claimLiteral(connectionElement, sr.PolarityLocation, location); + } + + } + + class PolarityLocationRadioSelectionFactory extends ReadFactoryImpl { + private String location; + + public PolarityLocationRadioSelectionFactory(String location) { + this.location = location; + } + + @Override + public Object getIdentity(Object inputContents) { + return new Triple>(inputContents, location, getClass()); + } + + @Override + public Boolean perform(ReadGraph graph, Resource dependencyConnection) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + String location = graph.getPossibleRelatedValue(dependencyConnection, sr.PolarityLocation, Bindings.STRING); + if(DependencyNode.OUTSIDE.equals(this.location)) { + return ObjectUtils.objectEquals(this.location, location); + } else { + if(location == null) + return true; + else + return ObjectUtils.objectEquals(this.location, location); + } + } } class PolaritySelectionListener extends SelectionListenerImpl { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/PlaybackExperimentTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/PlaybackExperimentTab.java new file mode 100644 index 00000000..f2eb35e1 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/PlaybackExperimentTab.java @@ -0,0 +1,206 @@ +/******************************************************************************* + * Copyright (c) 2007, 2011 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties; + +import java.util.ArrayList; + +import org.eclipse.jface.layout.GridDataFactory; +import org.eclipse.jface.layout.GridLayoutFactory; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Group; +import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.widgets.Button; +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.util.RemoverUtil; +import org.simantics.db.management.ISessionContext; +import org.simantics.diagram.stubs.G2DResource; +import org.simantics.layer0.utils.direct.GraphUtils; +import org.simantics.utils.datastructures.Triple; +import org.simantics.utils.ui.color.Color; +import org.simantics.utils.ui.color.ColorGradient; +import org.simantics.utils.ui.color.ColorValue; + +public class PlaybackExperimentTab extends LabelPropertyTabContributor { + + private static int gradientWidth = 250; + private static int gradientHeight = 20; + + @Override + public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + Composite composite = new Composite(body, SWT.NONE); + GridDataFactory.fillDefaults().grab(true, true).applyTo(composite); + GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(composite); + + Group gradientGroup = new Group(composite, SWT.NONE); + gradientGroup.setText("Color scale"); + GridDataFactory.fillDefaults().applyTo(gradientGroup); + GridLayoutFactory.fillDefaults().margins(3, 3).applyTo(gradientGroup); + + + ColorValue cv1 = new ColorValue(new Color(0, 62, 133), 0.0); + ColorValue cv2 = new ColorValue(new Color(255, 230, 0), 1.0); + ColorValue[] values = new ColorValue[] {cv1, cv2}; + ColorGradient cg = new ColorGradient(values, ColorGradient.HSV); + Image image = cg.getGradientImage(gradientWidth, gradientHeight, SWT.HORIZONTAL); + Button b = new Button(gradientGroup, support, SWT.RADIO); + GridDataFactory.fillDefaults().hint(gradientWidth, gradientHeight).applyTo(b.getWidget()); + b.setImage(image); + b.addSelectionListener(new GradientSelectionListener(context, values)); + b.setSelectionFactory(new GradientSelectionFactory(values)); + + cv1 = new ColorValue(new Color(255, 230, 0), 0.0); + cv2 = new ColorValue(new Color(0, 62, 133), 1.0); + values = new ColorValue[] {cv1, cv2}; + cg = new ColorGradient(values, ColorGradient.HSV); + image = cg.getGradientImage(gradientWidth, gradientHeight, SWT.HORIZONTAL); + b = new Button(gradientGroup, support, SWT.RADIO); + GridDataFactory.fillDefaults().hint(gradientWidth, gradientHeight).applyTo(b.getWidget()); + b.setImage(image); + b.addSelectionListener(new GradientSelectionListener(context, values)); + b.setSelectionFactory(new GradientSelectionFactory(values)); + + cv1 = new ColorValue(new Color(0, 0, 0), 0.0); + cv2 = new ColorValue(new Color(255, 255, 255), 1.0); + values = new ColorValue[] {cv1, cv2}; + cg = new ColorGradient(values, ColorGradient.HSV); + image = cg.getGradientImage(gradientWidth, gradientHeight, SWT.HORIZONTAL); + b = new Button(gradientGroup, support, SWT.RADIO); + GridDataFactory.fillDefaults().hint(gradientWidth, gradientHeight).applyTo(b.getWidget()); + b.setImage(image); + b.addSelectionListener(new GradientSelectionListener(context, values)); + b.setSelectionFactory(new GradientSelectionFactory(values)); + + cv1 = new ColorValue(new Color(0, 0, 255), 0.0); + cv2 = new ColorValue(new Color(255, 0, 0), 1.0); + values = new ColorValue[] {cv1, cv2}; + cg = new ColorGradient(values, ColorGradient.HSV); + image = cg.getGradientImage(gradientWidth, gradientHeight, SWT.HORIZONTAL); + b = new Button(gradientGroup, support, SWT.RADIO); + GridDataFactory.fillDefaults().hint(gradientWidth, gradientHeight).applyTo(b.getWidget()); + b.setImage(image); + b.addSelectionListener(new GradientSelectionListener(context, values)); + b.setSelectionFactory(new GradientSelectionFactory(values)); + + + cv1 = new ColorValue(new Color(255, 0, 0), 0.0); + cv2 = new ColorValue(new Color(0, 0, 255), 1.0); + values = new ColorValue[] {cv1, cv2}; + cg = new ColorGradient(values, ColorGradient.HSV); + image = cg.getGradientImage(gradientWidth, gradientHeight, SWT.HORIZONTAL); + b = new Button(gradientGroup, support, SWT.RADIO); + GridDataFactory.fillDefaults().hint(gradientWidth, gradientHeight).applyTo(b.getWidget()); + b.setImage(image); + b.addSelectionListener(new GradientSelectionListener(context, values)); + b.setSelectionFactory(new GradientSelectionFactory(values)); + } + + + class GradientSelectionListener extends SelectionListenerImpl { + private ArrayList colorValues; + + public GradientSelectionListener(ISessionContext context, ColorValue[] colorValues) { + super(context); + this.colorValues = new ArrayList(); + for(ColorValue cv : colorValues) + this.colorValues.add(cv); + } + + @Override + public void apply(WriteGraph graph, Resource experiment) throws DatabaseException { + G2DResource g2d = G2DResource.getInstance(graph); + + Resource gradient = graph.getPossibleObject(experiment, g2d.HasColorGradient); + + if(gradient != null) { + graph.denyStatement(experiment, g2d.HasColorGradient, gradient); + RemoverUtil.remove(graph, gradient); + } + + gradient = GraphUtils.create2(graph, g2d.ColorGradient); + graph.claim(experiment, g2d.HasColorGradient, gradient); + + for(ColorValue cv : colorValues) { + Resource placement = GraphUtils.create2(graph, g2d.ColorPlacement, + g2d.HasGradientPosition, cv.getValue()); + graph.claimLiteral(placement, g2d.HasColor, cv.getColor().getAWTColor().getColorComponents(new float[4]), Bindings.FLOAT_ARRAY); + graph.claim(gradient, g2d.HasColorPlacement, placement); + } + } + + } + + class GradientSelectionFactory extends ReadFactoryImpl { + private ArrayList colorValues; + + public GradientSelectionFactory(ColorValue[] colorValues) { + this.colorValues = new ArrayList(); + for(ColorValue cv : colorValues) + this.colorValues.add(cv); + } + + @Override + public Object getIdentity(Object inputContents) { + return new Triple>(inputContents, colorValues, getClass()); + } + + @Override + public Boolean perform(ReadGraph graph, Resource experiment) throws DatabaseException { + G2DResource g2d = G2DResource.getInstance(graph); + Resource gradient = graph.getPossibleObject(experiment, g2d.HasColorGradient); + if(gradient == null) { + return Boolean.FALSE; + } + + for(Resource placement : graph.syncRequest(new ObjectsWithType(gradient, g2d.HasColorPlacement, g2d.ColorPlacement))) { + Double position = graph.getPossibleRelatedValue(placement, g2d.HasGradientPosition); + if(position == null) + return Boolean.FALSE; + int index = -1; + + // First look for a color with matching value + for(int i = 0; i < colorValues.size(); i++) { + if(position.equals(colorValues.get(i).getValue())) { + + index = i; + break; + } + } + + // If matching value was found, see if the color is the same + if(index >= 0) { + Color c = colorValues.get(index).getColor(); + float[] cArray = c.getAWTColor().getColorComponents(new float[4]); + float[] color = graph.getPossibleRelatedValue(placement, g2d.HasColor, Bindings.FLOAT_ARRAY); + for(int i = 0; i < color.length; i++) { + if(cArray[i] != color[i]) + // Some inconsistency found in colors, return false + return Boolean.FALSE; + } + } + } + + // Everything matched + return Boolean.TRUE; + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java index 058d265a..c453dd79 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ResourceSelectionProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Association for Decentralized Information Management in + * Copyright (c) 2010, 2011 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 @@ -197,6 +197,13 @@ public class ResourceSelectionProcessor implements SelectionProcessor timeListeners = new ArrayList(); - + ScheduledExecutorService playbackExecutionService; PlaybackConfiguration playbackConfiguration; @@ -48,8 +48,8 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam super(experiment, model); this.time = 0; } - - + + /** * Interrupts a possible ongoing playback * @@ -59,7 +59,7 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam stopPlayback(); setTime(time); } - + /** * Sets a new time and continues playback from that point if * playback was running @@ -74,16 +74,24 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam setTime(time); } } - + private void setTime(double time) { this.time = time; fireValuesChanged(); } - + public double getTime() { return this.time; } - + + public double getStartTime() { + return this.startTime; + } + + public double getEndTime() { + return this.endTime; + } + public void setPlaybackDuration(long duration) { this.playbackDuration = duration; if(isPlaybackRunning()) { @@ -91,11 +99,11 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam startPlayback(); } } - + public long getPlaybackDuration() { return this.playbackDuration; } - + @Override public void init(ReadGraph g) { this.session = g.getSession(); @@ -107,12 +115,13 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam final Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration); sysdynModel = SysdynModelManager.getInstance(session).getModel(graph, configuration); toggleActivation(graph, true); + getPlaybackConfiguration(graph); startSimulation(); } }); } - - + + // PLAYBACK CONTROLS public void startPlayback() { startPlayback(0); @@ -128,24 +137,24 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam setTime(playbackConfiguration.startTime); playbackConfiguration = getPlaybackConfiguration(); } - + Runnable playbackSimulationTask = new PlaybackSimulationTask(time, playbackConfiguration.simulationStepLength); - + long delay = (long) (playbackConfiguration.playbackDuration / playbackConfiguration.intervals); ScheduledFuture stepper = playbackExecutionService.scheduleWithFixedDelay( playbackSimulationTask, initialDelay, delay, TimeUnit.MILLISECONDS - ); - + ); + Runnable stopSimulationTask = new StopSimulationTask(stepper, playbackConfiguration.endTime); playbackExecutionService.schedule(stopSimulationTask, playbackConfiguration.playbackDuration + initialDelay, TimeUnit.MILLISECONDS); - + changeState(ExperimentState.RUNNING); } - + public boolean isPlaybackRunning() { return playbackExecutionService != null && !playbackExecutionService.isShutdown(); } - + public void resetPlayback() { double startTime = 0.0; if(isPlaybackRunning() && playbackConfiguration != null) { @@ -155,7 +164,7 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam } setTimeInterrupting(startTime); } - + public void stopPlayback() { if(isPlaybackRunning()) { playbackExecutionService.shutdownNow(); @@ -165,29 +174,29 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam changeState(ExperimentState.STOPPED); } } - - + + private class PlaybackSimulationTask implements Runnable { private int stepCount; private double startTime, stepLength; - + public PlaybackSimulationTask(double startTime, double stepLength) { this.startTime = startTime; this.stepLength = stepLength; } - + public void run() { - ++stepCount; - setTime(startTime + stepCount * stepLength); + ++stepCount; + setTime(startTime + stepCount * stepLength); // System.out.println("Playback step at time: " + (startTime + stepCount * stepLength) + " (step: " + stepCount + ")"); } - } + } private class StopSimulationTask implements Runnable { private ScheduledFuture scheduledFuture; private double endTime; - + public StopSimulationTask(ScheduledFuture aSchedFuture, double endTime){ scheduledFuture = aSchedFuture; this.endTime = endTime; @@ -200,26 +209,31 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam } } - + private PlaybackConfiguration getPlaybackConfiguration() { - Double[] numbers = new Double[3]; + PlaybackConfiguration config = null; try { - numbers = Simantics.getSession().syncRequest(new Read() { + config = Simantics.getSession().syncRequest(new Read() { + @Override - public Double[] perform(ReadGraph graph) throws DatabaseException { - Double[] numbers = new Double[3]; - Resource model = getModel(); - SysdynResource sr = SysdynResource.getInstance(graph); - numbers[0] = graph.getRelatedValue(model, sr.HasStartTime); - numbers[1] = graph.getRelatedValue(model, sr.HasStopTime); - numbers[2] = graph.getPossibleRelatedValue(model, sr.HasOutputInterval); - return numbers; + public PlaybackConfiguration perform(ReadGraph graph) throws DatabaseException { + return getPlaybackConfiguration(graph); } + }); - } catch (DatabaseException e1) { - e1.printStackTrace(); + } catch (DatabaseException e) { + e.printStackTrace(); } - + return config; + } + private PlaybackConfiguration getPlaybackConfiguration(ReadGraph graph) throws DatabaseException { + Double[] numbers = new Double[3]; + Resource model = getModel(); + SysdynResource sr = SysdynResource.getInstance(graph); + numbers[0] = graph.getRelatedValue(model, sr.HasStartTime); + numbers[1] = graph.getRelatedValue(model, sr.HasStopTime); + numbers[2] = graph.getPossibleRelatedValue(model, sr.HasOutputInterval); + PlaybackConfiguration config = new PlaybackConfiguration(); config.simulationDuration = numbers[1] - numbers[0] - time; config.playbackDuration = (long) (config.simulationDuration / (config.simulationDuration + time) * playbackDuration); @@ -227,37 +241,40 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam config.simulationStepLength = config.simulationDuration / config.intervals; config.endTime = numbers[1]; config.startTime = numbers[0]; + + this.startTime = config.startTime; + this.endTime = config.endTime; return config; } - + private class PlaybackConfiguration { public double simulationDuration, simulationStepLength, intervals, endTime, startTime; public long playbackDuration; } - + protected void localStateChange() { super.localStateChange(); - + ExperimentState state = getState(); if(ExperimentState.DISPOSED.equals(state)) { stopPlayback(); } } - + // TIME LISTENERS public void addTimeListener(Runnable timeListener) { if(!this.timeListeners.contains(timeListener)) this.timeListeners.add(timeListener); } - + public Collection getTimeListeners() { return this.timeListeners; } - + public void removeTimeListener(Runnable timeListener) { this.timeListeners.remove(timeListener); } - + @Override public void fireValuesChanged() { for(Runnable listener : timeListeners) { @@ -266,5 +283,5 @@ public class SysdynPlaybackExperiment extends SysdynExperiment implements IDynam super.fireValuesChanged(); } - + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java index f0030c8c..f95cde94 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/StockExpression.java @@ -11,11 +11,14 @@ *******************************************************************************/ package org.simantics.sysdyn.representation.expressions; +import java.io.StringReader; import java.util.ArrayList; import java.util.Iterator; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.expressionParser.ExpressionParser; +import org.simantics.sysdyn.expressionParser.ParseException; import org.simantics.sysdyn.representation.ArrayIndexes; import org.simantics.sysdyn.representation.Enumeration; import org.simantics.sysdyn.representation.IndependentVariable; @@ -32,7 +35,10 @@ public class StockExpression extends Expression { @Override public String getDeclaration(IndependentVariable variable) { - Double value = getStartValue(variable); +// Double value = getStartValue(variable); + String value = null; + if(useStartValue()) + value = initialEquation; ArrayIndexes ai = variable.getArrayIndexes(); ArrayList enumerations = null; @@ -55,11 +61,13 @@ public class StockExpression extends Expression { } String each = ""; - if(ai != null && !ai.getEnumerations().isEmpty()) - each = "each"; if (value == null) { + if(ai != null && !ai.getEnumerations().isEmpty()) + each = "each"; return " " + variable.getType() + " " + variable.getName() + range + "(" + each + " fixed=false);\n"; } else { + if(ai != null && !ai.getEnumerations().isEmpty() && getStartValue(variable) != null) + each = "each"; return " " + variable.getType() + " " + variable.getName() + range + "(" + each+ " start=" + value + "," + each + " fixed=true);\n"; } } @@ -101,15 +109,27 @@ public class StockExpression extends Expression { b.append(";\n"); return b.toString(); } + + private boolean useStartValue() { + ExpressionParser parser = new ExpressionParser(new StringReader(initialEquation)); + try { + parser.expr(); + return parser.getReferences().isEmpty(); + } catch (ParseException e) { + } + return false; + } @Override public String getInitialEquation(IndependentVariable variable) { - try { - Double.parseDouble(initialEquation); - return null; - } catch (Exception e){ - // Has an initial equation - } +// try { +// Double.parseDouble(initialEquation); +// return null; +// } catch (Exception e){ +// // Has an initial equation +// } + if(useStartValue()) + return null; String equation = FormatUtils.formatExpressionForModelica(variable, initialEquation); String range = IndexUtils.rangeToIndexes(variable, this.getArrayRange()); if(range == null) @@ -125,8 +145,11 @@ public class StockExpression extends Expression { if(expressions.size() == 1) { IExpression e = expressions.get(0); if(e.getInitialEquation(variable) == null) { - // Has start value - value = Double.parseDouble(initialEquation); + try { + value = Double.parseDouble(initialEquation); + } catch(NumberFormatException e1) { + + } } } return value;