X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.document.server%2Fsrc%2Forg%2Fsimantics%2Fdocument%2Fserver%2Frequest%2FServerSCLValueRequest.java;h=b2dc03b73f3fee14c847a686f9b7fe6c6229d886;hb=68ce0966a57f5153b133c6283fdbae10f683b745;hp=e6cdf6ff5b43712b098594d172a91b7595de63cb;hpb=41d461afeab1da2a4e10b726c5f2b14996706dcd;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java index e6cdf6ff5..b2dc03b73 100644 --- a/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java +++ b/bundles/org.simantics.document.server/src/org/simantics/document/server/request/ServerSCLValueRequest.java @@ -1,6 +1,6 @@ package org.simantics.document.server.request; -import java.util.ArrayList; +import java.util.Collections; import java.util.Map; import org.simantics.databoard.Bindings; @@ -8,12 +8,14 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.common.procedure.adapter.TransientCacheListener; import org.simantics.db.common.request.IndexRoot; +import org.simantics.db.common.request.PossibleTypedParent; import org.simantics.db.common.request.UnaryRead; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.scl.AbstractExpressionCompilationContext; import org.simantics.db.layer0.scl.AbstractExpressionCompilationRequest; import org.simantics.db.layer0.util.RuntimeEnvironmentRequest2; import org.simantics.db.layer0.variable.Variable; +import org.simantics.document.base.ontology.DocumentationResource; import org.simantics.document.server.request.ServerSCLValueRequest.CompilationContext; import org.simantics.layer0.Layer0; import org.simantics.scl.compiler.common.names.Name; @@ -29,8 +31,6 @@ import org.simantics.scl.compiler.environment.Environments; import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification; import org.simantics.scl.compiler.runtime.RuntimeEnvironment; import org.simantics.scl.compiler.top.SCLExpressionCompilationException; -import org.simantics.scl.compiler.types.TMetaVar; -import org.simantics.scl.compiler.types.TVar; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.Types; import org.simantics.scl.compiler.types.kinds.Kinds; @@ -45,8 +45,6 @@ import org.slf4j.LoggerFactory; public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest { - private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLValueRequest.class); - private final Pair componentTypeAndRoot; private final Resource literal; protected String possibleExpectedValueType; @@ -69,7 +67,11 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest< } public ServerSCLValueRequest(ReadGraph graph, Variable context) throws DatabaseException { - this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context)); + this(getComponentTypeAndRoot(graph, context), context.getRepresents(graph), resolveExpectedValueType(graph, context.getPredicateResource(graph))); + } + + public ServerSCLValueRequest(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException { + this(getComponentTypeAndRoot(graph, s), o, resolveExpectedValueType(graph, p)); } private static Pair getComponentTypeAndRoot(ReadGraph graph, Variable property) throws DatabaseException { @@ -79,20 +81,50 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest< Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(represents)); if(type != null) { Resource root = graph.syncRequest(new IndexRoot(type)); + //System.err.println("getComponentTypeAndRoot1 " + property.getURI(graph) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root)); + return Pair.make(type, root); + } + } + while(represents == null) { + parent = parent.getParent(graph); + represents = parent.getPossibleRepresents(graph); + } +// Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph))); + Resource componentType = parent.getType(graph); + Resource root = graph.syncRequest(new IndexRoot(parent.getRepresents(graph))); + //System.err.println("getComponentTypeAndRoot2 " + property.getURI(graph) + " => " + graph.getPossibleURI(componentType) + " " + graph.getPossibleURI(root)); + return Pair.make(componentType, root); + } + + private static Pair getComponentTypeAndRoot(ReadGraph graph, Resource component) throws DatabaseException { + if(component != null) { + Resource type = graph.syncRequest(new FindPossibleComponentTypeRequest(component)); + if(type != null) { + Resource root = graph.syncRequest(new IndexRoot(type)); + // System.err.println("getComponentTypeAndRoot3 " + graph.getPossibleURI(component) + " => " + graph.getPossibleURI(type) + " " + graph.getPossibleURI(root)); return Pair.make(type, root); + } else { + Resource doc = graph.syncRequest(new PossibleTypedParent(component, DocumentationResource.getInstance(graph).Document)); + if(doc != null) { + Resource componentType = graph.getSingleType(doc); + Resource root = graph.syncRequest(new IndexRoot(doc)); + return Pair.make(componentType, root); + } else { + System.err.println("component = " + component); + Resource root = graph.syncRequest(new IndexRoot(component)); +// Resource componentType = graph.getSingleType(doc); + return Pair.make(null, root); + } } } - parent = parent.getParent(graph); - Resource root = graph.syncRequest(new IndexRoot(property.getRepresents(graph))); - return Pair.make(parent.getType(graph), root); + throw new IllegalStateException(); } public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException { SCLContext sclContext = SCLContext.getCurrent(); Object oldGraph = sclContext.get("graph"); try { - Function1 exp = graph.syncRequest(new ServerSCLValueRequest(graph, context), - TransientCacheListener.>instance()); + Function1 exp = compile(graph, context); sclContext.put("graph", graph); return exp.apply(context); } catch (DatabaseException e) { @@ -103,6 +135,14 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest< sclContext.put("graph", oldGraph); } } + + public static Function1 compile(ReadGraph graph, Variable context) throws DatabaseException { + return graph.syncRequest(new ServerSCLValueRequest(graph, context), TransientCacheListener.>instance()); + } + + public static Function1 compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException { + return graph.syncRequest(new ServerSCLValueRequest(graph, s, o, p), TransientCacheListener.>instance()); + } @Override protected String getExpressionText(ReadGraph graph) @@ -128,10 +168,14 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest< public CompilationContext perform(ReadGraph graph) throws DatabaseException { RuntimeEnvironment runtimeEnvironment = graph.syncRequest(getRuntimeEnvironmentRequest(parameter.first, parameter.second)); - Map propertyMap = - graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()), - TransientCacheListener.>instance()); - return new CompilationContext(runtimeEnvironment, propertyMap); + if(parameter.first != null) { + Map propertyMap = + graph.syncRequest(new ReadComponentTypeInterfaceRequest(parameter.first, runtimeEnvironment.getEnvironment()), + TransientCacheListener.>instance()); + return new CompilationContext(runtimeEnvironment, propertyMap); + } else { + return new CompilationContext(runtimeEnvironment, Collections.emptyMap()); + } } }); } @@ -206,15 +250,6 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest< @Override protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException { - if(possibleExpectedValueType != null) { - try { - Type type = Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType); - type = Types.instantiate(Types.forAll(Types.freeVars(type).toArray(new TVar[0]), type), new ArrayList()); - return type; - } catch (SCLExpressionCompilationException e) { - LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e); - } - } return super.getExpectedType(graph, context); } @@ -233,4 +268,29 @@ public class ServerSCLValueRequest extends AbstractExpressionCompilationRequest< ServerSCLValueRequest other = (ServerSCLValueRequest)obj; return literal.equals(other.literal) && componentTypeAndRoot.equals(other.componentTypeAndRoot); } + + public static Function1 validate(ReadGraph graph, Variable context) throws DatabaseException { + return graph.syncRequest(new ServerSCLValueValidationRequest(graph, context), TransientCacheListener.>instance()); + } + + public static class ServerSCLValueValidationRequest extends ServerSCLValueRequest { + + private static final Logger LOGGER = LoggerFactory.getLogger(ServerSCLHandlerValueRequest.class); + + public ServerSCLValueValidationRequest(ReadGraph graph, Variable context) throws DatabaseException { + super(graph, context); + } + + @Override + protected Type getExpectedType(ReadGraph graph, CompilationContext context) throws DatabaseException { + if(possibleExpectedValueType != null) { + try { + return Environments.getType(context.runtimeEnvironment.getEnvironment(), possibleExpectedValueType); + } catch (SCLExpressionCompilationException e) { + LOGGER.error("Could not get type for " + String.valueOf(possibleExpectedValueType), e); + } + } + return super.getExpectedType(graph, context); + } + } }