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", "Label", "Description"};
+ new String[] {"Name", "Type", "Expression", "Unit", "Label", "Description"};
private static final int[] COLUMN_LENGTHS =
- new int[] { 120, 100, 100, 100, 100 };
+ new int[] { 120, 100, 100, 70, 100, 100 };
private static final int[] COLUMN_WEIGHTS =
- new int[] { 0, 0, 100, 0, 0 };
+ new int[] { 0, 0, 100, 0, 0, 0 };
private static Function4<RequestProcessor, Resource, Resource, String, String> VALIDATE_MONITOR_EXPRESSION =
new Function4<RequestProcessor, Resource, Resource, String, String>() {
@Override
}
};
+ private static final Logger LOGGER = LoggerFactory.getLogger(DerivedPropertiesSection.class);
+
ComponentTypeViewerData data;
Table table;
TableColumn[] columns;
}, VALIDATE_MONITOR_EXPRESSION);
break;
-// case 3:
-// editUnit(table2, editor2, propertyInfo, selectedItem, column);
-// break;
-
case 3:
+ data.editUnit(table, editor, propertyInfo, selectedItem, column);
+ break;
+
+ case 4:
data.editValue(table, editor, propertyInfo, selectedItem, column, propertyInfo.immutable ? null : new StringWriter() {
@Override
public void perform(WriteGraph graph, String newValue) throws DatabaseException {
}, null);
break;
- case 4:
+ case 5:
data.editMultilineText(table, editor, propertyInfo, selectedItem, selectedItemBounds, column, new StringWriter() {
@Override
public void perform(WriteGraph graph, String newValue) throws DatabaseException {
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.<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 {
- // 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.<CompiledExpression>instance());
+ CompileProceduralSCLMonitorRequest.compileAndEvaluate(graph, context);
} else {
- //CompilationContext cc = new CompileSCLMonitorRequest(literal, componentType, indexRoot).getContext(graph);
- //graph.syncRequest(new ActualCompileRequest(expression, cc), TransientCacheListener.<CompiledExpression>instance());
- //graph.syncRequest(new CompileSCLMonitorRequest(graph, context));
+ compileAndEvaluate(graph, context, expression);
}
} catch (Exception e) {
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<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())
item.setText(0, info.valid != null ? info.name + " (!)" : info.name);
item.setText(1, info.type);
item.setText(2, info.expression);
- //item.setText(3, unitStr(info));
- item.setText(3, info.label);
- item.setText(4, info.description);
+ item.setText(3, info.unitString());
+ item.setText(4, info.label);
+ item.setText(5, info.description);
item.setForeground(fg);
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