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.ServerSCLHandlerValueRequest;
66 import org.simantics.document.server.request.ServerSCLValueRequest;
67 import org.simantics.document.server.serverResponse.ServerResponse;
68 import org.simantics.document.server.serverResponse.SuccessResponse;
69 import org.simantics.modeling.ModelingResources;
70 import org.simantics.modeling.scl.SCLRealm;
71 import org.simantics.modeling.scl.SCLSessionManager;
72 import org.simantics.modeling.services.CaseInsensitiveComponentFunctionNamingStrategy;
73 import org.simantics.modeling.services.ComponentNamingStrategy;
74 import org.simantics.operation.Layer0X;
75 import org.simantics.project.IProject;
76 import org.simantics.scl.compiler.types.TCon;
77 import org.simantics.scl.compiler.types.Type;
78 import org.simantics.scl.compiler.types.Types;
79 import org.simantics.scl.reflection.annotations.SCLValue;
80 import org.simantics.scl.runtime.SCLContext;
81 import org.simantics.scl.runtime.function.Function;
82 import org.simantics.scl.runtime.function.Function1;
83 import org.simantics.scl.runtime.function.FunctionImpl1;
84 import org.simantics.scl.runtime.function.FunctionImpl4;
85 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
86 import org.simantics.scl.runtime.tuple.Tuple;
87 import org.simantics.scl.runtime.tuple.Tuple0;
88 import org.simantics.scl.runtime.tuple.Tuple2;
89 import org.simantics.scl.runtime.tuple.Tuple3;
90 import org.simantics.scl.runtime.tuple.Tuple4;
91 import org.simantics.scl.runtime.tuple.Tuple5;
92 import org.simantics.simulation.experiment.IExperiment;
93 import org.simantics.simulation.ontology.SimulationResource;
94 import org.simantics.simulation.project.IExperimentManager;
95 import org.simantics.structural2.variables.Connection;
96 import org.simantics.structural2.variables.StandardProceduralChildVariable;
97 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
99 import gnu.trove.map.hash.THashMap;
101 public class Functions {
103 private static class PrimitivePropertyStatementsProcedure implements AsyncProcedure<DirectStatements> {
105 public DirectStatements result;
108 public void execute(AsyncReadGraph graph, DirectStatements result) {
109 this.result = result;
113 public void exception(AsyncReadGraph graph, Throwable throwable) {
118 @SCLValue(type = "VariableMap")
119 public static VariableMap primitiveProperties = new VariableMapImpl() {
122 public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
123 return All.getStandardPropertyDomainPropertyVariableFromValue(graph, context, name);
127 public Map<String, Variable> getVariables(final ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
129 Variable parent = context.getParent(graph);
131 DocumentationResource DOC = DocumentationResource.getInstance(graph);
133 if(parent instanceof StandardProceduralChildVariable) {
135 StandardProceduralChildVariable procedural = (StandardProceduralChildVariable)parent;
136 for(Variable property : procedural.getProperties(graph/*, DocumentationResource.URIs.Document_AttributeRelation*/)) {
137 if(property instanceof StandardAssertedGraphPropertyVariable) {
138 StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
139 if("datadefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)) {
140 Object value = property.getPossibleValue(graph);
141 if(value != null) map.put(ass.property.name, new ConstantPropertyVariable(parent, ass.property.name, value, null));
145 Resource predicate = property.getPossiblePredicateResource(graph);
146 if(predicate != null) {
147 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
148 if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
149 Variable prop = parent.getProperty(graph, predicate);
150 Object value = prop.getValue(graph);
151 if(map == null) map = new HashMap<String,Variable>();
152 map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null));
159 DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
160 PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure();
162 dqs.forEachDirectPersistentStatement(graph, parent.getRepresents(graph), foo);
164 for(Statement stm : foo.result) {
165 Resource predicate = stm.getPredicate();
166 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
167 if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
168 if(map == null) map = new HashMap<String,Variable>();
169 Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate);
170 Object value = prop.getValue(graph);
171 map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null));
175 Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions);
176 Object value = prop.getPossibleValue(graph);
177 if(value != null) map.put("dataDefinitions", new ConstantPropertyVariable(parent, "dataDefinitions", value, null));
178 prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands);
179 value = prop.getPossibleValue(graph);
180 if(value != null) map.put("commands", new ConstantPropertyVariable(parent, "commands", value, null));
184 if(map == null) return Collections.emptyMap();
192 @SCLValue(type = "VariableMap")
193 public static VariableMap inputSpaceChildren = new VariableMapImpl() {
195 private Variable getProxy(ReadGraph graph, Variable context) throws DatabaseException {
196 Variable root = Variables.getRootVariable(graph);
197 return new DocumentProxyChildVariable(context, context, root, ProxyChildVariable.CONTEXT_BEGIN);
201 public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
203 if(ProxyChildVariable.CONTEXT_BEGIN.equals(name)) return getProxy(graph, context);
204 return All.standardChildDomainChildren.getVariable(graph, context, name);
209 public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
211 map = All.standardChildDomainChildren.getVariables(graph, context, map);
212 if(map == null) map = new THashMap<String,Variable>();
213 map.put(ProxyChildVariable.CONTEXT_BEGIN, getProxy(graph, context));
220 static class DocumentProxyChildVariable extends ProxyChildVariable {
222 public DocumentProxyChildVariable(Variable base, Variable parent, Variable other, String name) {
223 super(base, parent, other, name);
227 public Variable create(Variable base, Variable parent, Variable other, String name) {
228 return new DocumentProxyChildVariable(base, parent, other, name);
231 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
233 if(CONTEXT_END.equals(name)) {
234 if(other instanceof ProxyChildVariable) {
235 // The context is also a proxy - let it do the job
236 return super.getPossibleChild(graph, name);
238 return new RootVariable(this, base.getRepresents(graph));
242 return super.getPossibleChild(graph, name);
246 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
248 Collection<Variable> result = super.getChildren(graph);
249 if(!(base instanceof ProxyChildVariable)) {
250 result.add(new RootVariable(this, base.getRepresents(graph)));
258 static class RootVariable extends StandardGraphChildVariable {
260 public RootVariable(DocumentProxyChildVariable parent, Resource resource) {
261 super(parent, null, resource);
265 public String getName(ReadGraph graph) throws DatabaseException {
266 return ProxyChildVariable.CONTEXT_END;
269 @SuppressWarnings("deprecation")
271 public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
272 return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
277 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
278 public static Variable input(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
279 Variable session = graph.syncRequest(new ProxySessionRequest(context));
280 DocumentationResource DOC = DocumentationResource.getInstance(graph);
281 String uri = session.getPossiblePropertyValue(graph, DOC.Session_inputURI);
283 // TODO HAXX - Please fix this
284 // we have no URI so this probably means that someone has inserted a non-session
285 // into the proxy variable => return that instead
288 return Variables.getVariable(graph, uri);
291 public static Variable stateVariable(ReadGraph graph, Variable self) throws DatabaseException {
292 Variable session = graph.syncRequest(new ProxySessionRequest(self));
294 throw new DatabaseException("No state for " + self.getURI(graph));
295 return session.getPossibleChild(graph, "__scl__");
298 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
299 public static Variable state(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
300 Variable session = graph.syncRequest(new ProxySessionRequest(context));
302 throw new DatabaseException("No state for " + context.getURI(graph));
303 return session.getPossibleChild(graph, "__scl__");
306 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
307 public static Variable icstate(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
308 Variable session = graph.syncRequest(new ProxySessionRequest(context));
309 return session.getPossibleChild(graph, "__icstate__");
312 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
313 public static Variable session(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
314 return graph.syncRequest(new ProxySessionRequest(context));
317 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
318 public static Variable experiment(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
319 // Try if experiment (run) has been used as the input
320 Variable var = input(graph, converter, context);
321 SimulationResource SR = SimulationResource.getInstance(graph);
322 while(var != null && !graph.isInstanceOf(var.getRepresents(graph), SR.Run)) {
323 var = var.getParent(graph);
327 IExperiment exp = getExperiment(graph, var);
335 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
336 public static Variable model(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
337 Variable var = input(graph, converter, context);
338 ModelingResources MOD = ModelingResources.getInstance(graph);
339 while(var != null && !graph.isInstanceOf(var.getRepresents(graph), MOD.StructuralModel)) {
340 var = var.getParent(graph);
346 private static Collection<Variable> getBroadcasted(ReadGraph graph, Variable target) throws DatabaseException {
348 ArrayList<Variable> result = new ArrayList<Variable>();
350 DocumentationResource DOC = DocumentationResource.getInstance(graph);
352 Variable broadcasted = target.getPossibleProperty(graph, DOC.Relations_broadcasted);
353 if(broadcasted != null) result.add(broadcasted);
355 for(Variable child : DocumentServerUtils.getChildrenInOrdinalOrder(graph, target)) {
356 result.addAll(getBroadcasted(graph, child));
363 private static List<Command> getCommands(ReadGraph graph, Collection<Variable> commandVariables, String trigger, CommandContext constants, boolean broadcast) throws DatabaseException {
365 if(commandVariables.isEmpty()) return Collections.emptyList();
368 TreeMap<Integer, List<Command>> sequences = new TreeMap<Integer, List<Command>>();
370 DocumentationResource DOC = DocumentationResource.getInstance(graph);
373 for (Variable c : commandVariables) {
376 t = c.getName(graph);
378 Connection conn = c.getValue(graph);
379 Variable targetConnectionPoint = DocumentServerUtils.getPossibleCommandTriggerConnectionPoint(graph, c, conn);
380 if (targetConnectionPoint != null) {
381 Variable target = targetConnectionPoint.getParent(graph);
382 if (target != null) {
384 Boolean enabled = target.getPossiblePropertyValue(graph, DOC.Properties_exists, Bindings.BOOLEAN);
385 if(enabled != null && !enabled) continue;
393 String o = c.getPossiblePropertyValue(graph, "ordinal");
395 ordinal = Integer.parseInt(o);
396 } catch (NumberFormatException e) {}
399 String constantKey = target.getPossiblePropertyValue(graph, "constantKey");
400 String constantValue = target.getPossiblePropertyValue(graph, "constantValue");
402 CommandContextMutable newConstants = (CommandContextMutable)constants;
403 if(constantKey != null && constantValue != null) {
404 if(!constantKey.isEmpty()) {
405 newConstants = new CommandContextImpl().merge(constants);
406 newConstants.putString(constantKey, constantValue);
410 String requiredKey = target.getPossiblePropertyValue(graph, "requiredKey");
411 if(requiredKey != null && !requiredKey.isEmpty()) {
412 if (newConstants == constants) {
413 newConstants = new CommandContextImpl().merge(constants);
415 newConstants.putRow(CommandContext.REQUIRED_KEYS, Collections.<Object>singletonList(requiredKey));
418 String forbiddenKey = target.getPossiblePropertyValue(graph, "forbiddenKey");
419 if(forbiddenKey != null && !forbiddenKey.isEmpty()) {
420 if (newConstants == constants) {
421 newConstants = new CommandContextImpl().merge(constants);
423 newConstants.putRow(CommandContext.FORBIDDEN_KEYS, Collections.<Object>singletonList(forbiddenKey));
426 if(DOC.Relations_broadcast.equals(targetConnectionPoint.getPredicateResource(graph))) {
428 // This is a broadcast terminal of a container
429 List<Command> broadcastCommands = getCommands(graph, getBroadcasted(graph, target), t, newConstants, true);
430 sequences.put(ordinal, broadcastCommands);
434 Command command = new Command(DocumentServerUtils.getId(graph, target), t,
435 targetConnectionPoint.getName(graph), newConstants);
436 sequences.put(ordinal, Collections.singletonList(command));
444 List<Command> commands = new ArrayList<Command>();
445 for (List<Command> commandList : sequences.values()) {
446 for (Command command : commandList) {
447 commands.add(command);
460 * @throws DatabaseException
462 public static List<Command> commandList(ReadGraph graph, Variable variable) throws DatabaseException {
463 return getCommands(graph, DocumentServerUtils.getTriggerCommands(graph, variable.getParent(graph)), null, new CommandContextImpl(), false);
472 * @throws DatabaseException
474 public static List<DataDefinition> dataDefinitions(ReadGraph graph, Variable variable) throws DatabaseException {
475 DocumentationResource DOC = DocumentationResource.getInstance(graph);
476 ArrayList<DataDefinition> dataDefinitions = new ArrayList<DataDefinition>();
477 // Find data definition connections
478 for (Variable dataDefinitionRelation : DocumentServerUtils.getDataDefinitions(graph, variable.getParent(graph))) {
479 Connection dataDefinitionConnection = dataDefinitionRelation.getValue(graph);
480 // Find data the other end of definition connection
481 Collection<Variable> dataDefinitionProviders = DocumentServerUtils.getPossibleOtherConnectionPoints(graph,
482 dataDefinitionRelation, dataDefinitionConnection);
483 if (dataDefinitionProviders != null) {
485 for(Variable dataDefinitionProvider : dataDefinitionProviders) {
487 Variable dataDefinition = dataDefinitionProvider.getParent(graph);
488 if (dataDefinition != null) {
489 // Found other end. Is should contain ONE data
490 // definition connection to the actual data
491 Collection<Variable> dataCollection = DocumentServerUtils.getDataRelations(graph, dataDefinition);
492 if (dataCollection.size() == 1) {
493 Variable dataRelation = dataCollection.iterator().next();
494 Connection dataConnection = dataRelation.getValue(graph);
495 // Find data the other end of definition connection
496 Variable dataConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph,
497 dataRelation, dataConnection);
498 if (dataConnectionPoint != null) {
499 Variable data = dataConnectionPoint.getParent(graph);
500 Resource type = dataDefinition.getPossibleType(graph, DOC.Components_Component);
502 if (graph.isInheritedFrom(type, DOC.Components_DefVar)) {
504 String sourceProperty = dataDefinition.getPropertyValue(graph, DOC.Properties_source,
506 String targetProperty = dataDefinition.getPropertyValue(graph, DOC.Properties_target,
509 dataDefinitions.add(new DataDefinition(DocumentServerUtils.getId(graph, data),
510 sourceProperty, targetProperty));
512 } else if (graph.isInheritedFrom(type, DOC.Components_DefVars)) {
514 List<String> sourcesProperty = toList(dataDefinition.getPropertyValue(graph, DOC.Properties_sources), String.class);
515 List<String> targetsProperty = toList(dataDefinition.getPropertyValue(graph, DOC.Properties_targets), String.class);
517 for (int i = 0; i < Math.min(sourcesProperty.size(), targetsProperty.size()); i++) {
518 dataDefinitions.add(new DataDefinition(DocumentServerUtils.getId(graph, data),
519 sourcesProperty.get(i), targetsProperty.get(i)));
528 return dataDefinitions;
531 @SuppressWarnings("unchecked")
532 private static <T> List<T> toList(Object o, Class<T> c) {
533 List<T> result = null;
534 if (o instanceof List) {
536 } else if (o instanceof Object[]) {
537 result = new ArrayList<T>(((Object[])o).length);
538 for (T item : (T[])o) {
543 return Collections.<T>emptyList();
547 public static AbstractEventHandler emptyOnClick(ReadGraph graph) throws DatabaseException {
548 return new EventHandler() {
550 public ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException {
556 public static AbstractEventHandler writeEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
558 final Session session = graph.getSession();
560 return new AbstractEventHandler() {
563 public CommandResult handle(final CommandContext parameters) {
565 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
569 String result = session.sync(new WriteResultRequest<String>() {
572 public String perform(WriteGraph graph) throws DatabaseException {
573 SCLContext sclContext = SCLContext.getCurrent();
574 Object oldGraph = sclContext.put("graph", graph);
575 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
577 Function1<String,String> pf = new FunctionImpl1<String,String>() {
579 public String apply(String key) {
580 return parameters.getString(key);
583 Object response = (String)fn.apply(variable, pf);
584 if(response instanceof String) {
585 return (String)response;
588 } catch (Throwable t) {
591 sclContext.put("graph", oldGraph);
592 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
601 return new SuccessResponse(result);
603 } catch (Throwable e) {
604 Logger.defaultLogError(e);
605 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
613 public static AbstractEventHandler readEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
615 final Session session = graph.getSession();
617 return new AbstractEventHandler() {
620 public CommandResult handle(final CommandContext parameters) {
622 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
626 String result = session.sync(new UniqueRead<String>() {
629 public String perform(ReadGraph graph) throws DatabaseException {
630 SCLContext sclContext = SCLContext.getCurrent();
631 Object oldGraph = sclContext.put("graph", graph);
632 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
634 Function1<String,String> pf = new FunctionImpl1<String,String>() {
636 public String apply(String key) {
637 return parameters.getString(key);
640 Object response = (String)fn.apply(variable, pf);
641 if(response instanceof String) {
642 return (String)response;
645 } catch (Throwable t) {
648 sclContext.put("graph", oldGraph);
649 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
658 return new SuccessResponse(result);
660 } catch (Throwable e) {
661 Logger.defaultLogError(e);
662 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
670 public static AbstractEventHandler readEventHandler2(ReadGraph graph, final Function fn) {
672 final Session session = graph.getSession();
674 return new AbstractEventHandler() {
677 public CommandResult handle(final CommandContext parameters) {
679 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
683 CommandResult result = session.sync(new UniqueRead<CommandResult>() {
686 public CommandResult perform(ReadGraph graph) throws DatabaseException {
687 SCLContext sclContext = SCLContext.getCurrent();
688 Object oldGraph = sclContext.put("graph", graph);
689 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
691 Object response = fn.apply(parameters);
692 if(response instanceof CommandResult) {
693 return (CommandResult)response;
696 } catch (Throwable t) {
699 sclContext.put("graph", oldGraph);
700 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
711 } catch (Throwable e) {
712 Logger.defaultLogError(e);
713 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
721 public static AbstractEventHandler responseHandler(ReadGraph graph, Variable self, String function) throws DatabaseException {
723 Variable anyFunction = self.browse(graph, ".#" + function);
724 if(anyFunction == null) return null;
726 final List<TCon> effects = ServerSCLHandlerValueRequest.getEffects(graph, anyFunction);
728 final Function1<CommandContext, CommandResult> fn = anyFunction.getValue(graph);
729 String expression = anyFunction.getPropertyValue(graph, "expression");
731 final Session session = graph.getSession();
733 return new AbstractResponseHandler(expression) {
735 private String formatError(RequestProcessor proc, Throwable t) {
739 return proc.syncRequest(new UniqueRead<String>() {
742 public String perform(ReadGraph graph) throws DatabaseException {
743 Variable proxy = ProxyVariables.proxyVariableRoot(graph, anyFunction);
744 String uri2 = proxy.getURI(graph);
745 String uri = self.getParent(graph).getURI(graph);
747 String path = uri.substring(uri2.length());
749 String expr = anyFunction.getPossiblePropertyValue(graph, "expression");
750 StringBuilder message = new StringBuilder();
751 message.append("Handler execution failed\n");
752 message.append(" handler=" + path + "\n");
753 message.append(" expression=" + expr + "\n");
754 message.append(" message=" + t.getMessage() + "\n");
756 StringWriter sw = new StringWriter();
757 t.printStackTrace(new PrintWriter(sw));
758 message.append(" stack trace=" + sw);
760 return message.toString();
765 } catch (DatabaseException e) {
767 return e.getMessage();
774 public CommandResult handle(final CommandContext parameters) {
776 IConsole console = parameters.getValue("__console__");
777 SCLReportingHandler printer = (console != null) ? new ConsoleSCLReportingHandler(console)
778 : (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
782 Object result = null;
783 if(effects.contains(Types.WRITE_GRAPH)) {
785 result = session.syncRequest(new WriteResultRequest<Object>() {
788 public Object perform(WriteGraph graph)
789 throws DatabaseException {
790 SCLContext sclContext = SCLContext.getCurrent();
791 Object oldGraph = sclContext.put("graph", graph);
792 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
794 Object response = fn.apply(parameters);
796 } catch (Throwable t) {
797 return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
799 sclContext.put("graph", oldGraph);
800 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
806 } else if(effects.contains(Types.READ_GRAPH)) {
808 result = session.sync(new UniqueRead<Object>() {
811 public Object perform(ReadGraph graph) throws DatabaseException {
813 SCLContext sclContext = SCLContext.getCurrent();
814 Object oldGraph = sclContext.put("graph", graph);
815 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
818 Object response = fn.apply(parameters);
820 } catch (Throwable t) {
821 return new org.simantics.document.server.serverResponse.Error(formatError(graph, t));
823 sclContext.put("graph", oldGraph);
824 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
832 SCLContext sclContext = SCLContext.getCurrent();
833 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
835 result = fn.apply(parameters);
837 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
842 if (result instanceof org.simantics.document.server.serverResponse.Error) {
843 return (CommandResult)result;
846 if (result instanceof CommandResult) {
847 return (CommandResult)result;
849 CommandContextMutable assignments = new CommandContextImpl();
850 assignments.putValue("result", result);
851 return new ServerResponse(200, "", assignments);
854 } catch (Throwable e) {
855 return new org.simantics.document.server.serverResponse.Error(formatError(Simantics.getSession(), e));
863 public static AbstractEventHandler writeEventHandler2(ReadGraph graph, final Function fn) {
865 final Session session = graph.getSession();
867 return new AbstractEventHandler() {
870 public CommandResult handle(final CommandContext parameters) {
872 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
876 CommandResult result = session.syncRequest(new WriteResultRequest<CommandResult>() {
879 public CommandResult perform(WriteGraph graph) throws DatabaseException {
880 SCLContext sclContext = SCLContext.getCurrent();
881 Object oldGraph = sclContext.put("graph", graph);
882 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
884 Object response = fn.apply(parameters);
885 if(response instanceof CommandResult) {
886 return (CommandResult)response;
889 } catch (Throwable t) {
892 sclContext.put("graph", oldGraph);
893 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
904 } catch (Throwable e) {
905 Logger.defaultLogError(e);
906 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
914 public static AbstractEventHandler eventHandler2(ReadGraph graph, final Function fn) {
916 return new AbstractEventHandler() {
919 public CommandResult handle(final CommandContext parameters) {
921 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
925 SCLContext sclContext = SCLContext.getCurrent();
926 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
928 Object response = fn.apply(parameters);
929 if(response instanceof CommandResult) {
930 return (CommandResult)response;
933 } catch (Throwable t) {
936 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
941 } catch (Throwable e) {
942 Logger.defaultLogError(e);
943 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
951 public static AbstractEventHandler eventHandler(ReadGraph graph, final Function fn) {
953 return new AbstractEventHandler() {
956 public CommandResult handle(final CommandContext parameters) {
958 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
964 SCLContext sclContext = SCLContext.getCurrent();
965 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
967 Function1<String,String> pf = new FunctionImpl1<String,String>() {
969 public String apply(String key) {
970 return parameters.getString(key);
973 Object response = (String)fn.apply(pf);
974 if(response instanceof String) {
975 result = (String)response;
977 } catch (Throwable t) {
980 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
983 return new SuccessResponse(result);
985 } catch (Throwable e) {
986 Logger.defaultLogError(e);
987 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
995 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
996 public static Object sclValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
997 return ServerSCLValueRequest.compileAndEvaluate(graph, context);
1000 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
1001 public static Object sclHandlerValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1002 return ServerSCLHandlerValueRequest.compileAndEvaluate(graph, context);
1005 @SCLValue(type = "ReadGraph -> Resource -> Resource -> ComponentNamingStrategy")
1006 public static ComponentNamingStrategy componentNamingStrategy(ReadGraph graph, Resource converter, Resource context) throws DatabaseException {
1007 return createComponentNamingStrategy(graph);
1010 public static CaseInsensitiveComponentFunctionNamingStrategy createComponentNamingStrategy(ReadGraph graph) throws DatabaseException {
1011 Layer0X L0X = Layer0X.getInstance(graph);
1012 @SuppressWarnings({ "unchecked", "rawtypes" })
1013 final Function dependencies = graph.syncRequest(new Adapter(L0X.DependencyResources, Function.class), TransientCacheListener.<Function>instance());
1014 @SuppressWarnings("rawtypes")
1015 Function moduleNameFunction = new FunctionImpl4<ReadGraph, Resource, String, Integer, List<Map<String, Object>>>() {
1017 public Object apply(ReadGraph p0, Resource p1, String p2) {
1018 return apply(p0, p1, p2);
1020 @SuppressWarnings("unchecked")
1022 public List<Map<String, Object>> apply(ReadGraph graph, Resource model, String search, Integer maxResults) {
1023 return (List<Map<String, Object>>)dependencies.apply(graph, model, search, maxResults);
1026 return new CaseInsensitiveComponentFunctionNamingStrategy("%s%02d", moduleNameFunction);
1029 public static IExperiment getExperiment(ReadGraph graph, Variable variable) throws DatabaseException {
1030 if (variable == null)
1033 SimulationResource SIMU = SimulationResource.getInstance(graph);
1035 Variable var = variable;
1036 Resource represents = var.getRepresents(graph);
1037 Resource activeRun = null;
1038 if (represents != null && graph.isInstanceOf(represents, SIMU.Run)) {
1039 activeRun = represents;
1042 IProject project = Simantics.peekProject();
1043 if (activeRun != null && project != null) {
1044 IExperimentManager expMan = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
1046 return expMan.getExperiment(NameUtils.getSafeName(graph, activeRun));
1052 public static CommandContextMutable putTuple(CommandContextMutable context, String key, Object tuple) {
1053 List<Object> list = new ArrayList<Object>();
1054 if (tuple instanceof Tuple) {
1055 Collections.addAll(list, ((Tuple)tuple).toArray());
1059 context.putRow(key, list);
1063 public static List<Object> getTuples(CommandContext context, String key) {
1064 List<List<Object>> rows = context.getRows(key);
1065 List<Object> tuples = new ArrayList<Object>();
1067 for (List<Object> row : rows) {
1068 switch (row.size()) {
1069 case 0: tuples.add(Tuple0.INSTANCE); break;
1070 case 1: tuples.add(row.get(1)); break;
1071 case 2: tuples.add(new Tuple2(row.get(0), row.get(1))); break;
1072 case 3: tuples.add(new Tuple3(row.get(0), row.get(1), row.get(2))); break;
1073 case 4: tuples.add(new Tuple4(row.get(0), row.get(1), row.get(2), row.get(3))); break;
1074 case 5: tuples.add(new Tuple5(row.get(0), row.get(1), row.get(2), row.get(3), row.get(4))); break;
1081 public static String printContext(CommandContext context) {
1082 return context.toString();
1085 @SCLValue(type = "AbstractEventHandler")
1086 public static AbstractEventHandler emptyEvent = new AbstractEventHandler() {
1089 public CommandResult handle(CommandContext context) {
1095 public static String sclStateKey(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
1097 String baseURI = base.getURI(graph);
1099 String selfURI = self.getURI(graph);
1101 String prefix = selfURI.substring(0, selfURI.indexOf(ProxyChildVariable.CONTEXT_BEGIN));
1102 String suffix = selfURI.substring(selfURI.lastIndexOf(ProxyChildVariable.CONTEXT_END) + ProxyChildVariable.CONTEXT_END.length());
1103 String stripped = prefix + suffix;
1105 String relative = Variables.getRelativeRVI(baseURI, stripped);
1107 return Variables.getRVI(relative, ref);
1111 public static Variable sclStateVariable(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
1113 String id = sclStateKey(graph, base, self, ref);
1115 Variable sclVar = base.getPossibleChild(graph, "__scl__");
1116 if(sclVar == null) return null;
1118 return sclVar.getPossibleProperty(graph, id);
1122 public static Object sclStateValueOrDefault(ReadGraph graph, Variable base, Variable self, String ref, Object defaultValue) throws DatabaseException {
1124 Variable stateVariable = sclStateVariable(graph, base, self, ref);
1125 if (stateVariable != null) {
1127 return stateVariable.getValue(graph);
1131 String id = sclStateKey(graph, base, self, ref);
1133 SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph) + "/__scl__");
1134 realm.getConnection().setVariable(id, getSCLType(defaultValue), defaultValue);
1136 return defaultValue;
1141 public static void setSclStateValue(WriteGraph graph, Variable base, Variable self, String ref, Object value) throws DatabaseException {
1143 String id = sclStateKey(graph, base, self, ref);
1145 SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph)+"/__scl__");
1146 realm.getConnection().setVariable(id, getSCLType(value), value);
1147 realm.refreshVariablesSync();
1151 public static Object projectComponentState(ReadGraph graph, Variable self, String ref, Object defaultValue) throws DatabaseException {
1152 Resource project = Simantics.getProjectResource();
1153 Variable component = self.getParent(graph);
1154 Variable projectVariable = Variables.getVariable(graph, project);
1155 return sclStateValueOrDefault(graph, projectVariable, component, ref, defaultValue);
1158 public static void setProjectComponentState(WriteGraph graph, Variable self, String ref, Object value) throws DatabaseException {
1159 Resource project = Simantics.getProjectResource();
1160 Variable component = self.getParent(graph);
1161 Variable projectVariable = Variables.getVariable(graph, project);
1162 setSclStateValue(graph, projectVariable, component, ref, value);
1165 private static Type getSCLType(Object value) throws DatabaseException {
1166 Binding b = Bindings.getBindingUnchecked(value.getClass());
1167 Datatype t = b.type();
1168 if(Datatypes.STRING.equals(t)) return Types.STRING;
1169 if(Datatypes.DOUBLE.equals(t)) return Types.DOUBLE;
1170 throw new DatabaseException("Type not supported");
1173 public static List<Variable> documentModelContribution(ReadGraph graph, Resource doc) throws DatabaseException {
1174 Resource project = Simantics.getProjectResource();
1175 ArrayList<Variable> result = new ArrayList<Variable>();
1176 for(Resource model : graph.syncRequest(new ProjectModels(project))) {
1177 result.add(Variables.getVariable(graph, model));
1182 public static String documentModelContributionLabel(ReadGraph graph, Variable var) throws DatabaseException {
1183 return var.getName(graph);
1186 public static Object getPropertyValueCached(ReadGraph graph, Variable variable, String name, Binding binding) throws DatabaseException {
1187 Variable property = graph.syncRequest(new VariableProperty(variable, name));
1188 return graph.syncRequest(new VariableValueWithBinding<Object>(property, binding));
1191 public static class ParentExistsRequest extends VariableRead<Boolean> {
1193 public ParentExistsRequest(Variable parent) {
1198 public Boolean perform(ReadGraph graph) throws DatabaseException {
1200 Variable existsProperty = variable.getPossibleProperty(graph, "exists");
1201 if(existsProperty == null) return true;
1203 Boolean exists = existsProperty.getPossibleValue(graph, Bindings.BOOLEAN);
1204 if (exists == null || !exists) return false;
1206 return graph.syncRequest(new ParentExistsRequest(variable.getParent(graph)));
1212 public static class PathExistsRequest extends VariableRead<Boolean> {
1214 public PathExistsRequest(Variable variable) {
1219 public Boolean perform(ReadGraph graph) throws DatabaseException {
1221 Variable widget = variable.getParent(graph);
1223 Boolean exists = widget.getPossiblePropertyValue(graph, "exists");
1224 if (exists == null || !exists) return false;
1226 if (!graph.syncRequest(new ParentExistsRequest(widget.getParent(graph)))) return false;
1228 DocumentationResource DOC = DocumentationResource.getInstance(graph);
1229 Collection <Variable> cps = widget.getProperties(graph, DocumentationResource.URIs.Relations_parentRelation);
1230 for (Variable cp : cps) {
1232 Connection conn = cp.getValue(graph);
1233 Variable otherCp = DocumentServerUtils.getPossibleChildConnectionPoint(graph, cp, conn);
1234 if (otherCp != null) {
1235 Variable parentWidget = otherCp.getParent(graph);
1236 if (parentWidget.<Boolean>getPropertyValue(graph, "pathExists")) {
1240 Variable parentCp = graph.sync(new UnaryRead<Connection, Variable>(conn) {
1242 public Variable perform(ReadGraph graph) throws DatabaseException {
1243 DocumentationResource DOC = DocumentationResource.getInstance(graph);
1244 Collection<VariableConnectionPointDescriptor> descs = parameter.getConnectionPointDescriptors(graph, null);
1246 for(VariableConnectionPointDescriptor desc : descs) {
1247 if (DOC.Relations_partN.equals(desc.getConnectionPointResource(graph))) {
1248 return desc.getVariable(graph);
1254 if (parentCp != null) {
1255 Variable parentWidget = parentCp.getParent(graph);
1256 if (parentWidget.<Boolean>getPropertyValue(graph, "pathExists")) {
1263 Resource type = widget.getType(graph);
1264 return graph.isInheritedFrom(type, DOC.Components_ParentlessComponent) ||
1265 (graph.isInheritedFrom(type, DOC.DocumentComponent) && cps.isEmpty());
1269 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Boolean")
1270 public static boolean pathExists(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1271 return graph.syncRequest(new PathExistsRequest(context));