From: lempinen Date: Tue, 2 Jul 2013 11:26:11 +0000 (+0000) Subject: Created methods for getting readable documentation definitions from variables and... X-Git-Tag: 1.8.1~279 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=0ab45e87cffaac8d6120f6d213ebe4b3a849489b;p=simantics%2Fsysdyn.git Created methods for getting readable documentation definitions from variables and expressions (refs #3906) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27672 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java index 78b36e62..94eb56da 100644 --- a/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java +++ b/org.simantics.jfreechart/src/org/simantics/jfreechart/chart/ChartUtils.java @@ -163,9 +163,7 @@ public class ChartUtils { return realizationURI; } - - public static void writeSVG(org.jfree.chart.JFreeChart chart, Rectangle2D bounds, File file) throws IOException { - + public static void writeSVG(org.jfree.chart.JFreeChart chart, Rectangle2D bounds, Writer out) throws IOException { // Get a DOMImplementation. DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); @@ -179,12 +177,16 @@ public class ChartUtils { svgGenerator.setSVGCanvasSize(new Dimension((int)bounds.getWidth(), (int)bounds.getHeight())); chart.draw(svgGenerator, bounds); - // Finally, stream out SVG to the standard output using - // UTF-8 encoding. + // Finally, write to out + svgGenerator.stream(out, false); + + } + + public static void writeSVG(org.jfree.chart.JFreeChart chart, Rectangle2D bounds, File file) throws IOException { OutputStream outputStream = new FileOutputStream(file); Writer out = new OutputStreamWriter(outputStream, "UTF-8"); - svgGenerator.stream(out, false); + writeSVG(chart, bounds, out); outputStream.flush(); - outputStream.close(); + outputStream.close(); } } diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index 2dea0afb..32671a80 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -1842,6 +1842,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.Charts" preference="2.0"> + + @@ -2009,6 +2013,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.ChartsLabeler" preference="2.0"> + + diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java index 919b02d1..89b3c7aa 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/AbstractNodeImager.java @@ -43,6 +43,7 @@ import org.simantics.sysdyn.ui.browser.nodes.ModelNode; import org.simantics.sysdyn.ui.browser.nodes.ModuleNode; import org.simantics.sysdyn.ui.browser.nodes.ModuleTypeNode; import org.simantics.sysdyn.ui.browser.nodes.ModulesNode; +import org.simantics.sysdyn.ui.browser.nodes.SCLModulesFolder; import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode; import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder; import org.simantics.sysdyn.ui.browser.nodes.SheetNode; @@ -66,6 +67,7 @@ public class AbstractNodeImager extends ImagerContributor node instanceof ModulesNode || node instanceof FunctionsFolder || node instanceof FunctionLibraryNode || + node instanceof SCLModulesFolder || node instanceof SharedFunctionLibraryNode) image = "icons/folder.png"; else if (node instanceof ModuleTypeNode || node instanceof ModuleNode || node instanceof ConfigurationNode) diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java index 01c21fb1..1f0a4458 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/Model.java @@ -56,6 +56,7 @@ public class Model extends ViewpointContributor { result.add(new ModulesNode(model.data)); result.add(new FunctionsFolder(model.data)); result.add(new ChartsFolder(model.data)); +// result.add(new SCLModulesFolder(model.data)); return result; } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModules.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModules.java new file mode 100644 index 00000000..c08691e5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModules.java @@ -0,0 +1,37 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import java.util.ArrayList; +import java.util.Collection; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.browsing.ui.graph.contributor.viewpoint.ViewpointContributor; +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.layer0.Layer0; +import org.simantics.sysdyn.ui.browser.nodes.SCLModule; +import org.simantics.sysdyn.ui.browser.nodes.SCLModulesFolder; + +public class SCLModules extends ViewpointContributor { + + @Override + public Collection getContribution(ReadGraph graph, SCLModulesFolder model) + throws DatabaseException { + + ArrayList> result = new ArrayList>(); + Layer0 l0 = Layer0.getInstance(graph); + + for(Resource r : graph.syncRequest(new ObjectsWithType(model.data, l0.ConsistsOf, L0.SCLModule))) { + result.add(new SCLModule(r)); + } + return result; + + } + + @Override + public String getViewpointId() { + return "Standard"; + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModulesLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModulesLabeler.java new file mode 100644 index 00000000..01e1b055 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModulesLabeler.java @@ -0,0 +1,14 @@ +package org.simantics.sysdyn.ui.browser.contributions; + +import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.SCLModulesFolder; + +public class SCLModulesLabeler extends LabelerContributor{ + + @Override + public String getLabel(ReadGraph graph, SCLModulesFolder input) throws DatabaseException { + return "SCL Modules"; + } +} \ No newline at end of file diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SCLModule.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SCLModule.java new file mode 100644 index 00000000..689d51f1 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SCLModule.java @@ -0,0 +1,12 @@ +package org.simantics.sysdyn.ui.browser.nodes; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.db.Resource; + +public class SCLModule extends AbstractNode { + + public SCLModule(Resource resource) { + super(resource); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SCLModulesFolder.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SCLModulesFolder.java new file mode 100644 index 00000000..8fd7b354 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/SCLModulesFolder.java @@ -0,0 +1,20 @@ +package org.simantics.sysdyn.ui.browser.nodes; + +import org.simantics.browsing.ui.common.node.AbstractNode; +import org.simantics.db.Resource; + +public class SCLModulesFolder extends AbstractNode { + + public SCLModulesFolder(Resource resource) { + super(resource); + } + + @SuppressWarnings("rawtypes") + @Override + public Object getAdapter(Class adapter) { + if(clazz == adapter) // There is no resource for this node.. + return null; + return super.getAdapter(adapter); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java index 5cccc191..19146ffa 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/InputVariableTab.java @@ -34,6 +34,8 @@ import org.simantics.db.exception.DatabaseException; import org.simantics.db.management.ISessionContext; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.ui.properties.widgets.IsOutputWidget; import org.simantics.sysdyn.ui.properties.widgets.UnitComboWidget; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyFactory; @@ -148,7 +150,15 @@ public class InputVariableTab extends LabelPropertyTabContributor { variability = ""; if(variability != null) { + graph.denyValue(input, sr.Variable_variability); graph.claimLiteral(input, sr.Variable_variability, variability); + + Resource conf = graph.getPossibleObject(input, Layer0.getInstance(graph).PartOf); + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + SysdynModel sm = smm.getModel(graph, conf); + sm.getMapping().domainModified(input); + sm.update(graph); + } } diff --git a/org.simantics.sysdyn.ui/sysdyn.product b/org.simantics.sysdyn.ui/sysdyn.product index 2a0177f1..0ce6f714 100644 --- a/org.simantics.sysdyn.ui/sysdyn.product +++ b/org.simantics.sysdyn.ui/sysdyn.product @@ -31,9 +31,8 @@ - - org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6 + org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7 diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index ce2df41d..564a20ba 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -29,7 +29,10 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.1.0", org.simantics.fmu;bundle-version="1.0.0", org.simantics.fmu.me.win32;bundle-version="1.0.0", org.simantics.issues;bundle-version="1.1.0", - org.simantics.issues.common;bundle-version="1.1.0" + org.simantics.issues.common;bundle-version="1.1.0", + org.simantics.jfreechart;bundle-version="1.0.0", + org.jfree.jchart;bundle-version="1.0.13", + org.jfree.jcommon;bundle-version="1.0.16" Export-Package: org.simantics.sysdyn, org.simantics.sysdyn.adapter, org.simantics.sysdyn.expressionParser, diff --git a/org.simantics.sysdyn/plugin.xml b/org.simantics.sysdyn/plugin.xml index 965913ce..711fcb7e 100644 --- a/org.simantics.sysdyn/plugin.xml +++ b/org.simantics.sysdyn/plugin.xml @@ -15,5 +15,12 @@ + + + + diff --git a/org.simantics.sysdyn/scl/Sysdyn.scl b/org.simantics.sysdyn/scl/Sysdyn.scl new file mode 100644 index 00000000..31d2dec4 --- /dev/null +++ b/org.simantics.sysdyn/scl/Sysdyn.scl @@ -0,0 +1,32 @@ +include "http://www.simantics.org/DB" +include "http://www.simantics.org/Variables" + +importJava "org.simantics.sysdyn.utils.DocumentationUtils" where + + @JavaName getModules + getModules :: Variable -> [Resource] + + @JavaName getConfiguration + getConfiguration :: Resource -> Resource + + @JavaName getConfigurationVariables + getConfigurationVariables :: Resource -> [Resource] + + @JavaName getName + getName :: Resource -> String + + @JavaName getType + getType :: Resource -> String + + @JavaName getUnit + getUnit :: Resource -> String + + @JavaName getVariability + getVariability :: Resource -> String + + @JavaName getDescription + getDescription :: Resource -> String + + @JavaName getDocumentationDefinition + getDocumentationDefinition :: Resource -> String + \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java index ef856249..bf050c72 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java @@ -17,6 +17,7 @@ import java.util.Iterator; import org.simantics.objmap.annotations.RelatedElements; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.representation.expressions.IExpression; +import org.simantics.sysdyn.representation.expressions.StockExpression; import org.simantics.sysdyn.representation.utils.FormatUtils; /** @@ -154,4 +155,48 @@ public abstract class IndependentVariable extends Variable { public ArrayList getIncomingDependencies() { return isHeadOf; } + + @Override + public String getDocumentationDefinition() { + String declaration = getDeclaration(); + String initialEquation = getInitialEquation(); + + StringBuilder result = new StringBuilder(); + + if(declaration != null && declaration.length() > 0 && declaration.contains("=")) { + result.append("

"); + if(getExpressions().size() > 1) + result.append(formatForHTML(declaration)); + else + result.append(formatForHTML(declaration.substring(declaration.indexOf("=") + 1))); + result.append("

"); + } + + if(initialEquation != null && initialEquation.length() > 0) { + result.append("

"); + if(getExpressions().size() > 1) + result.append(formatForHTML(initialEquation)); + else + result.append(formatForHTML(initialEquation.substring(initialEquation.indexOf("=") + 1))); + result.append("

"); + } + + result.append("

"); + for(IExpression exp : getExpressions()) { + String expression = exp.getDocumentationExpression(); + if(!(exp instanceof StockExpression)) + expression = expression.substring(expression.indexOf("=") + 1); + result.append(formatForHTML(expression)); + } + result.append("

"); + + result.append(super.getDocumentationDefinition()); + + return result.toString(); + } + + private String formatForHTML(String text) { + text = text.replaceAll("\\r\\n|\\r|\\n", "
"); + return text.trim(); + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java index 97de3b0f..05227bc3 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java @@ -120,4 +120,20 @@ public class Input extends Variable { boolean continuous = variability == null || variability.isEmpty(); return " " + (continuous ? "" : variability + " ") + getType() + " " + getName() + range + ";\n"; } + + @Override + public String getDocumentationDefinition() { + String expression = getDefaultInputValue() + "// Default value"; + + if(isHeadOf.size() > 0 && isHeadOf.get(0) instanceof Dependency) { + Dependency dependency = (Dependency) isHeadOf.get(0); + Module module = (Module)dependency.getTail(); + Variable reference = (Variable)dependency.refersTo(); + // If reference exists, use reference name. Otherwise, use default value. + if(reference != null && reference.getName() != null) + expression = module.getName() + "." + reference.getName() + ";\n"; + } + + return expression + super.getDocumentationDefinition(); + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java index 9d55df02..1ff935eb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -264,4 +264,48 @@ public class Module implements IElement { } + + public String getDocumentationDefinition() { + String enumerationRedeclarations = getEnumerationRedeclarations(); + String parameterOverrides = getParameterOverrideString(); + + StringBuilder result = new StringBuilder(); + + String[] erecs = enumerationRedeclarations.split(","); + boolean first = true; + for(String s : erecs) { + if(s.length() == 0) + continue; + + if(first) { + result.append("
"); + result.append("Enumeration redeclarations:"); + result.append("
"); + } + first = false; + result.append(s.trim()); + result.append("
"); + } + + + first = true; + String[] poverrds = parameterOverrides.split(","); + for(String s : poverrds) { + if(s.length() == 0) + continue; + + if(first) { + result.append("
"); + result.append("Parameter overrides:"); + result.append("
"); + } + first = false; + result.append(s.trim()); + result.append("
"); + } + first = true; + + + return result.toString(); + } } 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 13124654..853ce552 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -48,6 +48,12 @@ public abstract class Variable implements IElement { @RelatedValue(SysdynResource.URIs.Variable_unit) protected String unit; + @RelatedValue(Layer0.URIs.HasDescription) + protected String description; + + @RelatedElement(SysdynResource.URIs.IsOutput) + protected IElement isOutput; + /** * Return the variability of this variable (Used in practice only with Input variables) * @return Variability @@ -110,4 +116,34 @@ public abstract class Variable implements IElement { return this.unit; } + /** + * Get description + * @return description + */ + public String getDescription() { + if(description == null) + return ""; + else + return description; + } + + public boolean isOutput() { + return !(isOutput == null); + } + + /** + * Get definition for documentation. + * + * For independent variables this is the equations + * For enumerations this is the indexes + * etc. + * @return + */ + public String getDocumentationDefinition() { + if(isOutput()) + return "
Is output"; + else + return ""; + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java index 7bc07c11..76d7dd22 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java @@ -76,4 +76,9 @@ public abstract class Expression implements IExpression { public IndependentVariable getParent() { return parent; } + + @Override + public String getDocumentationExpression() { + return getEquation(); + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java index ff168d00..249dc083 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java @@ -81,4 +81,11 @@ public interface IExpression { * @return owner of this expression */ IndependentVariable getParent(); + + + /** + * Get the representation of this expression to web documentation + * @return + */ + String getDocumentationExpression(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java index 78d90d96..997a316a 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java @@ -11,7 +11,16 @@ *******************************************************************************/ package org.simantics.sysdyn.representation.expressions; +import java.awt.geom.Rectangle2D; +import java.io.IOException; +import java.io.StringWriter; + +import org.jfree.chart.ChartFactory; +import org.jfree.chart.JFreeChart; +import org.jfree.chart.plot.PlotOrientation; +import org.jfree.data.xy.DefaultXYDataset; import org.simantics.db.ReadGraph; +import org.simantics.jfreechart.chart.ChartUtils; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; @@ -61,4 +70,51 @@ public class WithLookupExpression extends Expression { return result; } + + @Override + public String getDocumentationExpression() { + String expression = super.getDocumentationExpression(); + + // assume lookup table has format: {{1,2},{3,4}} + String table = lookupTable; + table = table.replace("{", ""); + table = table.replace("}", ""); + String[] split = table.split(","); + double[] x = new double[split.length / 2]; + double[] y = new double[split.length / 2]; + + try { + for(int i = 0; i < split.length / 2; i++) { + x[i] = Double.parseDouble(split[i*2]); + y[i] = Double.parseDouble(split[i*2+1]); + } + + DefaultXYDataset dataset = new DefaultXYDataset(); + dataset.addSeries("lookup", new double[][] {y, x}); + JFreeChart chart = ChartFactory.createXYLineChart( + "", // chart title + "", // domain axis label + "", // range axis label + dataset, // data + PlotOrientation.HORIZONTAL, // Orientation + false, // include legend + false, // tooltips + false // urls + ); + + StringWriter writer = new StringWriter(); + ChartUtils.writeSVG(chart, new Rectangle2D.Double(0, 0, 200, 100), writer); + writer.flush(); + String svg = writer.toString(); + writer.close(); + svg = svg.substring(svg.indexOf("" + svg + ""; + } catch (NumberFormatException e) { + + } catch (IOException e) { + } + + return expression; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java new file mode 100644 index 00000000..0a54dda6 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java @@ -0,0 +1,223 @@ +package org.simantics.sysdyn.utils; + +import java.util.ArrayList; +import java.util.List; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.request.ObjectsWithSupertype; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.structural.stubs.StructuralResource2; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.manager.SysdynModelManager; +import org.simantics.sysdyn.representation.Book; +import org.simantics.sysdyn.representation.Enumeration; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.Module; +import org.simantics.sysdyn.representation.Variability; +import org.simantics.sysdyn.representation.Variable; + +public class DocumentationUtils { + + public static List getModules(ReadGraph graph, org.simantics.db.layer0.variable.Variable model) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + Resource modelResource = model.getRepresents(graph); + + ArrayList result = new ArrayList(); + + if(graph.isInstanceOf(modelResource, SR.SysdynModel)) { + result.add(modelResource); + result.addAll(graph.syncRequest(new ObjectsWithSupertype(modelResource, Layer0.getInstance(graph).ConsistsOf, SR.Module))); + } + + return result; + } + + public static Resource getConfiguration(ReadGraph graph, Resource module) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + + if(graph.isInstanceOf(module, SR.SysdynModel)) { + return graph.getSingleObject(module, SimulationResource.getInstance(graph).HasConfiguration); + } else if(graph.isInheritedFrom(module, SR.Module)) { + return graph.getSingleObject(module, StructuralResource2.getInstance(graph).IsDefinedBy); + } else { + return null; + } + } + + public static List getConfigurationVariables(ReadGraph graph, Resource configuration) throws DatabaseException { + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + SysdynModel sm = smm.getModel(graph, configuration); + + ArrayList result = new ArrayList(); + for(IElement element : sm.getConfiguration().getElements()) { + if((element instanceof Variable || element instanceof Module) && !(element instanceof Book)) { + result.add(sm.getMapping().inverseGet(element)); + } + } + + return result; + } + + public static String getName(ReadGraph graph, Resource variable) throws DatabaseException { + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + Resource configuration = graph.syncRequest(new GetConfigurationRequest(variable)); + SysdynModel sm = smm.getModel(graph,configuration); + + IElement element = sm.getElement(variable); + + if(element instanceof Variable) { + Variable v = (Variable) element; + + StringBuilder sb = new StringBuilder(); + sb.append(v.getName()); + + + if(v.getArrayIndexes() != null && v.getArrayIndexes().size() > 0) { + sb.append("["); + boolean first = true; + for(Enumeration e : v.getArrayIndexes()) { + if(!first) + sb.append(", "); + first = false; + sb.append(e.getName()); + } + + sb.append("]"); + } + + return sb.toString(); + } + + if(element instanceof Module) { + Module m = (Module) element; + return m.getName(); + } + + return ""; + } + + public static String getType(ReadGraph graph, Resource variable) throws DatabaseException { + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + Resource configuration = graph.syncRequest(new GetConfigurationRequest(variable)); + SysdynModel sm = smm.getModel(graph,configuration); + + IElement element = sm.getElement(variable); + + if(element instanceof Variable) { + String result = ((Variable)element).getType(); + if(result == null) + return ""; + else + return element.getClass().getSimpleName() + " " + result; + } + + if(element instanceof Module) { + Module m = (Module) element; + return m.getType().getName(); + } + + return ""; + } + + public static String getUnit(ReadGraph graph, Resource variable) throws DatabaseException { + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + Resource configuration = graph.syncRequest(new GetConfigurationRequest(variable)); + SysdynModel sm = smm.getModel(graph,configuration); + + IElement element = sm.getElement(variable); + + if(element instanceof Variable) { + String result = ((Variable)element).getUnit(); + if(result == null) + return ""; + else + return result; + } + + return ""; + } + + + public static String getVariability(ReadGraph graph, Resource variable) throws DatabaseException { + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + Resource configuration = graph.syncRequest(new GetConfigurationRequest(variable)); + SysdynModel sm = smm.getModel(graph,configuration); + + IElement element = sm.getElement(variable); + + if(element instanceof IndependentVariable) { + Variability variability = Variability.getVariability((IndependentVariable)element); + if(variability == Variability.CONTINUOUS) + return ""; + return variability.toString().toLowerCase(); + } + if(element instanceof Variable) { + String result = ((Variable)element).getVariability(); + if(result == null) + return ""; + else { + return result; + } + } + + return ""; + } + + public static String getDescription(ReadGraph graph, Resource variable) throws DatabaseException { + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + Resource configuration = graph.syncRequest(new GetConfigurationRequest(variable)); + SysdynModel sm = smm.getModel(graph,configuration); + + IElement element = sm.getElement(variable); + + if(element instanceof Variable) { + String result = ((Variable)element).getDescription(); + if(result == null) + return ""; + else + return result; + } + + return ""; + } + + + public static String getDocumentationDefinition(ReadGraph graph, Resource variable) throws DatabaseException { + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + Resource configuration = graph.syncRequest(new GetConfigurationRequest(variable)); + SysdynModel sm = smm.getModel(graph,configuration); + + IElement element = sm.getElement(variable); + + if(element instanceof Variable) { + Variable v = (Variable) element; + String result = v.getDocumentationDefinition(); + if(result == null) + return ""; + else + return result; + } + + if(element instanceof Module) { + Module m = (Module) element; + return m.getDocumentationDefinition(); + // Print interface variables and parameter overrides + } + + return ""; + + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/GetConfigurationRequest.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/GetConfigurationRequest.java new file mode 100644 index 00000000..4aa51554 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/GetConfigurationRequest.java @@ -0,0 +1,42 @@ +package org.simantics.sysdyn.utils; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.sysdyn.SysdynResource; + +public class GetConfigurationRequest implements Read { + + private Resource resource; + + public GetConfigurationRequest(Resource resource) { + this.resource = resource; + } + + @Override + public Resource perform(ReadGraph graph) throws DatabaseException { + return getConfiguration(graph, resource); + } + + private Resource getConfiguration(ReadGraph graph, Resource resource) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + Resource parent = graph.getPossibleObject(resource, L0.PartOf); + if(parent == null) + return null; + + SysdynResource SR = SysdynResource.getInstance(graph); + if(graph.isInstanceOf(parent, SR.SysdynModel)) { + parent = graph.getPossibleObject(parent, SimulationResource.getInstance(graph).HasConfiguration); + } + + if(graph.isInstanceOf(parent, SR.Configuration)) { + return parent; + } + + else + return getConfiguration(graph, parent); + } +}