From: lempinen Date: Mon, 6 May 2013 10:24:16 +0000 (+0000) Subject: Unit validation for module interfaces (refs #4268) X-Git-Tag: 1.8.1~310 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=f8fc63e6b7e8d56d0dd4cfefb7d8b24fc12bf9a0;p=simantics%2Fsysdyn.git Unit validation for module interfaces (refs #4268) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27310 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index da045bf5..87734e26 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index 9f646f05..54d927db 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -88,8 +88,8 @@ SYSDYN.DefaultRealization -- SYSDYN.Variable.type --> L0.String -- SYSDYN.Variable.expressions --> SYSDYN.Expressions -- SYSDYN.Variable.expressionList --> L0.List -- SYSDYN.Module.redeclaration --> SYSDYN.Redeclaration -- SYSDYN.Module.parameterOverride --> SYSDYN.Module.ParameterOverride L0X = ISSUE = SYSDYN = +STR = VALIDATIONS = SYSDYN.Validations : L0.Library @@ -27,9 +28,12 @@ VALIDATIONS.Expressions.expressionValidator : L0.Function VALIDATIONS.Enumerations : L0.Library VALIDATIONS.Enumerations.enumerationIndexValidator : L0.Function -// ENUMERATIONS +// UNITS VALIDATIONS.Units : L0.Library VALIDATIONS.Units.unitValidator : L0.Function +VALIDATIONS.Units.moduleInterfaceExtension : L0.Function + + SYSDYN.Variable @VALIDATIONS.constraint @@ -62,15 +66,18 @@ SYSDYN.Variable @L0.assert L0.HasName "MissingDependencyConnectionsIssueSource" VALIDATIONS.Dependencies.missingDependencyValidator VALIDATIONS.Functions.baseRealizationFunction - -SYSDYN.IndependentVariable + +SYSDYN.Component @VALIDATIONS.listeningConstraint VALIDATIONS.UnitConstraint VALIDATIONS.Units.UnitIssueSource @L0.assert L0.HasName "UnitIssueSource" + L0.Asserts _ : L0.Assertion + L0.HasPredicate ISSUE.Sources.DependencyTracker.HasExtension + L0.HasObject VALIDATIONS.Units.moduleInterfaceExtension VALIDATIONS.Units.unitValidator VALIDATIONS.Functions.baseRealizationFunction - + VALIDATIONS.ExpressionIssue @VALIDATIONS.issue ISSUE.Severity.Error @@ -134,6 +141,19 @@ VALIDATIONS.UnitWarning L0.HasValueType "String" VALIDATIONS.Functions.path +VALIDATIONS.ModuleInputUnitWarning + @VALIDATIONS.issue + ISSUE.Severity.Warning + VALIDATIONS.Units.moduleInputUnitWarningDescription : L0.Function + L0.HasValueType "String" + VALIDATIONS.Functions.path + +VALIDATIONS.ModuleOutputUnitWarning + @VALIDATIONS.issue + ISSUE.Severity.Warning + VALIDATIONS.Units.moduleOutputUnitWarningDescription : L0.Function + L0.HasValueType "String" + VALIDATIONS.Functions.path // IssueSource template VALIDATIONS.constraint : L0.Template @@ -151,7 +171,6 @@ VALIDATIONS.constraint : L0.Template L0.Asserts _ : L0.Assertion L0.HasPredicate ISSUE.Sources.DependencyTracker.HasBaseFunction L0.HasObject %baseFunction - VALIDATIONS.listeningConstraint : L0.Template @template %type %constraint %source %validator %baseFunction diff --git a/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java b/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java index 2109bb0b..e972eab9 100644 --- a/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java +++ b/org.simantics.sysdyn.ontology/src/org/simantics/sysdyn/SysdynResource.java @@ -321,6 +321,7 @@ public class SysdynResource { public final Resource Charts_SensitivityPlot; public final Resource Cloud; public final Resource CloudSymbol; + public final Resource Component; public final Resource Configuration; public final Resource ConfigurationDiagram; public final Resource ConfigurationDiagramTemplate; @@ -643,6 +644,8 @@ public class SysdynResource { public final Resource Validations_Issue_stringContexts_Inverse; public final Resource Validations_MissingDependencyConstraint; public final Resource Validations_MissingLinkIssue; + public final Resource Validations_ModuleInputUnitWarning; + public final Resource Validations_ModuleOutputUnitWarning; public final Resource Validations_NoSuchVariableIssue; public final Resource Validations_RangeIssue; public final Resource Validations_RangeWarning; @@ -650,6 +653,9 @@ public class SysdynResource { public final Resource Validations_UnitWarning; public final Resource Validations_Units; public final Resource Validations_Units_UnitIssueSource; + public final Resource Validations_Units_moduleInputUnitWarningDescription; + public final Resource Validations_Units_moduleInterfaceExtension; + public final Resource Validations_Units_moduleOutputUnitWarningDescription; public final Resource Validations_Units_unitValidator; public final Resource Validations_Units_unitWarningDescription; public final Resource Validations_UnusedDependencyIssue; @@ -1015,6 +1021,7 @@ public class SysdynResource { public static final String Charts_SensitivityPlot = "http://www.simantics.org/Sysdyn-1.1/Charts/SensitivityPlot"; public static final String Cloud = "http://www.simantics.org/Sysdyn-1.1/Cloud"; public static final String CloudSymbol = "http://www.simantics.org/Sysdyn-1.1/CloudSymbol"; + public static final String Component = "http://www.simantics.org/Sysdyn-1.1/Component"; public static final String Configuration = "http://www.simantics.org/Sysdyn-1.1/Configuration"; public static final String ConfigurationDiagram = "http://www.simantics.org/Sysdyn-1.1/ConfigurationDiagram"; public static final String ConfigurationDiagramTemplate = "http://www.simantics.org/Sysdyn-1.1/ConfigurationDiagramTemplate"; @@ -1337,6 +1344,8 @@ public class SysdynResource { public static final String Validations_Issue_stringContexts_Inverse = "http://www.simantics.org/Sysdyn-1.1/Validations/Issue/stringContexts/Inverse"; public static final String Validations_MissingDependencyConstraint = "http://www.simantics.org/Sysdyn-1.1/Validations/MissingDependencyConstraint"; public static final String Validations_MissingLinkIssue = "http://www.simantics.org/Sysdyn-1.1/Validations/MissingLinkIssue"; + public static final String Validations_ModuleInputUnitWarning = "http://www.simantics.org/Sysdyn-1.1/Validations/ModuleInputUnitWarning"; + public static final String Validations_ModuleOutputUnitWarning = "http://www.simantics.org/Sysdyn-1.1/Validations/ModuleOutputUnitWarning"; public static final String Validations_NoSuchVariableIssue = "http://www.simantics.org/Sysdyn-1.1/Validations/NoSuchVariableIssue"; public static final String Validations_RangeIssue = "http://www.simantics.org/Sysdyn-1.1/Validations/RangeIssue"; public static final String Validations_RangeWarning = "http://www.simantics.org/Sysdyn-1.1/Validations/RangeWarning"; @@ -1344,6 +1353,9 @@ public class SysdynResource { public static final String Validations_UnitWarning = "http://www.simantics.org/Sysdyn-1.1/Validations/UnitWarning"; public static final String Validations_Units = "http://www.simantics.org/Sysdyn-1.1/Validations/Units"; public static final String Validations_Units_UnitIssueSource = "http://www.simantics.org/Sysdyn-1.1/Validations/Units/UnitIssueSource"; + public static final String Validations_Units_moduleInputUnitWarningDescription = "http://www.simantics.org/Sysdyn-1.1/Validations/Units/moduleInputUnitWarningDescription"; + public static final String Validations_Units_moduleInterfaceExtension = "http://www.simantics.org/Sysdyn-1.1/Validations/Units/moduleInterfaceExtension"; + public static final String Validations_Units_moduleOutputUnitWarningDescription = "http://www.simantics.org/Sysdyn-1.1/Validations/Units/moduleOutputUnitWarningDescription"; public static final String Validations_Units_unitValidator = "http://www.simantics.org/Sysdyn-1.1/Validations/Units/unitValidator"; public static final String Validations_Units_unitWarningDescription = "http://www.simantics.org/Sysdyn-1.1/Validations/Units/unitWarningDescription"; public static final String Validations_UnusedDependencyIssue = "http://www.simantics.org/Sysdyn-1.1/Validations/UnusedDependencyIssue"; @@ -1719,6 +1731,7 @@ public class SysdynResource { Charts_SensitivityPlot = getResourceOrNull(graph, URIs.Charts_SensitivityPlot); Cloud = getResourceOrNull(graph, URIs.Cloud); CloudSymbol = getResourceOrNull(graph, URIs.CloudSymbol); + Component = getResourceOrNull(graph, URIs.Component); Configuration = getResourceOrNull(graph, URIs.Configuration); ConfigurationDiagram = getResourceOrNull(graph, URIs.ConfigurationDiagram); ConfigurationDiagramTemplate = getResourceOrNull(graph, URIs.ConfigurationDiagramTemplate); @@ -2041,6 +2054,8 @@ public class SysdynResource { Validations_Issue_stringContexts_Inverse = getResourceOrNull(graph, URIs.Validations_Issue_stringContexts_Inverse); Validations_MissingDependencyConstraint = getResourceOrNull(graph, URIs.Validations_MissingDependencyConstraint); Validations_MissingLinkIssue = getResourceOrNull(graph, URIs.Validations_MissingLinkIssue); + Validations_ModuleInputUnitWarning = getResourceOrNull(graph, URIs.Validations_ModuleInputUnitWarning); + Validations_ModuleOutputUnitWarning = getResourceOrNull(graph, URIs.Validations_ModuleOutputUnitWarning); Validations_NoSuchVariableIssue = getResourceOrNull(graph, URIs.Validations_NoSuchVariableIssue); Validations_RangeIssue = getResourceOrNull(graph, URIs.Validations_RangeIssue); Validations_RangeWarning = getResourceOrNull(graph, URIs.Validations_RangeWarning); @@ -2048,6 +2063,9 @@ public class SysdynResource { Validations_UnitWarning = getResourceOrNull(graph, URIs.Validations_UnitWarning); Validations_Units = getResourceOrNull(graph, URIs.Validations_Units); Validations_Units_UnitIssueSource = getResourceOrNull(graph, URIs.Validations_Units_UnitIssueSource); + Validations_Units_moduleInputUnitWarningDescription = getResourceOrNull(graph, URIs.Validations_Units_moduleInputUnitWarningDescription); + Validations_Units_moduleInterfaceExtension = getResourceOrNull(graph, URIs.Validations_Units_moduleInterfaceExtension); + Validations_Units_moduleOutputUnitWarningDescription = getResourceOrNull(graph, URIs.Validations_Units_moduleOutputUnitWarningDescription); Validations_Units_unitValidator = getResourceOrNull(graph, URIs.Validations_Units_unitValidator); Validations_Units_unitWarningDescription = getResourceOrNull(graph, URIs.Validations_Units_unitWarningDescription); Validations_UnusedDependencyIssue = getResourceOrNull(graph, URIs.Validations_UnusedDependencyIssue); diff --git a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF index 37de08ac..e4d2ebbc 100644 --- a/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF +++ b/org.simantics.sysdyn.ui/META-INF/MANIFEST.MF @@ -55,7 +55,8 @@ Require-Bundle: org.simantics.layer0.utils;bundle-version="0.6.2", org.simantics.scenegraph.swing;bundle-version="1.0.0", org.eclipse.nebula.widgets.tablecombo;bundle-version="1.0.0", org.simantics.fmu;bundle-version="1.0.0", - org.simantics.jfreechart;bundle-version="1.0.0" + org.simantics.jfreechart;bundle-version="1.0.0", + org.simantics.db.indexing;bundle-version="1.1.0" Bundle-Activator: org.simantics.sysdyn.ui.Activator Bundle-ActivationPolicy: lazy Export-Package: org.simantics.sysdyn.ui.browser.nodes diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/profiles/IssueDecorationStyle.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/profiles/IssueDecorationStyle.java index a0289d2d..e3db689f 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/profiles/IssueDecorationStyle.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/elements/profiles/IssueDecorationStyle.java @@ -127,7 +127,7 @@ public class IssueDecorationStyle extends StyleBase { list = graph.getPossibleObject(issueResource, ISSUE.Issue_HasContexts); List contexts = ListUtils.toList(graph, list); if(!contexts.isEmpty()) { - if(ListUtils.toList(graph, list).get(0).equals(component)) { + if(ListUtils.toList(graph, list).subList(0, 1).contains(component)) { return new IssueResult(severity, transform); } } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java index 8d5befed..2664e975 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/project/SysdynProject.java @@ -21,21 +21,19 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.Session; import org.simantics.db.WriteGraph; -import org.simantics.db.common.changeset.GenericChangeListener; import org.simantics.db.common.request.ReadRequest; import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.adapter.RuntimeValuations; import org.simantics.db.layer0.adapter.TrendVariable; -import org.simantics.db.layer0.genericrelation.DependenciesRelation.DependencyChangesRequest; -import org.simantics.db.layer0.genericrelation.DependencyChanges; import org.simantics.db.layer0.service.ActivationManager; import org.simantics.db.request.Read; import org.simantics.db.service.GraphChangeListenerSupport; import org.simantics.db.service.VirtualGraphSupport; +import org.simantics.fmu.FMUControlJNI; import org.simantics.issues.common.IssueUtils; import org.simantics.layer0.Layer0; -import org.simantics.fmu.FMUControlJNI; +import org.simantics.modeling.ModelingUtils; import org.simantics.modeling.services.CaseInsensitiveComponentNamingStrategy2; import org.simantics.modeling.services.ComponentNamingStrategy; import org.simantics.project.IProject; @@ -248,20 +246,23 @@ public class SysdynProject extends AbstractProjectFeature { public void onActivated(final ReadGraph graph, final IProject project) throws DatabaseException { - GraphChangeListenerSupport changeSupport = graph.getService(GraphChangeListenerSupport.class); - changeSupport.addMetadataListener(new GenericChangeListener() { - - @Override - public void onEvent(ReadGraph graph, DependencyChanges event) throws DatabaseException { - - WriteGraph w = (WriteGraph)graph; - w.addMetadata(event); - - } - - }); +// GraphChangeListenerSupport changeSupport = graph.getService(GraphChangeListenerSupport.class); +// changeSupport.addMetadataListener(new GenericChangeListener() { +// +// @Override +// public void onEvent(ReadGraph graph, DependencyChanges event) throws DatabaseException { +// +// WriteGraph w = (WriteGraph)graph; +// w.addMetadata(event); +// +// } +// +// }); IssueUtils.listenActiveProjectIssueSources(graph, project.get()); + + ModelingUtils.trackDependencies(graph); + } diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java index 4758261b..477c6086 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/ConfigurationTab.java @@ -210,6 +210,9 @@ public class ConfigurationTab extends LabelPropertyTabContributor { new PossibleObjectWithType(model, Layer0X.getInstance(graph).Activates, SysdynResource.getInstance(graph).Validations_Units_UnitIssueSource)); + if(unitIssueSource == null) + return false; + Boolean result = graph.getPossibleRelatedValue(unitIssueSource, IssueResource.getInstance(graph).IssueSource_active, Bindings.BOOLEAN); return Boolean.TRUE.equals(result); } 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 9578e18b..5cccc191 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 @@ -35,6 +35,7 @@ import org.simantics.db.management.ISessionContext; import org.simantics.layer0.Layer0; import org.simantics.sysdyn.SysdynResource; 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; import org.simantics.sysdyn.ui.properties.widgets.factories.DoublePropertyModifier; import org.simantics.sysdyn.ui.properties.widgets.factories.VariableNamePropertyModifier; @@ -76,6 +77,19 @@ public class InputVariableTab extends LabelPropertyTabContributor { defaultValue.setInputValidator(new DoubleValidator()); GridDataFactory.fillDefaults().grab(true, false).applyTo(defaultValue.getWidget()); + + Composite unitComposite = new Composite(composite, SWT.NONE); + GridDataFactory.fillDefaults().applyTo(unitComposite); + GridLayoutFactory.fillDefaults().numColumns(2).applyTo(unitComposite); + + label = new Label(unitComposite, SWT.NULL ); + label.setText("Unit:"); + GridDataFactory.fillDefaults().applyTo(label); + + TrackedCombo unitCombo = new UnitComboWidget(unitComposite, support, SWT.DROP_DOWN | SWT.BORDER); + GridDataFactory.fillDefaults().grab(true, false).applyTo(unitCombo.getWidget()); + + Composite variabilityComposite = new Composite(composite, SWT.NONE); GridDataFactory.fillDefaults().grab(false, true).applyTo(variabilityComposite); GridLayoutFactory.fillDefaults().numColumns(2).applyTo(variabilityComposite); diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitComboWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitComboWidget.java new file mode 100644 index 00000000..d2a770fc --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/UnitComboWidget.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * 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.properties.widgets; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.swt.widgets.Composite; +import org.simantics.browsing.ui.swt.widgets.TrackedCombo; +import org.simantics.browsing.ui.swt.widgets.impl.ComboModifyListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.ReadFactoryImpl; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; +import org.simantics.sysdyn.manager.SysdynModelManager; + +public class UnitComboWidget extends TrackedCombo { + + public UnitComboWidget(Composite parent, WidgetSupport support, int style) { + super(parent, support, style); + + + // Add all units used in the model to the unit combo + setItemFactory(new ReadFactoryImpl>() { + + @Override + public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { + Map map = new HashMap(); + + SysdynResource sr = SysdynResource.getInstance(graph); + Layer0 l0 = Layer0.getInstance(graph); + Resource model = graph.getPossibleObject(input, l0.PartOf); + if (model != null) { + Collection variables = graph.getObjects(model, l0.ConsistsOf); + for(Resource v : variables) { + Object unit = graph.getPossibleRelatedValue(v, sr.Variable_unit); + if (unit != null && !map.keySet().contains(unit)) { + map.put((String)unit, (String)unit); + + } + } + } + return map; + } + }); + + // Set initial selection of unit combo + setSelectionFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + String unit = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).Variable_unit); + if(unit == null) + return ""; + else + return unit; + } + }); + + // Modify unit + addModifyListener(new ComboModifyListenerImpl() { + + @Override + public void applyText(WriteGraph graph, Resource input, String text) throws DatabaseException { + graph.denyValue(input, SysdynResource.getInstance(graph).Variable_unit); + graph.claimLiteral(input, SysdynResource.getInstance(graph).Variable_unit, text); + + 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/src/org/simantics/sysdyn/ui/utils/ModelUtils.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java index 8e4833b3..f9e47fef 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/utils/ModelUtils.java @@ -103,8 +103,8 @@ public class ModelUtils { L0X.IsActivatedBy, model, l0.PartOf, model, IssueResource.getInstance(g).IssueSource_active, false - ); - + ); + Resource conf = GraphUtils.create2(g, sr.Configuration, l0.PartOf, model, diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ModuleStandardIssue.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ModuleStandardIssue.java new file mode 100644 index 00000000..4cbdf730 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/ModuleStandardIssue.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * 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.validation; + +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.issue.StandardIssue; +import org.simantics.db.exception.DatabaseException; +import org.simantics.issues.ontology.IssueResource; + +public class ModuleStandardIssue extends StandardIssue { + + public ModuleStandardIssue(Resource type, Resource ... contexts) { + super(type, contexts); + } + + @Override + public void writeAdditionalContext(WriteGraph graph, Resource issue) throws DatabaseException { + super.writeAdditionalContext(graph, issue); + + if(contexts.length >= 2) { + IssueResource IR = IssueResource.getInstance(graph); + // Input / output context + graph.claim(issue, IR.Issue_HasContext, IR.Issue_HasContext_Inverse, contexts[1]); + } + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java index f8e5839b..c01fbadf 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java @@ -12,14 +12,19 @@ package org.simantics.sysdyn.ui.validation; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; +import org.simantics.Simantics; import org.simantics.db.Issue; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.issue.StandardIssue; +import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.common.utils.ListUtils; +import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.issues.common.IssueUtils; @@ -30,40 +35,49 @@ import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.IndependentVariable; import org.simantics.sysdyn.representation.expressions.IExpression; +import org.simantics.sysdyn.representation.utils.UnitUtils; public class UnitFunction { - + @SCLValue(type = "ReadGraph -> Resource -> [Issue]") - public static List unitValidator(ReadGraph graph, Resource independentVariable) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); + public static List unitValidator(ReadGraph graph, Resource component) throws DatabaseException { SysdynResource SR = SysdynResource.getInstance(graph); - - - if(independentVariable == null) + if(graph.isInstanceOf(component, SR.Variable)) + return variableValidator(graph, component); + else if(graph.isInstanceOf(component, SR.Module)) + return moduleValidator(graph, component); + else return Collections.emptyList(); - - Resource configuration = graph.getPossibleObject(independentVariable, L0.PartOf); + } + + private static List variableValidator(ReadGraph graph, Resource variable) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + SysdynResource SR = SysdynResource.getInstance(graph); + + Resource configuration = graph.getPossibleObject(variable, L0.PartOf); if(configuration == null) return Collections.emptyList(); - + // Make sure unit updates are listened to - Resource unitResource = graph.getPossibleObject(independentVariable, SR.Variable_unit); - String unit = graph.getPossibleRelatedValue(independentVariable, SR.Variable_unit); + String unit = graph.getPossibleRelatedValue(variable, SR.Variable_unit); if(unit == null || unit.trim().length() == 0) - return Collections.singletonList(new StandardIssue(SR.Validations_UnitWarning, independentVariable)); - - Resource expressionList = graph.getPossibleObject(independentVariable, SR.Variable_expressionList); + return Collections.singletonList(new StandardIssue(SR.Validations_UnitWarning, variable)); + + if(!graph.isInstanceOf(variable, SR.IndependentVariable)) + return Collections.emptyList(); + Resource expressionList = graph.getPossibleObject(variable, SR.Variable_expressionList); + if(expressionList == null) return Collections.emptyList(); - + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); SysdynModel sm = smm.getModel(graph, configuration); - sm.getMapping().domainModified(independentVariable); + sm.getMapping().domainModified(variable); sm.update(graph); - Object var = sm.getMapping().get(independentVariable); + Object var = sm.getMapping().get(variable); ArrayList issues = new ArrayList(); @@ -71,54 +85,196 @@ public class UnitFunction { for(Resource r : graph.getObjects(expression, SR.Expression_equation)) { graph.getValue(r); } - + sm.getMapping().domainModified(expression); sm.update(graph); - + Object expr = sm.getMapping().get(expression); if(expr != null && var != null && expr instanceof IExpression && var instanceof IndependentVariable) { String result = ((IExpression)expr).validateUnits(graph, sm); if(result != null) { - Issue issue = new StandardIssue(SR.Validations_UnitWarning, independentVariable, expression, unitResource); + Issue issue = new StandardIssue(SR.Validations_UnitWarning, variable, expression, graph.getPossibleObject(variable, SR.Variable_unit)); issues.add(issue); } } } + return issues; + } + + private static List moduleValidator(ReadGraph graph, Resource module) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + SysdynResource SR = SysdynResource.getInstance(graph); + + if(module == null) + return Collections.emptyList(); + + Resource configuration = graph.getPossibleObject(module, L0.PartOf); + if(configuration == null) + return Collections.emptyList(); + String result = null; + ArrayList issues = new ArrayList(); + for(Resource outputRelation : graph.getObjects(module, SR.Variable_isTailOf)) { + Resource reference = graph.getPossibleObject(outputRelation, SR.Dependency_refersTo); + if(reference != null) { + Resource output = graph.getPossibleObject(outputRelation, SR.Variable_HasHead); + String left = graph.getPossibleRelatedValue(output, SR.Variable_unit); + String right = graph.getPossibleRelatedValue(reference, SR.Variable_unit); + result = UnitUtils.matchUnits(left, right); + if(result != null) + issues.add(new ModuleStandardIssue(SR.Validations_ModuleOutputUnitWarning, module, output, reference)); + } + } + for(Resource inputRelation : graph.getObjects(module, SR.Variable_isHeadOf)) { + Resource reference = graph.getPossibleObject(inputRelation, SR.Dependency_refersTo); + if(reference != null) { + Resource variable = graph.getPossibleObject(inputRelation, SR.Variable_HasTail); + String left = graph.getPossibleRelatedValue(variable, SR.Variable_unit); + String right = graph.getPossibleRelatedValue(reference, SR.Variable_unit); + result = UnitUtils.matchUnits(left, right); + if(result != null) + issues.add(new ModuleStandardIssue(SR.Validations_ModuleInputUnitWarning, module, variable, reference)); + } + } return issues; } + @SCLValue(type = "ReadGraph -> Resource -> Variable -> String") public static String unitWarningDescription(ReadGraph graph, Resource converter, Variable property) throws DatabaseException { List contexts = IssueUtils.getContextsForProperty(graph, property); - + if(contexts.size() == 0) return "Error, No contexts"; - + Resource variable = contexts.get(0); String unit = graph.getPossibleRelatedValue(variable, SysdynResource.getInstance(graph).Variable_unit); if(unit == null || unit.length() == 0) return "No unit defined"; + if(contexts.size() < 2) + return "Error, not enough contexts"; + Layer0 L0 = Layer0.getInstance(graph); Resource configuration = graph.getPossibleObject(variable, L0.PartOf); if(configuration == null) return "Error, No Confiugration"; - + SysdynModelManager smm = SysdynModelManager.getInstance(graph.getSession()); SysdynModel sm = smm.getModel(graph, configuration); - + SysdynResource SR = SysdynResource.getInstance(graph); for(Resource r : graph.getObjects(contexts.get(1), SR.Expression_equation)) { graph.getValue(r); } - + Object expr = sm.getMapping().get(contexts.get(1)); String result = ((IExpression)expr).validateUnits(graph, sm); + return result; } + @SCLValue(type = "ReadGraph -> Resource -> Variable -> String") + public static String moduleOutputUnitWarningDescription(ReadGraph graph, Resource converter, Variable property) throws DatabaseException { + List contexts = IssueUtils.getContextsForProperty(graph, property); + + if(contexts.size() < 3) + return "Error, No contexts"; + + Resource output = contexts.get(1); + Resource reference = contexts.get(2); + + return moduleInterfaceDescription(graph, output, reference, "Module output has different unit. (", ")"); + + } + + @SCLValue(type = "ReadGraph -> Resource -> Variable -> String") + public static String moduleInputUnitWarningDescription(ReadGraph graph, Resource converter, Variable property) throws DatabaseException { + List contexts = IssueUtils.getContextsForProperty(graph, property); + + if(contexts.size() < 3) + return "Error, No contexts"; + + Resource output = contexts.get(1); + Resource reference = contexts.get(2); + + return moduleInterfaceDescription(graph, output, reference, "Module input has different unit. (", ")"); + + } + + private static String moduleInterfaceDescription(ReadGraph graph, Resource leftResource, Resource rightResource, String prefix, String suffix) throws DatabaseException { + if(leftResource == null) + return "Erroneus error: left == null"; + + if(rightResource == null) + return "Erroneus error: right == null"; + + SysdynResource SR = SysdynResource.getInstance(graph); + + String left = graph.getPossibleRelatedValue(leftResource, SR.Variable_unit); + if(left == null || left.isEmpty()) + return "No unit defined for " + NameUtils.getSafeName(graph, leftResource); + + String right = graph.getPossibleRelatedValue(rightResource, SR.Variable_unit); + if(right == null || right.isEmpty()) + return "No unit defined for " + NameUtils.getSafeName(graph, rightResource); + + String result = UnitUtils.matchUnits(left, right); + + if(result != null) + result = prefix + result + suffix; + + return result; + } + + + + + @SCLValue(type = "ReadGraph -> [Resource] -> [Resource]") + public static List moduleInterfaceExtension(ReadGraph graph, List rs) throws DatabaseException { + SysdynResource SR = SysdynResource.getInstance(graph); + + HashSet components = new HashSet(); + HashSet searchedModuleTypes = new HashSet(); + for(Resource r : rs) { + if(!graph.isInstanceOf(r, SR.Variable)) + continue; + + if(graph.isInstanceOf(r, SR.Input)) { + Resource connection = graph.getPossibleObject(r, SR.Variable_isHeadOf); + if(connection != null) + components.add(graph.getPossibleObject(connection, SR.Variable_HasTail)); + } + + for(Resource connection : graph.getObjects(r, SR.Variable_isTailOf)) { + Resource head = graph.getPossibleObject(connection, SR.Variable_HasHead); + if(graph.isInstanceOf(head, SR.Module)) { + components.add(head); + } + } + + Layer0 L0 = Layer0.getInstance(graph); + Resource configuration = graph.getPossibleObject(r, L0.PartOf); + Resource possibleModule = graph.getPossibleObject(configuration, L0.PartOf); + if(graph.isInheritedFrom(possibleModule, SR.Module) && !searchedModuleTypes.contains(possibleModule)) { + searchedModuleTypes.add(possibleModule); + for(Resource model : graph.syncRequest(new ObjectsWithType(Simantics.getProject().get(), L0.ConsistsOf, SR.SysdynModel))) { + Collection found = org.simantics.db.indexing.IndexUtils.findByType(graph, model, possibleModule); + components.addAll(found); + } + } + + } + + if(!components.isEmpty()) { + rs.addAll(components); + } + + return rs; + + } + } \ No newline at end of file diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java index 668c83fe..732509d9 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java @@ -18,6 +18,7 @@ import java.util.Set; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; import org.simantics.simulation.ontology.SimulationResource; @@ -175,5 +176,38 @@ public class UnitUtils { } } } + + + public static String matchUnits(String left, String right) throws DatabaseException { + if(left == null || right == null || left.isEmpty() || right.isEmpty()) + return "No unit defined"; + + try { + + StringReader leftReader = new StringReader(left); + UnitParser leftParser = new UnitParser(leftReader); + UnitCheckingNode leftNode = (UnitCheckingNode) leftParser.expr(); + leftReader.close(); + + StringReader rightReader = new StringReader(right); + UnitParser rightParser = new UnitParser(rightReader); + UnitCheckingNode rightNode = (UnitCheckingNode) rightParser.expr(); + rightReader.close(); + + try { + UnitResult leftUnits = leftNode.getUnits(null); + UnitResult rightUnits = rightNode.getUnits(null); + + if(!rightUnits.equals(leftUnits)) + return leftUnits.getCleanFullUnit() + " != " + rightUnits.getCleanFullUnit(); + } catch (UnitCheckingException e) { + return e.getMessage(); + } + + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } }