From: Tuukka Lehtonen Date: Sun, 24 Mar 2019 22:13:54 +0000 (+0000) Subject: Merge "Added effectful Kleisli composition to Prelude." X-Git-Tag: v1.43.0~136^2~171 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=9d780ffc68dbcb27bd90b5f80f4288de62d149fd;hp=bd78deb4759a49ed84200b95fddc9569438512f7 Merge "Added effectful Kleisli composition to Prelude." --- diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java index ab5e7a8e4..8ca55eaa5 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java @@ -1,5 +1,6 @@ package org.simantics.document.server.request; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -65,7 +66,7 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR } public ServerSCLHandlerValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException { - this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p)); + this(getComponentTypeAndRoot(graph, s, o), o, resolveExpectedValueType(graph, p)); } private static Pair getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException { @@ -83,7 +84,7 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR return Pair.make(parent.getType(graph), root); } - private static Pair getComponentTypeAndRoot(ReadGraph graph, Resource component) throws DatabaseException { + private static Pair getComponentTypeAndRoot(ReadGraph graph, Resource component, Resource literal) throws DatabaseException { if(component != null) { Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component)); if(type != null) { @@ -95,8 +96,15 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR Resource root = graph.syncRequest(new IndexRoot(doc)); return Pair.make(componentType, root); } + // TODO: For Antti to consider and fix later + // Introduced to handle procedural user components where component == null + } else if (literal != null) { + Resource root = graph.syncRequest(new IndexRoot(literal)); + return Pair.make(null, root); + } else { + throw new DatabaseException("Couldn't resolve component type and root for component == null && literal == null"); } - throw new IllegalStateException(); + //throw new IllegalStateException(); } public static List getEffects(ReadGraph graph, Variable context) throws DatabaseException { @@ -158,9 +166,19 @@ public class ServerSCLHandlerValueRequest extends AbstractExpressionCompilationR @Override public CompilationContext perform(ReadGraph graph) throws DatabaseException { RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second)); - Map propertyMap = - graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()), - TransientCacheListener.>instance()); + Map propertyMap; + if (parameter.first != null) { + propertyMap = + graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()), + TransientCacheListener.>instance()); + } else { + // TODO: Antti to consider + // To handle procedural user components + propertyMap = Collections.emptyMap(); + } +// Map propertyMap = +// graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()), +// TransientCacheListener.>instance()); return new CompilationContext(runtimeEnvironment, propertyMap); } }); diff --git a/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph b/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph index e3c551d0a..47931cc4f 100644 --- a/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph +++ b/bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph @@ -26,6 +26,12 @@ MOD.SCLLabelRule -- MOD.SCLImageRule.getImages ==> "Resource -> [(String,ImageDescriptor)]" -- MOD.SCLLabelForegroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)]" -- MOD.SCLLabelBackgroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)]" MOD.SCLAction.action ==> "Resource -> ()" [(String,ImageDescriptor)]" +MOD.sclLabelForegroundColorRule : L0.Template + @template %action %expression + %action : MOD.SCLLabelForegroundColorRule + MOD.SCLLabelForegroundColorRule.getColor _ : MOD.SCLValue + L0.SCLValue.expression %expression + L0.HasValueType "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)" + +MOD.sclLabelBackgroundColorRule : L0.Template + @template %action %expression + %action : MOD.SCLLabelBackgroundColorRule + MOD.SCLLabelBackgroundColorRule.getColor _ : MOD.SCLValue + L0.SCLValue.expression %expression + L0.HasValueType "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> Maybe (Double, Double, Double)" + MOD.sclAction : L0.Template @template %action %expression %action : MOD.SCLAction diff --git a/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl b/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl index 106d93bb3..590ddd21c 100644 --- a/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl +++ b/bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl @@ -78,6 +78,8 @@ importJava "org.simantics.browsing.ui.content.ImageDecorator" where importJava "org.eclipse.jface.resource.DeviceResourceDescriptor" where data FontDescriptor + +importJava "org.eclipse.jface.resource.ColorDescriptor" where data ColorDescriptor importJava "org.eclipse.jface.resource.FontDescriptor" where @@ -85,6 +87,11 @@ importJava "org.eclipse.jface.resource.FontDescriptor" where @JavaName createFrom createFontDescriptorFrom :: String -> Integer -> Integer -> FontDescriptor +importJava "org.simantics.modeling.ColorDescriptorUtil" where + colorDescriptorAsHex :: ColorDescriptor -> String + colorDescriptorAsTuple3 :: ColorDescriptor -> (Double, Double, Double) + colorDescriptor :: (Double, Double, Double) -> ColorDescriptor + defaultFontDescriptor = createFontDescriptorFrom "Arial" 12 0 importJava "org.simantics.browsing.ui.content.LabelDecorator" where diff --git a/bundles/org.simantics.modeling/META-INF/MANIFEST.MF b/bundles/org.simantics.modeling/META-INF/MANIFEST.MF index 78a1d3b59..c1d891b0a 100644 --- a/bundles/org.simantics.modeling/META-INF/MANIFEST.MF +++ b/bundles/org.simantics.modeling/META-INF/MANIFEST.MF @@ -40,7 +40,8 @@ Require-Bundle: org.simantics.simulation;bundle-version="1.0.0", org.slf4j.api, org.simantics.graphfile.ontology, org.apache.batik, - org.simantics.graph.compiler + org.simantics.graph.compiler, + org.simantics.browsing.ui;bundle-version="1.1.0" Export-Package: org.simantics.modeling, org.simantics.modeling.actions, org.simantics.modeling.adapters, diff --git a/bundles/org.simantics.modeling/adapters.xml b/bundles/org.simantics.modeling/adapters.xml index 0e104229a..7693586d8 100644 --- a/bundles/org.simantics.modeling/adapters.xml +++ b/bundles/org.simantics.modeling/adapters.xml @@ -197,6 +197,22 @@ + + + + + + + + + + + + + + diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java new file mode 100644 index 000000000..3f02f54f7 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java @@ -0,0 +1,23 @@ +package org.simantics.modeling; + +import org.eclipse.jface.resource.ColorDescriptor; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; +import org.simantics.scl.runtime.tuple.Tuple3; + +public class ColorDescriptorUtil { + public static ColorDescriptor colorDescriptor(Tuple3 color) { + return ColorDescriptor.createFrom(new RGB((int)(((double)color.c0) * 255), (int)(((double)color.c1) * 255), (int)(((double)color.c2) * 255))); + } + + public static Tuple3 colorDescriptorAsTuple3(ColorDescriptor descriptor) { + Color color = ((ColorDescriptor)descriptor).createColor(Display.getDefault()); + return new Tuple3(color.getRed(), color.getGreen(), color.getBlue()); + } + + public synchronized static String colorDescriptorAsHex(ColorDescriptor descriptor) { + Color color = ((ColorDescriptor)descriptor).createColor(Display.getDefault()); + return String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue()); + } +} diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java new file mode 100644 index 000000000..04b1e1873 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java @@ -0,0 +1,26 @@ +package org.simantics.modeling.adapters; + +import org.simantics.browsing.ui.content.LabelDecorator; +import org.simantics.browsing.ui.model.labeldecorators.AbstractLabelDecorator; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.modeling.ModelingResources; + +public class SCLLabelBackgroundColorRule extends SCLLabelColorRule { + + public SCLLabelBackgroundColorRule(ReadGraph graph, Resource rule) { + super(graph, rule, ModelingResources.getInstance(graph).SCLLabelBackgroundColorRule_getColor); + } + + @Override + public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException { + return new AbstractLabelDecorator() { + + @Override + public Color decorateBackground(Color color, String column, int itemIndex) { + return decorateColor(graph, content, color, column, itemIndex); + } + }; + } +} \ No newline at end of file diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java new file mode 100644 index 000000000..db3cf4b94 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java @@ -0,0 +1,50 @@ +package org.simantics.modeling.adapters; + +import org.eclipse.jface.resource.ColorDescriptor; +import org.eclipse.swt.graphics.RGB; +import org.simantics.Simantics; +import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule; +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.modeling.ColorDescriptorUtil; +import org.simantics.scl.runtime.function.Function; +import org.simantics.scl.runtime.tuple.Tuple3; + +public abstract class SCLLabelColorRule implements LabelDecorationRule { + + private Resource rule; + private Resource predicate; + + public SCLLabelColorRule(ReadGraph graph, Resource rule, Resource predicate) { + this.rule = rule; + this.predicate = predicate; + } + + @Override + public boolean isCompatible(Class contentType) { + return contentType.equals(Resource.class); + } + + @SuppressWarnings("unchecked") + protected Color decorateColor(ReadGraph graph, Object content, Color color, String column, int itemIndex) { + try { + Variable ruleVariable = Variables.getVariable(graph, rule); + Function fn = ruleVariable.getPossiblePropertyValue(graph, predicate); + if (fn != null) { + Tuple3 prevColor = color != null ? ColorDescriptorUtil.colorDescriptorAsTuple3((ColorDescriptor)color) : null; + Tuple3 result = Simantics.applySCLRead(graph, fn, content, prevColor, column, itemIndex); + int r = (int)(((double)result.c0) * 255); + int g = (int)(((double)result.c1) * 255); + int b = (int)(((double)result.c2) * 255); + return (Color)ColorDescriptor.createFrom(new RGB(r, g, b)); + } else { + return color; + } + } catch (DatabaseException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java new file mode 100644 index 000000000..bf9c45874 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java @@ -0,0 +1,26 @@ +package org.simantics.modeling.adapters; + +import org.simantics.browsing.ui.content.LabelDecorator; +import org.simantics.browsing.ui.model.labeldecorators.AbstractLabelDecorator; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.modeling.ModelingResources; + +public class SCLLabelForegroundColorRule extends SCLLabelColorRule { + + public SCLLabelForegroundColorRule(ReadGraph graph, Resource rule) { + super(graph, rule, ModelingResources.getInstance(graph).SCLLabelForegroundColorRule_getColor); + } + + @Override + public LabelDecorator getLabelDecorator(ReadGraph graph, Object content) throws DatabaseException { + return new AbstractLabelDecorator() { + + @Override + public Color decorateForeground(Color color, String column, int itemIndex) { + return decorateColor(graph, content, color, column, itemIndex); + } + }; + } +} \ No newline at end of file