X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.document.server%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fserver%2FFunctions.java;h=8dea97ad118da8235b185d56b0e0ce539d9d7f0a;hp=8b68cc409cde83d0442ae081163c15e3dcf0071c;hb=HEAD;hpb=596709570dae626a884ddfe0341af98c80241e82 diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java index 8b68cc409..8dea97ad1 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/Functions.java @@ -8,6 +8,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.TreeMap; import org.simantics.Simantics; @@ -24,13 +25,16 @@ import org.simantics.db.Session; import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.common.primitiverequest.Adapter; +import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener; import org.simantics.db.common.procedure.adapter.TransientCacheListener; -import org.simantics.db.common.request.UnaryRead; +import org.simantics.db.common.request.BinaryRead; +import org.simantics.db.common.request.ResourceAsyncRead; import org.simantics.db.common.request.UniqueRead; import org.simantics.db.common.request.WriteResultRequest; import org.simantics.db.common.utils.Logger; import org.simantics.db.common.utils.NameUtils; import org.simantics.db.exception.DatabaseException; +import org.simantics.db.exception.ServiceException; import org.simantics.db.layer0.function.All; import org.simantics.db.layer0.request.ProjectModels; import org.simantics.db.layer0.request.PropertyInfo; @@ -38,12 +42,13 @@ import org.simantics.db.layer0.request.PropertyInfoRequest; import org.simantics.db.layer0.request.VariableProperty; import org.simantics.db.layer0.request.VariableRead; import org.simantics.db.layer0.request.VariableValueWithBinding; +import org.simantics.db.layer0.scl.SCLDatabaseException; import org.simantics.db.layer0.variable.ConstantPropertyVariable; import org.simantics.db.layer0.variable.ProxyChildVariable; import org.simantics.db.layer0.variable.ProxySessionRequest; +import org.simantics.db.layer0.variable.ProxyVariableSupport; import org.simantics.db.layer0.variable.ProxyVariables; import org.simantics.db.layer0.variable.StandardAssertedGraphPropertyVariable; -import org.simantics.db.layer0.variable.StandardGraphChildVariable; import org.simantics.db.layer0.variable.StandardGraphPropertyVariable; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.VariableMap; @@ -62,17 +67,20 @@ import org.simantics.document.server.io.CommandContextImpl; import org.simantics.document.server.io.CommandContextMutable; import org.simantics.document.server.io.CommandResult; import org.simantics.document.server.io.IConsole; +import org.simantics.document.server.request.NodeRequest; import org.simantics.document.server.request.ServerSCLHandlerValueRequest; import org.simantics.document.server.request.ServerSCLValueRequest; import org.simantics.document.server.serverResponse.ServerResponse; import org.simantics.document.server.serverResponse.SuccessResponse; +import org.simantics.document.server.state.StateNodeManager; +import org.simantics.document.server.state.StateRealm; +import org.simantics.document.server.state.StateSessionManager; import org.simantics.modeling.ModelingResources; -import org.simantics.modeling.scl.SCLRealm; -import org.simantics.modeling.scl.SCLSessionManager; import org.simantics.modeling.services.CaseInsensitiveComponentFunctionNamingStrategy; import org.simantics.modeling.services.ComponentNamingStrategy; import org.simantics.operation.Layer0X; import org.simantics.project.IProject; +import org.simantics.scl.compiler.module.repository.ImportFailureException; import org.simantics.scl.compiler.types.TCon; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; @@ -95,29 +103,45 @@ import org.simantics.simulation.project.IExperimentManager; import org.simantics.structural2.variables.Connection; import org.simantics.structural2.variables.StandardProceduralChildVariable; import org.simantics.structural2.variables.VariableConnectionPointDescriptor; +import org.slf4j.LoggerFactory; import gnu.trove.map.hash.THashMap; public class Functions { - private static class PrimitivePropertyStatementsProcedure implements AsyncProcedure { - - public DirectStatements result; - - @Override - public void execute(AsyncReadGraph graph, DirectStatements result) { - this.result = result; - } - - @Override - public void exception(AsyncReadGraph graph, Throwable throwable) { - } - - } - + private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Functions.class); + @SCLValue(type = "VariableMap") public static VariableMap primitiveProperties = new VariableMapImpl() { + private void storePropertyValueAndExceptions(ReadGraph graph, Variable parent, String name, Variable property, Map map) { + try { + Object value = property.getValue(graph); + map.put(name, new ConstantPropertyVariable(parent, name, value, null)); + } catch (DatabaseException e) { + Variable propertyExceptions = map.get(NodeRequest.PROPERTY_VALUE_EXCEPTIONS); + Map exceptionMap; + if (propertyExceptions == null) { + exceptionMap = new TreeMap(); + propertyExceptions = new ConstantPropertyVariable(parent, NodeRequest.PROPERTY_VALUE_EXCEPTIONS, exceptionMap, null); + map.put(NodeRequest.PROPERTY_VALUE_EXCEPTIONS, propertyExceptions); + } else { + try { + exceptionMap = propertyExceptions.getValue(graph); + } catch (DatabaseException e1) { + Logger.defaultLogError(e1); + return; + } + } + String label = name; + try { + label = property.getLabel(graph); + } catch (DatabaseException e2) { + } + exceptionMap.put(label, e); + } + } + @Override public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException { return All.getStandardPropertyDomainPropertyVariableFromValue(graph, context, name); @@ -126,6 +150,8 @@ public class Functions { @Override public Map getVariables(final ReadGraph graph, Variable context, Map map) throws DatabaseException { + if(map == null) map = new HashMap(); + Variable parent = context.getParent(graph); DocumentationResource DOC = DocumentationResource.getInstance(graph); @@ -136,59 +162,175 @@ public class Functions { for(Variable property : procedural.getProperties(graph/*, DocumentationResource.URIs.Document_AttributeRelation*/)) { if(property instanceof StandardAssertedGraphPropertyVariable) { StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property; - if("datadefinitions".equals(ass.property.name) || "commands".equals(ass.property.name)) { - Object value = property.getPossibleValue(graph); - if(value != null) map.put(ass.property.name, new ConstantPropertyVariable(parent, ass.property.name, value, null)); + if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name) || "pollingFunction".equals(ass.property.name)) { + storePropertyValueAndExceptions(graph, parent, ass.property.name, property, map); + continue; } - continue; } Resource predicate = property.getPossiblePredicateResource(graph); if(predicate != null) { PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate)); if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) { - Variable prop = parent.getProperty(graph, predicate); - Object value = prop.getValue(graph); - if(map == null) map = new HashMap(); - map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null)); + Variable prop = parent.getProperty(graph, predicate); + storePropertyValueAndExceptions(graph, parent, info.name, prop, map); } } } } else { + Resource parentRes = parent.getRepresents(graph); + { + Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands); + storePropertyValueAndExceptions(graph, parent, "commands", prop, map); + } + + if (graph.getPossibleObject(parentRes, DOC.Properties_dataDefinitions) != null) { + Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions); + storePropertyValueAndExceptions(graph, parent, "dataDefinitions", prop, map); + } + DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class); - PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure(); + //PrimitivePropertyStatementsProcedure foo = new PrimitivePropertyStatementsProcedure(); + + DirectStatements ds = dqs.getDirectPersistentStatements(graph, parentRes); - dqs.forEachDirectPersistentStatement(graph, parent.getRepresents(graph), foo); - - for(Statement stm : foo.result) { + for(Statement stm : ds) { Resource predicate = stm.getPredicate(); PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate)); + if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) { - if(map == null) map = new HashMap(); - Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate); - Object value = prop.getValue(graph); - map.put(info.name, new ConstantPropertyVariable(parent, info.name, value, null)); + Variable prop = new StandardGraphPropertyVariable(graph, parent, predicate); + storePropertyValueAndExceptions(graph, parent, info.name, prop, map); + } else { + Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation); + if(definition != null) { + PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition)); + Variable prop = new StandardGraphPropertyVariable(graph, parent, definition); + map.put(info2.name, new PrimitiveValueVariable(parent, info2.name, prop)); + } } } + } + return map; + + } + + }; + + static class DocumentPropertyKeys extends ResourceAsyncRead> { + + protected DocumentPropertyKeys(Resource resource) { + super(resource); + } + + @Override + public void perform(AsyncReadGraph graph, final AsyncProcedure> procedure) { + + final List result = new ArrayList<>(); - Variable prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_dataDefinitions); - Object value = prop.getPossibleValue(graph); - if(value != null) map.put("dataDefinitions", new ConstantPropertyVariable(parent, "dataDefinitions", value, null)); - prop = new StandardGraphPropertyVariable(graph, parent, DOC.Properties_commands); - value = prop.getPossibleValue(graph); - if(value != null) map.put("commands", new ConstantPropertyVariable(parent, "commands", value, null)); + DocumentationResource DOC = DocumentationResource.getInstance(graph); + try { + if(graph.hasStatement(resource, DOC.Properties_commands)) + result.add("commands"); + if(graph.hasStatement(resource, DOC.Properties_dataDefinitions)) + result.add("dataDefinitions"); + } catch(ServiceException e) { + LOGGER.info(e.getMessage(), e); } + + graph.forEachDirectPredicate(resource, new AsyncProcedure>() { - if(map == null) return Collections.emptyMap(); + @Override + public void execute(AsyncReadGraph graph, Set predicates) { + + for(Resource predicate : predicates) { - return map; + try { + + PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate)); + + if(info.isHasProperty && info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) { + result.add(info.name); + } else { + Resource definition = graph.getPossibleObject(predicate, DOC.Document_definesAttributeRelation); + if(definition != null) { + PropertyInfo info2 = graph.syncRequest(new PropertyInfoRequest(definition)); + result.add(info2.name); + } + } + + } catch (DatabaseException e) { + LOGGER.info(e.getMessage(), e); + } + + } + + procedure.execute(graph, result); + + } + + @Override + public void exception(AsyncReadGraph graph, Throwable throwable) { + LOGGER.info(throwable.getMessage(), throwable); + procedure.exception(graph, throwable); + } + + }); } - }; + } + + static class StandardDocumentProperties implements DocumentProperties { + + @Override + public Collection getKeys(ReadGraph graph, Variable parent) throws DatabaseException { + + if(parent instanceof StandardProceduralChildVariable) { + StandardProceduralChildVariable procedural = (StandardProceduralChildVariable)parent; + List result = new ArrayList<>(); + for(Variable property : procedural.getProperties(graph)) { + if(property instanceof StandardAssertedGraphPropertyVariable) { + StandardAssertedGraphPropertyVariable ass = (StandardAssertedGraphPropertyVariable)property; + if("dataDefinitions".equals(ass.property.name) || "commands".equals(ass.property.name) || "pollingFunction".equals(ass.property.name)) { + result.add(ass.property.name); + continue; + } + } + Resource predicate = property.getPossiblePredicateResource(graph); + if(predicate != null) { + PropertyInfo info = graph.syncRequest(new PropertyInfoRequest(predicate)); + if(info.hasClassification(DocumentationResource.URIs.Document_AttributeRelation)) { + result.add(info.name); + } + } + } + + return result; + + } else { + + Resource parentRes = parent.getRepresents(graph); + return graph.syncRequest(new DocumentPropertyKeys(parentRes), TransientCacheAsyncListener.instance()); + + } + + } + + @Override + public Object getValue(ReadGraph graph, Variable context, String key) throws DatabaseException { + return context.getPropertyValue(graph, key); + } + + } + + public static DocumentProperties primitiveProperties() throws DatabaseException { + return new StandardDocumentProperties(); + } + @SCLValue(type = "VariableMap") public static VariableMap inputSpaceChildren = new VariableMapImpl() { @@ -217,7 +359,7 @@ public class Functions { }; - static class DocumentProxyChildVariable extends ProxyChildVariable { + static class DocumentProxyChildVariable extends ProxyChildVariable implements ProxyVariableSupport { public DocumentProxyChildVariable(Variable base, Variable parent, Variable other, String name) { super(base, parent, other, name); @@ -234,42 +376,26 @@ public class Functions { if(other instanceof ProxyChildVariable) { // The context is also a proxy - let it do the job return super.getPossibleChild(graph, name); - } else { - return new RootVariable(this, base.getRepresents(graph)); - } - } - - return super.getPossibleChild(graph, name); - - } - - public Collection getChildren(ReadGraph graph) throws DatabaseException { - - Collection result = super.getChildren(graph); - if(!(base instanceof ProxyChildVariable)) { - result.add(new RootVariable(this, base.getRepresents(graph))); + } else { + return ProxyVariables.tryToOwnRenamed(graph, this, base, CONTEXT_END); + } } - return result; - - } - - } - static class RootVariable extends StandardGraphChildVariable { + return super.getPossibleChild(graph, name); - public RootVariable(DocumentProxyChildVariable parent, Resource resource) { - super(parent, null, resource); } @Override - public String getName(ReadGraph graph) throws DatabaseException { - return ProxyChildVariable.CONTEXT_END; + public Variable attachTo(ReadGraph graph, Variable parent) { + return attachToRenamed(graph, parent, name); } - - @SuppressWarnings("deprecation") + @Override - public Variable getNameVariable(ReadGraph graph) throws DatabaseException { - return new ConstantPropertyVariable(this, Variables.NAME, ProxyChildVariable.CONTEXT_END, Bindings.STRING); + public Variable attachToRenamed(ReadGraph graph, Variable parent, String name) { + if(this.parent.equals(base)) + return new DocumentProxyChildVariable(parent, parent, other, name); + else + return new DocumentProxyChildVariable(base, parent, other, name); } } @@ -544,6 +670,7 @@ public class Functions { } } + @Deprecated public static AbstractEventHandler emptyOnClick(ReadGraph graph) throws DatabaseException { return new EventHandler() { @Override @@ -553,6 +680,7 @@ public class Functions { }; } + @Deprecated public static AbstractEventHandler writeEventHandler(ReadGraph graph, final Variable variable, final Function fn) { final Session session = graph.getSession(); @@ -585,15 +713,11 @@ public class Functions { return (String)response; } return null; - } catch (Throwable t) { - t.printStackTrace(); } finally { sclContext.put("graph", oldGraph); sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); } - return null; - } }); @@ -610,6 +734,7 @@ public class Functions { }; } + @Deprecated public static AbstractEventHandler readEventHandler(ReadGraph graph, final Variable variable, final Function fn) { final Session session = graph.getSession(); @@ -642,15 +767,11 @@ public class Functions { return (String)response; } return null; - } catch (Throwable t) { - t.printStackTrace(); } finally { sclContext.put("graph", oldGraph); sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); } - return null; - } }); @@ -667,6 +788,7 @@ public class Functions { }; } + @Deprecated public static AbstractEventHandler readEventHandler2(ReadGraph graph, final Function fn) { final Session session = graph.getSession(); @@ -693,15 +815,11 @@ public class Functions { return (CommandResult)response; } return null; - } catch (Throwable t) { - t.printStackTrace(); } finally { sclContext.put("graph", oldGraph); sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); } - return null; - } }); @@ -725,12 +843,12 @@ public class Functions { final List effects = ServerSCLHandlerValueRequest.getEffects(graph, anyFunction); - final Function1 fn = anyFunction.getValue(graph); - String expression = anyFunction.getPropertyValue(graph, "expression"); + Function1 fn = anyFunction.getValue(graph); + //String expression = anyFunction.getPropertyValue(graph, "expression"); final Session session = graph.getSession(); - return new AbstractResponseHandler(expression) { + return new AbstractResponseHandler(fn) { private String formatError(RequestProcessor proc, Throwable t) { @@ -791,10 +909,7 @@ public class Functions { Object oldGraph = sclContext.put("graph", graph); Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer); try { - Object response = fn.apply(parameters); - return response; - } catch (Throwable t) { - return new org.simantics.document.server.serverResponse.Error(formatError(graph, t)); + return fn.apply(parameters); } finally { sclContext.put("graph", oldGraph); sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); @@ -815,10 +930,7 @@ public class Functions { Object oldPrinter = sclContext.put(SCLReportingHandler.REPORTING_HANDLER, printer); try { - Object response = fn.apply(parameters); - return response; - } catch (Throwable t) { - return new org.simantics.document.server.serverResponse.Error(formatError(graph, t)); + return fn.apply(parameters); } finally { sclContext.put("graph", oldGraph); sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); @@ -860,6 +972,7 @@ public class Functions { }; } + @Deprecated public static AbstractEventHandler writeEventHandler2(ReadGraph graph, final Function fn) { final Session session = graph.getSession(); @@ -886,15 +999,11 @@ public class Functions { return (CommandResult)response; } return null; - } catch (Throwable t) { - t.printStackTrace(); } finally { sclContext.put("graph", oldGraph); sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); } - return null; - } }); @@ -911,6 +1020,8 @@ public class Functions { }; } + + @Deprecated public static AbstractEventHandler eventHandler2(ReadGraph graph, final Function fn) { return new AbstractEventHandler() { @@ -930,14 +1041,10 @@ public class Functions { return (CommandResult)response; } return null; - } catch (Throwable t) { - t.printStackTrace(); } finally { sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); } - return null; - } catch (Throwable e) { Logger.defaultLogError(e); return new org.simantics.document.server.serverResponse.Error(e.getMessage()); @@ -948,6 +1055,7 @@ public class Functions { }; } + @Deprecated public static AbstractEventHandler eventHandler(ReadGraph graph, final Function fn) { return new AbstractEventHandler() { @@ -974,8 +1082,6 @@ public class Functions { if(response instanceof String) { result = (String)response; } - } catch (Throwable t) { - t.printStackTrace(); } finally { sclContext.put(SCLReportingHandler.REPORTING_HANDLER, oldPrinter); } @@ -1128,11 +1234,6 @@ public class Functions { } else { - String id = sclStateKey(graph, base, self, ref); - - SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph) + "/__scl__"); - realm.getConnection().setVariable(id, getSCLType(defaultValue), defaultValue); - return defaultValue; } @@ -1142,9 +1243,9 @@ public class Functions { String id = sclStateKey(graph, base, self, ref); - SCLRealm realm = SCLSessionManager.getOrCreateSCLRealm(base.getURI(graph)+"/__scl__"); - realm.getConnection().setVariable(id, getSCLType(value), value); - realm.refreshVariablesSync(); + StateRealm realm = (StateRealm) StateSessionManager.getInstance().getOrCreateRealm(graph, base.getURI(graph)+"/__scl__"); + StateNodeManager nodeManager = (StateNodeManager) realm.getNodeManager(); + nodeManager.setState(id, value); } @@ -1237,11 +1338,11 @@ public class Functions { return true; } } else { - Variable parentCp = graph.sync(new UnaryRead(conn) { + Variable parentCp = graph.sync(new BinaryRead(widget, conn) { @Override public Variable perform(ReadGraph graph) throws DatabaseException { DocumentationResource DOC = DocumentationResource.getInstance(graph); - Collection descs = parameter.getConnectionPointDescriptors(graph, null); + Collection descs = parameter2.getConnection2().getConnectionPointDescriptors(graph, parameter, null); for(VariableConnectionPointDescriptor desc : descs) { if (DOC.Relations_partN.equals(desc.getConnectionPointResource(graph))) { @@ -1271,4 +1372,36 @@ public class Functions { return graph.syncRequest(new PathExistsRequest(context)); } + public static String compileDocumentSCLValueExpression(ReadGraph graph, Variable context) { + try { + ServerSCLValueRequest.validate(graph, context); + return ""; + } catch (Exception e) { + return resolveIssueMessage(e); + } + } + + public static String compileDocumentSCLHandlerValueExpression(ReadGraph graph, Variable context) { + try { + ServerSCLHandlerValueRequest.compile(graph, context); + return ""; + } catch (Exception e) { + return resolveIssueMessage(e); + } + } + + private static String resolveIssueMessage(Exception e) { + if (e instanceof ImportFailureException) + return ""; + if (e.getCause() != null && e.getCause() instanceof ImportFailureException) + return ""; + if (e instanceof SCLDatabaseException) { + SCLDatabaseException ee = (SCLDatabaseException) e; + return ee.getMessage(); + } + if (LOGGER.isDebugEnabled()) + LOGGER.debug("", e); + return e.getMessage(); + } + } \ No newline at end of file