1 package org.simantics.document.server;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Collections;
8 import java.util.HashMap;
11 import java.util.TreeMap;
13 import org.simantics.Simantics;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.Datatypes;
16 import org.simantics.databoard.binding.Binding;
17 import org.simantics.databoard.type.Datatype;
18 import org.simantics.db.AsyncReadGraph;
19 import org.simantics.db.DirectStatements;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.RequestProcessor;
22 import org.simantics.db.Resource;
23 import org.simantics.db.Session;
24 import org.simantics.db.Statement;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.common.primitiverequest.Adapter;
27 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
28 import org.simantics.db.common.request.UnaryRead;
29 import org.simantics.db.common.request.UniqueRead;
30 import org.simantics.db.common.request.WriteResultRequest;
31 import org.simantics.db.common.utils.Logger;
32 import org.simantics.db.common.utils.NameUtils;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.function.All;
35 import org.simantics.db.layer0.request.ProjectModels;
36 import org.simantics.db.layer0.request.PropertyInfo;
37 import org.simantics.db.layer0.request.PropertyInfoRequest;
38 import org.simantics.db.layer0.request.VariableProperty;
39 import org.simantics.db.layer0.request.VariableRead;
40 import org.simantics.db.layer0.request.VariableValueWithBinding;
41 import org.simantics.db.layer0.variable.ConstantPropertyVariable;
42 import org.simantics.db.layer0.variable.ProxyChildVariable;
43 import org.simantics.db.layer0.variable.ProxySessionRequest;
44 import org.simantics.db.layer0.variable.ProxyVariables;
45 import org.simantics.db.layer0.variable.StandardAssertedGraphPropertyVariable;
46 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
47 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
48 import org.simantics.db.layer0.variable.Variable;
49 import org.simantics.db.layer0.variable.VariableMap;
50 import org.simantics.db.layer0.variable.VariableMapImpl;
51 import org.simantics.db.layer0.variable.Variables;
52 import org.simantics.db.procedure.AsyncProcedure;
53 import org.simantics.db.service.DirectQuerySupport;
54 import org.simantics.document.base.ontology.DocumentationResource;
55 import org.simantics.document.server.bean.Command;
56 import org.simantics.document.server.bean.DataDefinition;
57 import org.simantics.document.server.handler.AbstractEventHandler;
58 import org.simantics.document.server.handler.AbstractResponseHandler;
59 import org.simantics.document.server.handler.EventHandler;
60 import org.simantics.document.server.io.CommandContext;
61 import org.simantics.document.server.io.CommandContextImpl;
62 import org.simantics.document.server.io.CommandContextMutable;
63 import org.simantics.document.server.io.CommandResult;
64 import org.simantics.document.server.io.IConsole;
65 import org.simantics.document.server.request.NodeRequest;
66 import org.simantics.document.server.request.ServerSCLHandlerValueRequest;
67 import org.simantics.document.server.request.ServerSCLValueRequest;
68 import org.simantics.document.server.serverResponse.ServerResponse;
69 import org.simantics.document.server.serverResponse.SuccessResponse;
70 import org.simantics.modeling.ModelingResources;
71 import org.simantics.modeling.scl.SCLRealm;
72 import org.simantics.modeling.scl.SCLSessionManager;
73 import org.simantics.modeling.services.CaseInsensitiveComponentFunctionNamingStrategy;
74 import org.simantics.modeling.services.ComponentNamingStrategy;
75 import org.simantics.operation.Layer0X;
76 import org.simantics.project.IProject;
77 import org.simantics.scl.compiler.types.TCon;
78 import org.simantics.scl.compiler.types.Type;
79 import org.simantics.scl.compiler.types.Types;
80 import org.simantics.scl.reflection.annotations.SCLValue;
81 import org.simantics.scl.runtime.SCLContext;
82 import org.simantics.scl.runtime.function.Function;
83 import org.simantics.scl.runtime.function.Function1;
84 import org.simantics.scl.runtime.function.FunctionImpl1;
85 import org.simantics.scl.runtime.function.FunctionImpl4;
86 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
87 import org.simantics.scl.runtime.tuple.Tuple;
88 import org.simantics.scl.runtime.tuple.Tuple0;
89 import org.simantics.scl.runtime.tuple.Tuple2;
90 import org.simantics.scl.runtime.tuple.Tuple3;
91 import org.simantics.scl.runtime.tuple.Tuple4;
92 import org.simantics.scl.runtime.tuple.Tuple5;
93 import org.simantics.simulation.experiment.IExperiment;
94 import org.simantics.simulation.ontology.SimulationResource;
95 import org.simantics.simulation.project.IExperimentManager;
96 import org.simantics.structural2.variables.Connection;
97 import org.simantics.structural2.variables.StandardProceduralChildVariable;
98 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
100 import gnu.trove.map.hash.THashMap;
102 public class Functions {
104 private static class PrimitivePropertyStatementsProcedure implements AsyncProcedure<DirectStatements> {
106 public DirectStatements result;
109 public void execute(AsyncReadGraph graph, DirectStatements result) {
110 this.result = result;
114 public void exception(AsyncReadGraph graph, Throwable throwable) {
119 @SCLValue(type = "VariableMap")
120 public static VariableMap primitiveProperties = new VariableMapImpl() {
121 private void storePropertyValueAndExceptions(ReadGraph graph, Variable parent, String name, Variable property, Map<String, Variable> map) {
123 Object value = property.getValue(graph);
124 map.put(name, new ConstantPropertyVariable(parent, name, value, null));
125 } catch (DatabaseException e) {
126 Variable propertyExceptions = map.get(NodeRequest.PROPERTY_VALUE_EXCEPTIONS);
127 Map<String, Exception> exceptionMap;
128 if (propertyExceptions == null) {
129 exceptionMap = new TreeMap<String, Exception>();
130 propertyExceptions = new ConstantPropertyVariable(parent, NodeRequest.PROPERTY_VALUE_EXCEPTIONS, exceptionMap, null);
131 map.put(NodeRequest.PROPERTY_VALUE_EXCEPTIONS, propertyExceptions);
134 exceptionMap = propertyExceptions.getValue(graph);
135 } catch (DatabaseException e1) {
136 Logger.defaultLogError(e1);
142 label = property.getLabel(graph);
143 } catch (DatabaseException e2) {
146 exceptionMap.put(label, e);
151 public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
152 return All.getStandardPropertyDomainPropertyVariableFromValue(graph, context, name);
156 public Map<String, Variable> getVariables(final ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
158 if(map == null) map = new HashMap<String,Variable>();
160 Variable parent = context.getParent(graph);
162 DocumentationResource DOC = DocumentationResource.getInstance(graph);
164 if(parent instanceof StandardProceduralChildVariable) {
166 StandardProceduralChildVariable procedural = (StandardProceduralChildVariable)parent;
167 for(Variable property : procedural.getProperties(graph/*, DocumentationResource.URIs.Document_AttributeRelation*/)) {
168 if(property instanceof StandardAssertedGraphPropertyVariable) {
169 StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
170 if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name) || "pollingFunction".equals(ass.property.name)) {
171 storePropertyValueAndExceptions(graph, parent, ass.property.name, property, map);
175 Resource predicate = property.getPossiblePredicateResource(graph);
176 if(predicate != null) {
177 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
178 if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
179 Variable prop = parent.getProperty(graph, predicate);
180 storePropertyValueAndExceptions(graph, parent, info.name, prop, map);
187 Resource parentRes = parent.getRepresents(graph);
189 Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands);
190 storePropertyValueAndExceptions(graph, parent, "commands", prop, map);
193 if (graph.getPossibleObject(parentRes, DOC.Properties_dataDefinitions) != null) {
194 Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions);
195 storePropertyValueAndExceptions(graph, parent, "dataDefinitions", prop, map);
198 DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
199 PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure();
201 dqs.forEachDirectPersistentStatement(graph, parentRes, foo);
203 for(Statement stm : foo.result) {
204 Resource predicate = stm.getPredicate();
205 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
207 if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
208 Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate);
209 storePropertyValueAndExceptions(graph, parent, info.name, prop, map);
211 Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation);
212 if(definition != null) {
213 PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
214 Variable prop = new StandardGraphPropertyVariable(graph, parent, definition);
215 map.put(info2.name, new PrimitiveValueVariable(parent, info2.name, prop));
226 @SCLValue(type = "VariableMap")
227 public static VariableMap inputSpaceChildren = new VariableMapImpl() {
229 private Variable getProxy(ReadGraph graph, Variable context) throws DatabaseException {
230 Variable root = Variables.getRootVariable(graph);
231 return new DocumentProxyChildVariable(context, context, root, ProxyChildVariable.CONTEXT_BEGIN);
235 public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
237 if(ProxyChildVariable.CONTEXT_BEGIN.equals(name)) return getProxy(graph, context);
238 return All.standardChildDomainChildren.getVariable(graph, context, name);
243 public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
245 map = All.standardChildDomainChildren.getVariables(graph, context, map);
246 if(map == null) map = new THashMap<String,Variable>();
247 map.put(ProxyChildVariable.CONTEXT_BEGIN, getProxy(graph, context));
254 static class DocumentProxyChildVariable extends ProxyChildVariable {
256 public DocumentProxyChildVariable(Variable base, Variable parent, Variable other, String name) {
257 super(base, parent, other, name);
261 public Variable create(Variable base, Variable parent, Variable other, String name) {
262 return new DocumentProxyChildVariable(base, parent, other, name);
265 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
267 if(CONTEXT_END.equals(name)) {
268 if(other instanceof ProxyChildVariable) {
269 // The context is also a proxy - let it do the job
270 return super.getPossibleChild(graph, name);
272 return new RootVariable(this, base.getRepresents(graph));
276 return super.getPossibleChild(graph, name);
280 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
282 Collection<Variable> result = super.getChildren(graph);
283 if(!(base instanceof ProxyChildVariable)) {
284 result.add(new RootVariable(this, base.getRepresents(graph)));
292 static class RootVariable extends StandardGraphChildVariable {
294 public RootVariable(DocumentProxyChildVariable parent, Resource resource) {
295 super(parent, null, resource);
299 public String getName(ReadGraph graph) throws DatabaseException {
300 return ProxyChildVariable.CONTEXT_END;
303 @SuppressWarnings("deprecation")
305 public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
306 return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
311 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
312 public static Variable input(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
313 Variable session = graph.syncRequest(new ProxySessionRequest(context));
314 DocumentationResource DOC = DocumentationResource.getInstance(graph);
315 String uri = session.getPossiblePropertyValue(graph, DOC.Session_inputURI);
317 // TODO HAXX - Please fix this
318 // we have no URI so this probably means that someone has inserted a non-session
319 // into the proxy variable => return that instead
322 return Variables.getVariable(graph, uri);
325 public static Variable stateVariable(ReadGraph graph, Variable self) throws DatabaseException {
326 Variable session = graph.syncRequest(new ProxySessionRequest(self));
328 throw new DatabaseException("No state for " + self.getURI(graph));
329 return session.getPossibleChild(graph, "__scl__");
332 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
333 public static Variable state(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
334 Variable session = graph.syncRequest(new ProxySessionRequest(context));
336 throw new DatabaseException("No state for " + context.getURI(graph));
337 return session.getPossibleChild(graph, "__scl__");
340 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
341 public static Variable icstate(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
342 Variable session = graph.syncRequest(new ProxySessionRequest(context));
343 return session.getPossibleChild(graph, "__icstate__");
346 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
347 public static Variable session(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
348 return graph.syncRequest(new ProxySessionRequest(context));
351 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
352 public static Variable experiment(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
353 // Try if experiment (run) has been used as the input
354 Variable var = input(graph, converter, context);
355 SimulationResource SR = SimulationResource.getInstance(graph);
356 while(var != null && !graph.isInstanceOf(var.getRepresents(graph), SR.Run)) {
357 var = var.getParent(graph);
361 IExperiment exp = getExperiment(graph, var);
369 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
370 public static Variable model(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
371 Variable var = input(graph, converter, context);
372 ModelingResources MOD = ModelingResources.getInstance(graph);
373 while(var != null && !graph.isInstanceOf(var.getRepresents(graph), MOD.StructuralModel)) {
374 var = var.getParent(graph);
380 private static Collection<Variable> getBroadcasted(ReadGraph graph, Variable target) throws DatabaseException {
382 ArrayList<Variable> result = new ArrayList<Variable>();
384 DocumentationResource DOC = DocumentationResource.getInstance(graph);
386 Variable broadcasted = target.getPossibleProperty(graph, DOC.Relations_broadcasted);
387 if(broadcasted != null) result.add(broadcasted);
389 for(Variable child : DocumentServerUtils.getChildrenInOrdinalOrder(graph, target)) {
390 result.addAll(getBroadcasted(graph, child));
397 private static List<Command> getCommands(ReadGraph graph, Collection<Variable> commandVariables, String trigger, CommandContext constants, boolean broadcast) throws DatabaseException {
399 if(commandVariables.isEmpty()) return Collections.emptyList();
402 TreeMap<Integer, List<Command>> sequences = new TreeMap<Integer, List<Command>>();
404 DocumentationResource DOC = DocumentationResource.getInstance(graph);
407 for (Variable c : commandVariables) {
410 t = c.getName(graph);
412 Connection conn = c.getValue(graph);
413 Variable targetConnectionPoint = DocumentServerUtils.getPossibleCommandTriggerConnectionPoint(graph, c, conn);
414 if (targetConnectionPoint != null) {
415 Variable target = targetConnectionPoint.getParent(graph);
416 if (target != null) {
418 Boolean enabled = target.getPossiblePropertyValue(graph, DOC.Properties_exists, Bindings.BOOLEAN);
419 if(enabled != null && !enabled) continue;
427 String o = c.getPossiblePropertyValue(graph, "ordinal");
429 ordinal = Integer.parseInt(o);
430 } catch (NumberFormatException e) {}
433 String constantKey = target.getPossiblePropertyValue(graph, "constantKey");
434 String constantValue = target.getPossiblePropertyValue(graph, "constantValue");
436 CommandContextMutable newConstants = (CommandContextMutable)constants;
437 if(constantKey != null && constantValue != null) {
438 if(!constantKey.isEmpty()) {
439 newConstants = new CommandContextImpl().merge(constants);
440 newConstants.putString(constantKey, constantValue);
444 String requiredKey = target.getPossiblePropertyValue(graph, "requiredKey");
445 if(requiredKey != null && !requiredKey.isEmpty()) {
446 if (newConstants == constants) {
447 newConstants = new CommandContextImpl().merge(constants);
449 newConstants.putRow(CommandContext.REQUIRED_KEYS, Collections.<Object>singletonList(requiredKey));
452 String forbiddenKey = target.getPossiblePropertyValue(graph, "forbiddenKey");
453 if(forbiddenKey != null && !forbiddenKey.isEmpty()) {
454 if (newConstants == constants) {
455 newConstants = new CommandContextImpl().merge(constants);
457 newConstants.putRow(CommandContext.FORBIDDEN_KEYS, Collections.<Object>singletonList(forbiddenKey));
460 if(DOC.Relations_broadcast.equals(targetConnectionPoint.getPredicateResource(graph))) {
462 // This is a broadcast terminal of a container
463 List<Command> broadcastCommands = getCommands(graph, getBroadcasted(graph, target), t, newConstants, true);
464 sequences.put(ordinal, broadcastCommands);
468 Command command = new Command(DocumentServerUtils.getId(graph, target), t,
469 targetConnectionPoint.getName(graph), newConstants);
470 sequences.put(ordinal, Collections.singletonList(command));
478 List<Command> commands = new ArrayList<Command>();
479 for (List<Command> commandList : sequences.values()) {
480 for (Command command : commandList) {
481 commands.add(command);
494 * @throws DatabaseException
496 public static List<Command> commandList(ReadGraph graph, Variable variable) throws DatabaseException {
497 return getCommands(graph, DocumentServerUtils.getTriggerCommands(graph, variable.getParent(graph)), null, new CommandContextImpl(), false);
506 * @throws DatabaseException
508 public static List<DataDefinition> dataDefinitions(ReadGraph graph, Variable variable) throws DatabaseException {
509 DocumentationResource DOC = DocumentationResource.getInstance(graph);
510 ArrayList<DataDefinition> dataDefinitions = new ArrayList<DataDefinition>();
511 // Find data definition connections
512 for (Variable dataDefinitionRelation : DocumentServerUtils.getDataDefinitions(graph, variable.getParent(graph))) {
513 Connection dataDefinitionConnection = dataDefinitionRelation.getValue(graph);
514 // Find data the other end of definition connection
515 Collection<Variable> dataDefinitionProviders = DocumentServerUtils.getPossibleOtherConnectionPoints(graph,
516 dataDefinitionRelation, dataDefinitionConnection);
517 if (dataDefinitionProviders != null) {
519 for(Variable dataDefinitionProvider : dataDefinitionProviders) {
521 Variable dataDefinition = dataDefinitionProvider.getParent(graph);
522 if (dataDefinition != null) {
523 // Found other end. Is should contain ONE data
524 // definition connection to the actual data
525 Collection<Variable> dataCollection = DocumentServerUtils.getDataRelations(graph, dataDefinition);
526 if (dataCollection.size() == 1) {
527 Variable dataRelation = dataCollection.iterator().next();
528 Connection dataConnection = dataRelation.getValue(graph);
529 // Find data the other end of definition connection
530 Variable dataConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph,
531 dataRelation, dataConnection);
532 if (dataConnectionPoint != null) {
533 Variable data = dataConnectionPoint.getParent(graph);
534 Resource type = dataDefinition.getPossibleType(graph, DOC.Components_Component);
536 if (graph.isInheritedFrom(type, DOC.Components_DefVar)) {
538 String sourceProperty = dataDefinition.getPropertyValue(graph, DOC.Properties_source,
540 String targetProperty = dataDefinition.getPropertyValue(graph, DOC.Properties_target,
543 dataDefinitions.add(new DataDefinition(DocumentServerUtils.getId(graph, data),
544 sourceProperty, targetProperty));
546 } else if (graph.isInheritedFrom(type, DOC.Components_DefVars)) {
548 List<String> sourcesProperty = toList(dataDefinition.getPropertyValue(graph, DOC.Properties_sources), String.class);
549 List<String> targetsProperty = toList(dataDefinition.getPropertyValue(graph, DOC.Properties_targets), String.class);
551 for (int i = 0; i < Math.min(sourcesProperty.size(), targetsProperty.size()); i++) {
552 dataDefinitions.add(new DataDefinition(DocumentServerUtils.getId(graph, data),
553 sourcesProperty.get(i), targetsProperty.get(i)));
562 return dataDefinitions;
565 @SuppressWarnings("unchecked")
566 private static <T> List<T> toList(Object o, Class<T> c) {
567 List<T> result = null;
568 if (o instanceof List) {
570 } else if (o instanceof Object[]) {
571 result = new ArrayList<T>(((Object[])o).length);
572 for (T item : (T[])o) {
577 return Collections.<T>emptyList();
581 public static AbstractEventHandler emptyOnClick(ReadGraph graph) throws DatabaseException {
582 return new EventHandler() {
584 public ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException {
590 public static AbstractEventHandler writeEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
592 final Session session = graph.getSession();
594 return new AbstractEventHandler() {
597 public CommandResult handle(final CommandContext parameters) {
599 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
603 String result = session.sync(new WriteResultRequest<String>() {
606 public String perform(WriteGraph graph) throws DatabaseException {
607 SCLContext sclContext = SCLContext.getCurrent();
608 Object oldGraph = sclContext.put("graph", graph);
609 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
611 Function1<String,String> pf = new FunctionImpl1<String,String>() {
613 public String apply(String key) {
614 return parameters.getString(key);
617 Object response = (String)fn.apply(variable, pf);
618 if(response instanceof String) {
619 return (String)response;
622 } catch (Throwable t) {
625 sclContext.put("graph", oldGraph);
626 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
635 return new SuccessResponse(result);
637 } catch (Throwable e) {
638 Logger.defaultLogError(e);
639 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
647 public static AbstractEventHandler readEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
649 final Session session = graph.getSession();
651 return new AbstractEventHandler() {
654 public CommandResult handle(final CommandContext parameters) {
656 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
660 String result = session.sync(new UniqueRead<String>() {
663 public String perform(ReadGraph graph) throws DatabaseException {
664 SCLContext sclContext = SCLContext.getCurrent();
665 Object oldGraph = sclContext.put("graph", graph);
666 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
668 Function1<String,String> pf = new FunctionImpl1<String,String>() {
670 public String apply(String key) {
671 return parameters.getString(key);
674 Object response = (String)fn.apply(variable, pf);
675 if(response instanceof String) {
676 return (String)response;
679 } catch (Throwable t) {
682 sclContext.put("graph", oldGraph);
683 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
692 return new SuccessResponse(result);
694 } catch (Throwable e) {
695 Logger.defaultLogError(e);
696 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
704 public static AbstractEventHandler readEventHandler2(ReadGraph graph, final Function fn) {
706 final Session session = graph.getSession();
708 return new AbstractEventHandler() {
711 public CommandResult handle(final CommandContext parameters) {
713 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
717 CommandResult result = session.sync(new UniqueRead<CommandResult>() {
720 public CommandResult perform(ReadGraph graph) throws DatabaseException {
721 SCLContext sclContext = SCLContext.getCurrent();
722 Object oldGraph = sclContext.put("graph", graph);
723 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
725 Object response = fn.apply(parameters);
726 if(response instanceof CommandResult) {
727 return (CommandResult)response;
730 } catch (Throwable t) {
733 sclContext.put("graph", oldGraph);
734 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
745 } catch (Throwable e) {
746 Logger.defaultLogError(e);
747 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
755 public static AbstractEventHandler responseHandler(ReadGraph graph, Variable self, String function) throws DatabaseException {
757 Variable anyFunction = self.browse(graph, ".#" + function);
758 if(anyFunction == null) return null;
760 final List<TCon> effects = ServerSCLHandlerValueRequest.getEffects(graph, anyFunction);
762 final Function1<CommandContext, CommandResult> fn = anyFunction.getValue(graph);
763 String expression = anyFunction.getPropertyValue(graph, "expression");
765 final Session session = graph.getSession();
767 return new AbstractResponseHandler(expression) {
769 private String formatError(RequestProcessor proc, Throwable t) {
773 return proc.syncRequest(new UniqueRead<String>() {
776 public String perform(ReadGraph graph) throws DatabaseException {
777 Variable proxy = ProxyVariables.proxyVariableRoot(graph, anyFunction);
778 String uri2 = proxy.getURI(graph);
779 String uri = self.getParent(graph).getURI(graph);
781 String path = uri.substring(uri2.length());
783 String expr = anyFunction.getPossiblePropertyValue(graph, "expression");
784 StringBuilder message = new StringBuilder();
785 message.append("Handler execution failed\n");
786 message.append(" handler=" + path + "\n");
787 message.append(" expression=" + expr + "\n");
788 message.append(" message=" + t.getMessage() + "\n");
790 StringWriter sw = new StringWriter();
791 t.printStackTrace(new PrintWriter(sw));
792 message.append(" stack trace=" + sw);
794 return message.toString();
799 } catch (DatabaseException e) {
801 return e.getMessage();
808 public CommandResult handle(final CommandContext parameters) {
810 IConsole console = parameters.getValue("__console__");
811 SCLReportingHandler printer = (console != null) ? new ConsoleSCLReportingHandler(console)
812 : (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
816 Object result = null;
817 if(effects.contains(Types.WRITE_GRAPH)) {
819 result = session.syncRequest(new WriteResultRequest<Object>() {
822 public Object perform(WriteGraph graph)
823 throws DatabaseException {
824 SCLContext sclContext = SCLContext.getCurrent();
825 Object oldGraph = sclContext.put("graph", graph);
826 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
828 Object response = fn.apply(parameters);
830 } catch (Throwable t) {
831 return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
833 sclContext.put("graph", oldGraph);
834 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
840 } else if(effects.contains(Types.READ_GRAPH)) {
842 result = session.sync(new UniqueRead<Object>() {
845 public Object perform(ReadGraph graph) throws DatabaseException {
847 SCLContext sclContext = SCLContext.getCurrent();
848 Object oldGraph = sclContext.put("graph", graph);
849 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
852 Object response = fn.apply(parameters);
854 } catch (Throwable t) {
855 return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
857 sclContext.put("graph", oldGraph);
858 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
866 SCLContext sclContext = SCLContext.getCurrent();
867 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
869 result = fn.apply(parameters);
871 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
876 if (result instanceof org.simantics.document.server.serverResponse.Error) {
877 return (CommandResult)result;
880 if (result instanceof CommandResult) {
881 return (CommandResult)result;
883 CommandContextMutable assignments = new CommandContextImpl();
884 assignments.putValue("result", result);
885 return new ServerResponse(200, "", assignments);
888 } catch (Throwable e) {
889 return new org.simantics.document.server.serverResponse.Error(formatError(Simantics.getSession(), e));
897 public static AbstractEventHandler writeEventHandler2(ReadGraph graph, final Function fn) {
899 final Session session = graph.getSession();
901 return new AbstractEventHandler() {
904 public CommandResult handle(final CommandContext parameters) {
906 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
910 CommandResult result = session.syncRequest(new WriteResultRequest<CommandResult>() {
913 public CommandResult perform(WriteGraph graph) throws DatabaseException {
914 SCLContext sclContext = SCLContext.getCurrent();
915 Object oldGraph = sclContext.put("graph", graph);
916 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
918 Object response = fn.apply(parameters);
919 if(response instanceof CommandResult) {
920 return (CommandResult)response;
923 } catch (Throwable t) {
926 sclContext.put("graph", oldGraph);
927 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
938 } catch (Throwable e) {
939 Logger.defaultLogError(e);
940 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
948 public static AbstractEventHandler eventHandler2(ReadGraph graph, final Function fn) {
950 return new AbstractEventHandler() {
953 public CommandResult handle(final CommandContext parameters) {
955 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
959 SCLContext sclContext = SCLContext.getCurrent();
960 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
962 Object response = fn.apply(parameters);
963 if(response instanceof CommandResult) {
964 return (CommandResult)response;
967 } catch (Throwable t) {
970 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
975 } catch (Throwable e) {
976 Logger.defaultLogError(e);
977 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
985 public static AbstractEventHandler eventHandler(ReadGraph graph, final Function fn) {
987 return new AbstractEventHandler() {
990 public CommandResult handle(final CommandContext parameters) {
992 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
998 SCLContext sclContext = SCLContext.getCurrent();
999 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
1001 Function1<String,String> pf = new FunctionImpl1<String,String>() {
1003 public String apply(String key) {
1004 return parameters.getString(key);
1007 Object response = (String)fn.apply(pf);
1008 if(response instanceof String) {
1009 result = (String)response;
1011 } catch (Throwable t) {
1012 t.printStackTrace();
1014 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
1017 return new SuccessResponse(result);
1019 } catch (Throwable e) {
1020 Logger.defaultLogError(e);
1021 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
1029 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
1030 public static Object sclValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1031 return ServerSCLValueRequest.compileAndEvaluate(graph, context);
1034 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
1035 public static Object sclHandlerValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1036 return ServerSCLHandlerValueRequest.compileAndEvaluate(graph, context);
1039 @SCLValue(type = "ReadGraph -> Resource -> Resource -> ComponentNamingStrategy")
1040 public static ComponentNamingStrategy componentNamingStrategy(ReadGraph graph, Resource converter, Resource context) throws DatabaseException {
1041 return createComponentNamingStrategy(graph);
1044 public static CaseInsensitiveComponentFunctionNamingStrategy createComponentNamingStrategy(ReadGraph graph) throws DatabaseException {
1045 Layer0X L0X = Layer0X.getInstance(graph);
1046 @SuppressWarnings({ "unchecked", "rawtypes" })
1047 final Function dependencies = graph.syncRequest(new Adapter(L0X.DependencyResources, Function.class), TransientCacheListener.<Function>instance());
1048 @SuppressWarnings("rawtypes")
1049 Function moduleNameFunction = new FunctionImpl4<ReadGraph, Resource, String, Integer, List<Map<String, Object>>>() {
1051 public Object apply(ReadGraph p0, Resource p1, String p2) {
1052 return apply(p0, p1, p2);
1054 @SuppressWarnings("unchecked")
1056 public List<Map<String, Object>> apply(ReadGraph graph, Resource model, String search, Integer maxResults) {
1057 return (List<Map<String, Object>>)dependencies.apply(graph, model, search, maxResults);
1060 return new CaseInsensitiveComponentFunctionNamingStrategy("%s%02d", moduleNameFunction);
1063 public static IExperiment getExperiment(ReadGraph graph, Variable variable) throws DatabaseException {
1064 if (variable == null)
1067 SimulationResource SIMU = SimulationResource.getInstance(graph);
1069 Variable var = variable;
1070 Resource represents = var.getRepresents(graph);
1071 Resource activeRun = null;
1072 if (represents != null && graph.isInstanceOf(represents, SIMU.Run)) {
1073 activeRun = represents;
1076 IProject project = Simantics.peekProject();
1077 if (activeRun != null && project != null) {
1078 IExperimentManager expMan = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
1080 return expMan.getExperiment(NameUtils.getSafeName(graph, activeRun));
1086 public static CommandContextMutable putTuple(CommandContextMutable context, String key, Object tuple) {
1087 List<Object> list = new ArrayList<Object>();
1088 if (tuple instanceof Tuple) {
1089 Collections.addAll(list, ((Tuple)tuple).toArray());
1093 context.putRow(key, list);
1097 public static List<Object> getTuples(CommandContext context, String key) {
1098 List<List<Object>> rows = context.getRows(key);
1099 List<Object> tuples = new ArrayList<Object>();
1101 for (List<Object> row : rows) {
1102 switch (row.size()) {
1103 case 0: tuples.add(Tuple0.INSTANCE); break;
1104 case 1: tuples.add(row.get(1)); break;
1105 case 2: tuples.add(new Tuple2(row.get(0), row.get(1))); break;
1106 case 3: tuples.add(new Tuple3(row.get(0), row.get(1), row.get(2))); break;
1107 case 4: tuples.add(new Tuple4(row.get(0), row.get(1), row.get(2), row.get(3))); break;
1108 case 5: tuples.add(new Tuple5(row.get(0), row.get(1), row.get(2), row.get(3), row.get(4))); break;
1115 public static String printContext(CommandContext context) {
1116 return context.toString();
1119 @SCLValue(type = "AbstractEventHandler")
1120 public static AbstractEventHandler emptyEvent = new AbstractEventHandler() {
1123 public CommandResult handle(CommandContext context) {
1129 public static String sclStateKey(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
1131 String baseURI = base.getURI(graph);
1133 String selfURI = self.getURI(graph);
1135 String prefix = selfURI.substring(0, selfURI.indexOf(ProxyChildVariable.CONTEXT_BEGIN));
1136 String suffix = selfURI.substring(selfURI.lastIndexOf(ProxyChildVariable.CONTEXT_END) + ProxyChildVariable.CONTEXT_END.length());
1137 String stripped = prefix + suffix;
1139 String relative = Variables.getRelativeRVI(baseURI, stripped);
1141 return Variables.getRVI(relative, ref);
1145 public static Variable sclStateVariable(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
1147 String id = sclStateKey(graph, base, self, ref);
1149 Variable sclVar = base.getPossibleChild(graph, "__scl__");
1150 if(sclVar == null) return null;
1152 return sclVar.getPossibleProperty(graph, id);
1156 public static Object sclStateValueOrDefault(ReadGraph graph, Variable base, Variable self, String ref, Object defaultValue) throws DatabaseException {
1158 Variable stateVariable = sclStateVariable(graph, base, self, ref);
1159 if (stateVariable != null) {
1161 return stateVariable.getValue(graph);
1165 String id = sclStateKey(graph, base, self, ref);
1167 SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph) + "/__scl__");
1168 realm.getConnection().setVariable(id, getSCLType(defaultValue), defaultValue);
1170 return defaultValue;
1175 public static void setSclStateValue(WriteGraph graph, Variable base, Variable self, String ref, Object value) throws DatabaseException {
1177 String id = sclStateKey(graph, base, self, ref);
1179 SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph)+"/__scl__");
1180 realm.getConnection().setVariable(id, getSCLType(value), value);
1181 realm.refreshVariablesSync();
1185 public static Object projectComponentState(ReadGraph graph, Variable self, String ref, Object defaultValue) throws DatabaseException {
1186 Resource project = Simantics.getProjectResource();
1187 Variable component = self.getParent(graph);
1188 Variable projectVariable = Variables.getVariable(graph, project);
1189 return sclStateValueOrDefault(graph, projectVariable, component, ref, defaultValue);
1192 public static void setProjectComponentState(WriteGraph graph, Variable self, String ref, Object value) throws DatabaseException {
1193 Resource project = Simantics.getProjectResource();
1194 Variable component = self.getParent(graph);
1195 Variable projectVariable = Variables.getVariable(graph, project);
1196 setSclStateValue(graph, projectVariable, component, ref, value);
1199 private static Type getSCLType(Object value) throws DatabaseException {
1200 Binding b = Bindings.getBindingUnchecked(value.getClass());
1201 Datatype t = b.type();
1202 if(Datatypes.STRING.equals(t)) return Types.STRING;
1203 if(Datatypes.DOUBLE.equals(t)) return Types.DOUBLE;
1204 throw new DatabaseException("Type not supported");
1207 public static List<Variable> documentModelContribution(ReadGraph graph, Resource doc) throws DatabaseException {
1208 Resource project = Simantics.getProjectResource();
1209 ArrayList<Variable> result = new ArrayList<Variable>();
1210 for(Resource model : graph.syncRequest(new ProjectModels(project))) {
1211 result.add(Variables.getVariable(graph, model));
1216 public static String documentModelContributionLabel(ReadGraph graph, Variable var) throws DatabaseException {
1217 return var.getName(graph);
1220 public static Object getPropertyValueCached(ReadGraph graph, Variable variable, String name, Binding binding) throws DatabaseException {
1221 Variable property = graph.syncRequest(new VariableProperty(variable, name));
1222 return graph.syncRequest(new VariableValueWithBinding<Object>(property, binding));
1225 public static class ParentExistsRequest extends VariableRead<Boolean> {
1227 public ParentExistsRequest(Variable parent) {
1232 public Boolean perform(ReadGraph graph) throws DatabaseException {
1234 Variable existsProperty = variable.getPossibleProperty(graph, "exists");
1235 if(existsProperty == null) return true;
1237 Boolean exists = existsProperty.getPossibleValue(graph, Bindings.BOOLEAN);
1238 if (exists == null || !exists) return false;
1240 return graph.syncRequest(new ParentExistsRequest(variable.getParent(graph)));
1246 public static class PathExistsRequest extends VariableRead<Boolean> {
1248 public PathExistsRequest(Variable variable) {
1253 public Boolean perform(ReadGraph graph) throws DatabaseException {
1255 Variable widget = variable.getParent(graph);
1257 Boolean exists = widget.getPossiblePropertyValue(graph, "exists");
1258 if (exists == null || !exists) return false;
1260 if (!graph.syncRequest(new ParentExistsRequest(widget.getParent(graph)))) return false;
1262 DocumentationResource DOC = DocumentationResource.getInstance(graph);
1263 Collection <Variable> cps = widget.getProperties(graph, DocumentationResource.URIs.Relations_parentRelation);
1264 for (Variable cp : cps) {
1266 Connection conn = cp.getValue(graph);
1267 Variable otherCp = DocumentServerUtils.getPossibleChildConnectionPoint(graph, cp, conn);
1268 if (otherCp != null) {
1269 Variable parentWidget = otherCp.getParent(graph);
1270 if (parentWidget.<Boolean>getPropertyValue(graph, "pathExists")) {
1274 Variable parentCp = graph.sync(new UnaryRead<Connection, Variable>(conn) {
1276 public Variable perform(ReadGraph graph) throws DatabaseException {
1277 DocumentationResource DOC = DocumentationResource.getInstance(graph);
1278 Collection<VariableConnectionPointDescriptor> descs = parameter.getConnectionPointDescriptors(graph, null);
1280 for(VariableConnectionPointDescriptor desc : descs) {
1281 if (DOC.Relations_partN.equals(desc.getConnectionPointResource(graph))) {
1282 return desc.getVariable(graph);
1288 if (parentCp != null) {
1289 Variable parentWidget = parentCp.getParent(graph);
1290 if (parentWidget.<Boolean>getPropertyValue(graph, "pathExists")) {
1297 Resource type = widget.getType(graph);
1298 return graph.isInheritedFrom(type, DOC.Components_ParentlessComponent) ||
1299 (graph.isInheritedFrom(type, DOC.DocumentComponent) && cps.isEmpty());
1303 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Boolean")
1304 public static boolean pathExists(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1305 return graph.syncRequest(new PathExistsRequest(context));