From 11532e91770beadd55d85037c4b5e00bbcace69f Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Tue, 5 Sep 2017 13:01:50 +0300 Subject: [PATCH] Better support for ontological profiles refs #7465 Change-Id: I53e4dcdd4c0ffde62cf231bbcbde325e585096f9 --- .../graph/DiagramProfiles.pgraph | 12 +++ .../diagram/profile/function/All.java | 36 ++++++-- .../profile/view/ProfileCheckContributor.java | 8 +- bundles/org.simantics.diagram/adapters.xml | 5 ++ .../diagram/profile/SCLTextGridStyle.java | 88 +++++++++++++++++++ .../META-INF/MANIFEST.MF | 3 +- .../graph/PlatformUI.pgraph | 10 ++- .../graph/scl/SCLMain.scl | 10 +++ .../scenegraph/profile/ProfileUtils.java | 46 ++++++++++ .../request/ProfileActiveEntryResources.java | 4 +- .../RuntimeProfileActiveEntryResources.java | 10 ++- 11 files changed, 215 insertions(+), 17 deletions(-) create mode 100644 bundles/org.simantics.diagram/src/org/simantics/diagram/profile/SCLTextGridStyle.java diff --git a/bundles/org.simantics.diagram.ontology/graph/DiagramProfiles.pgraph b/bundles/org.simantics.diagram.ontology/graph/DiagramProfiles.pgraph index e1e48e03b..ef868a504 100644 --- a/bundles/org.simantics.diagram.ontology/graph/DiagramProfiles.pgraph +++ b/bundles/org.simantics.diagram.ontology/graph/DiagramProfiles.pgraph @@ -36,6 +36,12 @@ DIA.ProfileEntry.HasPriority -- DIA.SCLTextGridStyle.function ==> "Variable -> String" -- DIA.ProfileActivationState.HasProfile --> DIA.Profile () { - public void processRecursively(WriteGraph graph, Resource runtimeProfile, Resource entry, boolean checked) throws DatabaseException { + public void processRecursively(WriteGraph graph, Resource runtimeDiagram, Resource runtimeProfile, Resource entry, boolean checked) throws DatabaseException { DiagramResource DIA = DiagramResource.getInstance(graph); @@ -110,7 +110,7 @@ public class All { singleSelGroups.add(group); } } - processRecursively(graph, runtimeProfile, child, checked); + processRecursively(graph, runtimeDiagram, runtimeProfile, child, checked); } } else if(graph.isInstanceOf(entry, DIA.ProfileEntry)) { @@ -120,23 +120,43 @@ public class All { //enable selected item from single selection groups, disable the rest. Collection entries = graph.getObjects(group, DIA.ProfileEntry_HasGroup_Inverse); for (Resource e : entries) { - graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, e); + deactivate(graph, runtimeDiagram, runtimeProfile, e); } - graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry); + activate(graph, runtimeDiagram, runtimeProfile, entry); } else { - graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry); + deactivate(graph, runtimeDiagram, runtimeProfile, entry); } } else { if(checked) { - graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry); + activate(graph, runtimeDiagram, runtimeProfile, entry); } else { - graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry); + deactivate(graph, runtimeDiagram, runtimeProfile, entry); } } } } + + private void activate(WriteGraph graph, Resource runtimeDiagram, Resource runtimeProfile, Resource entry) throws DatabaseException { + if(graph.isImmutable(runtimeProfile)) { + Resource activationState = ProfileUtils.claimProfileActivationState(graph, runtimeDiagram, runtimeProfile, entry); + if(activationState != null) + graph.claim(activationState, SimulationResource.getInstance(graph).IsActive, null, entry); + } else { + graph.claim(runtimeProfile, SimulationResource.getInstance(graph).IsActive, null, entry); + } + } + + private void deactivate(WriteGraph graph, Resource runtimeDiagram, Resource runtimeProfile, Resource entry) throws DatabaseException { + if(graph.isImmutable(runtimeProfile)) { + Resource activationState = ProfileUtils.claimProfileActivationState(graph, runtimeDiagram, runtimeProfile, entry); + if(activationState != null) + graph.denyStatement(activationState, SimulationResource.getInstance(graph).IsActive, entry); + } else { + graph.denyStatement(runtimeProfile, SimulationResource.getInstance(graph).IsActive, entry); + } + } @Override public Boolean apply(Object _event) { @@ -162,7 +182,7 @@ public class All { DiagramResource DIA = DiagramResource.getInstance(graph); Resource runtimeProfile = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasRuntimeProfile); - processRecursively(graph, runtimeProfile, entry.getSecond(), checked); + processRecursively(graph, runtimeDiagram, runtimeProfile, entry.getSecond(), checked); } diff --git a/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileCheckContributor.java b/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileCheckContributor.java index 2bc9f320f..544c394c5 100644 --- a/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileCheckContributor.java +++ b/bundles/org.simantics.diagram.profile/src/org/simantics/diagram/profile/view/ProfileCheckContributor.java @@ -35,22 +35,22 @@ public class ProfileCheckContributor implements CheckedStateRule { throws DatabaseException { ResourcePair entry = (ResourcePair)parent; + Resource activeProfile = entry.getFirst(); DiagramResource DIA = DiagramResource.getInstance(graph); SimulationResource SIMU = SimulationResource.getInstance(graph); if(graph.isInstanceOf(entry.getSecond(), DIA.Profile)) { - Resource list = graph.getPossibleObject(entry.getSecond(), DIA.HasEntries); - Collection activeEntries = graph.syncRequest( new ProfileActiveEntryResources(entry.getFirst(), list) ); + Collection activeEntries = graph.syncRequest( new ProfileActiveEntryResources(activeProfile, list) ); if (activeEntries.isEmpty()) { return CheckedState.NOT_CHECKED; } else { - Collection entries = graph.syncRequest( new ProfileEntryResources(entry.getFirst(), list) ); + Collection entries = graph.syncRequest( new ProfileEntryResources(activeProfile, list) ); return entries.equals(activeEntries) ? CheckedState.CHECKED : CheckedState.GRAYED; } } else if(graph.isInstanceOf(entry.getSecond(), DIA.ProfileEntry)) { - if(graph.hasStatement(entry.getFirst(), SIMU.IsActive, entry.getSecond())) return CheckedState.CHECKED; + if(graph.hasStatement(activeProfile, SIMU.IsActive, entry.getSecond())) return CheckedState.CHECKED; } return CheckedState.NOT_CHECKED; diff --git a/bundles/org.simantics.diagram/adapters.xml b/bundles/org.simantics.diagram/adapters.xml index 04e85be36..c7e65bfad 100644 --- a/bundles/org.simantics.diagram/adapters.xml +++ b/bundles/org.simantics.diagram/adapters.xml @@ -34,6 +34,11 @@ class="org.simantics.diagram.adapter.ExpressionStyle"> + + + diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/SCLTextGridStyle.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/SCLTextGridStyle.java new file mode 100644 index 000000000..5850ffb37 --- /dev/null +++ b/bundles/org.simantics.diagram/src/org/simantics/diagram/profile/SCLTextGridStyle.java @@ -0,0 +1,88 @@ +package org.simantics.diagram.profile; + +import java.awt.geom.AffineTransform; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +import org.simantics.Simantics; +import org.simantics.common.format.Formatter; +import org.simantics.databoard.Bindings; +import org.simantics.datatypes.literal.Vec2d; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; +import org.simantics.db.layer0.variable.Variables; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.diagram.synchronization.graph.DiagramGraphUtil; +import org.simantics.modeling.ModelingResources; +import org.simantics.scl.runtime.function.Function1; +import org.simantics.utils.datastructures.Pair; + +/** + * @author Antti Villberg + */ +public class SCLTextGridStyle extends TextGridStyle { + + final Resource style; + + public SCLTextGridStyle(Resource style) { + this.style = style; + } + + @Override + public Resource getPropertyRelation(ReadGraph graph, Resource module) { + throw new Error("Fix this"); + } + + @Override + protected Object getIdentity(Resource entry) { + return new Pair(style, entry); + } + + @Override + protected String rowId() { + return getNodeName(); + } + + @Override + public MonitorTextGridResult calculateStyle(ReadGraph graph, Resource runtimeDiagram, Resource entry, Resource element, Variable configuration) + throws DatabaseException { + + DiagramResource DIA = DiagramResource.getInstance(graph); + ModelingResources MOD = ModelingResources.getInstance(graph); + + String variableURI = graph.getPossibleRelatedValue(runtimeDiagram, DIA.RuntimeDiagram_HasVariable, Bindings.STRING); + Variable activeVariable = org.simantics.db.layer0.variable.Variables.getPossibleVariable(graph, variableURI); + if (activeVariable == null) + return null; + + Resource module = graph.getPossibleObject(element, MOD.ElementToComponent); + if (module == null) + return null; + + Variable moduleVariable = activeVariable.browsePossible(graph, module); + if (moduleVariable == null) + return null; + + Variable styleVariable = Variables.getVariable(graph, style); + Function1 function = styleVariable.getPossiblePropertyValue(graph, "function"); + String result = Simantics.applySCLRead(graph, function, moduleVariable); + + AffineTransform transform = DiagramGraphUtil.getAffineTransform(graph, element); + Vec2d offset = DiagramGraphUtil.getOffset(graph, element); + boolean enabled = !DiagramGraphUtil.getProfileMonitorsHidden(graph, element); + boolean up = DiagramGraphUtil.getProfileMonitorsUp(graph, element); + double spacing = DiagramGraphUtil.getProfileMonitorSpacing(graph, element); + + return new MonitorTextGridResult(rowId(), result, "", "", enabled, up, spacing, null, null, ElementTranslation.function(element), transform, offset); + + } + + @Override + public String getNodeName() { + return "" + style.getResourceId(); + } + +} diff --git a/bundles/org.simantics.platform.ui.ontology/META-INF/MANIFEST.MF b/bundles/org.simantics.platform.ui.ontology/META-INF/MANIFEST.MF index 9db6c4593..ef76a5147 100644 --- a/bundles/org.simantics.platform.ui.ontology/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.platform.ui.ontology/META-INF/MANIFEST.MF @@ -17,6 +17,7 @@ Require-Bundle: org.simantics.layer0, org.simantics.annotation.ontology;bundle-version="1.0.0", org.simantics.modeling.template2d.ontology;bundle-version="1.0.0", org.simantics.spreadsheet.ontology;bundle-version="1.2.0", - org.simantics.selectionview.ui.ontology;bundle-version="1.1.0" + org.simantics.selectionview.ui.ontology;bundle-version="1.1.0", + org.simantics.diagram.ontology;bundle-version="2.2.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Export-Package: org.simantics.platform.ui diff --git a/bundles/org.simantics.platform.ui.ontology/graph/PlatformUI.pgraph b/bundles/org.simantics.platform.ui.ontology/graph/PlatformUI.pgraph index 88f2d0fa7..35e306a89 100644 --- a/bundles/org.simantics.platform.ui.ontology/graph/PlatformUI.pgraph +++ b/bundles/org.simantics.platform.ui.ontology/graph/PlatformUI.pgraph @@ -1,6 +1,7 @@ L0 = PROJECT = MOD = +DIA = STR = SEL = SEL_UI = @@ -43,4 +44,11 @@ PlatformUI.StandardPropertiesTabContribution : SEL.TypedVariableTabContribution SEL.TypedVariableTabContribution.HasType L0.Entity SEL.VariableTabContribution.HasView SEL_UI.StandardProperties SEL.VariableTabContribution.HasPriority -1 - L0.HasLabel "Properties" \ No newline at end of file + @MOD.scl SEL.VariableTabContribution.transformation "standardPropertiesElementTransformation" "Variable -> Variable" + L0.HasLabel "Properties" + +PlatformUI.StandardElementProperties : SEL.TypedVariableTabContribution + SEL.TypedVariableTabContribution.HasType DIA.Element + L0.HasLabel "Element Properties" + SEL.VariableTabContribution.HasPriority -2 + SEL.VariableTabContribution.HasView SEL_UI.StandardProperties diff --git a/bundles/org.simantics.platform.ui.ontology/graph/scl/SCLMain.scl b/bundles/org.simantics.platform.ui.ontology/graph/scl/SCLMain.scl index 83bb80ea8..44bf61267 100644 --- a/bundles/org.simantics.platform.ui.ontology/graph/scl/SCLMain.scl +++ b/bundles/org.simantics.platform.ui.ontology/graph/scl/SCLMain.scl @@ -80,4 +80,14 @@ modifyCodeHandler self input ctx = do variable = currentRangeExpressionVariable self input setExpression variable val Nothing + +standardPropertiesElementTransformation :: Variable -> Variable +standardPropertiesElementTransformation var = do + match getPossibleType var with + Nothing -> var + Just resourceType -> if isInheritedFrom resourceType DIA.Element then do + match possibleObject (represents var) MOD.ElementToComponent with + Nothing -> var + Just component -> resourceVariable component + else var \ No newline at end of file diff --git a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java index 989b7bb05..88a69065b 100644 --- a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java +++ b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java @@ -2,11 +2,18 @@ package org.simantics.scenegraph.profile; import java.util.Collections; import java.util.List; +import java.util.UUID; +import org.simantics.databoard.Bindings; 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.request.PossibleIndexRoot; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.layer0.Layer0; +import org.simantics.scenegraph.profile.request.ProfileActiveEntryResources; public class ProfileUtils { @@ -22,4 +29,43 @@ public class ProfileUtils { return graph.getRelatedValue2(entries, DIA.Profile_children, entries); } + public static Resource getPossibleProfileActivationState(ReadGraph graph, Resource runtimeDiagram, Resource profile) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource conf = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration); + if(conf == null) return null; + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(conf)); + if(indexRoot == null) return null; + // Find existing state + for(Resource state : graph.syncRequest(new ObjectsWithType(indexRoot, L0.ConsistsOf, DIA.ProfileActivationState))) { + Resource ref = graph.getPossibleObject(state, DIA.ProfileActivationState_HasProfile); + if(profile.equals(ref)) return state; + } + return null; + + } + + public static Resource claimProfileActivationState(WriteGraph graph, Resource runtimeDiagram, Resource runtimeProfile, Resource entry) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource conf = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration); + if(conf == null) return null; + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(conf)); + if(indexRoot == null) return null; + // Find existing state + for(Resource state : graph.syncRequest(new ObjectsWithType(indexRoot, L0.ConsistsOf, DIA.ProfileActivationState))) { + Resource profile = graph.getPossibleObject(state, DIA.ProfileActivationState_HasProfile); + if(profile.equals(runtimeProfile)) return state; + } + // Create new state + Resource state = graph.newResource(); + graph.claim(state, L0.InstanceOf, DIA.ProfileActivationState); + graph.claimLiteral(state, L0.HasName, L0.String, UUID.randomUUID().toString(), Bindings.STRING); + graph.claim(state, DIA.ProfileActivationState_HasProfile, runtimeProfile); + graph.claim(indexRoot, L0.ConsistsOf, state); + return state; + } + + } diff --git a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/ProfileActiveEntryResources.java b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/ProfileActiveEntryResources.java index 93bab97c4..641362a7a 100644 --- a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/ProfileActiveEntryResources.java +++ b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/ProfileActiveEntryResources.java @@ -25,8 +25,8 @@ public class ProfileActiveEntryResources extends ProfileEntryResources { SimulationResource SIMU; - public ProfileActiveEntryResources(Resource profile, Resource entry) { - super(profile, entry); + public ProfileActiveEntryResources(Resource activationResource, Resource entry) { + super(activationResource, entry); } @Override diff --git a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/RuntimeProfileActiveEntryResources.java b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/RuntimeProfileActiveEntryResources.java index 5c438533d..f59c1d71a 100644 --- a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/RuntimeProfileActiveEntryResources.java +++ b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/request/RuntimeProfileActiveEntryResources.java @@ -21,6 +21,7 @@ import org.simantics.db.common.NamedResource; import org.simantics.db.common.request.ResourceRead; import org.simantics.db.exception.DatabaseException; import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.scenegraph.profile.ProfileUtils; /** * @author Antti Villberg @@ -53,7 +54,14 @@ public class RuntimeProfileActiveEntryResources extends ResourceRead