From 15ba5d5a7ae1606a28c93bcb0b409cc0f12139b6 Mon Sep 17 00:00:00 2001 From: lempinen Date: Tue, 29 Jun 2010 14:48:44 +0000 Subject: [PATCH] First introduction of modules: basic user-created module with a few variables. Modelicawriter can flatten the model but not make connections between modules git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@16426 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/adapters.xml | 2 +- org.simantics.sysdyn.ui/plugin.xml | 14 ++- .../browser/contributions/Configuration.java | 44 +++++++ .../ui/browser/contributions/Module.java | 45 +++++++ .../browser/contributions/ModuleImager.java | 16 +++ .../browser/contributions/ModuleLabeler.java | 37 ++++++ .../ui/browser/contributions/Variable.java | 49 -------- .../sysdyn/ui/browser/nodes/ModuleNode.java | 11 ++ .../sysdyn/ui/equation/EquationView.java | 2 +- .../sysdyn/ui/equation/NameComposite.java | 2 +- .../sysdyn/ui/properties/ModuleTab.java | 15 +++ .../ResourceSelectionProcessor.java | 7 ++ .../properties/widgets/ExpressionWidget.java | 13 +- .../expressions/ExpressionValidation.java | 2 +- .../org/simantics/sysdyn/SysdynResource.java | 12 +- .../simantics/sysdyn/manager/SysdynModel.java | 4 +- .../sysdyn/modelica/ModelicaWriter.java | 102 +++++++++------ .../sysdyn/representation/Configuration.java | 29 +++-- .../sysdyn/representation/IModule.java | 11 ++ .../sysdyn/representation/Module.java | 62 ++++++++++ .../sysdyn/representation/SysdynSchema.java | 1 + .../sysdyn/representation/Variable.java | 2 - .../visitors/ElementVisitorVoidAdapter.java | 9 ++ .../visitors/IElementVisitorVoid.java | 5 +- .../sysdyn/simulation/SimulationJob.java | 2 +- sysdyn_ontologies/sysdyn.graph | 116 ++++++++++++++++-- 26 files changed, 494 insertions(+), 120 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Configuration.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Module.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleImager.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java delete mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Variable.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java diff --git a/org.simantics.sysdyn.ui/adapters.xml b/org.simantics.sysdyn.ui/adapters.xml index 4c6b40d2..2b16b667 100644 --- a/org.simantics.sysdyn.ui/adapters.xml +++ b/org.simantics.sysdyn.ui/adapters.xml @@ -25,5 +25,5 @@ - none 0 fill 1 + none 0 fill 1 \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 4d1fce63..e3c3fe68 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -510,7 +510,7 @@ + + + + + + { + + @Override + public Collection getContribution(ReadGraph graph, ConfigurationNode configuration) throws DatabaseException { + ArrayList result = new ArrayList(); + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + for(Resource r : graph.syncRequest(new ObjectsWithType(configuration.resource, b.ConsistsOf, sr.IndependentVariable))) { + try { + result.add(graph.adapt(r, AbstractNode.class)); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + for(Resource r : graph.syncRequest(new ObjectsWithType(configuration.resource, b.ConsistsOf, sr.Module))) { + try { + result.add(graph.adapt(r, AbstractNode.class)); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + return result; + } + + @Override + public String getViewpointId() { + return "Standard"; + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Module.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Module.java new file mode 100644 index 00000000..4ba24a41 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Module.java @@ -0,0 +1,45 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +import org.simantics.db.Builtins; +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.sysdyn.SysdynResource; +import org.simantics.sysdyn.ui.browser.nodes.AbstractNode; +import org.simantics.sysdyn.ui.browser.nodes.ModuleNode; + +public class Module extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, ModuleNode module) throws DatabaseException { + ArrayList result = new ArrayList(); + Builtins b = graph.getBuiltins(); + SysdynResource sr = SysdynResource.getInstance(graph); + for(Resource r : graph.syncRequest(new ObjectsWithType(module.resource, b.ConsistsOf, sr.IndependentVariable))) { + try { + result.add(graph.adapt(r, AbstractNode.class)); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + for(Resource r : graph.syncRequest(new ObjectsWithType(module.resource, b.ConsistsOf, sr.Module))) { + try { + result.add(graph.adapt(r, AbstractNode.class)); + } catch(DatabaseException e) { + e.printStackTrace(); + } + } + return result; + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleImager.java new file mode 100644 index 00000000..b6ecea38 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleImager.java @@ -0,0 +1,16 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.simantics.browsing.ui.swt.ImagerContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.Activator; +import org.simantics.sysdyn.ui.browser.nodes.ModuleNode; + +public class ModuleImager extends ImagerContributor { + + @Override + public ImageDescriptor getDescriptor(ReadGraph graph, ModuleNode node) throws DatabaseException { + return ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource("icons/bricks.png")); + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java new file mode 100644 index 00000000..78e02ffe --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/ModuleLabeler.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 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.browser.contributions; + +import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor; +import org.simantics.db.Builtins; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.ModuleNode; + +public class ModuleLabeler extends LabelerContributor{ + + @Override + public String getLabel(ReadGraph graph, ModuleNode module) throws DatabaseException { + Builtins b = graph.getBuiltins(); + Resource resource = module.resource; + StringBuilder sb = new StringBuilder(); + for(Resource r : graph.getObjects(resource, b.HasName)) + sb.append(graph.getValue(r)); + sb.append(" : "); + for(Resource t : graph.getObjects(resource, b.InstanceOf)) + for(Resource r : graph.getObjects(t, b.HasName)) + sb.append(graph.getValue(r)); + return sb.toString(); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Variable.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Variable.java deleted file mode 100644 index 333f1c25..00000000 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Variable.java +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 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.browser.contributions; - -import java.util.ArrayList; -import java.util.Collection; - -import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; -import org.simantics.db.Builtins; -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.sysdyn.SysdynResource; -import org.simantics.sysdyn.ui.browser.nodes.AbstractNode; -import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode; - -public class Variable extends ViewpointContributor { - - @Override - public Collection getContribution(ReadGraph graph, ConfigurationNode configuration) throws DatabaseException { - ArrayList result = new ArrayList(); - Builtins b = graph.getBuiltins(); - SysdynResource sr = SysdynResource.getInstance(graph); - for(Resource r : graph.syncRequest(new ObjectsWithType(configuration.resource, b.ConsistsOf, sr.IndependentVariable))) { - try { - result.add(graph.adapt(r, AbstractNode.class)); - } catch(DatabaseException e) { - e.printStackTrace(); - } - } - return result; - } - - @Override - public String getViewpointId() { - return "Standard"; - } - -} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java new file mode 100644 index 00000000..1689c075 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleNode.java @@ -0,0 +1,11 @@ +package org.simantics.sysdyn.ui.browser.nodes; + +import org.simantics.browsing.ui.common.node.IDeletable; +import org.simantics.db.Resource; + +public class ModuleNode extends AbstractNode implements IDeletable { + + public ModuleNode(Resource resource) { + super(resource); + } +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java index 2e155544..b3015879 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/EquationView.java @@ -404,7 +404,7 @@ public class EquationView extends ViewPart implements ISelectionListener { SysdynModelManager sdm = SysdynModelManager.getInstance(SimanticsUI.getSession()); SysdynModel model = sdm.getModel(configuration); Configuration conf = model.getConfiguration(); - ArrayList elements = conf.elements; + ArrayList elements = conf.getElements(); for(IElement e : elements) { if(e instanceof Variable) { Variable v = (Variable) e; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java index f5418efa..fbc5fef6 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/equation/NameComposite.java @@ -116,7 +116,7 @@ public class NameComposite extends Composite { SysdynModel model = sdm.getModel(rei.getResource()); Configuration conf = model.getConfiguration(); IElement thisElement = model.getElement(variable); - ArrayList elements = conf.elements; + ArrayList elements = conf.getElements(); for(IElement e : elements) { if(e instanceof Variable) { Variable v = (Variable)e; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java new file mode 100644 index 00000000..5977c09c --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ModuleTab.java @@ -0,0 +1,15 @@ +package org.simantics.sysdyn.ui.properties; + +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IWorkbenchSite; +import org.simantics.browsing.ui.swt.PropertyTabContributorImpl; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.management.ISessionContext; + +public class ModuleTab extends PropertyTabContributorImpl { + + @Override + public void createControls(Composite body, IWorkbenchSite site, ISessionContext context, WidgetSupport support) { + + } +} 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 18fd9e91..2f2bc298 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 @@ -62,6 +62,13 @@ public class ResourceSelectionProcessor implements SelectionProcessor elements = conf.elements; + ArrayList elements = conf.getElements(); for(IElement e : elements) { if(e instanceof Variable) { Variable v = (Variable) e; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java index 723dc234..e847edb4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -42,11 +42,13 @@ public class SysdynResource { public final Resource HasExpression; public final Resource HasHead; public final Resource HasInitialEquation; + public final Resource HasInput; public final Resource HasLookup; public final Resource HasMaxX; public final Resource HasMaxY; public final Resource HasMinX; public final Resource HasMinY; + public final Resource HasOutput; public final Resource HasParameterFile; public final Resource HasRangeEnd; public final Resource HasRangeStart; @@ -57,11 +59,11 @@ public class SysdynResource { public final Resource HasStopTime; public final Resource HasTail; public final Resource HasTerminal; - public final Resource HasType; public final Resource HasUnit; public final Resource HasX; public final Resource HasY; public final Resource IndependentVariable; + public final Resource Input; public final Resource IsHeadOf; public final Resource IsHeadOfTerminal; public final Resource IsTailOf; @@ -110,11 +112,13 @@ public class SysdynResource { public static final String HasExpression = "http://www.simantics.org/Sysdyn-1.0/HasExpression"; public static final String HasHead = "http://www.simantics.org/Sysdyn-1.0/HasHead"; public static final String HasInitialEquation = "http://www.simantics.org/Sysdyn-1.0/HasInitialEquation"; + public static final String HasInput = "http://www.simantics.org/Sysdyn-1.0/HasInput"; public static final String HasLookup = "http://www.simantics.org/Sysdyn-1.0/HasLookup"; public static final String HasMaxX = "http://www.simantics.org/Sysdyn-1.0/HasMaxX"; public static final String HasMaxY = "http://www.simantics.org/Sysdyn-1.0/HasMaxY"; public static final String HasMinX = "http://www.simantics.org/Sysdyn-1.0/HasMinX"; public static final String HasMinY = "http://www.simantics.org/Sysdyn-1.0/HasMinY"; + public static final String HasOutput = "http://www.simantics.org/Sysdyn-1.0/HasOutput"; public static final String HasParameterFile = "http://www.simantics.org/Sysdyn-1.0/HasParameterFile"; public static final String HasRangeEnd = "http://www.simantics.org/Sysdyn-1.0/HasRangeEnd"; public static final String HasRangeStart = "http://www.simantics.org/Sysdyn-1.0/HasRangeStart"; @@ -125,11 +129,11 @@ public class SysdynResource { public static final String HasStopTime = "http://www.simantics.org/Sysdyn-1.0/HasStopTime"; public static final String HasTail = "http://www.simantics.org/Sysdyn-1.0/HasTail"; public static final String HasTerminal = "http://www.simantics.org/Sysdyn-1.0/HasTerminal"; - public static final String HasType = "http://www.simantics.org/Sysdyn-1.0/HasType"; public static final String HasUnit = "http://www.simantics.org/Sysdyn-1.0/HasUnit"; public static final String HasX = "http://www.simantics.org/Sysdyn-1.0/HasX"; public static final String HasY = "http://www.simantics.org/Sysdyn-1.0/HasY"; public static final String IndependentVariable = "http://www.simantics.org/Sysdyn-1.0/IndependentVariable"; + public static final String Input = "http://www.simantics.org/Sysdyn-1.0/Input"; public static final String IsHeadOf = "http://www.simantics.org/Sysdyn-1.0/IsHeadOf"; public static final String IsHeadOfTerminal = "http://www.simantics.org/Sysdyn-1.0/IsHeadOfTerminal"; public static final String IsTailOf = "http://www.simantics.org/Sysdyn-1.0/IsTailOf"; @@ -188,11 +192,13 @@ public class SysdynResource { HasExpression = getResourceOrNull(graph, URIs.HasExpression); HasHead = getResourceOrNull(graph, URIs.HasHead); HasInitialEquation = getResourceOrNull(graph, URIs.HasInitialEquation); + HasInput = getResourceOrNull(graph, URIs.HasInput); HasLookup = getResourceOrNull(graph, URIs.HasLookup); HasMaxX = getResourceOrNull(graph, URIs.HasMaxX); HasMaxY = getResourceOrNull(graph, URIs.HasMaxY); HasMinX = getResourceOrNull(graph, URIs.HasMinX); HasMinY = getResourceOrNull(graph, URIs.HasMinY); + HasOutput = getResourceOrNull(graph, URIs.HasOutput); HasParameterFile = getResourceOrNull(graph, URIs.HasParameterFile); HasRangeEnd = getResourceOrNull(graph, URIs.HasRangeEnd); HasRangeStart = getResourceOrNull(graph, URIs.HasRangeStart); @@ -203,11 +209,11 @@ public class SysdynResource { HasStopTime = getResourceOrNull(graph, URIs.HasStopTime); HasTail = getResourceOrNull(graph, URIs.HasTail); HasTerminal = getResourceOrNull(graph, URIs.HasTerminal); - HasType = getResourceOrNull(graph, URIs.HasType); HasUnit = getResourceOrNull(graph, URIs.HasUnit); HasX = getResourceOrNull(graph, URIs.HasX); HasY = getResourceOrNull(graph, URIs.HasY); IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable); + Input = getResourceOrNull(graph, URIs.Input); IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf); IsHeadOfTerminal = getResourceOrNull(graph, URIs.IsHeadOfTerminal); IsTailOf = getResourceOrNull(graph, URIs.IsTailOf); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java index 0d888a64..1aea00e4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/manager/SysdynModel.java @@ -63,7 +63,7 @@ public class SysdynModel implements IMappingListener, IModel { SysdynSchema schema = new SysdynSchema(g); mapping = Mappings.createWithListening(schema); mapping.addMappingListener(SysdynModel.this); - configuration = (Configuration)mapping.map(g, configurationResource); + configuration = (Configuration)mapping.map(g, configurationResource); } public SysdynModel(ReadGraph g, Resource configurationResource) { @@ -91,7 +91,7 @@ public class SysdynModel implements IMappingListener, IModel { writer.write(configuration); result = ModelicaManager.runModelica( - configuration.name, + configuration.getName(), writer.toString(), monitor, configuration.startTime, diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java index 8ba54a90..b7072dcc 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -11,8 +11,12 @@ *******************************************************************************/ package org.simantics.sysdyn.modelica; +import java.util.ArrayList; + import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.IModule; +import org.simantics.sysdyn.representation.Module; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Variable; import org.simantics.sysdyn.representation.expressions.IExpression; @@ -26,43 +30,9 @@ public class ModelicaWriter { boolean insertInterpolate = false; public void write(Configuration conf) { - conf.update(); - b.append("model ").append(conf.name.replace(" ", "")).append('\n'); - for(IElement element : conf.elements) - if(element instanceof Variable) { - IExpression expr = ((Variable)element).expression; - if(expr != null) { - app = expr.getDeclaration((Variable)element); - if (app != null) b.append(app); - if (expr instanceof WithLookupExpression) insertInterpolate = true; - } - } - - for(IElement element : conf.elements) - if(element instanceof Stock) { - IExpression expr = ((Stock)element).expression; - if(expr != null) { - app = expr.getInitialEquation((Stock)element); - if (app != null) { - if(initialEquations == false) { - initialEquations = true; - b.append("initial equation\n"); - } - b.append(app); - } - } - } - b.append("equation\n"); - for(IElement element : conf.elements) - if(element instanceof Variable) { - IExpression expr = ((Variable)element).expression; - if(expr != null) { - app = expr.getEquation((Variable)element); - if (app != null) b.append(app); - } - } - b.append("end ").append(conf.name.replace(" ", "")).append(";\n"); + writeModule(conf); + if(insertInterpolate) b.append( "\nfunction Interpolate\n" + @@ -81,10 +51,68 @@ public class ModelicaWriter { " end if;\n" + " end for;\n" + " end Interpolate;\n"); + } + + private void writeModule(IModule module) { + module.update(); + + ArrayList variables = new ArrayList(); + ArrayList modules = new ArrayList(); + ArrayList stocks = new ArrayList(); + + // Initialize lists + for(IElement element : module.getElements()) { + if(element instanceof Variable) { + variables.add(element); + if(element instanceof Stock) + stocks.add(element); + } else if (element instanceof Module) { + modules.add((Module)element); + } + } - } + b.append("class ").append(module.getName().replace(" ", "")).append('\n'); + for(IElement element : variables) { + IExpression expr = ((Variable)element).expression; + if(expr != null && insertInterpolate == false) { + app = expr.getDeclaration((Variable)element); + if (app != null) b.append(app); + if (expr instanceof WithLookupExpression) insertInterpolate = true; + } + } + + for(IElement element : stocks) { + IExpression expr = ((Stock)element).expression; + if(expr != null) { + app = expr.getInitialEquation((Stock)element); + if (app != null) { + if(initialEquations == false) { + initialEquations = true; + b.append("initial equation\n"); + } + b.append(app); + } + } + } + + b.append("equation\n"); + for(IElement element : variables) { + IExpression expr = ((Variable)element).expression; + if(expr != null) { + app = expr.getEquation((Variable)element); + if (app != null) b.append(app); + } + } + b.append("end ").append(module.getName().replace(" ", "")).append(";\n\n"); + + + for(Module m : modules) { + writeModule(m); + } + } + public String escape(String name) { return name.replace(' ', '_'); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java index cc0df887..2308eaa4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java @@ -21,9 +21,7 @@ import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; @GraphType("http://www.simantics.org/Sysdyn-1.0/Configuration") -public class Configuration { - @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") - public String name = "Unnamed"; +public class Configuration implements IModule { @RelatedValue("http://www.simantics.org/Sysdyn-1.0/HasStartTime") public Double startTime = 0.0; @@ -31,18 +29,21 @@ public class Configuration { @RelatedValue("http://www.simantics.org/Sysdyn-1.0/HasStopTime") public Double stopTime = 10.0; + @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") + private String name; + @RelatedElements( - value = "http://www.simantics.org/Layer0-1.0/ConsistsOf", - composition = true) - public ArrayList elements = new ArrayList(); - + value = "http://www.simantics.org/Layer0-1.0/ConsistsOf", + composition = true) + private ArrayList elements = new ArrayList(); + IElementVisitorVoid clearVisitor = new ElementVisitorVoidAdapter() { public void visit(Stock stock) { stock.incomingFlows.clear(); stock.outgoingFlows.clear(); } }; - + IElementVisitorVoid updateVisitor = new ElementVisitorVoidAdapter() { public void visit(Flow flow) { if(flow.head instanceof Stock) { @@ -60,4 +61,16 @@ public class Configuration { for(IElement element : elements) element.accept(updateVisitor); } + + @Override + public ArrayList getElements() { + return elements; + } + + @Override + public String getName() { + return name; + } + + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java new file mode 100644 index 00000000..af462ef3 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IModule.java @@ -0,0 +1,11 @@ +package org.simantics.sysdyn.representation; + +import java.util.ArrayList; + +public interface IModule { + + public ArrayList getElements(); + public String getName(); + public void update(); + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java new file mode 100644 index 00000000..b19e804f --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -0,0 +1,62 @@ +package org.simantics.sysdyn.representation; + +import java.util.ArrayList; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedElements; +import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn-1.0/Module") +public class Module implements IElement, IModule { + + @RelatedValue("http://www.simantics.org/Layer0-1.0/HasName") + private String name; + + @RelatedElements( + value = "http://www.simantics.org/Layer0-1.0/ConsistsOf", + composition = true) + private ArrayList elements = new ArrayList(); + + IElementVisitorVoid clearVisitor = new ElementVisitorVoidAdapter() { + public void visit(Stock stock) { + stock.incomingFlows.clear(); + stock.outgoingFlows.clear(); + } + }; + + IElementVisitorVoid updateVisitor = new ElementVisitorVoidAdapter() { + public void visit(Flow flow) { + if(flow.head instanceof Stock) { + ((Stock)flow.head).incomingFlows.add((Variable)flow.tail); + } + else if(flow.tail instanceof Stock) { + ((Stock)flow.tail).outgoingFlows.add((Variable)flow.head); + } + } + }; + + public void update() { + for(IElement element : elements) + element.accept(clearVisitor); + for(IElement element : elements) + element.accept(updateVisitor); + } + + + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } + + @Override + public ArrayList getElements() { + return elements; + } + + @Override + public String getName() { + return name; + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java index 0ce8a6e3..a4279318 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/SysdynSchema.java @@ -33,6 +33,7 @@ public class SysdynSchema extends SimpleSchema { addLinkType(MappingSchemas.fromAnnotations(g, Flow.class)); addLinkType(MappingSchemas.fromAnnotations(g, Stock.class)); addLinkType(MappingSchemas.fromAnnotations(g, Valve.class)); + addLinkType(MappingSchemas.fromAnnotations(g, Module.class)); addLinkType(MappingSchemas.fromAnnotations(g, NormalExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, ParameterExpression.class)); addLinkType(MappingSchemas.fromAnnotations(g, StockExpression.class)); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java index 10372b32..6c0d58c5 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -20,8 +20,6 @@ public abstract class Variable implements IElement { protected String name; @RelatedValue("http://www.simantics.org/Layer0-1.0/HasType") protected String type; - //@RelatedValue("http://www.simantics.org/Sysdyn-1.0/HasValue") - //public String value; @RelatedElement("http://www.simantics.org/Sysdyn-1.0/HasExpression") public IExpression expression; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java index ecb0c11f..b5f37225 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java @@ -13,8 +13,10 @@ package org.simantics.sysdyn.representation.visitors; import org.simantics.sysdyn.representation.Auxiliary; import org.simantics.sysdyn.representation.Cloud; +import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.Dependency; import org.simantics.sysdyn.representation.Flow; +import org.simantics.sysdyn.representation.Module; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Valve; @@ -43,5 +45,12 @@ public class ElementVisitorVoidAdapter implements IElementVisitorVoid { @Override public void visit(Flow flow) { } + + @Override + public void visit(Module module) { + } + @Override + public void visit(Configuration configuration) { + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java index 3e12a3d2..a80acc0d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java @@ -13,8 +13,10 @@ package org.simantics.sysdyn.representation.visitors; import org.simantics.sysdyn.representation.Auxiliary; import org.simantics.sysdyn.representation.Cloud; +import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.Dependency; import org.simantics.sysdyn.representation.Flow; +import org.simantics.sysdyn.representation.Module; import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Valve; @@ -26,5 +28,6 @@ public interface IElementVisitorVoid { void visit(Cloud cloud); void visit(Dependency dependency); void visit(Flow flow); - + void visit(Module module); + void visit(Configuration configuration); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java index 5b086659..172d3275 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/simulation/SimulationJob.java @@ -29,7 +29,7 @@ public class SimulationJob extends Job { ModelicaMonitor monitor; public SimulationJob(SysdynModel model) { - super("Simulate " + model.getConfiguration().name); + super("Simulate " + model.getConfiguration().getName()); this.model = model; this.monitor = new ModelicaMonitor(); } diff --git a/sysdyn_ontologies/sysdyn.graph b/sysdyn_ontologies/sysdyn.graph index adbc0f3b..a38e4853 100644 --- a/sysdyn_ontologies/sysdyn.graph +++ b/sysdyn_ontologies/sysdyn.graph @@ -53,6 +53,11 @@ SysdynProject : PROJ.Feature PROJ.RequiresFeature MOD.ModelingProject PROJ.RequiresFeature PROJ.ExperimentControl + +SysdynModuleTestProject : PROJ.Project2 + PROJ.HasFeature SysdynProject + L0.PartOf L0.Projects + ###################################################################### # Model ###################################################################### @@ -88,8 +93,8 @@ SymbolReferences : DIA.SymbolReferenceLibrary L0.ConsistsOf BasicSymbolLibrary @ "Basic Symbols" : DIA.SymbolReferenceLibrary -Configuration