From 0b4ab685b0aa54ba8fbe0a5742e27726c862cfd9 Mon Sep 17 00:00:00 2001 From: Antti Villberg Date: Thu, 23 Jan 2020 01:47:21 +0200 Subject: [PATCH] Support for creating shared ontology dump to git File name escaping from FileUtils gitlab #452 Change-Id: I1e2d55f04f916a0fd0938fc27cb72ca56fa30000 --- .../ui/graph/impl/GetEnumerationValue.java | 32 +- .../db/common/utils/CommonDBUtils.java | 30 +- .../ComponentTypePropertiesResultRequest.java | 45 +++ .../ComponentTypeViewer.java | 112 +------ .../ComponentTypeViewerData.java | 2 +- .../ComponentTypeViewerSection.java | 2 + .../ConfigurationPropertiesSection.java | 2 + .../DerivedPropertiesSection.java | 50 +-- .../wizard/SharedOntologyExporter.java | 14 + .../utils}/ComponentTypePropertiesResult.java | 2 +- .../ComponentTypeViewerPropertyInfo.java | 2 +- .../modeling/utils/DumpOntologyStructure.java | 286 ++++++++++++++++++ ...sComponentTypePropertiesResultRequest.java | 238 +++++++++++++++ .../simantics/selectionview/function/All.java | 3 +- 14 files changed, 633 insertions(+), 187 deletions(-) create mode 100644 bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResultRequest.java rename bundles/{org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor => org.simantics.modeling/src/org/simantics/modeling/utils}/ComponentTypePropertiesResult.java (93%) rename bundles/{org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor => org.simantics.modeling/src/org/simantics/modeling/utils}/ComponentTypeViewerPropertyInfo.java (99%) create mode 100644 bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java create mode 100644 bundles/org.simantics.modeling/src/org/simantics/modeling/utils/HeadlessComponentTypePropertiesResultRequest.java diff --git a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java index 0f9d119ac..37b5cfa18 100644 --- a/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java +++ b/bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/GetEnumerationValue.java @@ -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 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 enumerate(ReadGraph graph, Resource resource) throws DatabaseException { Layer0 l0 = Layer0.getInstance(graph); Set types = graph.getTypes(resource); @@ -69,7 +65,7 @@ public class GetEnumerationValue extends ResourceRead Collection values = graph.getObjects(type, l0.ConsistsOf); List> result = new ArrayList>(values.size()); for (Resource value : values) { - result.add(new EnumeratedValue(getEnumerationValueName(graph, value), value)); + result.add(new EnumeratedValue(CommonDBUtils.getEnumerationValueName(graph, value), value)); } Enumeration enumeration = new Enumeration(result); return new EnumerationValue(enumeration, enumeration.find(resource)); @@ -79,22 +75,4 @@ public class GetEnumerationValue extends ResourceRead return null; } - private static String safeName(ReadGraph graph, Resource value) throws DatabaseException { - return graph.syncRequest(new NameRequest(value)); - } - - public static class NameRequest extends ResourceRead { - 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); - } - } - } - } diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java index 747e43cf1..919f06b68 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/CommonDBUtils.java @@ -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 { + 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 index 000000000..a1bb4f441 --- /dev/null +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResultRequest.java @@ -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 sections; + + /** + * @param componentTypeViewer + */ + public ComponentTypePropertiesResultRequest(Resource componentType, Collection 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 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewer.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewer.java index 38f24b4f7..089d0bdbc 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewer.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewer.java @@ -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() { - @Override - public ComponentTypePropertiesResult perform(ReadGraph graph) throws DatabaseException { - List result = new ArrayList<>(); - List 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() { + Simantics.getSession().asyncRequest(new ComponentTypePropertiesResultRequest(this.data.componentType, this.sections), new Listener() { @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(); } diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java index 01c799d45..73b09f7bf 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerData.java @@ -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; diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerSection.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerSection.java index 45dd8868e..90d2217dc 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerSection.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerSection.java @@ -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 { diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ConfigurationPropertiesSection.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ConfigurationPropertiesSection.java index c11463f13..ef44ddf12 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ConfigurationPropertiesSection.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ConfigurationPropertiesSection.java @@ -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; diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/DerivedPropertiesSection.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/DerivedPropertiesSection.java index 23777604d..9ef0b31cd 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/DerivedPropertiesSection.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/DerivedPropertiesSection.java @@ -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 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 diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java index 410d17976..eae769574 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java +++ b/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/SharedOntologyExporter.java @@ -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() { + + @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.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResult.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypePropertiesResult.java similarity index 93% rename from bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResult.java rename to bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypePropertiesResult.java index e4c670cf5..b37f1be3f 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypePropertiesResult.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypePropertiesResult.java @@ -1,4 +1,4 @@ -package org.simantics.modeling.ui.componentTypeEditor; +package org.simantics.modeling.utils; import java.util.List; diff --git a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerPropertyInfo.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypeViewerPropertyInfo.java similarity index 99% rename from bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerPropertyInfo.java rename to bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypeViewerPropertyInfo.java index c5e77383f..86047c31e 100644 --- a/bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/componentTypeEditor/ComponentTypeViewerPropertyInfo.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/ComponentTypeViewerPropertyInfo.java @@ -1,4 +1,4 @@ -package org.simantics.modeling.ui.componentTypeEditor; +package org.simantics.modeling.utils; import org.simantics.databoard.annotations.Optional; import org.simantics.databoard.type.NumberType; 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 index 000000000..b19b17523 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java @@ -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 containers = new HashSet<>(); + private Set folders = new HashSet<>(); + private Map names = new HashMap<>(); + private Map parents = new HashMap<>(); + private Map libraryFolders = new HashMap<>(); + private Set modules = new HashSet<>(); + private Map moduleCodes = new HashMap<>(); + private Set pgraphs = new HashSet<>(); + private Map pgraphCodes = new HashMap<>(); + private Set graphFiles = new HashSet<>(); + private Map graphFileBytes = new HashMap<>(); + private Set componentTypes = new HashSet<>(); + private Map componentTypeDumps = new HashMap<>(); + private Set components = new HashSet<>(); + private Map 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 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 index 000000000..a2f7138b4 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/HeadlessComponentTypePropertiesResultRequest.java @@ -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 { + + 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 result = new ArrayList<>(); + List 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() { + + @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.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 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 diff --git a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/function/All.java b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/function/All.java index d26d86682..fa3f0f8d1 100644 --- a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/function/All.java +++ b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/function/All.java @@ -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); } } -- 2.47.1