Merge "Added effectful Kleisli composition to Prelude."
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Sun, 24 Mar 2019 22:13:54 +0000 (22:13 +0000)
committerGerrit Code Review <gerrit2@simantics>
Sun, 24 Mar 2019 22:13:54 +0000 (22:13 +0000)
bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLHandlerValueRequest.java
bundles/org.simantics.modeling.ontology/graph/ModelingViewpoint.pgraph
bundles/org.simantics.modeling.ui/scl/Simantics/Testing/BrowseContext.scl
bundles/org.simantics.modeling/META-INF/MANIFEST.MF
bundles/org.simantics.modeling/adapters.xml
bundles/org.simantics.modeling/src/org/simantics/modeling/ColorDescriptorUtil.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelBackgroundColorRule.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelColorRule.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/SCLLabelForegroundColorRule.java [new file with mode: 0644]

index ab5e7a8e40e985a8a52e4b3b81b5013eaf621def..8ca55eaa5ab67affe0460dcd7ff0c2ff4a81d277 100644 (file)
@@ -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<Resource,Resource> 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<Resource,Resource> getComponentTypeAndRoot(ReadGraph graph, Resource component)  throws DatabaseException {
+       private static Pair<Resource,Resource> 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<TCon> 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<String, ComponentTypeProperty> propertyMap =
-                                               graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
-                                                               TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+                Map<String, ComponentTypeProperty> propertyMap;
+                if (parameter.first != null) {
+                    propertyMap =
+                        graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
+                                TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
+                } else {
+                    // TODO: Antti to consider
+                    // To handle procedural user components
+                    propertyMap = Collections.emptyMap();
+                }
+//                             Map<String, ComponentTypeProperty> propertyMap =
+//                                             graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()),
+//                                                             TransientCacheListener.<Map<String, ComponentTypeProperty>>instance());
                                return new CompilationContext(runtimeEnvironment, propertyMap);
                        }
                });
index e3c551d0a2f45faf7d361a98e6c90bc77fa3eae1..47931cc4f1172e7eb0bc631471114d8fd648209b 100644 (file)
@@ -26,6 +26,12 @@ MOD.SCLLabelRule <T VP.VisualsRule
 MOD.SCLImageRule <T VP.VisualsRule
   >-- MOD.SCLImageRule.getImages ==> "Resource -> <ReadGraph> [(String,ImageDescriptor)]" <R L0.HasProperty : L0.FunctionalRelation
 
+MOD.SCLLabelForegroundColorRule <T VP.VisualsRule
+  >-- MOD.SCLLabelForegroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> <ReadGraph> Maybe (Double, Double, Double)]" <R L0.HasProperty : L0.FunctionalRelation
+
+MOD.SCLLabelBackgroundColorRule <T VP.VisualsRule
+  >-- MOD.SCLLabelBackgroundColorRule.getColor ==> "Resource -> Maybe (Double, Double, Double) -> String -> Integer -> <ReadGraph> Maybe (Double, Double, Double)]" <R L0.HasProperty : L0.FunctionalRelation
+
 MOD.SCLAction <T ACT.Action
   --> MOD.SCLAction.action ==> "Resource -> <Proc> ()" <R L0.HasProperty : L0.FunctionalRelation 
 
@@ -392,6 +398,20 @@ MOD.sclImageRule : L0.Template
             L0.SCLValue.expression %expression
             L0.HasValueType "Resource -> <ReadGraph> [(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 -> <ReadGraph> 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 -> <ReadGraph> Maybe (Double, Double, Double)"
+
 MOD.sclAction : L0.Template
     @template %action %expression
         %action : MOD.SCLAction
index 106d93bb322410cbc6e2d2d4728c53174bda04e2..590ddd21ce8f658d40b133dd6cb0fddd3be8607d 100644 (file)
@@ -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 -> <Proc> String
+    colorDescriptorAsTuple3 :: ColorDescriptor -> <Proc> (Double, Double, Double)
+    colorDescriptor :: (Double, Double, Double) -> <Proc> ColorDescriptor
+
 defaultFontDescriptor = createFontDescriptorFrom "Arial" 12 0 
 
 importJava "org.simantics.browsing.ui.content.LabelDecorator" where
index 78a1d3b59a54522562be445e34c7beae4c4358d2..c1d891b0aed6c0fad748cffca51d84d6ca8ecdf8 100644 (file)
@@ -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,
index 0e104229aaee577d25d988f58be4e1863e6f0597..7693586d8029b79dcb5ccc80d838e3821644e791 100644 (file)
         </type>
     </target>    
 
+    <target interface="org.simantics.browsing.ui.model.visuals.VisualsRule">
+        <type uri="http://www.simantics.org/Modeling-0.0/SCLLabelForegroundColorRule"
+            class="org.simantics.modeling.adapters.SCLLabelForegroundColorRule">
+            <graph />
+            <this />
+        </type>
+    </target>
+
+    <target interface="org.simantics.browsing.ui.model.visuals.VisualsRule">
+        <type uri="http://www.simantics.org/Modeling-0.0/SCLLabelBackgroundColorRule"
+            class="org.simantics.modeling.adapters.SCLLabelBackgroundColorRule">
+            <graph />
+            <this />
+        </type>
+    </target>
+
     <target interface="org.simantics.browsing.ui.model.tests.Test">
         <type uri="http://www.simantics.org/Modeling-0.0/SCLTest"
             class="org.simantics.modeling.adapters.SCLTest">
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 (file)
index 0000000..3f02f54
--- /dev/null
@@ -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 (file)
index 0000000..04b1e18
--- /dev/null
@@ -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> 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 (file)
index 0000000..db3cf4b
--- /dev/null
@@ -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> 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 (file)
index 0000000..bf9c458
--- /dev/null
@@ -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> Color decorateForeground(Color color, String column, int itemIndex) {
+                return decorateColor(graph, content, color, column, itemIndex);
+            }
+        };
+    }
+}
\ No newline at end of file