From: Antti Villberg Date: Tue, 13 Nov 2018 10:42:49 +0000 (+0200) Subject: Variable optimizations for documents (Simupedia) X-Git-Tag: v1.43.0~136^2~274 X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=commitdiff_plain;h=6ff5e65793559d92443fcd1eb7e8d12a3bed9e9e Variable optimizations for documents (Simupedia) * Documentation changes based on previous new features gitlab #169 Change-Id: Ief514661ccc3dd16eb10c17ed660dd1976504b3d --- diff --git a/bundles/org.simantics.document.base.ontology/graph/Components.pgraph b/bundles/org.simantics.document.base.ontology/graph/Components.pgraph index 099755380..65a75d288 100644 --- a/bundles/org.simantics.document.base.ontology/graph/Components.pgraph +++ b/bundles/org.simantics.document.base.ontology/graph/Components.pgraph @@ -67,9 +67,7 @@ COMPONENTS.Component "Variable" PROPERTIES.primitiveProperties "String" + ==> "DocumentProperties" PROPERTIES.exists @defAttribute L0.Value diff --git a/bundles/org.simantics.document.server/scl/Document/All.scl b/bundles/org.simantics.document.server/scl/Document/All.scl index 4db639f5b..6eaf328a3 100644 --- a/bundles/org.simantics.document.server/scl/Document/All.scl +++ b/bundles/org.simantics.document.server/scl/Document/All.scl @@ -2,6 +2,9 @@ import "Simantics/DB" import "Simantics/Variables" import "JavaBuiltin" as Java +importJava "org.simantics.document.server.DocumentProperties" where + data DocumentProperties + importJava "org.simantics.document.server.io.IConsole" where data IConsole addMessage :: IConsole -> String -> () @@ -110,6 +113,9 @@ importJava "org.simantics.document.server.Functions" where dataDefinitions :: Variable -> [DataDefinition] + @JavaName "dataDefinitions" + computeDataDefinitions :: Variable -> [DataDefinition] + emptyOnClick :: () -> AbstractEventHandler writeEventHandler :: Variable -> (Variable -> (String -> Maybe String) -> String) -> AbstractEventHandler @@ -138,6 +144,8 @@ importJava "org.simantics.document.server.Functions" where stateVariable :: Variable -> Variable compileDocumentSCLValueExpression :: Variable -> String + + primitiveProperties :: DocumentProperties propertyValueCached :: Serializable a => Typeable a => Variable -> String -> a propertyValueCached var prop = propertyValueCached_ var prop binding diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentProperties.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentProperties.java new file mode 100644 index 000000000..72d475a4f --- /dev/null +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentProperties.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2018 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.document.server; + +import java.util.Collection; + +import org.simantics.db.ReadGraph; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.layer0.variable.Variable; + +/** + * This interface abstracts the computation of document component properties. + * The key,value -pairs returned by this interface are used to build the + * org.simantics.document.server.JSONObject values used in the document + * scene graph. + * + * @author Antti Villberg + * @since 1.36.0 + */ +public interface DocumentProperties { + + /* + * Returns the set of attribute keys for the given document component. + * Does not return null. + */ + Collection getKeys(ReadGraph graph, Variable component) throws DatabaseException; + /* + * Returns a value for a key in the key set. + * Null values are allowed and normal DatabaseException + * are thrown when evaluating the value. + */ + Object getValue(ReadGraph graph, Variable component, String key) throws DatabaseException; + +} diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java index 5e1f5cc30..ec65027ec 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/DocumentServerUtils.java @@ -19,12 +19,10 @@ import org.simantics.db.layer0.variable.ProxyChildVariable; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.Variables; import org.simantics.document.base.ontology.DocumentationResource; -import org.simantics.document.server.Functions.RootVariable; import org.simantics.document.server.request.NodeRequest; import org.simantics.document.server.request.NodeRequestUtils; import org.simantics.structural2.variables.Connection; import org.simantics.structural2.variables.VariableConnectionPointDescriptor; -import org.simantics.utils.datastructures.Pair; import org.simantics.utils.strings.AlphanumComparator; public class DocumentServerUtils { @@ -127,7 +125,7 @@ public class DocumentServerUtils { public static Variable getPossibleOtherConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException { - Collection descs = conn.getConnectionPointDescriptors(graph, null); + Collection descs = conn.getConnection2().getConnectionPointDescriptors(graph, connectionPoint.getParent(graph), null); if(descs.size() != 2) return null; for(VariableConnectionPointDescriptor desc : descs) { @@ -141,7 +139,7 @@ public class DocumentServerUtils { public static Variable getPossibleChildConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException { - Collection descs = conn.getConnectionPointDescriptors(graph, null); + Collection descs = conn.getConnection2().getConnectionPointDescriptors(graph, connectionPoint.getParent(graph), null); if(descs.size() != 2) return null; DocumentationResource DOC = DocumentationResource.getInstance(graph); @@ -174,7 +172,7 @@ public class DocumentServerUtils { public static Variable getPossibleCommandTriggerConnectionPoint(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException { - Collection cpts = conn.getConnectionPoints(graph, null); + Collection cpts = conn.getConnection2().getConnectionPoints(graph, connectionPoint.getParent(graph), null); Variable result = null; @@ -192,7 +190,7 @@ public class DocumentServerUtils { public static Collection getPossibleOtherConnectionPoints(ReadGraph graph, Variable connectionPoint, Connection conn) throws DatabaseException { - Collection cpts = conn.getConnectionPoints(graph, null); + Collection cpts = conn.getConnection2().getConnectionPoints(graph, connectionPoint.getParent(graph), null); if(cpts.size() < 2) return Collections.emptyList(); @@ -206,12 +204,17 @@ public class DocumentServerUtils { public static String getId(ReadGraph graph, Variable node) throws DatabaseException { - if(node == null) return "root"; - else { - String uri = node.getURI(graph); - int l = uri.lastIndexOf(ProxyChildVariable.CONTEXT_END); - return uri.substring(l+4); - } + if(node == null) return "root"; + else { + String name = node.getName(graph); + if(ProxyChildVariable.CONTEXT_END.equals(name)) return ""; + else { + String parentId = getId(graph, node.getParent(graph)); + if(parentId.isEmpty()) return name; + else return parentId + "/" + name; + } + + } } @@ -293,82 +296,67 @@ public class DocumentServerUtils { public static Collection getAttributes(ReadGraph graph, DocumentationResource DOC, Variable variable) throws DatabaseException { return variable.getProperties(graph, DOC.Document_AttributeRelation); } - - public static class AttributesRequest extends VariableRead>> { + + public static class AttributesRequest extends VariableRead { public AttributesRequest(Variable variable) { super(variable); } @Override - public Pair> perform(ReadGraph graph) throws DatabaseException { - ArrayList statics = new ArrayList(); - DocumentationResource DOC = DocumentationResource.getInstance(graph); - - Variable primitives = variable.getProperty(graph, DOC.Properties_primitiveProperties); - for(Variable property : primitives.getProperties(graph)) { - statics.add(property); - // NO SUPPORT FOR DYNAMICS AT THIS STAGE - } - - JSONObject staticContent = computeStatic(graph, variable, statics); - - return new Pair>(staticContent, Collections.emptyList()); - + public JSONObject perform(ReadGraph graph) throws DatabaseException { + DocumentationResource DOC = DocumentationResource.getInstance(graph); + DocumentProperties properties = variable.getPropertyValue(graph, DOC.Properties_primitiveProperties); + return computeStatic(graph, variable, properties); } - - JSONObject computeStatic(ReadGraph graph, Variable variable, ArrayList statics) throws DatabaseException { - + + JSONObject computeStatic(ReadGraph graph, Variable variable, DocumentProperties statics) throws DatabaseException { + JSONObject base = graph.syncRequest(new org.simantics.document.server.request.DefaultFields(variable)); JSONObject object = base.clone(); - - for(Variable attrib : statics) { - String name = attrib.getName(graph); + + for(String name : statics.getKeys(graph, variable)) { try { if (name.equals(NodeRequest.PROPERTY_VALUE_EXCEPTIONS)) { - @SuppressWarnings("unchecked") - Map exceptions = (Map)DocumentServerUtils.getValue(graph, attrib); - - List errorList = object.getJSONField(NodeRequest.ERRORS); - if(errorList == null) - errorList = new ArrayList(); - - for (Map.Entry entry : exceptions.entrySet()) { - String errorMessage = NodeRequestUtils.formatErrorMessage(entry.getKey(), entry.getValue()); - errorList.add(errorMessage); - } - object.addJSONField(NodeRequest.ERRORS, errorList); - - } else { - Object value = DocumentServerUtils.getValue(graph, attrib); - object.addJSONField(name, value); - } + @SuppressWarnings("unchecked") + Map exceptions = (Map)statics.getValue(graph, variable, name); + + List errorList = object.getJSONField(NodeRequest.ERRORS); + if(errorList == null) + errorList = new ArrayList<>(); + + for (Map.Entry entry : exceptions.entrySet()) { + String errorMessage = NodeRequestUtils.formatErrorMessage(entry.getKey(), entry.getValue()); + errorList.add(errorMessage); + } + object.addJSONField(NodeRequest.ERRORS, errorList); + } else { + object.addJSONField(name, statics.getValue(graph, variable, name)); + } } catch (Throwable t) { - List errorList = object.getJSONField(NodeRequest.ERRORS); - if(errorList == null) - errorList = new ArrayList(); - - String errorMessage = NodeRequestUtils.formatErrorMessage(name, t); - - errorList.add(errorMessage); - object.addJSONField(NodeRequest.ERRORS, errorList); + List errorList = object.getJSONField(NodeRequest.ERRORS); + if(errorList == null) + errorList = new ArrayList<>(1); + + String errorMessage = NodeRequestUtils.formatErrorMessage(name, t); + errorList.add(errorMessage); + object.addJSONField(NodeRequest.ERRORS, errorList); } } return object; - + } - + } public static Collection getDynamicAttributes(ReadGraph graph, final DocumentationResource DOC, Variable variable) throws DatabaseException { - Pair> attribs = graph.syncRequest(new AttributesRequest(variable)); - return attribs.second; + return Collections.emptyList(); } - + public static Variable getPossibleDocumentRootVariable(ReadGraph graph, Variable documentPart) throws DatabaseException { - if(documentPart instanceof RootVariable) return documentPart; + if(ProxyChildVariable.CONTEXT_END.equals(documentPart.getName(graph))) return documentPart; Variable parent = documentPart.getParent(graph); if(parent == null) return null; return getPossibleDocumentRootVariable(graph, parent); 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 aabe7b29f..6ec2e6734 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 @@ -15,7 +15,6 @@ import org.simantics.databoard.Bindings; import org.simantics.databoard.Datatypes; import org.simantics.databoard.binding.Binding; import org.simantics.databoard.type.Datatype; -import org.simantics.db.AsyncReadGraph; import org.simantics.db.DirectStatements; import org.simantics.db.ReadGraph; import org.simantics.db.RequestProcessor; @@ -25,7 +24,7 @@ import org.simantics.db.Statement; import org.simantics.db.WriteGraph; import org.simantics.db.common.primitiverequest.Adapter; 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.UniqueRead; import org.simantics.db.common.request.WriteResultRequest; import org.simantics.db.common.utils.Logger; @@ -42,15 +41,14 @@ 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; import org.simantics.db.layer0.variable.VariableMapImpl; import org.simantics.db.layer0.variable.Variables; -import org.simantics.db.procedure.AsyncProcedure; import org.simantics.db.service.DirectQuerySupport; import org.simantics.document.base.ontology.DocumentationResource; import org.simantics.document.server.bean.Command; @@ -107,21 +105,6 @@ public class Functions { private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Functions.class); - 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) { - } - - } - @SCLValue(type = "VariableMap") public static VariableMap primitiveProperties = new VariableMapImpl() { private void storePropertyValueAndExceptions(ReadGraph graph, Variable parent, String name, Variable property, Map map) { @@ -229,6 +212,34 @@ public class Functions { }; + static class StandardDocumentProperties implements DocumentProperties { + + @Override + public Collection getKeys(ReadGraph graph, Variable context) throws DatabaseException { + + DocumentationResource DOC = DocumentationResource.getInstance(graph); + StandardGraphPropertyVariable asd = new StandardGraphPropertyVariable(graph, context, DOC.Properties_primitiveProperties); + Map ps = primitiveProperties.getVariables(graph, asd, null); + return ps.keySet(); + + } + + @Override + public Object getValue(ReadGraph graph, Variable context, String key) throws DatabaseException { + + DocumentationResource DOC = DocumentationResource.getInstance(graph); + StandardGraphPropertyVariable asd = new StandardGraphPropertyVariable(graph, context, DOC.Properties_primitiveProperties); + Map ps = primitiveProperties.getVariables(graph, asd, null); + return ps.get(key).getValue(graph); + + } + + } + + public static DocumentProperties primitiveProperties() throws DatabaseException { + return new StandardDocumentProperties(); + } + @SCLValue(type = "VariableMap") public static VariableMap inputSpaceChildren = new VariableMapImpl() { @@ -257,7 +268,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); @@ -274,42 +285,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); } } @@ -1252,11 +1247,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))) { diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java index d9418db99..8d96c4798 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequest.java @@ -1,77 +1,35 @@ package org.simantics.document.server.request; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; - import org.simantics.db.ReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.request.VariableRead; import org.simantics.db.layer0.variable.Variable; import org.simantics.document.server.DocumentServerUtils.AttributesRequest; import org.simantics.document.server.JSONObject; -import org.simantics.utils.datastructures.Pair; public class NodeRequest extends VariableRead { public static final String ERRORS = "Errors"; public static final String PROPERTY_VALUE_EXCEPTIONS = "_PropertyValueExceptions"; - - public NodeRequest(Variable node) { - super(node); - } - - @SuppressWarnings("unchecked") - @Override - public JSONObject perform(ReadGraph graph) throws DatabaseException { - - long s = System.nanoTime(); - - Pair> attribs = graph.syncRequest(new AttributesRequest(variable)); - - JSONObject staticContent = attribs.first; - if(attribs.second.isEmpty()) { - - if(DocumentRequest.PROFILE) { - long dura = System.nanoTime()-s; - if(dura > DocumentRequest.PROFILE_THRESHOLD_NODEREQUEST * 1e3) { - System.err.println("NodeRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph)); - } - } - return staticContent; - } - - Map dynamicContent = graph.syncRequest(new NodeRequestDynamic(variable)); - if(dynamicContent.isEmpty()) { - return staticContent; - } - - JSONObject result = staticContent.clone(); - result.add(dynamicContent); - - if(dynamicContent.containsKey(ERRORS) || staticContent.getJSONField(ERRORS) != null) { - ArrayList errorList = new ArrayList<>(); - - if(dynamicContent.containsKey(ERRORS)) - errorList.addAll((List)dynamicContent.get(ERRORS)); + public NodeRequest(Variable node) { + super(node); + } - if(staticContent.getJSONField(ERRORS) != null) - errorList.addAll((List)staticContent.getJSONField(ERRORS)); + @Override + public JSONObject perform(ReadGraph graph) throws DatabaseException { + long s = DocumentRequest.PROFILE ? System.nanoTime() : 0L; - result.addJSONField(ERRORS, errorList); - } + JSONObject staticContent = graph.syncRequest(new AttributesRequest(variable)); - if(DocumentRequest.PROFILE) { + if (DocumentRequest.PROFILE) { long dura = System.nanoTime()-s; - if(dura > DocumentRequest.PROFILE_THRESHOLD_NODEREQUEST * 1e3) { + if (dura > DocumentRequest.PROFILE_THRESHOLD_NODEREQUEST * 1e3) { System.err.println("NodeRequest " + System.identityHashCode(this) + " in " + 1e-6*dura + "ms. " + variable.getURI(graph)); } } - - return result; + return staticContent; } -} \ No newline at end of file +}