From 3f9241299c38bcc1e11794d5a18bda9e6ceb315b Mon Sep 17 00:00:00 2001 From: niemisto Date: Wed, 18 Nov 2009 13:40:38 +0000 Subject: [PATCH] git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@13134 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn/.classpath | 7 ++ org.simantics.sysdyn/.hgignore | 5 + org.simantics.sysdyn/.project | 28 +++++ .../.settings/org.eclipse.jdt.core.prefs | 8 ++ org.simantics.sysdyn/META-INF/MANIFEST.MF | 12 +++ org.simantics.sysdyn/build.properties | 4 + .../org/simantics/sysdyn/SysdynResource.java | 102 ++++++++++++++++++ .../sysdyn/modelica/ModelicaWriter.java | 53 +++++++++ .../sysdyn/representation/Auxiliary.java | 15 +++ .../sysdyn/representation/Cloud.java | 12 +++ .../sysdyn/representation/Configuration.java | 43 ++++++++ .../sysdyn/representation/Dependency.java | 18 ++++ .../simantics/sysdyn/representation/Flow.java | 18 ++++ .../sysdyn/representation/IElement.java | 7 ++ .../sysdyn/representation/Stock.java | 23 ++++ .../sysdyn/representation/Valve.java | 12 +++ .../sysdyn/representation/Variable.java | 8 ++ .../visitors/ElementVisitorVoidAdapter.java | 36 +++++++ .../visitors/IElementVisitorVoid.java | 19 ++++ 19 files changed, 430 insertions(+) create mode 100644 org.simantics.sysdyn/.classpath create mode 100644 org.simantics.sysdyn/.hgignore create mode 100644 org.simantics.sysdyn/.project create mode 100644 org.simantics.sysdyn/.settings/org.eclipse.jdt.core.prefs create mode 100644 org.simantics.sysdyn/META-INF/MANIFEST.MF create mode 100644 org.simantics.sysdyn/build.properties create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Cloud.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IElement.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java diff --git a/org.simantics.sysdyn/.classpath b/org.simantics.sysdyn/.classpath new file mode 100644 index 00000000..8a8f1668 --- /dev/null +++ b/org.simantics.sysdyn/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/org.simantics.sysdyn/.hgignore b/org.simantics.sysdyn/.hgignore new file mode 100644 index 00000000..73df90f6 --- /dev/null +++ b/org.simantics.sysdyn/.hgignore @@ -0,0 +1,5 @@ +syntax: regexp +^bin/ + +syntax: glob +*.svn/* \ No newline at end of file diff --git a/org.simantics.sysdyn/.project b/org.simantics.sysdyn/.project new file mode 100644 index 00000000..05d44396 --- /dev/null +++ b/org.simantics.sysdyn/.project @@ -0,0 +1,28 @@ + + + org.simantics.sysdyn + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/org.simantics.sysdyn/.settings/org.eclipse.jdt.core.prefs b/org.simantics.sysdyn/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..785aae6f --- /dev/null +++ b/org.simantics.sysdyn/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,8 @@ +#Mon Nov 16 15:37:44 EET 2009 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF new file mode 100644 index 00000000..6e6d17f2 --- /dev/null +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -0,0 +1,12 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Sysdyn +Bundle-SymbolicName: org.simantics.sysdyn +Bundle-Version: 1.0.0.qualifier +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Require-Bundle: org.simantics.objmap;bundle-version="0.1.0", + org.simantics.db;bundle-version="0.6.2" +Export-Package: org.simantics.sysdyn, + org.simantics.sysdyn.modelica, + org.simantics.sysdyn.representation, + org.simantics.sysdyn.representation.visitors diff --git a/org.simantics.sysdyn/build.properties b/org.simantics.sysdyn/build.properties new file mode 100644 index 00000000..41eb6ade --- /dev/null +++ b/org.simantics.sysdyn/build.properties @@ -0,0 +1,4 @@ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java new file mode 100644 index 00000000..01448ff3 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/SysdynResource.java @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2007- VTT Technical Research Centre of Finland. + * 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; + + +import org.simantics.db.Resource; +import org.simantics.db.ReadGraph; +import org.simantics.db.Session; +import org.simantics.db.exception.DatabaseException; + +public class SysdynResource { + + public final Resource Auxiliary; + public final Resource Cloud; + public final Resource Configuration; + public final Resource Dependency; + public final Resource Flow; + public final Resource HasAngle; + public final Resource HasHead; + public final Resource HasTail; + public final Resource HasValue; + public final Resource HasX; + public final Resource HasY; + public final Resource IndependentVariable; + public final Resource IsHeadOf; + public final Resource IsTailOf; + public final Resource Stock; + public final Resource SysdynProject; + public final Resource Valve; + public final Resource Variable; + + public static class URIs { + public static final String Auxiliary = "http://www.simantics.org/Sysdyn#Auxiliary"; + public static final String Cloud = "http://www.simantics.org/Sysdyn#Cloud"; + public static final String Configuration = "http://www.simantics.org/Sysdyn#Configuration"; + public static final String Dependency = "http://www.simantics.org/Sysdyn#Dependency"; + public static final String Flow = "http://www.simantics.org/Sysdyn#Flow"; + public static final String HasAngle = "http://www.simantics.org/Sysdyn#HasAngle"; + public static final String HasHead = "http://www.simantics.org/Sysdyn#HasHead"; + public static final String HasTail = "http://www.simantics.org/Sysdyn#HasTail"; + public static final String HasValue = "http://www.simantics.org/Sysdyn#HasValue"; + public static final String HasX = "http://www.simantics.org/Sysdyn#HasX"; + public static final String HasY = "http://www.simantics.org/Sysdyn#HasY"; + public static final String IndependentVariable = "http://www.simantics.org/Sysdyn#IndependentVariable"; + public static final String IsHeadOf = "http://www.simantics.org/Sysdyn#IsHeadOf"; + public static final String IsTailOf = "http://www.simantics.org/Sysdyn#IsTailOf"; + public static final String Stock = "http://www.simantics.org/Sysdyn#Stock"; + public static final String SysdynProject = "http://www.simantics.org/Sysdyn#SysdynProject"; + public static final String Valve = "http://www.simantics.org/Sysdyn#Valve"; + public static final String Variable = "http://www.simantics.org/Sysdyn#Variable"; + } + + public static Resource getResourceOrNull(ReadGraph graph, String uri) { + try { + return graph.getResourceByURI(uri); + } catch(DatabaseException e) { + System.err.println(e.getMessage()); + return null; + } + } + + public SysdynResource(ReadGraph graph) { + Auxiliary = getResourceOrNull(graph, URIs.Auxiliary); + Cloud = getResourceOrNull(graph, URIs.Cloud); + Configuration = getResourceOrNull(graph, URIs.Configuration); + Dependency = getResourceOrNull(graph, URIs.Dependency); + Flow = getResourceOrNull(graph, URIs.Flow); + HasAngle = getResourceOrNull(graph, URIs.HasAngle); + HasHead = getResourceOrNull(graph, URIs.HasHead); + HasTail = getResourceOrNull(graph, URIs.HasTail); + HasValue = getResourceOrNull(graph, URIs.HasValue); + HasX = getResourceOrNull(graph, URIs.HasX); + HasY = getResourceOrNull(graph, URIs.HasY); + IndependentVariable = getResourceOrNull(graph, URIs.IndependentVariable); + IsHeadOf = getResourceOrNull(graph, URIs.IsHeadOf); + IsTailOf = getResourceOrNull(graph, URIs.IsTailOf); + Stock = getResourceOrNull(graph, URIs.Stock); + SysdynProject = getResourceOrNull(graph, URIs.SysdynProject); + Valve = getResourceOrNull(graph, URIs.Valve); + Variable = getResourceOrNull(graph, URIs.Variable); + } + + public static SysdynResource getInstance(ReadGraph graph) { + Session session = graph.getSession(); + SysdynResource ret = session.getService(SysdynResource.class); + if(ret == null) { + ret = new SysdynResource(graph); + session.registerService(SysdynResource.class, ret); + } + return ret; + } + +} + diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java new file mode 100644 index 00000000..a63897e5 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/modelica/ModelicaWriter.java @@ -0,0 +1,53 @@ +package org.simantics.sysdyn.modelica; + +import org.simantics.sysdyn.representation.Auxiliary; +import org.simantics.sysdyn.representation.Configuration; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.Stock; +import org.simantics.sysdyn.representation.Valve; +import org.simantics.sysdyn.representation.visitors.ElementVisitorVoidAdapter; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +public class ModelicaWriter { + + StringBuilder b; + + IElementVisitorVoid componentWriter = new ElementVisitorVoidAdapter() { + public void visit(Auxiliary auxiliary) { + b.append(" Real ").append(escape(auxiliary.name)).append(";\n"); + } + public void visit(Stock stock) { + b.append(" Real ").append(escape(stock.name)).append(";\n"); + } + public void visit(Valve valve) { + // TODO + } + }; + + IElementVisitorVoid equationWriter = new ElementVisitorVoidAdapter() { + public void visit(Auxiliary auxiliary) { + b.append(" ").append(escape(auxiliary.name)) + .append(" = ").append(auxiliary.value).append(";\n"); + } + public void visit(Stock stock) { + } + public void visit(Valve valve) { + } + }; + + public void write(Configuration conf) { + conf.update(); + b.append("model ").append(conf.name).append('\n'); + for(IElement element : conf.elements) + element.accept(componentWriter); + b.append("equation\n"); + for(IElement element : conf.elements) + element.accept(equationWriter); + b.append("end ").append(conf.name).append(";\n"); + } + + public String escape(String name) { + return name; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java new file mode 100644 index 00000000..97cb4465 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Auxiliary.java @@ -0,0 +1,15 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn#Auxiliary") +public class Auxiliary extends Variable { + // TODO + public String value; + + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Cloud.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Cloud.java new file mode 100644 index 00000000..9c1b74fe --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Cloud.java @@ -0,0 +1,12 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn#Cloud") +public class Cloud implements IElement { + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java new file mode 100644 index 00000000..86c640cd --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Configuration.java @@ -0,0 +1,43 @@ +package org.simantics.sysdyn.representation; + +import java.util.ArrayList; + +import org.simantics.objmap.annotations.GraphType; +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#Configuration") +public class Configuration { + @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName") + public String name = "Unnamed"; + + @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#ConsistsOf") + public 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); + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java new file mode 100644 index 00000000..631d2229 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Dependency.java @@ -0,0 +1,18 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedElement; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn#Cloud") +public class Dependency implements IElement { + @RelatedElement("http://www.simantics.org/Sysdyn#HasTail") + public IElement tail; + @RelatedElement("http://www.simantics.org/Sysdyn#HasHead") + public IElement head; + + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java new file mode 100644 index 00000000..6148dbfc --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Flow.java @@ -0,0 +1,18 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.objmap.annotations.RelatedElement; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn#Cloud") +public class Flow implements IElement { + @RelatedElement("http://www.simantics.org/Sysdyn#HasTail") + public IElement tail; + @RelatedElement("http://www.simantics.org/Sysdyn#HasHead") + public IElement head; + + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IElement.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IElement.java new file mode 100644 index 00000000..4cbaa5d3 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IElement.java @@ -0,0 +1,7 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +public interface IElement { + void accept(IElementVisitorVoid v); +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java new file mode 100644 index 00000000..3287853f --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Stock.java @@ -0,0 +1,23 @@ +package org.simantics.sysdyn.representation; + +import java.util.ArrayList; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn#Stock") +public class Stock extends Variable { + public ArrayList incomingFlows = new ArrayList(2); + public ArrayList outgoingFlows = new ArrayList(2); + + public Stock() { + name = "Valve" + hashCode(); // FIXME + } + + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } +} + + diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java new file mode 100644 index 00000000..ba45a7c9 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Valve.java @@ -0,0 +1,12 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.GraphType; +import org.simantics.sysdyn.representation.visitors.IElementVisitorVoid; + +@GraphType("http://www.simantics.org/Sysdyn#Valve") +public class Valve implements IElement { + @Override + public void accept(IElementVisitorVoid v) { + v.visit(this); + } +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java new file mode 100644 index 00000000..379edf5c --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -0,0 +1,8 @@ +package org.simantics.sysdyn.representation; + +import org.simantics.objmap.annotations.RelatedValue; + +public abstract class Variable implements IElement { + @RelatedValue("http://www.vtt.fi/Simantics/Layer0/1.0/Relations#HasName") + public String name; +} 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 new file mode 100644 index 00000000..d359afc6 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/ElementVisitorVoidAdapter.java @@ -0,0 +1,36 @@ +package org.simantics.sysdyn.representation.visitors; + +import org.simantics.sysdyn.representation.Auxiliary; +import org.simantics.sysdyn.representation.Cloud; +import org.simantics.sysdyn.representation.Dependency; +import org.simantics.sysdyn.representation.Flow; +import org.simantics.sysdyn.representation.Stock; +import org.simantics.sysdyn.representation.Valve; + +public class ElementVisitorVoidAdapter implements IElementVisitorVoid { + + @Override + public void visit(Auxiliary auxiliary) { + } + + @Override + public void visit(Stock stock) { + } + + @Override + public void visit(Valve valve) { + } + + @Override + public void visit(Cloud cloud) { + } + + @Override + public void visit(Dependency dependency) { + } + + @Override + public void visit(Flow flow) { + } + +} 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 new file mode 100644 index 00000000..ce699426 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/visitors/IElementVisitorVoid.java @@ -0,0 +1,19 @@ +package org.simantics.sysdyn.representation.visitors; + +import org.simantics.sysdyn.representation.Auxiliary; +import org.simantics.sysdyn.representation.Cloud; +import org.simantics.sysdyn.representation.Dependency; +import org.simantics.sysdyn.representation.Flow; +import org.simantics.sysdyn.representation.Stock; +import org.simantics.sysdyn.representation.Valve; + +public interface IElementVisitorVoid { + + void visit(Auxiliary auxiliary); + void visit(Stock stock); + void visit(Valve valve); + void visit(Cloud cloud); + void visit(Dependency dependency); + void visit(Flow flow); + +} -- 2.47.1