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.scl.SCLDatabaseException;
42 import org.simantics.db.layer0.variable.ConstantPropertyVariable;
43 import org.simantics.db.layer0.variable.ProxyChildVariable;
44 import org.simantics.db.layer0.variable.ProxySessionRequest;
45 import org.simantics.db.layer0.variable.ProxyVariables;
46 import org.simantics.db.layer0.variable.StandardAssertedGraphPropertyVariable;
47 import org.simantics.db.layer0.variable.StandardGraphChildVariable;
48 import org.simantics.db.layer0.variable.StandardGraphPropertyVariable;
49 import org.simantics.db.layer0.variable.Variable;
50 import org.simantics.db.layer0.variable.VariableMap;
51 import org.simantics.db.layer0.variable.VariableMapImpl;
52 import org.simantics.db.layer0.variable.Variables;
53 import org.simantics.db.procedure.AsyncProcedure;
54 import org.simantics.db.service.DirectQuerySupport;
55 import org.simantics.document.base.ontology.DocumentationResource;
56 import org.simantics.document.server.bean.Command;
57 import org.simantics.document.server.bean.DataDefinition;
58 import org.simantics.document.server.handler.AbstractEventHandler;
59 import org.simantics.document.server.handler.AbstractResponseHandler;
60 import org.simantics.document.server.handler.EventHandler;
61 import org.simantics.document.server.io.CommandContext;
62 import org.simantics.document.server.io.CommandContextImpl;
63 import org.simantics.document.server.io.CommandContextMutable;
64 import org.simantics.document.server.io.CommandResult;
65 import org.simantics.document.server.io.IConsole;
66 import org.simantics.document.server.request.NodeRequest;
67 import org.simantics.document.server.request.ServerSCLHandlerValueRequest;
68 import org.simantics.document.server.request.ServerSCLValueRequest;
69 import org.simantics.document.server.serverResponse.ServerResponse;
70 import org.simantics.document.server.serverResponse.SuccessResponse;
71 import org.simantics.modeling.ModelingResources;
72 import org.simantics.modeling.scl.SCLRealm;
73 import org.simantics.modeling.scl.SCLSessionManager;
74 import org.simantics.modeling.services.CaseInsensitiveComponentFunctionNamingStrategy;
75 import org.simantics.modeling.services.ComponentNamingStrategy;
76 import org.simantics.operation.Layer0X;
77 import org.simantics.project.IProject;
78 import org.simantics.scl.compiler.module.repository.ImportFailureException;
79 import org.simantics.scl.compiler.types.TCon;
80 import org.simantics.scl.compiler.types.Type;
81 import org.simantics.scl.compiler.types.Types;
82 import org.simantics.scl.reflection.annotations.SCLValue;
83 import org.simantics.scl.runtime.SCLContext;
84 import org.simantics.scl.runtime.function.Function;
85 import org.simantics.scl.runtime.function.Function1;
86 import org.simantics.scl.runtime.function.FunctionImpl1;
87 import org.simantics.scl.runtime.function.FunctionImpl4;
88 import org.simantics.scl.runtime.reporting.SCLReportingHandler;
89 import org.simantics.scl.runtime.tuple.Tuple;
90 import org.simantics.scl.runtime.tuple.Tuple0;
91 import org.simantics.scl.runtime.tuple.Tuple2;
92 import org.simantics.scl.runtime.tuple.Tuple3;
93 import org.simantics.scl.runtime.tuple.Tuple4;
94 import org.simantics.scl.runtime.tuple.Tuple5;
95 import org.simantics.simulation.experiment.IExperiment;
96 import org.simantics.simulation.ontology.SimulationResource;
97 import org.simantics.simulation.project.IExperimentManager;
98 import org.simantics.structural2.variables.Connection;
99 import org.simantics.structural2.variables.StandardProceduralChildVariable;
100 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
101 import org.slf4j.LoggerFactory;
103 import gnu.trove.map.hash.THashMap;
105 public class Functions {
107 private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Functions.class);
109 private static class PrimitivePropertyStatementsProcedure implements AsyncProcedure<DirectStatements> {
111 public DirectStatements result;
114 public void execute(AsyncReadGraph graph, DirectStatements result) {
115 this.result = result;
119 public void exception(AsyncReadGraph graph, Throwable throwable) {
124 @SCLValue(type = "VariableMap")
125 public static VariableMap primitiveProperties = new VariableMapImpl() {
126 private void storePropertyValueAndExceptions(ReadGraph graph, Variable parent, String name, Variable property, Map<String, Variable> map) {
128 Object value = property.getValue(graph);
129 map.put(name, new ConstantPropertyVariable(parent, name, value, null));
130 } catch (DatabaseException e) {
131 Variable propertyExceptions = map.get(NodeRequest.PROPERTY_VALUE_EXCEPTIONS);
132 Map<String, Exception> exceptionMap;
133 if (propertyExceptions == null) {
134 exceptionMap = new TreeMap<String, Exception>();
135 propertyExceptions = new ConstantPropertyVariable(parent, NodeRequest.PROPERTY_VALUE_EXCEPTIONS, exceptionMap, null);
136 map.put(NodeRequest.PROPERTY_VALUE_EXCEPTIONS, propertyExceptions);
139 exceptionMap = propertyExceptions.getValue(graph);
140 } catch (DatabaseException e1) {
141 Logger.defaultLogError(e1);
147 label = property.getLabel(graph);
148 } catch (DatabaseException e2) {
151 exceptionMap.put(label, e);
156 public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
157 return All.getStandardPropertyDomainPropertyVariableFromValue(graph, context, name);
161 public Map<String, Variable> getVariables(final ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
163 if(map == null) map = new HashMap<String,Variable>();
165 Variable parent = context.getParent(graph);
167 DocumentationResource DOC = DocumentationResource.getInstance(graph);
169 if(parent instanceof StandardProceduralChildVariable) {
171 StandardProceduralChildVariable procedural = (StandardProceduralChildVariable)parent;
172 for(Variable property : procedural.getProperties(graph/*, DocumentationResource.URIs.Document_AttributeRelation*/)) {
173 if(property instanceof StandardAssertedGraphPropertyVariable) {
174 StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property;
175 if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name) || "pollingFunction".equals(ass.property.name)) {
176 storePropertyValueAndExceptions(graph, parent, ass.property.name, property, map);
180 Resource predicate = property.getPossiblePredicateResource(graph);
181 if(predicate != null) {
182 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
183 if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
184 Variable prop = parent.getProperty(graph, predicate);
185 storePropertyValueAndExceptions(graph, parent, info.name, prop, map);
192 Resource parentRes = parent.getRepresents(graph);
194 Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands);
195 storePropertyValueAndExceptions(graph, parent, "commands", prop, map);
198 if (graph.getPossibleObject(parentRes, DOC.Properties_dataDefinitions) != null) {
199 Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions);
200 storePropertyValueAndExceptions(graph, parent, "dataDefinitions", prop, map);
203 DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
204 PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure();
206 dqs.forEachDirectPersistentStatement(graph, parentRes, foo);
208 for(Statement stm : foo.result) {
209 Resource predicate = stm.getPredicate();
210 PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate));
212 if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) {
213 Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate);
214 storePropertyValueAndExceptions(graph, parent, info.name, prop, map);
216 Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation);
217 if(definition != null) {
218 PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition));
219 Variable prop = new StandardGraphPropertyVariable(graph, parent, definition);
220 map.put(info2.name, new PrimitiveValueVariable(parent, info2.name, prop));
231 @SCLValue(type = "VariableMap")
232 public static VariableMap inputSpaceChildren = new VariableMapImpl() {
234 private Variable getProxy(ReadGraph graph, Variable context) throws DatabaseException {
235 Variable root = Variables.getRootVariable(graph);
236 return new DocumentProxyChildVariable(context, context, root, ProxyChildVariable.CONTEXT_BEGIN);
240 public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
242 if(ProxyChildVariable.CONTEXT_BEGIN.equals(name)) return getProxy(graph, context);
243 return All.standardChildDomainChildren.getVariable(graph, context, name);
248 public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
250 map = All.standardChildDomainChildren.getVariables(graph, context, map);
251 if(map == null) map = new THashMap<String,Variable>();
252 map.put(ProxyChildVariable.CONTEXT_BEGIN, getProxy(graph, context));
259 static class DocumentProxyChildVariable extends ProxyChildVariable {
261 public DocumentProxyChildVariable(Variable base, Variable parent, Variable other, String name) {
262 super(base, parent, other, name);
266 public Variable create(Variable base, Variable parent, Variable other, String name) {
267 return new DocumentProxyChildVariable(base, parent, other, name);
270 public Variable getPossibleChild(ReadGraph graph, String name) throws DatabaseException {
272 if(CONTEXT_END.equals(name)) {
273 if(other instanceof ProxyChildVariable) {
274 // The context is also a proxy - let it do the job
275 return super.getPossibleChild(graph, name);
277 return new RootVariable(this, base.getRepresents(graph));
281 return super.getPossibleChild(graph, name);
285 public Collection<Variable> getChildren(ReadGraph graph) throws DatabaseException {
287 Collection<Variable> result = super.getChildren(graph);
288 if(!(base instanceof ProxyChildVariable)) {
289 result.add(new RootVariable(this, base.getRepresents(graph)));
297 static class RootVariable extends StandardGraphChildVariable {
299 public RootVariable(DocumentProxyChildVariable parent, Resource resource) {
300 super(parent, null, resource);
304 public String getName(ReadGraph graph) throws DatabaseException {
305 return ProxyChildVariable.CONTEXT_END;
308 @SuppressWarnings("deprecation")
310 public Variable getNameVariable(ReadGraph graph) throws DatabaseException {
311 return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING);
316 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
317 public static Variable input(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
318 Variable session = graph.syncRequest(new ProxySessionRequest(context));
319 DocumentationResource DOC = DocumentationResource.getInstance(graph);
320 String uri = session.getPossiblePropertyValue(graph, DOC.Session_inputURI);
322 // TODO HAXX - Please fix this
323 // we have no URI so this probably means that someone has inserted a non-session
324 // into the proxy variable => return that instead
327 return Variables.getVariable(graph, uri);
330 public static Variable stateVariable(ReadGraph graph, Variable self) throws DatabaseException {
331 Variable session = graph.syncRequest(new ProxySessionRequest(self));
333 throw new DatabaseException("No state for " + self.getURI(graph));
334 return session.getPossibleChild(graph, "__scl__");
337 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
338 public static Variable state(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
339 Variable session = graph.syncRequest(new ProxySessionRequest(context));
341 throw new DatabaseException("No state for " + context.getURI(graph));
342 return session.getPossibleChild(graph, "__scl__");
345 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
346 public static Variable icstate(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
347 Variable session = graph.syncRequest(new ProxySessionRequest(context));
348 return session.getPossibleChild(graph, "__icstate__");
351 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
352 public static Variable session(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
353 return graph.syncRequest(new ProxySessionRequest(context));
356 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
357 public static Variable experiment(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
358 // Try if experiment (run) has been used as the input
359 Variable var = input(graph, converter, context);
360 SimulationResource SR = SimulationResource.getInstance(graph);
361 while(var != null && !graph.isInstanceOf(var.getRepresents(graph), SR.Run)) {
362 var = var.getParent(graph);
366 IExperiment exp = getExperiment(graph, var);
374 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Variable")
375 public static Variable model(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
376 Variable var = input(graph, converter, context);
377 ModelingResources MOD = ModelingResources.getInstance(graph);
378 while(var != null && !graph.isInstanceOf(var.getRepresents(graph), MOD.StructuralModel)) {
379 var = var.getParent(graph);
385 private static Collection<Variable> getBroadcasted(ReadGraph graph, Variable target) throws DatabaseException {
387 ArrayList<Variable> result = new ArrayList<Variable>();
389 DocumentationResource DOC = DocumentationResource.getInstance(graph);
391 Variable broadcasted = target.getPossibleProperty(graph, DOC.Relations_broadcasted);
392 if(broadcasted != null) result.add(broadcasted);
394 for(Variable child : DocumentServerUtils.getChildrenInOrdinalOrder(graph, target)) {
395 result.addAll(getBroadcasted(graph, child));
402 private static List<Command> getCommands(ReadGraph graph, Collection<Variable> commandVariables, String trigger, CommandContext constants, boolean broadcast) throws DatabaseException {
404 if(commandVariables.isEmpty()) return Collections.emptyList();
407 TreeMap<Integer, List<Command>> sequences = new TreeMap<Integer, List<Command>>();
409 DocumentationResource DOC = DocumentationResource.getInstance(graph);
412 for (Variable c : commandVariables) {
415 t = c.getName(graph);
417 Connection conn = c.getValue(graph);
418 Variable targetConnectionPoint = DocumentServerUtils.getPossibleCommandTriggerConnectionPoint(graph, c, conn);
419 if (targetConnectionPoint != null) {
420 Variable target = targetConnectionPoint.getParent(graph);
421 if (target != null) {
423 Boolean enabled = target.getPossiblePropertyValue(graph, DOC.Properties_exists, Bindings.BOOLEAN);
424 if(enabled != null && !enabled) continue;
432 String o = c.getPossiblePropertyValue(graph, "ordinal");
434 ordinal = Integer.parseInt(o);
435 } catch (NumberFormatException e) {}
438 String constantKey = target.getPossiblePropertyValue(graph, "constantKey");
439 String constantValue = target.getPossiblePropertyValue(graph, "constantValue");
441 CommandContextMutable newConstants = (CommandContextMutable)constants;
442 if(constantKey != null && constantValue != null) {
443 if(!constantKey.isEmpty()) {
444 newConstants = new CommandContextImpl().merge(constants);
445 newConstants.putString(constantKey, constantValue);
449 String requiredKey = target.getPossiblePropertyValue(graph, "requiredKey");
450 if(requiredKey != null && !requiredKey.isEmpty()) {
451 if (newConstants == constants) {
452 newConstants = new CommandContextImpl().merge(constants);
454 newConstants.putRow(CommandContext.REQUIRED_KEYS, Collections.<Object>singletonList(requiredKey));
457 String forbiddenKey = target.getPossiblePropertyValue(graph, "forbiddenKey");
458 if(forbiddenKey != null && !forbiddenKey.isEmpty()) {
459 if (newConstants == constants) {
460 newConstants = new CommandContextImpl().merge(constants);
462 newConstants.putRow(CommandContext.FORBIDDEN_KEYS, Collections.<Object>singletonList(forbiddenKey));
465 if(DOC.Relations_broadcast.equals(targetConnectionPoint.getPredicateResource(graph))) {
467 // This is a broadcast terminal of a container
468 List<Command> broadcastCommands = getCommands(graph, getBroadcasted(graph, target), t, newConstants, true);
469 sequences.put(ordinal, broadcastCommands);
473 Command command = new Command(DocumentServerUtils.getId(graph, target), t,
474 targetConnectionPoint.getName(graph), newConstants);
475 sequences.put(ordinal, Collections.singletonList(command));
483 List<Command> commands = new ArrayList<Command>();
484 for (List<Command> commandList : sequences.values()) {
485 for (Command command : commandList) {
486 commands.add(command);
499 * @throws DatabaseException
501 public static List<Command> commandList(ReadGraph graph, Variable variable) throws DatabaseException {
502 return getCommands(graph, DocumentServerUtils.getTriggerCommands(graph, variable.getParent(graph)), null, new CommandContextImpl(), false);
511 * @throws DatabaseException
513 public static List<DataDefinition> dataDefinitions(ReadGraph graph, Variable variable) throws DatabaseException {
514 DocumentationResource DOC = DocumentationResource.getInstance(graph);
515 ArrayList<DataDefinition> dataDefinitions = new ArrayList<DataDefinition>();
516 // Find data definition connections
517 for (Variable dataDefinitionRelation : DocumentServerUtils.getDataDefinitions(graph, variable.getParent(graph))) {
518 Connection dataDefinitionConnection = dataDefinitionRelation.getValue(graph);
519 // Find data the other end of definition connection
520 Collection<Variable> dataDefinitionProviders = DocumentServerUtils.getPossibleOtherConnectionPoints(graph,
521 dataDefinitionRelation, dataDefinitionConnection);
522 if (dataDefinitionProviders != null) {
524 for(Variable dataDefinitionProvider : dataDefinitionProviders) {
526 Variable dataDefinition = dataDefinitionProvider.getParent(graph);
527 if (dataDefinition != null) {
528 // Found other end. Is should contain ONE data
529 // definition connection to the actual data
530 Collection<Variable> dataCollection = DocumentServerUtils.getDataRelations(graph, dataDefinition);
531 if (dataCollection.size() == 1) {
532 Variable dataRelation = dataCollection.iterator().next();
533 Connection dataConnection = dataRelation.getValue(graph);
534 // Find data the other end of definition connection
535 Variable dataConnectionPoint = DocumentServerUtils.getPossibleOtherConnectionPoint(graph,
536 dataRelation, dataConnection);
537 if (dataConnectionPoint != null) {
538 Variable data = dataConnectionPoint.getParent(graph);
539 Resource type = dataDefinition.getPossibleType(graph, DOC.Components_Component);
541 if (graph.isInheritedFrom(type, DOC.Components_DefVar)) {
543 String sourceProperty = dataDefinition.getPropertyValue(graph, DOC.Properties_source,
545 String targetProperty = dataDefinition.getPropertyValue(graph, DOC.Properties_target,
548 dataDefinitions.add(new DataDefinition(DocumentServerUtils.getId(graph, data),
549 sourceProperty, targetProperty));
551 } else if (graph.isInheritedFrom(type, DOC.Components_DefVars)) {
553 List<String> sourcesProperty = toList(dataDefinition.getPropertyValue(graph, DOC.Properties_sources), String.class);
554 List<String> targetsProperty = toList(dataDefinition.getPropertyValue(graph, DOC.Properties_targets), String.class);
556 for (int i = 0; i < Math.min(sourcesProperty.size(), targetsProperty.size()); i++) {
557 dataDefinitions.add(new DataDefinition(DocumentServerUtils.getId(graph, data),
558 sourcesProperty.get(i), targetsProperty.get(i)));
567 return dataDefinitions;
570 @SuppressWarnings("unchecked")
571 private static <T> List<T> toList(Object o, Class<T> c) {
572 List<T> result = null;
573 if (o instanceof List) {
575 } else if (o instanceof Object[]) {
576 result = new ArrayList<T>(((Object[])o).length);
577 for (T item : (T[])o) {
582 return Collections.<T>emptyList();
587 public static AbstractEventHandler emptyOnClick(ReadGraph graph) throws DatabaseException {
588 return new EventHandler() {
590 public ServerResponse handle(ReadGraph graph, CommandContext parameters) throws DatabaseException {
597 public static AbstractEventHandler writeEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
599 final Session session = graph.getSession();
601 return new AbstractEventHandler() {
604 public CommandResult handle(final CommandContext parameters) {
606 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
610 String result = session.sync(new WriteResultRequest<String>() {
613 public String perform(WriteGraph graph) throws DatabaseException {
614 SCLContext sclContext = SCLContext.getCurrent();
615 Object oldGraph = sclContext.put("graph", graph);
616 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
618 Function1<String,String> pf = new FunctionImpl1<String,String>() {
620 public String apply(String key) {
621 return parameters.getString(key);
624 Object response = (String)fn.apply(variable, pf);
625 if(response instanceof String) {
626 return (String)response;
630 sclContext.put("graph", oldGraph);
631 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
638 return new SuccessResponse(result);
640 } catch (Throwable e) {
641 Logger.defaultLogError(e);
642 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
651 public static AbstractEventHandler readEventHandler(ReadGraph graph, final Variable variable, final Function fn) {
653 final Session session = graph.getSession();
655 return new AbstractEventHandler() {
658 public CommandResult handle(final CommandContext parameters) {
660 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
664 String result = session.sync(new UniqueRead<String>() {
667 public String perform(ReadGraph graph) throws DatabaseException {
668 SCLContext sclContext = SCLContext.getCurrent();
669 Object oldGraph = sclContext.put("graph", graph);
670 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
672 Function1<String,String> pf = new FunctionImpl1<String,String>() {
674 public String apply(String key) {
675 return parameters.getString(key);
678 Object response = (String)fn.apply(variable, pf);
679 if(response instanceof String) {
680 return (String)response;
684 sclContext.put("graph", oldGraph);
685 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());
705 public static AbstractEventHandler readEventHandler2(ReadGraph graph, final Function fn) {
707 final Session session = graph.getSession();
709 return new AbstractEventHandler() {
712 public CommandResult handle(final CommandContext parameters) {
714 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
718 CommandResult result = session.sync(new UniqueRead<CommandResult>() {
721 public CommandResult perform(ReadGraph graph) throws DatabaseException {
722 SCLContext sclContext = SCLContext.getCurrent();
723 Object oldGraph = sclContext.put("graph", graph);
724 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
726 Object response = fn.apply(parameters);
727 if(response instanceof CommandResult) {
728 return (CommandResult)response;
732 sclContext.put("graph", oldGraph);
733 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
742 } catch (Throwable e) {
743 Logger.defaultLogError(e);
744 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
752 public static AbstractEventHandler responseHandler(ReadGraph graph, Variable self, String function) throws DatabaseException {
754 Variable anyFunction = self.browse(graph, ".#" + function);
755 if(anyFunction == null) return null;
757 final List<TCon> effects = ServerSCLHandlerValueRequest.getEffects(graph, anyFunction);
759 Function1<CommandContext, CommandResult> fn = anyFunction.getValue(graph);
760 //String expression = anyFunction.getPropertyValue(graph, "expression");
762 final Session session = graph.getSession();
764 return new AbstractResponseHandler(fn) {
766 private String formatError(RequestProcessor proc, Throwable t) {
770 return proc.syncRequest(new UniqueRead<String>() {
773 public String perform(ReadGraph graph) throws DatabaseException {
774 Variable proxy = ProxyVariables.proxyVariableRoot(graph, anyFunction);
775 String uri2 = proxy.getURI(graph);
776 String uri = self.getParent(graph).getURI(graph);
778 String path = uri.substring(uri2.length());
780 String expr = anyFunction.getPossiblePropertyValue(graph, "expression");
781 StringBuilder message = new StringBuilder();
782 message.append("Handler execution failed\n");
783 message.append(" handler=" + path + "\n");
784 message.append(" expression=" + expr + "\n");
785 message.append(" message=" + t.getMessage() + "\n");
787 StringWriter sw = new StringWriter();
788 t.printStackTrace(new PrintWriter(sw));
789 message.append(" stack trace=" + sw);
791 return message.toString();
796 } catch (DatabaseException e) {
798 return e.getMessage();
805 public CommandResult handle(final CommandContext parameters) {
807 IConsole console = parameters.getValue("__console__");
808 SCLReportingHandler printer = (console != null) ? new ConsoleSCLReportingHandler(console)
809 : (SCLReportingHandler) SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
813 Object result = null;
814 if(effects.contains(Types.WRITE_GRAPH)) {
816 result = session.syncRequest(new WriteResultRequest<Object>() {
819 public Object perform(WriteGraph graph)
820 throws DatabaseException {
821 SCLContext sclContext = SCLContext.getCurrent();
822 Object oldGraph = sclContext.put("graph", graph);
823 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
825 return fn.apply(parameters);
827 sclContext.put("graph", oldGraph);
828 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
834 } else if(effects.contains(Types.READ_GRAPH)) {
836 result = session.sync(new UniqueRead<Object>() {
839 public Object perform(ReadGraph graph) throws DatabaseException {
841 SCLContext sclContext = SCLContext.getCurrent();
842 Object oldGraph = sclContext.put("graph", graph);
843 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
846 return fn.apply(parameters);
848 sclContext.put("graph", oldGraph);
849 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
857 SCLContext sclContext = SCLContext.getCurrent();
858 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
860 result = fn.apply(parameters);
862 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
867 if (result instanceof org.simantics.document.server.serverResponse.Error) {
868 return (CommandResult)result;
871 if (result instanceof CommandResult) {
872 return (CommandResult)result;
874 CommandContextMutable assignments = new CommandContextImpl();
875 assignments.putValue("result", result);
876 return new ServerResponse(200, "", assignments);
879 } catch (Throwable e) {
880 return new org.simantics.document.server.serverResponse.Error(formatError(Simantics.getSession(), e));
889 public static AbstractEventHandler writeEventHandler2(ReadGraph graph, final Function fn) {
891 final Session session = graph.getSession();
893 return new AbstractEventHandler() {
896 public CommandResult handle(final CommandContext parameters) {
898 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
902 CommandResult result = session.syncRequest(new WriteResultRequest<CommandResult>() {
905 public CommandResult perform(WriteGraph graph) throws DatabaseException {
906 SCLContext sclContext = SCLContext.getCurrent();
907 Object oldGraph = sclContext.put("graph", graph);
908 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
910 Object response = fn.apply(parameters);
911 if(response instanceof CommandResult) {
912 return (CommandResult)response;
916 sclContext.put("graph", oldGraph);
917 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
926 } catch (Throwable e) {
927 Logger.defaultLogError(e);
928 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
938 public static AbstractEventHandler eventHandler2(ReadGraph graph, final Function fn) {
940 return new AbstractEventHandler() {
943 public CommandResult handle(final CommandContext parameters) {
945 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
949 SCLContext sclContext = SCLContext.getCurrent();
950 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
952 Object response = fn.apply(parameters);
953 if(response instanceof CommandResult) {
954 return (CommandResult)response;
958 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
961 } catch (Throwable e) {
962 Logger.defaultLogError(e);
963 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
972 public static AbstractEventHandler eventHandler(ReadGraph graph, final Function fn) {
974 return new AbstractEventHandler() {
977 public CommandResult handle(final CommandContext parameters) {
979 final SCLReportingHandler printer = (SCLReportingHandler)SCLContext.getCurrent().get(SCLReportingHandler.REPORTING_HANDLER);
985 SCLContext sclContext = SCLContext.getCurrent();
986 Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer);
988 Function1<String,String> pf = new FunctionImpl1<String,String>() {
990 public String apply(String key) {
991 return parameters.getString(key);
994 Object response = (String)fn.apply(pf);
995 if(response instanceof String) {
996 result = (String)response;
999 sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter);
1002 return new SuccessResponse(result);
1004 } catch (Throwable e) {
1005 Logger.defaultLogError(e);
1006 return new org.simantics.document.server.serverResponse.Error(e.getMessage());
1014 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
1015 public static Object sclValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1016 return ServerSCLValueRequest.compileAndEvaluate(graph, context);
1019 @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
1020 public static Object sclHandlerValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1021 return ServerSCLHandlerValueRequest.compileAndEvaluate(graph, context);
1024 @SCLValue(type = "ReadGraph -> Resource -> Resource -> ComponentNamingStrategy")
1025 public static ComponentNamingStrategy componentNamingStrategy(ReadGraph graph, Resource converter, Resource context) throws DatabaseException {
1026 return createComponentNamingStrategy(graph);
1029 public static CaseInsensitiveComponentFunctionNamingStrategy createComponentNamingStrategy(ReadGraph graph) throws DatabaseException {
1030 Layer0X L0X = Layer0X.getInstance(graph);
1031 @SuppressWarnings({ "unchecked", "rawtypes" })
1032 final Function dependencies = graph.syncRequest(new Adapter(L0X.DependencyResources, Function.class), TransientCacheListener.<Function>instance());
1033 @SuppressWarnings("rawtypes")
1034 Function moduleNameFunction = new FunctionImpl4<ReadGraph, Resource, String, Integer, List<Map<String, Object>>>() {
1036 public Object apply(ReadGraph p0, Resource p1, String p2) {
1037 return apply(p0, p1, p2);
1039 @SuppressWarnings("unchecked")
1041 public List<Map<String, Object>> apply(ReadGraph graph, Resource model, String search, Integer maxResults) {
1042 return (List<Map<String, Object>>)dependencies.apply(graph, model, search, maxResults);
1045 return new CaseInsensitiveComponentFunctionNamingStrategy("%s%02d", moduleNameFunction);
1048 public static IExperiment getExperiment(ReadGraph graph, Variable variable) throws DatabaseException {
1049 if (variable == null)
1052 SimulationResource SIMU = SimulationResource.getInstance(graph);
1054 Variable var = variable;
1055 Resource represents = var.getRepresents(graph);
1056 Resource activeRun = null;
1057 if (represents != null && graph.isInstanceOf(represents, SIMU.Run)) {
1058 activeRun = represents;
1061 IProject project = Simantics.peekProject();
1062 if (activeRun != null && project != null) {
1063 IExperimentManager expMan = project.getHint(IExperimentManager.KEY_EXPERIMENT_MANAGER);
1065 return expMan.getExperiment(NameUtils.getSafeName(graph, activeRun));
1071 public static CommandContextMutable putTuple(CommandContextMutable context, String key, Object tuple) {
1072 List<Object> list = new ArrayList<Object>();
1073 if (tuple instanceof Tuple) {
1074 Collections.addAll(list, ((Tuple)tuple).toArray());
1078 context.putRow(key, list);
1082 public static List<Object> getTuples(CommandContext context, String key) {
1083 List<List<Object>> rows = context.getRows(key);
1084 List<Object> tuples = new ArrayList<Object>();
1086 for (List<Object> row : rows) {
1087 switch (row.size()) {
1088 case 0: tuples.add(Tuple0.INSTANCE); break;
1089 case 1: tuples.add(row.get(1)); break;
1090 case 2: tuples.add(new Tuple2(row.get(0), row.get(1))); break;
1091 case 3: tuples.add(new Tuple3(row.get(0), row.get(1), row.get(2))); break;
1092 case 4: tuples.add(new Tuple4(row.get(0), row.get(1), row.get(2), row.get(3))); break;
1093 case 5: tuples.add(new Tuple5(row.get(0), row.get(1), row.get(2), row.get(3), row.get(4))); break;
1100 public static String printContext(CommandContext context) {
1101 return context.toString();
1104 @SCLValue(type = "AbstractEventHandler")
1105 public static AbstractEventHandler emptyEvent = new AbstractEventHandler() {
1108 public CommandResult handle(CommandContext context) {
1114 public static String sclStateKey(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
1116 String baseURI = base.getURI(graph);
1118 String selfURI = self.getURI(graph);
1120 String prefix = selfURI.substring(0, selfURI.indexOf(ProxyChildVariable.CONTEXT_BEGIN));
1121 String suffix = selfURI.substring(selfURI.lastIndexOf(ProxyChildVariable.CONTEXT_END) + ProxyChildVariable.CONTEXT_END.length());
1122 String stripped = prefix + suffix;
1124 String relative = Variables.getRelativeRVI(baseURI, stripped);
1126 return Variables.getRVI(relative, ref);
1130 public static Variable sclStateVariable(ReadGraph graph, Variable base, Variable self, String ref) throws DatabaseException {
1132 String id = sclStateKey(graph, base, self, ref);
1134 Variable sclVar = base.getPossibleChild(graph, "__scl__");
1135 if(sclVar == null) return null;
1137 return sclVar.getPossibleProperty(graph, id);
1141 public static Object sclStateValueOrDefault(ReadGraph graph, Variable base, Variable self, String ref, Object defaultValue) throws DatabaseException {
1143 Variable stateVariable = sclStateVariable(graph, base, self, ref);
1144 if (stateVariable != null) {
1146 return stateVariable.getValue(graph);
1150 String id = sclStateKey(graph, base, self, ref);
1152 SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph) + "/__scl__");
1153 realm.getConnection().setVariable(id, getSCLType(defaultValue), defaultValue);
1155 return defaultValue;
1160 public static void setSclStateValue(WriteGraph graph, Variable base, Variable self, String ref, Object value) throws DatabaseException {
1162 String id = sclStateKey(graph, base, self, ref);
1164 SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph)+"/__scl__");
1165 realm.getConnection().setVariable(id, getSCLType(value), value);
1166 realm.refreshVariablesSync();
1170 public static Object projectComponentState(ReadGraph graph, Variable self, String ref, Object defaultValue) throws DatabaseException {
1171 Resource project = Simantics.getProjectResource();
1172 Variable component = self.getParent(graph);
1173 Variable projectVariable = Variables.getVariable(graph, project);
1174 return sclStateValueOrDefault(graph, projectVariable, component, ref, defaultValue);
1177 public static void setProjectComponentState(WriteGraph graph, Variable self, String ref, Object value) throws DatabaseException {
1178 Resource project = Simantics.getProjectResource();
1179 Variable component = self.getParent(graph);
1180 Variable projectVariable = Variables.getVariable(graph, project);
1181 setSclStateValue(graph, projectVariable, component, ref, value);
1184 private static Type getSCLType(Object value) throws DatabaseException {
1185 Binding b = Bindings.getBindingUnchecked(value.getClass());
1186 Datatype t = b.type();
1187 if(Datatypes.STRING.equals(t)) return Types.STRING;
1188 if(Datatypes.DOUBLE.equals(t)) return Types.DOUBLE;
1189 throw new DatabaseException("Type not supported");
1192 public static List<Variable> documentModelContribution(ReadGraph graph, Resource doc) throws DatabaseException {
1193 Resource project = Simantics.getProjectResource();
1194 ArrayList<Variable> result = new ArrayList<Variable>();
1195 for(Resource model : graph.syncRequest(new ProjectModels(project))) {
1196 result.add(Variables.getVariable(graph, model));
1201 public static String documentModelContributionLabel(ReadGraph graph, Variable var) throws DatabaseException {
1202 return var.getName(graph);
1205 public static Object getPropertyValueCached(ReadGraph graph, Variable variable, String name, Binding binding) throws DatabaseException {
1206 Variable property = graph.syncRequest(new VariableProperty(variable, name));
1207 return graph.syncRequest(new VariableValueWithBinding<Object>(property, binding));
1210 public static class ParentExistsRequest extends VariableRead<Boolean> {
1212 public ParentExistsRequest(Variable parent) {
1217 public Boolean perform(ReadGraph graph) throws DatabaseException {
1219 Variable existsProperty = variable.getPossibleProperty(graph, "exists");
1220 if(existsProperty == null) return true;
1222 Boolean exists = existsProperty.getPossibleValue(graph, Bindings.BOOLEAN);
1223 if (exists == null || !exists) return false;
1225 return graph.syncRequest(new ParentExistsRequest(variable.getParent(graph)));
1231 public static class PathExistsRequest extends VariableRead<Boolean> {
1233 public PathExistsRequest(Variable variable) {
1238 public Boolean perform(ReadGraph graph) throws DatabaseException {
1240 Variable widget = variable.getParent(graph);
1242 Boolean exists = widget.getPossiblePropertyValue(graph, "exists");
1243 if (exists == null || !exists) return false;
1245 if (!graph.syncRequest(new ParentExistsRequest(widget.getParent(graph)))) return false;
1247 DocumentationResource DOC = DocumentationResource.getInstance(graph);
1248 Collection <Variable> cps = widget.getProperties(graph, DocumentationResource.URIs.Relations_parentRelation);
1249 for (Variable cp : cps) {
1251 Connection conn = cp.getValue(graph);
1252 Variable otherCp = DocumentServerUtils.getPossibleChildConnectionPoint(graph, cp, conn);
1253 if (otherCp != null) {
1254 Variable parentWidget = otherCp.getParent(graph);
1255 if (parentWidget.<Boolean>getPropertyValue(graph, "pathExists")) {
1259 Variable parentCp = graph.sync(new UnaryRead<Connection, Variable>(conn) {
1261 public Variable perform(ReadGraph graph) throws DatabaseException {
1262 DocumentationResource DOC = DocumentationResource.getInstance(graph);
1263 Collection<VariableConnectionPointDescriptor> descs = parameter.getConnectionPointDescriptors(graph, null);
1265 for(VariableConnectionPointDescriptor desc : descs) {
1266 if (DOC.Relations_partN.equals(desc.getConnectionPointResource(graph))) {
1267 return desc.getVariable(graph);
1273 if (parentCp != null) {
1274 Variable parentWidget = parentCp.getParent(graph);
1275 if (parentWidget.<Boolean>getPropertyValue(graph, "pathExists")) {
1282 Resource type = widget.getType(graph);
1283 return graph.isInheritedFrom(type, DOC.Components_ParentlessComponent) ||
1284 (graph.isInheritedFrom(type, DOC.DocumentComponent) && cps.isEmpty());
1288 @SCLValue(type = "ReadGraph -> Resource -> Variable -> Boolean")
1289 public static boolean pathExists(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
1290 return graph.syncRequest(new PathExistsRequest(context));
1293 public static String compileDocumentSCLValueExpression(ReadGraph graph, Variable context) {
1295 ServerSCLValueRequest.validate(graph, context);
1297 } catch (Exception e) {
1298 return resolveIssueMessage(e);
1302 private static String resolveIssueMessage(Exception e) {
1303 if (e instanceof ImportFailureException)
1305 if (e.getCause() != null && e.getCause() instanceof ImportFailureException)
1307 if (e instanceof SCLDatabaseException) {
1308 SCLDatabaseException ee = (SCLDatabaseException) e;
1309 return ee.getMessage();
1311 if (LOGGER.isDebugEnabled())
1312 LOGGER.debug("", e);
1313 return e.getMessage();