Support for creating shared ontology dump to git 10/3810/4
authorAntti Villberg <antti.villberg@semantum.fi>
Wed, 22 Jan 2020 23:47:21 +0000 (01:47 +0200)
committerAntti Villberg <antti.villberg@semantum.fi>
Mon, 27 Jan 2020 12:00:18 +0000 (14:00 +0200)
File name escaping from FileUtils

gitlab #452

Change-Id: I1e2d55f04f916a0fd0938fc27cb72ca56fa30000

14 files changed:
bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java
bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResultRequest.java [new file with mode: 0644]
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewer.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerSection.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ConfigurationPropertiesSection.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/DerivedPropertiesSection.java
bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java
bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypePropertiesResult.java [moved from bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResult.java with 93% similarity]
bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypeViewerPropertyInfo.java [moved from bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerPropertyInfo.java with 98% similarity]
bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java [new file with mode: 0644]
bundles/org.simantics.modeling/src/org/simantics/modeling/utils/HeadlessComponentTypePropertiesResultRequest.java [new file with mode: 0644]
bundles/org.simantics.selectionview/src/org/simantics/selectionview/function/All.java

index 0f9d119aca3400a7cd6fbb32ff866514c0de50a8..37b5cfa18012d3e19b5203e57a044546d509c28f 100644 (file)
@@ -23,8 +23,7 @@ import org.simantics.databoard.Bindings;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.request.ResourceRead;
-import org.simantics.db.common.utils.NameUtils;
-import org.simantics.db.exception.AdaptionException;
+import org.simantics.db.common.utils.CommonDBUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.layer0.Layer0;
 
@@ -42,14 +41,11 @@ public class GetEnumerationValue extends ResourceRead<EnumerationValue<Resource>
         return enumerate(graph, resource);
     }
 
+    @Deprecated
     public static String getEnumerationValueName(ReadGraph graph, Resource resource) throws DatabaseException {
-        Layer0 L0 = Layer0.getInstance(graph);
-        String label = graph.getPossibleRelatedValue(resource, L0.HasLabel, Bindings.STRING);
-        if(label != null)
-            return label;
-        return safeName(graph, resource);
+        return CommonDBUtils.getEnumerationValueName(graph, resource);
     }
-
+    
     public static EnumerationValue<Resource> enumerate(ReadGraph graph, Resource resource) throws DatabaseException {
         Layer0 l0 = Layer0.getInstance(graph);
         Set<Resource> types = graph.getTypes(resource);
@@ -69,7 +65,7 @@ public class GetEnumerationValue extends ResourceRead<EnumerationValue<Resource>
                     Collection<Resource> values = graph.getObjects(type, l0.ConsistsOf);
                     List<EnumeratedValue<Resource>> result = new ArrayList<EnumeratedValue<Resource>>(values.size());
                     for (Resource value : values) {
-                        result.add(new EnumeratedValue<Resource>(getEnumerationValueName(graph, value), value));
+                        result.add(new EnumeratedValue<Resource>(CommonDBUtils.getEnumerationValueName(graph, value), value));
                     }
                     Enumeration<Resource> enumeration = new Enumeration<Resource>(result);
                     return new EnumerationValue<Resource>(enumeration, enumeration.find(resource));
@@ -79,22 +75,4 @@ public class GetEnumerationValue extends ResourceRead<EnumerationValue<Resource>
         return null;
     }
 
-    private static String safeName(ReadGraph graph, Resource value) throws DatabaseException {
-        return graph.syncRequest(new NameRequest(value));
-    }
-
-    public static class NameRequest extends ResourceRead<String> {
-        public NameRequest(Resource resource) {
-            super(resource);
-        }
-        @Override
-        public String perform(ReadGraph graph) throws DatabaseException {
-            try {
-                return graph.adapt(resource, String.class);
-            } catch (AdaptionException e) {
-                return NameUtils.getSafeName(graph, resource);
-            }
-        }
-    }
-
 }
index 747e43cf1a24f1e9b2fe0c34500c285894e501cf..919f06b6847a51d876469bec47a5043ab65a6f77 100644 (file)
@@ -18,7 +18,9 @@ import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.PossibleChild;
 import org.simantics.db.common.request.PossibleObjectWithType;
 import org.simantics.db.common.request.PossibleOwner;
+import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.common.request.RuntimeEnvironmentRequest;
+import org.simantics.db.exception.AdaptionException;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.InvalidResourceReferenceException;
 import org.simantics.db.service.ClusterUID;
@@ -350,5 +352,31 @@ public class CommonDBUtils {
         if(!graph.isInstanceOf(child, type)) return null;
         return child;
     }
-    
+
+    public static String getEnumerationValueName(ReadGraph graph, Resource resource) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        String label = graph.getPossibleRelatedValue(resource, L0.HasLabel, Bindings.STRING);
+        if(label != null)
+            return label;
+        return safeName(graph, resource);
+    }
+
+    private static String safeName(ReadGraph graph, Resource value) throws DatabaseException {
+        return graph.syncRequest(new StringAdapterRequest(value));
+    }
+
+    public static class StringAdapterRequest extends ResourceRead<String> {
+        public StringAdapterRequest(Resource resource) {
+            super(resource);
+        }
+        @Override
+        public String perform(ReadGraph graph) throws DatabaseException {
+            try {
+                return graph.adapt(resource, String.class);
+            } catch (AdaptionException e) {
+                return NameUtils.getSafeName(graph, resource);
+            }
+        }
+    }
+
 }
diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResultRequest.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResultRequest.java
new file mode 100644 (file)
index 0000000..a1bb4f4
--- /dev/null
@@ -0,0 +1,45 @@
+package org.simantics.modeling.ui.componentTypeEditor;
+
+import java.util.Collection;
+
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
+import org.simantics.modeling.utils.HeadlessComponentTypePropertiesResultRequest;
+
+public class ComponentTypePropertiesResultRequest extends HeadlessComponentTypePropertiesResultRequest {
+    
+    private final Collection<ComponentTypeViewerSection> sections;
+
+    /**
+     * @param componentTypeViewer
+     */
+    public ComponentTypePropertiesResultRequest(Resource componentType, Collection<ComponentTypeViewerSection> sections) {
+        super(componentType);
+        this.sections = sections;
+    }
+    
+    @Override
+    protected void readSectionSpecificData(ReadGraph graph, ComponentTypeViewerPropertyInfo info) throws DatabaseException {
+        
+        Object sectionSpecificData = null;
+        double priority = Double.NEGATIVE_INFINITY;
+        if(sections != null) {
+            for(ComponentTypeViewerSection section : sections) {
+                Object temp = section.getSectionSpecificData(graph, info);
+                if(temp != null) {
+                    double sectionPriority = section.getDataPriority();
+                    if(sectionPriority > priority) {
+                        sectionSpecificData = temp;
+                        priority = sectionPriority;
+                    }
+                }
+            }
+        }
+        
+        info.sectionSpecificData = sectionSpecificData;
+
+    }
+
+}
\ No newline at end of file
index 38f24b4f7ddb53d8d7b6b3dafc84403c35042533..089d0bdbc649336b608829bacaf7d70e1927ba03 100644 (file)
@@ -12,8 +12,6 @@
 package org.simantics.modeling.ui.componentTypeEditor;
 
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
 
 import org.eclipse.jface.dialogs.IMessageProvider;
 import org.eclipse.jface.resource.JFaceResources;
@@ -34,26 +32,19 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.simantics.Simantics;
-import org.simantics.browsing.ui.graph.impl.GetEnumerationValue;
 import org.simantics.databoard.Bindings;
 import org.simantics.databoard.binding.Binding;
-import org.simantics.databoard.binding.error.BindingException;
 import org.simantics.databoard.type.Datatype;
-import org.simantics.databoard.type.NumberType;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.common.NamedResource;
-import org.simantics.db.common.request.IsEnumeratedValue;
-import org.simantics.db.common.request.UniqueRead;
-import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
-import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.db.procedure.Listener;
 import org.simantics.db.request.Read;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ui.Activator;
-import org.simantics.operation.Layer0X;
-import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
 import org.simantics.utils.ui.ErrorLogger;
 import org.simantics.utils.ui.SWTUtils;
 
@@ -167,94 +158,7 @@ public class ComponentTypeViewer {
     }
     
     private void createGraphListener() {
-        Simantics.getSession().asyncRequest(new UniqueRead<ComponentTypePropertiesResult>() {
-            @Override
-            public ComponentTypePropertiesResult perform(ReadGraph graph) throws DatabaseException {
-                List<ComponentTypeViewerPropertyInfo> result = new ArrayList<>();
-                List<NamedResource> connectionPoints = new ArrayList<>();
-                Layer0 L0 = Layer0.getInstance(graph);
-                Layer0X L0X = Layer0X.getInstance(graph);
-                StructuralResource2 STR = StructuralResource2.getInstance(graph);
-
-                boolean typeIsImmutable = graph.isImmutable(data.componentType)
-                        || graph.hasStatement(data.componentType, STR.ComponentType_Locked)
-                        || Layer0Utils.isPublished(graph, data.componentType)
-                        || Layer0Utils.isContainerPublished(graph, data.componentType);
-
-                for(Resource relation : graph.getObjects(data.componentType, L0.DomainOf)) {
-                    if(graph.isSubrelationOf(relation, L0.HasProperty)) {
-                        String name = graph.getRelatedValue(relation, L0.HasName);
-                        String type =  graph.getPossibleRelatedValue(relation, L0.RequiresValueType);
-                        String label = graph.getPossibleRelatedValue(relation, L0.HasLabel);
-                        if (label == null)
-                            label = ""; //$NON-NLS-1$
-                        String description = graph.getPossibleRelatedValue(relation, L0.HasDescription);
-                        if (description == null)
-                            description = ""; //$NON-NLS-1$
-                        NumberType numberType = null;
-                        if(type == null)
-                            type = "Double"; //$NON-NLS-1$
-                        String unit = graph.getPossibleRelatedValue(relation, L0X.HasUnit, Bindings.STRING);
-                        String defaultValue = "0"; //$NON-NLS-1$
-                        String expression = null;
-                        
-                        for(Resource assertion : graph.getAssertedObjects(data.componentType, relation)) {
-                            try {
-                                expression = graph.getPossibleRelatedValue(assertion, L0.SCLValue_expression, Bindings.STRING);
-                                if(expression != null) {
-                                       defaultValue = "=" + expression; //$NON-NLS-1$
-                                } else if (graph.sync(new IsEnumeratedValue(assertion))) {
-                                    defaultValue = GetEnumerationValue.getEnumerationValueName(graph, assertion);
-                                } else {
-                                       Datatype dt = getPossibleDatatype(graph, assertion);
-                                       if (dt == null)
-                                           continue;
-                                       if (dt instanceof NumberType)
-                                           numberType = (NumberType) dt;
-                                       Binding binding = Bindings.getBinding(dt);
-                                       Object value = graph.getValue(assertion, binding);
-                                       try {
-                                           defaultValue = binding.toString(value, true);
-                                       } catch (BindingException e) {
-                                           ErrorLogger.defaultLogError(e);
-                                       }
-                                }
-                            } catch(DatabaseException e) {
-                                ErrorLogger.defaultLogError(e);
-                            }
-                        }
-                        
-                        String valid = expression != null ? DerivedPropertiesSection.validateMonitorExpression(graph, data.componentType, relation, expression) : null; 
-
-                        boolean immutable = typeIsImmutable || graph.isImmutable(relation);
-                        ComponentTypeViewerPropertyInfo info =
-                                new ComponentTypeViewerPropertyInfo(relation, name, type, defaultValue, numberType, unit, label, description, expression, valid, immutable);
-                        
-                        Object sectionSpecificData = null;
-                        double priority = Double.NEGATIVE_INFINITY;
-                        for(ComponentTypeViewerSection section : sections) {
-                            Object temp = section.getSectionSpecificData(graph, info);
-                            if(temp != null) {
-                                double sectionPriority = section.getDataPriority();
-                                if(sectionPriority > priority) {
-                                    sectionSpecificData = temp;
-                                    priority = sectionPriority;
-                                }
-                            }
-                        }
-                        info.sectionSpecificData = sectionSpecificData;
-                        
-                        result.add(info);
-
-                    } else if (graph.isInstanceOf(relation, STR.ConnectionRelation)) {
-                        NamedResource nr = new NamedResource(NameUtils.getSafeName(graph, relation), relation);
-                        connectionPoints.add(nr);
-                    }
-                }
-                Collections.sort(result);
-                return new ComponentTypePropertiesResult(result, connectionPoints, typeIsImmutable);
-            }
-        }, new Listener<ComponentTypePropertiesResult>() {
+        Simantics.getSession().asyncRequest(new ComponentTypePropertiesResultRequest(this.data.componentType, this.sections), new Listener<ComponentTypePropertiesResult>() {
             @Override
             public void execute(final ComponentTypePropertiesResult result) {  
                 SWTUtils.asyncExec(data.form.getDisplay(), new Runnable() {
@@ -284,16 +188,6 @@ public class ComponentTypeViewer {
         });
     }
 
-    protected Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {
-        Binding binding = Bindings.getBindingUnchecked(Datatype.class);
-        for (Resource dataTypeResource : graph.getObjects(literal, Layer0.getInstance(graph).HasDataType)) {
-            Datatype dt = graph.getPossibleValue(dataTypeResource, binding);
-            if (dt != null)
-                return dt;
-        }
-        return null;
-    }
-
     public void setFocus() {
         data.form.setFocus();
     }
index 01c799d45832b824835b1a477c20c3ecd3dfce16..73b09f7bf431776352fc30dc6ad540b5a5255dff 100644 (file)
@@ -52,9 +52,9 @@ import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.function.DbConsumer;
 import org.simantics.db.layer0.QueryIndexUtils;
-import org.simantics.db.layer0.util.Layer0Utils;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.userComponent.ComponentTypeCommands;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
 import org.simantics.scl.runtime.function.Function2;
 import org.simantics.scl.runtime.function.Function4;
 import org.simantics.utils.datastructures.Pair;
index 45dd8868ed4b0a8547ab1fc4677acb6d3cc553c4..90d2217dc45d18c40b36b8a7fe6dd62fa7c06844 100644 (file)
@@ -3,6 +3,8 @@ package org.simantics.modeling.ui.componentTypeEditor;
 import org.eclipse.ui.forms.widgets.Section;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
 
 public interface ComponentTypeViewerSection {
 
index c11463f1321848cdbd9c69c28da204209ad3700d..ef44ddf12adf4aa1c8d81fe379606eb5497e8fb8 100644 (file)
@@ -59,6 +59,8 @@ import org.simantics.layer0.Layer0;
 import org.simantics.modeling.ui.Activator;
 import org.simantics.modeling.ui.componentTypeEditor.LiftPropertiesDialog.LiftedProperty;
 import org.simantics.modeling.userComponent.ComponentTypeCommands;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
 import org.simantics.selectionview.SelectionViewResources;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.simantics.utils.datastructures.Pair;
index 23777604d35e43e6d221a60f7b2908e01566f777..9ef0b31cd24082a5f5a75aa6aee77db67f066235 100644 (file)
@@ -47,10 +47,10 @@ import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.layer0.Layer0;
 import org.simantics.modeling.scl.CompileProceduralSCLMonitorRequest;
-import org.simantics.modeling.scl.CompileSCLMonitorRequest;
 import org.simantics.modeling.userComponent.ComponentTypeCommands;
-import org.simantics.scl.runtime.SCLContext;
-import org.simantics.scl.runtime.function.Function1;
+import org.simantics.modeling.utils.ComponentTypePropertiesResult;
+import org.simantics.modeling.utils.ComponentTypeViewerPropertyInfo;
+import org.simantics.modeling.utils.HeadlessComponentTypePropertiesResultRequest;
 import org.simantics.scl.runtime.function.Function4;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.slf4j.Logger;
@@ -310,7 +310,7 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection {
                             if(graph.isInstanceOf(componentType, STR.ProceduralComponentType)) {
                                 CompileProceduralSCLMonitorRequest.compileAndEvaluate(graph, context);
                             } else {
-                                compileAndEvaluate(graph, context, expression);
+                                HeadlessComponentTypePropertiesResultRequest.compileAndEvaluate(graph, context, expression);
                             }
                             
                         } catch (Exception e) {
@@ -330,23 +330,6 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection {
         return null;
     }
 
-    public static void compileAndEvaluate(ReadGraph graph, Variable context, String expression) throws DatabaseException {
-        SCLContext sclContext = SCLContext.getCurrent();
-        Object oldGraph = sclContext.get("graph");
-        try {
-            CompileSCLMonitorRequest compileSCLMonitorRequest = new ValidationCompilationRequest(graph, context, expression);
-            Function1<Variable,Object> exp = graph.syncRequest(compileSCLMonitorRequest);
-            sclContext.put("graph", graph);
-            //return exp.apply(context.getParent(graph));
-        } catch (DatabaseException e) {
-            throw (DatabaseException)e;
-        } catch (Throwable t) {
-            throw new DatabaseException(t);
-        } finally {
-            sclContext.put("graph", oldGraph);
-        }
-    }
-    
     @Override
     public void update(ComponentTypePropertiesResult result) {
         if (table.isDisposed())
@@ -447,29 +430,4 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection {
         return 100.0;
     }
 
-    private static final class ValidationCompilationRequest extends CompileSCLMonitorRequest {
-        private final String expression;
-    
-        private ValidationCompilationRequest(ReadGraph graph, Variable context, String expression)
-                throws DatabaseException {
-            super(graph, context);
-            this.expression = expression;
-        }
-    
-        @Override
-        protected String getExpressionText(ReadGraph graph) throws DatabaseException {
-            return expression;
-        }
-    
-        @Override
-        public int hashCode() {
-            return super.hashCode() + 37 * expression.hashCode();
-        }
-    
-        @Override
-        public boolean equals(Object obj) {
-            return super.equals(obj) && ((ValidationCompilationRequest)obj).expression.equals(expression);
-        }
-    }
-
 }
\ No newline at end of file
index 410d1797661f7d3e62412cf717268bad5043b370..eae7695747153fdc45ddeb66eda859f49775a7da 100644 (file)
@@ -23,11 +23,14 @@ import org.eclipse.jface.operation.IRunnableWithProgress;
 import org.simantics.Simantics;
 import org.simantics.databoard.binding.error.BindingException;
 import org.simantics.databoard.serialization.SerializationException;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.graph.refactoring.FixExportedOntology;
 import org.simantics.modeling.ModelingUtils;
 import org.simantics.modeling.ModelingUtils.LibraryInfo;
+import org.simantics.modeling.utils.DumpOntologyStructure;
 import org.simantics.utils.ui.dialogs.ShowMessage;
 import org.slf4j.LoggerFactory;
 
@@ -83,6 +86,17 @@ public class SharedOntologyExporter implements IRunnableWithProgress {
             try {
                 Path input = Paths.get(location.toURI());
                 FixExportedOntology.createTGAndPGraph(input);
+                DumpOntologyStructure data = Simantics.sync(new UniqueRead<DumpOntologyStructure>() {
+
+                    @Override
+                    public DumpOntologyStructure perform(ReadGraph graph) throws DatabaseException {
+                        DumpOntologyStructure result = new DumpOntologyStructure();
+                        result.read(graph, info.library.getResource());
+                        return result;
+                    }
+                    
+                });
+                data.write(new File(new File(location.getParent(), location.getName() + ".dump"), info.library.getName()));
             } catch (Exception e) {
                 LOGGER.error("Could not generate TG and Pgraph", e);
             }
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java
new file mode 100644 (file)
index 0000000..b19b175
--- /dev/null
@@ -0,0 +1,286 @@
+package org.simantics.modeling.utils;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.utils.CommonDBUtils;
+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.graphfile.ontology.GraphFileResource;
+import org.simantics.layer0.Layer0;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.structural2.variables.Connection;
+import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
+import org.simantics.utils.FileUtils;
+
+public class DumpOntologyStructure {
+
+    private Resource ontology;
+    
+    private Set<Resource> containers = new HashSet<>(); 
+    private Set<Resource> folders = new HashSet<>();
+    private Map<Resource, String> names = new HashMap<>();
+    private Map<Resource,Resource> parents = new HashMap<>();
+    private Map<Resource, File> libraryFolders = new HashMap<>();
+    private Set<Resource> modules = new HashSet<>();
+    private Map<Resource, String> moduleCodes = new HashMap<>();
+    private Set<Resource> pgraphs = new HashSet<>();
+    private Map<Resource, String> pgraphCodes = new HashMap<>();
+    private Set<Resource> graphFiles = new HashSet<>();
+    private Map<Resource, byte[]> graphFileBytes = new HashMap<>();
+    private Set<Resource> componentTypes = new HashSet<>();
+    private Map<Resource, String> componentTypeDumps = new HashMap<>();
+    private Set<Resource> components = new HashSet<>();
+    private Map<Resource, String> componentDumps = new HashMap<>();
+
+    private void readNameAndParent(ReadGraph graph, Resource container, Resource r) throws DatabaseException {
+        parents.put(r, container);
+        names.put(r, NameUtils.getSafeName(graph, r));
+    }
+    
+    private void readLibraries(ReadGraph graph, Resource container) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.Library)) {
+            folders.add(r);
+            names.put(r, NameUtils.getSafeName(graph, r));
+            parents.put(r, container);
+            readNameAndParent(graph, container, r);
+            readLibraries(graph, r);
+        }
+        containers.addAll(folders);
+    }
+    
+    private void readComponentTypes(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        for(Resource container : containers) {
+            for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.ComponentType)) {
+                folders.add(r);
+                componentTypes.add(r);
+                readNameAndParent(graph, container, r);
+                ComponentTypePropertiesResult data = graph.syncRequest(new HeadlessComponentTypePropertiesResultRequest(r));
+                StringBuilder dump = new StringBuilder();
+                for(ComponentTypeViewerPropertyInfo pi : data.getProperties()) {
+                    dump.append(pi.name);
+                    dump.append(" ");
+                    dump.append(pi.type);
+                    dump.append(" ");
+                    dump.append(pi.defaultValue);
+                    if(pi.unit != null) {
+                        dump.append(" ");
+                        dump.append(pi.unit);
+                    }
+                    dump.append(" ");
+                    dump.append(pi.label);
+                    if(pi.description != null) {
+                        dump.append(" ");
+                        dump.append(pi.description);
+                    }
+                    dump.append("\n");
+                }
+                for(NamedResource nr : data.getConnectionPoints()) {
+                    dump.append("cp ");
+                    dump.append(nr.getName());
+                    dump.append("\n");
+                }
+                componentTypeDumps.put(r, dump.toString());
+            }
+        }
+        containers.addAll(folders);
+    }
+
+    private void readComposites(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        for(Resource container : containers) {
+            for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.Composite)) {
+                folders.add(r);
+                readNameAndParent(graph, container, r);
+            }
+        }
+        containers.addAll(folders);
+    }
+
+    private void readComponents(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+        for(Resource container : containers) {
+            for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, STR.Component)) {
+                if(folders.contains(r))
+                    continue;
+                components.add(r);
+                readNameAndParent(graph, container, r);
+                Variable v = Variables.getVariable(graph, r);
+                TreeMap<String,Variable> properties = new TreeMap<>();
+                for(Variable property : v.getProperties(graph))
+                    properties.put(property.getName(graph), property);
+                StringBuilder dump = new StringBuilder();
+                for(Variable property : properties.values()) {
+                    String possibleValue = property.getPossiblePropertyValue(graph, "HasDisplayValue", Bindings.STRING);
+                    if(possibleValue != null) {
+                        dump.append(property.getName(graph));
+                        dump.append(" ");
+                        dump.append(possibleValue);
+                        dump.append("\n");
+                    }
+                    if(property.getClassifications(graph).contains(StructuralResource2.URIs.ConnectionRelation)) {
+                        dump.append(property.getName(graph));
+                        Connection c = property.getValue(graph);
+                        for(VariableConnectionPointDescriptor desc : c.getConnectionPointDescriptors(graph, null)) {
+                            dump.append(" ");
+                            dump.append(desc.getRelativeRVI(graph, v));
+                        }
+                        dump.append("\n");
+                    }
+                }
+                componentDumps.put(r, dump.toString());
+            }
+        }
+    }
+
+    private void readSCLModules(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        for(Resource container : containers) {
+            for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.SCLModule)) {
+                String code = graph.getPossibleRelatedValue(r, L0.SCLModule_definition, Bindings.STRING);
+                if(code != null) {
+                    moduleCodes.put(r, code);
+                }
+                modules.add(r);
+                readNameAndParent(graph, container, r);
+            }
+        }
+    }
+    
+    private void readPGraphs(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        for(Resource container : containers) {
+            for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.PGraph)) {
+                String code = graph.getPossibleRelatedValue(r, L0.PGraph_definition, Bindings.STRING);
+                if(code != null) {
+                    pgraphCodes.put(r, code);
+                }
+                pgraphs.add(r);
+                readNameAndParent(graph, container, r);
+            }
+        }
+    }
+
+    private void readGraphFiles(ReadGraph graph) throws DatabaseException {
+        Layer0 L0 = Layer0.getInstance(graph);
+        GraphFileResource GF = GraphFileResource.getInstance(graph);
+        for(Resource container : containers) {
+            for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, GF.File)) {
+                byte[] bytes = graph.getPossibleRelatedValue(r, GF.HasFiledata, Bindings.BYTE_ARRAY);
+                if(bytes != null) {
+                    graphFileBytes.put(r, bytes);
+                }
+                graphFiles.add(r);
+                readNameAndParent(graph, container, r);
+            }
+        }
+    }
+
+    public void read(ReadGraph graph, Resource ontology) throws DatabaseException {
+        this.ontology = ontology;
+        containers.add(ontology);
+        readLibraries(graph, ontology);
+        readComponentTypes(graph);
+        readComposites(graph);
+        readComponents(graph);
+        readSCLModules(graph);
+        readPGraphs(graph);
+        readGraphFiles(graph);
+    }
+    
+    private File escapeFile(File file) {
+
+        if(file.exists())
+            return file;
+        
+        return new File(escapeFile(file.getParentFile()), FileUtils.escapeFileName(file.getName()));
+        
+    }
+    
+    public void write(File unsafeFolder) throws IOException {
+        File folder = escapeFile(unsafeFolder);
+        if(folder.exists())
+            FileUtils.deleteAll(folder);
+        folder.mkdirs();
+        writeLibraries(folder);
+        writeSCLModules(folder);
+        writePGraphs(folder);
+        writeGraphFiles(folder);
+        writeComponentTypes(folder);
+        writeComponents(folder);
+    }
+    
+    private File getFolder(File root, Resource library) {
+        if(ontology.equals(library))
+            return root;
+        Resource parent = parents.get(library);
+        File parentFolder = getFolder(root, parent);
+        return new File(parentFolder, FileUtils.escapeFileName(names.get(library))); 
+    }
+    
+    private File getParentFolder(File root, Resource r) {
+        Resource parent = parents.get(r);
+        return getFolder(root, parent);
+    }
+
+    private File getFile(File root, Resource r) {
+        return new File(getParentFolder(root, r), FileUtils.escapeFileName(names.get(r)));
+    }
+
+    private void writeLibraries(File rootFolder) {
+        for(Resource library : folders) {
+            File folder = getFolder(rootFolder, library);
+            folder.mkdirs();
+            libraryFolders.put(library, folder);
+        }
+    }
+    
+    private void writeSCLModules(File rootFolder) throws IOException {
+        for(Resource r : modules) {
+            FileUtils.writeFile(getFile(rootFolder, r), moduleCodes.get(r).getBytes());
+        }
+    }
+    
+    private void writePGraphs(File rootFolder) throws IOException {
+        for(Resource r : pgraphs) {
+            FileUtils.writeFile(getFile(rootFolder, r), pgraphCodes.get(r).getBytes());
+        }
+    }
+
+    private void writeGraphFiles(File rootFolder) throws IOException {
+        for(Resource r : graphFiles) {
+            FileUtils.writeFile(getFile(rootFolder, r), graphFileBytes.get(r));
+        }
+    }
+
+    private void writeComponentTypes(File rootFolder) throws IOException {
+        for(Resource r : componentTypes) {
+            File folder = getFolder(rootFolder, r);
+            File file = new File(folder, "__interface__");
+            FileUtils.writeFile(file, componentTypeDumps.get(r).getBytes());
+        }
+    }
+
+    private void writeComponents(File rootFolder) throws IOException {
+        for(Resource r : components) {
+            FileUtils.writeFile(getFile(rootFolder, r), componentDumps.get(r).getBytes());
+        }
+    }
+    
+}
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/HeadlessComponentTypePropertiesResultRequest.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/HeadlessComponentTypePropertiesResultRequest.java
new file mode 100644 (file)
index 0000000..a2f7138
--- /dev/null
@@ -0,0 +1,238 @@
+package org.simantics.modeling.utils;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.databoard.binding.Binding;
+import org.simantics.databoard.binding.error.BindingException;
+import org.simantics.databoard.type.Datatype;
+import org.simantics.databoard.type.NumberType;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.common.NamedResource;
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
+import org.simantics.db.common.request.IsEnumeratedValue;
+import org.simantics.db.common.request.UniqueRead;
+import org.simantics.db.common.utils.CommonDBUtils;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.request.PropertyInfo;
+import org.simantics.db.layer0.request.PropertyInfoRequest;
+import org.simantics.db.layer0.util.Layer0Utils;
+import org.simantics.db.layer0.variable.StandardGraphChildVariable;
+import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
+import org.simantics.db.layer0.variable.Variable;
+import org.simantics.layer0.Layer0;
+import org.simantics.modeling.scl.CompileProceduralSCLMonitorRequest;
+import org.simantics.modeling.scl.CompileSCLMonitorRequest;
+import org.simantics.operation.Layer0X;
+import org.simantics.scl.runtime.SCLContext;
+import org.simantics.scl.runtime.function.Function1;
+import org.simantics.structural.stubs.StructuralResource2;
+import org.simantics.utils.ui.ErrorLogger;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class HeadlessComponentTypePropertiesResultRequest extends UniqueRead<ComponentTypePropertiesResult> {
+    
+    private static final Logger LOGGER = LoggerFactory.getLogger(HeadlessComponentTypePropertiesResultRequest.class);
+
+    protected final Resource componentType;
+
+    /**
+     * @param componentTypeViewer
+     */
+    public HeadlessComponentTypePropertiesResultRequest(Resource componentType) {
+        this.componentType = componentType;
+    }
+
+    protected void readSectionSpecificData(ReadGraph graph, ComponentTypeViewerPropertyInfo info) throws DatabaseException {
+    }
+    
+    @Override
+    public ComponentTypePropertiesResult perform(ReadGraph graph) throws DatabaseException {
+        List<ComponentTypeViewerPropertyInfo> result = new ArrayList<>();
+        List<NamedResource> connectionPoints = new ArrayList<>();
+        Layer0 L0 = Layer0.getInstance(graph);
+        Layer0X L0X = Layer0X.getInstance(graph);
+        StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+        boolean typeIsImmutable = graph.isImmutable(componentType)
+                || graph.hasStatement(componentType, STR.ComponentType_Locked)
+                || Layer0Utils.isPublished(graph, componentType)
+                || Layer0Utils.isContainerPublished(graph, componentType);
+
+        for(Resource relation : graph.getObjects(componentType, L0.DomainOf)) {
+            if(graph.isSubrelationOf(relation, L0.HasProperty)) {
+                String name = graph.getRelatedValue(relation, L0.HasName);
+                String type =  graph.getPossibleRelatedValue(relation, L0.RequiresValueType);
+                String label = graph.getPossibleRelatedValue(relation, L0.HasLabel);
+                if (label == null)
+                    label = ""; //$NON-NLS-1$
+                String description = graph.getPossibleRelatedValue(relation, L0.HasDescription);
+                if (description == null)
+                    description = ""; //$NON-NLS-1$
+                NumberType numberType = null;
+                if(type == null)
+                    type = "Double"; //$NON-NLS-1$
+                String unit = graph.getPossibleRelatedValue(relation, L0X.HasUnit, Bindings.STRING);
+                String defaultValue = "0"; //$NON-NLS-1$
+                String expression = null;
+                
+                for(Resource assertion : graph.getAssertedObjects(componentType, relation)) {
+                    try {
+                        expression = graph.getPossibleRelatedValue(assertion, L0.SCLValue_expression, Bindings.STRING);
+                        if(expression != null) {
+                               defaultValue = "=" + expression; //$NON-NLS-1$
+                        } else if (graph.sync(new IsEnumeratedValue(assertion))) {
+                            defaultValue = CommonDBUtils.getEnumerationValueName(graph, assertion);
+                        } else {
+                            Datatype dt = getPossibleDatatype(graph, assertion);
+                            if (dt == null)
+                                continue;
+                            if (dt instanceof NumberType)
+                                numberType = (NumberType) dt;
+                            Binding binding = Bindings.getBinding(dt);
+                            Object value = graph.getValue(assertion, binding);
+                            try {
+                                defaultValue = binding.toString(value, true);
+                            } catch (BindingException e) {
+                                ErrorLogger.defaultLogError(e);
+                            }
+                        }
+                    } catch(DatabaseException e) {
+                        ErrorLogger.defaultLogError(e);
+                    }
+                }
+                
+                String valid = expression != null ? validateMonitorExpression(graph, componentType, relation, expression) : null; 
+
+                boolean immutable = typeIsImmutable || graph.isImmutable(relation);
+                ComponentTypeViewerPropertyInfo info =
+                        new ComponentTypeViewerPropertyInfo(relation, name, type, defaultValue, numberType, unit, label, description, expression, valid, immutable);
+                
+                readSectionSpecificData(graph, info);
+
+                result.add(info);
+
+            } else if (graph.isInstanceOf(relation, STR.ConnectionRelation)) {
+                NamedResource nr = new NamedResource(NameUtils.getSafeName(graph, relation), relation);
+                connectionPoints.add(nr);
+            }
+        }
+        Collections.sort(result);
+        return new ComponentTypePropertiesResult(result, connectionPoints, typeIsImmutable);
+    }
+    
+    private Datatype getPossibleDatatype(ReadGraph graph, Resource literal) throws DatabaseException {
+        Binding binding = Bindings.getBindingUnchecked(Datatype.class);
+        for (Resource dataTypeResource : graph.getObjects(literal, Layer0.getInstance(graph).HasDataType)) {
+            Datatype dt = graph.getPossibleValue(dataTypeResource, binding);
+            if (dt != null)
+                return dt;
+        }
+        return null;
+    }
+
+    public static String validateMonitorExpression(final RequestProcessor processor, final Resource componentType, final Resource relation, final String expression) {
+
+        try {
+            return processor.sync(new UniqueRead<String>() {
+
+                @Override
+                public String perform(ReadGraph graph) throws DatabaseException {
+
+                    StructuralResource2 STR = StructuralResource2.getInstance(graph);
+
+                    // TODO: this is a bit hackish but should get the job done in most parts and
+                    // importantly indicates something for the user
+                    PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(relation), TransientCacheAsyncListener.<PropertyInfo>instance());
+                    Variable parent = new StandardGraphChildVariable(null, null, componentType) {
+                        
+                        public Resource getType(ReadGraph graph) throws DatabaseException {
+                            return componentType;
+                        };
+                        
+                        public Variable getPossibleProperty(ReadGraph graph, String name) throws DatabaseException {
+                            Variable prop = super.getPossibleProperty(graph, name);
+                            if (prop != null) {
+                                return prop;
+                            } else {
+                                return getChild(graph, name);
+                            }
+                        };
+                    };
+                    
+                    for(Resource literal : graph.getAssertedObjects(componentType, relation)) {
+
+                        try {
+                            Variable context = new StandardGraphPropertyVariable(parent, null, null, info, literal);
+                            if(graph.isInstanceOf(componentType, STR.ProceduralComponentType)) {
+                                CompileProceduralSCLMonitorRequest.compileAndEvaluate(graph, context);
+                            } else {
+                                compileAndEvaluate(graph, context, expression);
+                            }
+                            
+                        } catch (Exception e) {
+                            String msg = e.getMessage();
+                            int index = msg.indexOf(":"); //$NON-NLS-1$
+                            if(index > 0) msg = msg.substring(index);
+                            return msg;
+                        }
+                    }
+                    return null;
+                }
+                
+            });
+        } catch (DatabaseException e) {
+            LOGGER.error("Could not validate ", e);
+        }
+        return null;
+    }
+    
+    public static void compileAndEvaluate(ReadGraph graph, Variable context, String expression) throws DatabaseException {
+        SCLContext sclContext = SCLContext.getCurrent();
+        Object oldGraph = sclContext.get("graph");
+        try {
+            CompileSCLMonitorRequest compileSCLMonitorRequest = new ValidationCompilationRequest(graph, context, expression);
+            Function1<Variable,Object> exp = graph.syncRequest(compileSCLMonitorRequest);
+            sclContext.put("graph", graph);
+            //return exp.apply(context.getParent(graph));
+        } catch (DatabaseException e) {
+            throw (DatabaseException)e;
+        } catch (Throwable t) {
+            throw new DatabaseException(t);
+        } finally {
+            sclContext.put("graph", oldGraph);
+        }
+    }
+
+    private static final class ValidationCompilationRequest extends CompileSCLMonitorRequest {
+        private final String expression;
+    
+        private ValidationCompilationRequest(ReadGraph graph, Variable context, String expression)
+                throws DatabaseException {
+            super(graph, context);
+            this.expression = expression;
+        }
+    
+        @Override
+        protected String getExpressionText(ReadGraph graph) throws DatabaseException {
+            return expression;
+        }
+    
+        @Override
+        public int hashCode() {
+            return super.hashCode() + 37 * expression.hashCode();
+        }
+    
+        @Override
+        public boolean equals(Object obj) {
+            return super.equals(obj) && ((ValidationCompilationRequest)obj).expression.equals(expression);
+        }
+    }
+    
+}
\ No newline at end of file
index d26d86682874a15e61551de5cfcd66907ec975b4..fa3f0f8d11b065881e428f46ab4ee989d746e374 100644 (file)
@@ -43,6 +43,7 @@ import org.simantics.db.common.request.InstanceEnumerationMap;
 import org.simantics.db.common.request.IsEnumeratedValue;
 import org.simantics.db.common.request.UniqueRead;
 import org.simantics.db.common.request.WriteRequest;
+import org.simantics.db.common.utils.CommonDBUtils;
 import org.simantics.db.common.utils.NameUtils;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.util.Layer0Utils;
@@ -472,7 +473,7 @@ public class All {
                        Resource possibleValue = context.getParent(graph).getPossibleRepresents(graph);
                        if(possibleValue != null) {
                                if(graph.syncRequest(new IsEnumeratedValue(possibleValue))) {
-                                       return GetEnumerationValue.getEnumerationValueName(graph, possibleValue);
+                                       return CommonDBUtils.getEnumerationValueName(graph, possibleValue);
                                }
                        }