From be6d08c80439884010d8468e6e2d4479ac5bdd10 Mon Sep 17 00:00:00 2001 From: lempinen Date: Fri, 5 Jul 2013 13:30:15 +0000 Subject: [PATCH] Documentation stuff: Documentation texts for variables, modules and all (refs #3511) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27698 ac1ea38d-2e2b-0410-8846-a27921b304fc --- org.simantics.sysdyn.ui/plugin.xml | 4 + .../ui/browser/contributions/Model.java | 1 + .../contributions/SCLModuleLabeler.java | 27 ++++ .../ui/browser/contributions/SCLModules.java | 11 ++ .../contributions/SCLModulesLabeler.java | 11 ++ .../ui/browser/nodes/ModuleTypeNode.java | 4 +- .../sysdyn/ui/browser/nodes/SCLModule.java | 11 ++ .../ui/browser/nodes/SCLModulesFolder.java | 11 ++ org.simantics.sysdyn.ui/sysdyn.product | 2 +- org.simantics.sysdyn/META-INF/MANIFEST.MF | 3 +- org.simantics.sysdyn/build.properties | 3 +- org.simantics.sysdyn/scl/Sysdyn.scl | 33 ++++ .../representation/IndependentVariable.java | 36 +++-- .../sysdyn/representation/Input.java | 10 +- .../sysdyn/representation/Module.java | 52 ++++++- .../sysdyn/representation/Shadow.java | 6 + .../sysdyn/representation/Sheet.java | 83 ++++++++++ .../sysdyn/representation/Variable.java | 4 +- .../expressions/Expression.java | 3 +- .../expressions/IExpression.java | 3 +- .../expressions/WithLookupExpression.java | 95 ++++++++++-- .../sysdyn/utils/DocumentationUtils.java | 101 +++++++++++- .../simantics/sysdyn/utils/ModuleSummary.java | 103 ++++++++++++ .../sysdyn/utils/ModuleSummaryRequest.java | 146 ++++++++++++++++++ .../simantics/sysdyn/utils/SheetUtils.java | 56 ++++++- 25 files changed, 766 insertions(+), 53 deletions(-) create mode 100644 org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModuleLabeler.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummary.java create mode 100644 org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummaryRequest.java diff --git a/org.simantics.sysdyn.ui/plugin.xml b/org.simantics.sysdyn.ui/plugin.xml index ed403bc7..c72e00db 100644 --- a/org.simantics.sysdyn.ui/plugin.xml +++ b/org.simantics.sysdyn.ui/plugin.xml @@ -2035,6 +2035,10 @@ class="org.simantics.sysdyn.ui.browser.contributions.SCLModulesLabeler" preference="2"> + + 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 1f0a4458..90079b11 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 @@ -29,6 +29,7 @@ import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder; import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder; import org.simantics.sysdyn.ui.browser.nodes.ModelNode; import org.simantics.sysdyn.ui.browser.nodes.ModulesNode; +import org.simantics.sysdyn.ui.browser.nodes.SCLModulesFolder; /** * Provides children for a model in model browser diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModuleLabeler.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModuleLabeler.java new file mode 100644 index 00000000..97cdb9b5 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/contributions/SCLModuleLabeler.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2013 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +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.common.utils.NameUtils; +import org.simantics.db.exception.DatabaseException; +import org.simantics.sysdyn.ui.browser.nodes.SCLModule; + +public class SCLModuleLabeler extends LabelerContributor{ + + @Override + public String getLabel(ReadGraph graph, SCLModule input) throws DatabaseException { + return NameUtils.getSafeName(graph, input.data); + } + +} 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 index c08691e5..edd997fe 100644 --- 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 @@ -1,3 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2013 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ package org.simantics.sysdyn.ui.browser.contributions; import java.util.ArrayList; 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 index 01e1b055..bb00c01d 100644 --- 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 @@ -1,3 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2013 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ package org.simantics.sysdyn.ui.browser.contributions; import org.simantics.browsing.ui.graph.contributor.labeler.LabelerContributor; diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleTypeNode.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleTypeNode.java index 22e62b2f..6d773446 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleTypeNode.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/browser/nodes/ModuleTypeNode.java @@ -162,8 +162,8 @@ public class ModuleTypeNode extends AbstractNode implements IDeletable } Collection moduleTypes = graph.syncRequest(new ObjectsWithType(model, l0.ConsistsOf, st.ComponentType)); for(Resource r : moduleTypes) { - Resource configuration = graph.getSingleObject(r, st.IsDefinedBy); - if(!graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) { + Resource configuration = graph.getPossibleObject(r, st.IsDefinedBy); + if(configuration != null && !graph.syncRequest(new ObjectsWithType(configuration, l0.ConsistsOf, type)).isEmpty()) { throw new ModuleDeleteException("The module is used at another module: " + graph.getRelatedValue(r, l0.HasName)); } } 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 index 689d51f1..0a102065 100644 --- 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 @@ -1,3 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2013 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ package org.simantics.sysdyn.ui.browser.nodes; import org.simantics.browsing.ui.common.node.AbstractNode; 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 index 8fd7b354..9900d886 100644 --- 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 @@ -1,3 +1,14 @@ +/******************************************************************************* + * Copyright (c) 2013 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ package org.simantics.sysdyn.ui.browser.nodes; import org.simantics.browsing.ui.common.node.AbstractNode; diff --git a/org.simantics.sysdyn.ui/sysdyn.product b/org.simantics.sysdyn.ui/sysdyn.product index 0ce6f714..0cea8366 100644 --- a/org.simantics.sysdyn.ui/sysdyn.product +++ b/org.simantics.sysdyn.ui/sysdyn.product @@ -32,7 +32,7 @@ - org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7 + org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6 diff --git a/org.simantics.sysdyn/META-INF/MANIFEST.MF b/org.simantics.sysdyn/META-INF/MANIFEST.MF index 564a20ba..2409e1bd 100644 --- a/org.simantics.sysdyn/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn/META-INF/MANIFEST.MF @@ -32,7 +32,8 @@ Require-Bundle: org.simantics.objmap;bundle-version="0.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" + org.jfree.jcommon;bundle-version="1.0.16", + org.simantics.spreadsheet.graph;bundle-version="1.1.0" Export-Package: org.simantics.sysdyn, org.simantics.sysdyn.adapter, org.simantics.sysdyn.expressionParser, diff --git a/org.simantics.sysdyn/build.properties b/org.simantics.sysdyn/build.properties index b2aac27d..26e468c9 100644 --- a/org.simantics.sysdyn/build.properties +++ b/org.simantics.sysdyn/build.properties @@ -14,4 +14,5 @@ output.. = bin/ bin.includes = META-INF/,\ .,\ adapters.xml,\ - plugin.xml + plugin.xml,\ + scl/ diff --git a/org.simantics.sysdyn/scl/Sysdyn.scl b/org.simantics.sysdyn/scl/Sysdyn.scl index 31d2dec4..f1d5fb35 100644 --- a/org.simantics.sysdyn/scl/Sysdyn.scl +++ b/org.simantics.sysdyn/scl/Sysdyn.scl @@ -29,4 +29,37 @@ importJava "org.simantics.sysdyn.utils.DocumentationUtils" where @JavaName getDocumentationDefinition getDocumentationDefinition :: Resource -> String + + + + @JavaName numberOfVariables + numberOfVariables :: Resource -> Boolean -> Integer + + @JavaName totalNumberOfVariables + totalNumberOfVariables :: Resource -> Boolean -> Integer + + @JavaName numberOfStocks + numberOfStocks :: Resource -> Boolean -> Integer + + @JavaName numberOfValves + numberOfValves :: Resource -> Boolean -> Integer + + @JavaName numberOfInputs + numberOfInputs :: Resource -> Boolean -> Integer + + @JavaName numberOfShadows + numberOfShadows :: Resource -> Boolean -> Integer + + @JavaName numberOfAuxiliaries + numberOfAuxiliaries :: Resource -> Boolean -> Integer + + @JavaName numberOfModules + numberOfModules :: Resource -> Boolean -> Integer + + @JavaName numberOfSheets + numberOfSheets :: Resource -> Boolean -> Integer + + @JavaName numberOfModuleTypes + numberOfModuleTypes :: Variable -> Integer + \ 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 bf050c72..c49eda85 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/IndependentVariable.java @@ -14,9 +14,12 @@ package org.simantics.sysdyn.representation; import java.util.ArrayList; import java.util.Iterator; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.annotations.RelatedElements; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.representation.expressions.IExpression; +import org.simantics.sysdyn.representation.expressions.ParameterExpression; import org.simantics.sysdyn.representation.expressions.StockExpression; import org.simantics.sysdyn.representation.utils.FormatUtils; @@ -157,7 +160,7 @@ public abstract class IndependentVariable extends Variable { } @Override - public String getDocumentationDefinition() { + public String getDocumentationDefinition(ReadGraph graph) throws DatabaseException { String declaration = getDeclaration(); String initialEquation = getInitialEquation(); @@ -167,13 +170,25 @@ public abstract class IndependentVariable extends Variable { result.append("

"); if(getExpressions().size() > 1) result.append(formatForHTML(declaration)); + if(this instanceof Stock) + result.append(formatForHTML(declaration.substring(declaration.indexOf("(") + 1, declaration.lastIndexOf(")")))); else result.append(formatForHTML(declaration.substring(declaration.indexOf("=") + 1))); result.append("

"); } + result.append("

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

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

"); + result.append("

Initial value:

"); if(getExpressions().size() > 1) result.append(formatForHTML(initialEquation)); else @@ -181,22 +196,17 @@ public abstract class IndependentVariable extends Variable { 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()); + result.append(super.getDocumentationDefinition(graph)); return result.toString(); } private String formatForHTML(String text) { + text= text.trim(); text = text.replaceAll("\\r\\n|\\r|\\n", "
"); - return text.trim(); + if(text.endsWith(";")) + text = text.substring(0, text.length() - 1); + text = text.replace("/* Actual value read from init file */", ""); + return text; } } 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 05227bc3..11bf8479 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Input.java @@ -15,6 +15,8 @@ import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElements; import org.simantics.objmap.annotations.RelatedValue; @@ -122,8 +124,8 @@ public class Input extends Variable { } @Override - public String getDocumentationDefinition() { - String expression = getDefaultInputValue() + "// Default value"; + public String getDocumentationDefinition(ReadGraph graph) throws DatabaseException { + String expression = getDefaultInputValue() + " // Default value"; if(isHeadOf.size() > 0 && isHeadOf.get(0) instanceof Dependency) { Dependency dependency = (Dependency) isHeadOf.get(0); @@ -131,9 +133,9 @@ public class Input extends Variable { 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"; + expression = "Reference:
" + module.getName() + "." + reference.getName() + ";\n"; } - return expression + super.getDocumentationDefinition(); + return expression + super.getDocumentationDefinition(graph); } } 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 1ff935eb..70829bed 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Module.java @@ -273,38 +273,78 @@ public class Module implements IElement { String[] erecs = enumerationRedeclarations.split(","); boolean first = true; + result.append("

"); for(String s : erecs) { if(s.length() == 0) continue; if(first) { - result.append("
"); - result.append("Enumeration redeclarations:"); - result.append("
"); + result.append("Enumeration redeclarations:
"); } first = false; result.append(s.trim()); result.append("
"); } + result.append("

"); + first = true; + result.append("

"); + String[] poverrds = parameterOverrides.split(","); for(String s : poverrds) { if(s.length() == 0) continue; if(first) { - result.append("
"); - result.append("Parameter overrides:"); - result.append("
"); + result.append("Parameter overrides:
"); } first = false; result.append(s.trim()); result.append("
"); } + result.append("

"); + + ArrayList inputs = new ArrayList(); + ArrayList outputs = new ArrayList(); + for(IElement e : parentConfiguration.getElements()) { + if(e instanceof Dependency) { + Dependency d = (Dependency)e; + if(this.equals(d.getHead()) && d.refersTo() != null) { + inputs.add(d); + } else if(this.equals(d.getTail()) && d.refersTo() != null) { + outputs.add(d); + } + } + } + first = true; + result.append("

"); + for(Dependency d : inputs) { + if(first) { + result.append("Inputs:
"); + } + first = false; + + result.append(getName() + "." + ((Variable)d.refersTo()).getName() + " = " + ((Variable)d.getTail()).getName()); + result.append("
"); + } + result.append("

"); + first = true; + result.append("

"); + for(Dependency d : outputs) { + if(first) { + result.append("Outputs:
"); + } + first = false; + + result.append(((Variable)d.getHead()).getName() + " = " + getName() + "." + ((Variable)d.refersTo()).getName()); + result.append("
"); + } + result.append("

"); + return result.toString(); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Shadow.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Shadow.java index b652f801..91358b71 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Shadow.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Shadow.java @@ -13,6 +13,8 @@ package org.simantics.sysdyn.representation; import java.util.ArrayList; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.sysdyn.SysdynResource; @@ -83,5 +85,9 @@ public class Shadow extends Variable { return super.getUnit(); } + @Override + public String getDocumentationDefinition(ReadGraph graph) throws DatabaseException { + return null; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java index db41a5bb..7037b585 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Sheet.java @@ -15,6 +15,8 @@ import gnu.trove.map.hash.THashMap; import gnu.trove.set.hash.THashSet; import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; import org.simantics.Simantics; @@ -27,10 +29,12 @@ import org.simantics.db.common.request.ReadRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.exception.MissingVariableException; import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.layer0.Layer0; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.UpdateMethod; +import org.simantics.spreadsheet.Range; import org.simantics.spreadsheet.SheetVariables; import org.simantics.spreadsheet.common.exception.CellParseException; import org.simantics.spreadsheet.resource.SpreadsheetResource; @@ -239,6 +243,85 @@ public class Sheet extends org.simantics.sysdyn.representation.Variable { usedRanges.add(usedRange); } + + @Override + public String getDocumentationDefinition(ReadGraph graph) throws DatabaseException { + Variable v = Variables.getVariable(graph, resource); + Collection children = v.getChildren(graph); + HashMap map = new HashMap(); + int minColumn = -1; + int maxColumn = -1; + int minRow = -1; + int maxRow = -1; + for(Variable child : children) { + Range range = SpreadsheetUtils.decodePossibleCellAbsolute(child.getName(graph)); + if(range != null) { + Variant valueVariant = child.getPropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT); + Object valueObject = valueVariant.getValue(); + if(valueObject instanceof String) { + String value = (String) valueObject; + if(!value.isEmpty()) { + map.put(child, range); + if(minColumn < 0 || range.startColumn < minColumn) + minColumn = range.startColumn; + if(range.startColumn > maxColumn) + maxColumn = range.startColumn; + if(minRow < 0 || range.startRow < minRow) + minRow = range.startRow; + if(range.startRow > maxRow) + maxRow = range.startRow; + } + } + } + } + + int nColumns = maxColumn - minColumn + 1; + int nRows = maxRow - minRow + 1; + String[][] table = new String[nRows][nColumns]; + for(int i = 0; i < nRows; i++) + for(int j = 0; j < nColumns; j++) + table[i][j] = ""; + + for(Variable cell : map.keySet()) { + Range range = map.get(cell); + Variant valueVariant = cell.getPropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT); + Object valueObject = valueVariant.getValue(); + table[range.startRow - minRow][range.startColumn - minColumn] = valueObject.toString(); + } + + StringBuilder result = new StringBuilder(); + result.append(""); + for(int i = -1; i < nRows; i++) { + result.append(""); + for(int j = -1; j < nColumns; j++) { + result.append(""); + } + result.append(""); + } + result.append("
"); + if(i == -1 || j == -1) + result.insert(result.length() - 2, "background-color:#D3D3D3; text-align:center;"); + if(i == -1 && j == -1) { + // Do nothing + } else if(i == -1) { + // First row, make column headers + result.append(SpreadsheetUtils.columnName(j + minColumn)); + } else if(j == -1){ + // First column, make row numbers. Rows start from 1 + result.append(i + minRow + 1); + } else { + // Actual content + result.append(table[i][j]); + } + result.append("
"); + + return result.toString(); + } + + + public Resource getResource() { + return resource; + } } 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 853ce552..09a23c4f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/Variable.java @@ -13,6 +13,8 @@ package org.simantics.sysdyn.representation; import java.util.ArrayList; +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.RelatedListElements; @@ -139,7 +141,7 @@ public abstract class Variable implements IElement { * etc. * @return */ - public String getDocumentationDefinition() { + public String getDocumentationDefinition(ReadGraph graph) throws DatabaseException { if(isOutput()) return "
Is output"; else 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 76d7dd22..b262bc73 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 @@ -12,6 +12,7 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.layer0.Layer0; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.RelatedValue; @@ -78,7 +79,7 @@ public abstract class Expression implements IExpression { } @Override - public String getDocumentationExpression() { + public String getDocumentationExpression(ReadGraph graph) throws DatabaseException { 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 249dc083..94a119ce 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 @@ -12,6 +12,7 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.IndependentVariable; @@ -87,5 +88,5 @@ public interface IExpression { * Get the representation of this expression to web documentation * @return */ - String getDocumentationExpression(); + String getDocumentationExpression(ReadGraph graph) throws DatabaseException; } 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 997a316a..def0b556 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 @@ -20,11 +20,22 @@ 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.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.jfreechart.chart.ChartUtils; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; +import org.simantics.spreadsheet.Range; +import org.simantics.spreadsheet.SheetVariables; +import org.simantics.spreadsheet.util.SpreadsheetUtils; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.representation.Configuration; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.IndependentVariable; +import org.simantics.sysdyn.representation.Model; +import org.simantics.sysdyn.representation.Sheet; import org.simantics.sysdyn.representation.utils.FormatUtils; import org.simantics.sysdyn.representation.utils.IndexUtils; import org.simantics.sysdyn.representation.utils.SheetFormatUtils; @@ -47,12 +58,12 @@ public class WithLookupExpression extends Expression { @Override public String getEquation() { String equation = FormatUtils.formatExpressionForModelica(parent, this.equation); - String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); + String range = IndexUtils.rangeToIndexes(parent, this.getArrayRange()); return - " " + parent.getName() + (range.equals("[:]") ? "" : range) + " = interpolate(" + equation + ", " + SheetFormatUtils.reformatSheetReferences(parent, lookupTable) + ");\n"; + " " + parent.getName() + (range.equals("[:]") ? "" : range) + " = interpolate(" + equation + ", " + SheetFormatUtils.reformatSheetReferences(parent, lookupTable) + ");\n"; } - + @Override public String getExpression() { return "interpolate(" + equation + ", " + lookupTable + ")"; @@ -62,33 +73,87 @@ public class WithLookupExpression extends Expression { public String validateUnits(ReadGraph graph, SysdynModel model) { if(parent.getUnit() == null) return "Unit not defined for " + parent.getName(); - + String result = UnitUtils.expressionUnitsValid(graph, model, parent.getParentConfiguration(), equation); if(result == null) { result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), lookupTable); } - + return result; } - + @Override - public String getDocumentationExpression() { - String expression = super.getDocumentationExpression(); - - // assume lookup table has format: {{1,2},{3,4}} + public String getDocumentationExpression(ReadGraph graph) throws DatabaseException { + String expression = super.getDocumentationExpression(graph); + + // assume lookup table has format: {{1,2},{3,4}} String table = lookupTable; + + Sheet sheet = null; + if(table.matches("[a-zA-Z0-9]*\\([a-zA-Z0-9:]*\\)")) { + // try if it is a sheet reference + String name = table.substring(0, table.indexOf("(")); + String range = table.substring(table.indexOf("(") + 1, table.indexOf(")")); + IndependentVariable independentVariable = getParent(); + Configuration conf = independentVariable.getParentConfiguration(); + Model model = conf.getModel(); + + if(model == null) { + Object parent = conf.getModuleType().getParent(); + if(parent instanceof Model) { + model = (Model)parent; + } + } + + if(model != null) { + for(IElement e : model.getModelConfiguration().getElements()) { + if(e instanceof Sheet && name.equals(((Sheet)e).getName())) { + sheet = (Sheet)e; + Variable v = Variables.getVariable(graph, sheet.getResource()); + Variable rangeVariable = v.getChild(graph, range); + Range r = SpreadsheetUtils.decodeRange(range); + if(r.endColumn - r.startColumn == 1) { + String[][] rangeCells = rangeVariable.getPropertyValue(graph, SheetVariables.RANGE_CELL_NAMES); + StringBuilder sb = new StringBuilder(); + boolean firstRow = true; + for(int i = 0; i < rangeCells.length; i++) { + if(!firstRow) + sb.append(","); + firstRow = false; + + boolean firstColumn = true; + for(int j = 0; j < rangeCells[i].length; j++) { + if(!firstColumn) + sb.append(","); + firstColumn = false; + sb.append(sheet.getCells().get(rangeCells[i][j])); + } + } + table = sb.toString(); + + } + break; + } + } + } + + if(sheet != null) { + + } + } + 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( @@ -101,7 +166,7 @@ public class WithLookupExpression extends Expression { false, // tooltips false // urls ); - + StringWriter writer = new StringWriter(); ChartUtils.writeSVG(chart, new Rectangle2D.Double(0, 0, 200, 100), writer); writer.flush(); @@ -111,10 +176,10 @@ public class WithLookupExpression extends Expression { svg = svg.replaceAll("[\r\n]+", ""); expression = expression + "
" + 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 index 0a54dda6..b044d2aa 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/DocumentationUtils.java @@ -1,6 +1,8 @@ package org.simantics.sysdyn.utils; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import org.simantics.db.ReadGraph; @@ -18,6 +20,8 @@ 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.Shadow; +import org.simantics.sysdyn.representation.Stock; import org.simantics.sysdyn.representation.Variability; import org.simantics.sysdyn.representation.Variable; @@ -54,13 +58,51 @@ public class DocumentationUtils { SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); SysdynModel sm = smm.getModel(graph, configuration); - ArrayList result = new ArrayList(); + ArrayList elements = new ArrayList(); + for(IElement element : sm.getConfiguration().getElements()) { - if((element instanceof Variable || element instanceof Module) && !(element instanceof Book)) { - result.add(sm.getMapping().inverseGet(element)); + if((element instanceof Variable || element instanceof Module) && !(element instanceof Book) && !(element instanceof Shadow)) { + elements.add(element); } } + Collections.sort(elements, new Comparator() { + + @Override + public int compare(IElement o1, IElement o2) { + if(!isApplied(o1) || !isApplied(o2)) + return 0; + else { + String name1 = getName(o1); + String name2 = getName(o2); + return name1.compareTo(name2); + } + } + + private boolean isApplied(IElement element) { + if((element instanceof Variable || element instanceof Module) && !(element instanceof Book)) { + return true; + } else { + return false; + } + + } + + private String getName(IElement element) { + if(element instanceof Variable) + return ((Variable)element).getName(); + else if(element instanceof Module) + return ((Module)element).getName(); + else + return ""; + } + + }); + + ArrayList result = new ArrayList(); + for(IElement e : elements) + result.add(sm.getMapping().inverseGet(e)); + return result; } @@ -121,7 +163,7 @@ public class DocumentationUtils { if(element instanceof Module) { Module m = (Module) element; - return m.getType().getName(); + return"Module " + m.getType().getName(); } return ""; @@ -155,6 +197,9 @@ public class DocumentationUtils { IElement element = sm.getElement(variable); + if(element instanceof Stock) + return ""; + if(element instanceof IndependentVariable) { Variability variability = Variability.getVariability((IndependentVariable)element); if(variability == Variability.CONTINUOUS) @@ -203,7 +248,7 @@ public class DocumentationUtils { if(element instanceof Variable) { Variable v = (Variable) element; - String result = v.getDocumentationDefinition(); + String result = v.getDocumentationDefinition(graph); if(result == null) return ""; else @@ -219,5 +264,51 @@ public class DocumentationUtils { return ""; } + + + public static int numberOfVariables(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getVariables(); + } + + public static int totalNumberOfVariables(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getTotalVariables(); + } + + public static int numberOfStocks(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getStocks(); + } + + public static int numberOfValves(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getValves(); + } + + public static int numberOfInputs(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getInputs(); + } + + public static int numberOfShadows(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getShadows(); + } + public static int numberOfAuxiliaries(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getAuxiliaries(); + } + + public static int numberOfModules(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getModules(); + } + + public static int numberOfSheets(ReadGraph graph, Resource configuration, boolean recursive) throws DatabaseException { + return graph.syncRequest(new ModuleSummaryRequest(configuration, recursive)).getSheets(); + } + + public static int numberOfModuleTypes(ReadGraph graph, org.simantics.db.layer0.variable.Variable model) throws DatabaseException { + Resource modelResource = model.getRepresents(graph); + return graph.syncRequest(new ObjectsWithSupertype( + modelResource, + Layer0.getInstance(graph).ConsistsOf, + SysdynResource.getInstance(graph).Module)) + .size(); + } + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummary.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummary.java new file mode 100644 index 00000000..ed0f25b0 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummary.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2013 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.utils; + +public class ModuleSummary { + + private int variables = 0; + private int totalVariables = 0; + private int auxiliaries = 0; + private int stocks = 0; + private int valves = 0; + private int inputs = 0; + private int shadows = 0; + private int modules = 0; + private int enumerations = 0; + private int sheets = 0; + + + + public int getVariables() { + return variables; + } + public void setVariables(int variables) { + this.variables = variables; + } + public int getTotalVariables() { + return totalVariables; + } + public void setTotalVariables(int totalVariables) { + this.totalVariables = totalVariables; + } + public int getAuxiliaries() { + return auxiliaries; + } + public void setAuxiliaries(int auxiliaries) { + this.auxiliaries = auxiliaries; + } + public int getStocks() { + return stocks; + } + public void setStocks(int stocks) { + this.stocks = stocks; + } + public int getValves() { + return valves; + } + public void setValves(int valves) { + this.valves = valves; + } + public int getInputs() { + return inputs; + } + public void setInputs(int inputs) { + this.inputs = inputs; + } + public int getShadows() { + return shadows; + } + public void setShadows(int shadows) { + this.shadows = shadows; + } + public int getModules() { + return modules; + } + public void setModules(int modules) { + this.modules = modules; + } + public int getEnumerations() { + return enumerations; + } + public void setEnumerations(int enumerations) { + this.enumerations = enumerations; + } + public int getSheets() { + return sheets; + } + public void setSheets(int sheets) { + this.sheets = sheets; + } + + public void add(ModuleSummary otherSummary) { + setAuxiliaries(getAuxiliaries() + otherSummary.getAuxiliaries()); + setEnumerations(getEnumerations() + otherSummary.getEnumerations()); + setInputs(getInputs() + otherSummary.getInputs()); + setModules(getModules() + otherSummary.getModules()); + setShadows(getShadows() + otherSummary.getShadows()); + setStocks(getStocks() + otherSummary.getStocks()); + setValves(getValves() + otherSummary.getValves()); + setVariables(getVariables() + otherSummary.getVariables()); + setTotalVariables(getTotalVariables() + otherSummary.getTotalVariables()); + setSheets(getSheets() + otherSummary.getSheets()); + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummaryRequest.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummaryRequest.java new file mode 100644 index 00000000..1d3267d1 --- /dev/null +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/ModuleSummaryRequest.java @@ -0,0 +1,146 @@ +/******************************************************************************* + * Copyright (c) 2013 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: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.utils; + +import java.util.HashMap; +import java.util.Map; + +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.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.Auxiliary; +import org.simantics.sysdyn.representation.Configuration; +import org.simantics.sysdyn.representation.Enumeration; +import org.simantics.sysdyn.representation.IElement; +import org.simantics.sysdyn.representation.Input; +import org.simantics.sysdyn.representation.Module; +import org.simantics.sysdyn.representation.Redeclaration; +import org.simantics.sysdyn.representation.Shadow; +import org.simantics.sysdyn.representation.Sheet; +import org.simantics.sysdyn.representation.Stock; +import org.simantics.sysdyn.representation.Valve; +import org.simantics.sysdyn.representation.Variable; + +public class ModuleSummaryRequest implements Read { + + private Resource module; + private boolean recursive; + + public ModuleSummaryRequest(Resource module, boolean recursive) { + this.module = module; + this.recursive = recursive; + } + + @Override + public ModuleSummary perform(ReadGraph graph) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + SimulationResource SIMU = SimulationResource.getInstance(graph); + StructuralResource2 SR2 = StructuralResource2.getInstance(graph); + + ModuleSummary result = new ModuleSummary(); + + if(module == null) + return result; + + Resource configuration; + if(graph.isInstanceOf(module, SR.SysdynModel)) + configuration = graph.getPossibleObject(module, SIMU.HasConfiguration); + else if(graph.isInheritedFrom(module, SR.Module)) + configuration = graph.getPossibleObject(module, SR2.IsDefinedBy); + else + configuration = null; + + if(configuration == null) + return result; + + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); + SysdynModel sm = smm.getModel(graph, configuration); + sm.update(graph); + + result.add(collectSummary(sm.getConfiguration(), recursive, new HashMap())); + + return result; + } + + private ModuleSummary collectSummary(Configuration configuration, boolean recursive, Map enumerationOverrides) { + ModuleSummary result = new ModuleSummary(); + + for(IElement e : configuration.getElements()) { + if(e instanceof Input) + result.setInputs(result.getInputs() + 1); + + if(e instanceof Auxiliary) + result.setAuxiliaries(result.getAuxiliaries() + 1); + + if(e instanceof Stock) + result.setStocks(result.getStocks() + 1); + + if(e instanceof Shadow) + result.setShadows(result.getShadows() + 1); + + if(e instanceof Valve) + result.setValves(result.getValves() + 1); + + if(e instanceof Enumeration) + result.setEnumerations(result.getEnumerations() + 1); + + if(e instanceof Sheet) + result.setSheets(result.getSheets() + 1); + + if(e instanceof Variable && !(e instanceof Sheet)) { + result.setVariables(result.getVariables() + 1); + + int n = 1; + Variable var = (Variable) e; + if(var.getArrayIndexes() != null && var.getArrayIndexes().size() > 0) { + for(Enumeration enumeration : var.getArrayIndexes()) { + if(enumerationOverrides.containsKey(enumeration.getName())) { + n = n * enumerationOverrides.get(enumeration.getName()); + } else { + n = n * enumeration.getEnumerationIndexes().size(); + } + } + } + result.setTotalVariables(result.getTotalVariables() + n); + } + + if(e instanceof Module) { + result.setModules(result.getModules() + 1); + + if(recursive) { + Module m = (Module) e; + HashMap redeclarations = new HashMap(); + for(Redeclaration redeclaration : m.getRedeclarations()) { + int n; + if(enumerationOverrides.containsKey(redeclaration.getReplacingEnumeration())) { + n = enumerationOverrides.get(redeclaration.getReplacingEnumeration()); + } else { + n = redeclaration.getReplacingEnumeration().getEnumerationIndexes().size(); + } + redeclarations.put(redeclaration.getReplacedEnumeration().getName(), n); + } + result.add(collectSummary(m.getType().getConfiguration(), recursive, redeclarations)); + } + } + } + + + return result; + } + +} diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SheetUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SheetUtils.java index ca9c70c0..9bda6cab 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SheetUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/utils/SheetUtils.java @@ -12,15 +12,22 @@ package org.simantics.sysdyn.utils; import org.simantics.databoard.Bindings; +import org.simantics.databoard.binding.mutable.Variant; +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.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; +import org.simantics.spreadsheet.SheetVariables; import org.simantics.spreadsheet.resource.SpreadsheetResource; public class SheetUtils { - + public static Resource createSheet(WriteGraph graph, Resource book, String name, String[] colNames, int[] colWidths) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); @@ -68,9 +75,54 @@ public class SheetUtils { // graph.claim(result, L0.HasChildVariables, L0.HasChildVariables_Inverse, newCell); // // } - + return result; } + public static String getStringRepresentation(ReadGraph graph, Resource model, String sheetName, String range) throws DatabaseException { + Resource configuration = graph.getPossibleObject(model, SimulationResource.getInstance(graph).HasConfiguration); + Layer0 L0 = Layer0.getInstance(graph); + SpreadsheetResource SHEET = SpreadsheetResource.getInstance(graph); + Variable sheetVariable = null; + for(Resource book : graph.syncRequest(new ObjectsWithType(configuration, L0.ConsistsOf, SHEET.Book))) { + for(Resource sheet : graph.syncRequest(new ObjectsWithType(book, L0.ConsistsOf, SHEET.Spreadsheet))) { + if(sheetName.equals(NameUtils.getSafeName(graph, sheet))) { + sheetVariable = Variables.getVariable(graph, sheet); + } + } + } + + if(sheetVariable == null) + return null; + + Variable rangeVariable = sheetVariable.getChild(graph, range); + + + String[][] rangeCells = rangeVariable.getPropertyValue(graph, SheetVariables.RANGE_CELL_NAMES); + if(rangeCells == null || rangeCells[0].length <= 1) + return null; + else { + StringBuilder sb = new StringBuilder(); + sb.append("{"); + for(int i = 0; i < rangeCells.length; i++) { + sb.append("{"); + for(int j = 0; j < rangeCells[i].length; j++) { + String valueCell = rangeCells[i][j]; + Variable cell = sheetVariable.getChild(graph, valueCell); + Variant valueVariant = cell.getPropertyValue(graph, SheetVariables.CONTENT, Bindings.VARIANT); + Object valueObject = valueVariant.getValue(); + sb.append(valueObject.toString()); + if(j < rangeCells[i].length - 1) + sb.append(", "); + } + sb.append("}"); + if(i < rangeCells.length - 1) + sb.append(", "); + } + sb.append("}"); + + return sb.toString(); + } + } } -- 2.47.1