X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.ui%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Fui%2FcomponentTypeEditor%2FDerivedPropertiesSection.java;h=1abe8434897ee8bc3071ff528baf45b41e76bbee;hp=19d9e6f29dc5c0f7e12b8e18844cb1d8999f6561;hb=fd2a6ff5d708baed2bdbaf94e6cd7bcc7b288069;hpb=6b4432af6fe79c859dd1ddb31fd185d64c729863 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 19d9e6f29..1abe84348 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 @@ -32,18 +32,35 @@ import org.simantics.db.ReadGraph; import org.simantics.db.RequestProcessor; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; -import org.simantics.db.common.request.PossibleIndexRoot; +import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteRequest; 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.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.modeling.userComponent.ComponentTypeCommands; +import org.simantics.scl.runtime.SCLContext; +import org.simantics.scl.runtime.function.Function1; import org.simantics.scl.runtime.function.Function4; import org.simantics.structural.stubs.StructuralResource2; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class DerivedPropertiesSection implements ComponentTypeViewerSection { - private static final String[] COLUMN_NAMES = - new String[] {"Name", "Type", "Expression", "Unit", "Label", "Description"}; + private static final String[] COLUMN_NAMES = { + Messages.DerivedPropertiesSection_Name, + Messages.DerivedPropertiesSection_Type, + Messages.DerivedPropertiesSection_Expression, + Messages.DerivedPropertiesSection_Unit, + Messages.DerivedPropertiesSection_Label, + Messages.DerivedPropertiesSection_Description + }; private static final int[] COLUMN_LENGTHS = new int[] { 120, 100, 100, 70, 100, 100 }; private static final int[] COLUMN_WEIGHTS = @@ -56,6 +73,8 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection { } }; + private static final Logger LOGGER = LoggerFactory.getLogger(DerivedPropertiesSection.class); + ComponentTypeViewerData data; Table table; TableColumn[] columns; @@ -71,7 +90,7 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection { Form form = data.form; section = tk.createSection(form.getBody(), Section.TITLE_BAR | Section.EXPANDED); section.setLayout(new FillLayout()); - section.setText("Derived properties"); + section.setText(Messages.DerivedPropertiesSection_DerivedProperties); Composite sectionBody = tk.createComposite(section); GridLayoutFactory.fillDefaults().numColumns(2).applyTo(sectionBody); @@ -143,7 +162,7 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection { break; case 1: - data.editType(table, editor, propertyInfo, selectedItem, column, false); + data.editType(table, editor, propertyInfo, selectedItem, column, null, false); break; case 2: @@ -191,9 +210,9 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection { GridDataFactory.fillDefaults().applyTo(buttons); GridLayoutFactory.fillDefaults().applyTo(buttons); - newProperty = tk.createButton(buttons, "New property", SWT.PUSH); + newProperty = tk.createButton(buttons, Messages.DerivedPropertiesSection_NewProperty, SWT.PUSH); GridDataFactory.fillDefaults().applyTo(newProperty); - removeProperty = tk.createButton(buttons, "Remove property", SWT.PUSH); + removeProperty = tk.createButton(buttons, Messages.DerivedPropertiesSection_RemoveProperty, SWT.PUSH); GridDataFactory.fillDefaults().applyTo(removeProperty); // Actions @@ -247,11 +266,11 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection { public static String validateMonitorExpression(final RequestProcessor processor, final Resource componentType, final Resource relation, final String expression) { if (expression.trim().isEmpty()) { - return "Expression is empty."; + return Messages.DerivedPropertiesSection_ExpressionIsEmpty; } if (expression.trim().isEmpty()) { - return "Expression is empty."; + return Messages.DerivedPropertiesSection_ExpressionIsEmpty; } try { return processor.sync(new UniqueRead() { @@ -261,47 +280,69 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection { StructuralResource2 STR = StructuralResource2.getInstance(graph); - Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(componentType)); - + // 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 { - // TODO validation + Variable context = new StandardGraphPropertyVariable(parent, null, null, info, literal); if(graph.isInstanceOf(componentType, STR.ProceduralComponentType)) { - //Layer0 L0 = Layer0.getInstance(graph); - //Resource environment = graph.getPossibleObject(literal, L0.SCLValue_environment); - //ContextModule context = graph.sync(new TypeMonitorContextRequest(componentType)); - //String SCLMain = graph.syncRequest(new SCLMainModuleRequest(indexRoot)); - //CompilationContext cc = new CompilationContext(environment, context, SCLMain); - //graph.syncRequest(new ActualCompileRequest(expression, cc), TransientCacheListener.instance()); + CompileProceduralSCLMonitorRequest.compileAndEvaluate(graph, context); } else { - //CompilationContext cc = new CompileSCLMonitorRequest(literal, componentType, indexRoot).getContext(graph); - //graph.syncRequest(new ActualCompileRequest(expression, cc), TransientCacheListener.instance()); - //graph.syncRequest(new CompileSCLMonitorRequest(graph, context)); + compileAndEvaluate(graph, context, expression); } } catch (Exception e) { String msg = e.getMessage(); - int index = msg.indexOf(":"); + int index = msg.indexOf(":"); //$NON-NLS-1$ if(index > 0) msg = msg.substring(index); return msg; } - } - return null; - } }); } catch (DatabaseException e) { - e.printStackTrace(); + 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); + } + } + @Override public void update(ComponentTypePropertiesResult result) { if (table.isDisposed()) @@ -327,7 +368,7 @@ public class DerivedPropertiesSection implements ComponentTypeViewerSection { TableItem item = new TableItem(table, SWT.NONE); - item.setText(0, info.valid != null ? info.name + " (!)" : info.name); + item.setText(0, info.valid != null ? info.name + " (!)" : info.name); //$NON-NLS-1$ item.setText(1, info.type); item.setText(2, info.expression); item.setText(3, info.unitString()); @@ -376,4 +417,29 @@ 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