From 53faf77c1ed7d8c0a64b246cfb5934ae8a0dfe15 Mon Sep 17 00:00:00 2001 From: augustearth Date: Wed, 3 Jul 2024 10:38:10 -0700 Subject: [PATCH] processVertex --- .../carnival/core/graph/GraphMethod.groovy | 6 +- .../core/graph/GraphMethodBase.groovy | 111 ++++++++++++++++++ .../core/graph/GraphMethodCall.groovy | 6 + .../core/graph/GraphMethodSpec.groovy | 20 ++++ 4 files changed, 142 insertions(+), 1 deletion(-) diff --git a/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethod.groovy b/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethod.groovy index d6dd716..cfe652c 100644 --- a/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethod.groovy +++ b/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethod.groovy @@ -62,11 +62,13 @@ abstract class GraphMethod extends GraphMethodBase { Instant stopTime Exception exception Instant startTime + GraphMethodCall gmcOut // execute the graph method recording the start // and stop times try { startTime = Instant.now() + gmcOut = graphMethodCallStart(graph, g, startTime) execute(graph, g) } catch (Exception e) { exception = e @@ -74,7 +76,9 @@ abstract class GraphMethod extends GraphMethodBase { stopTime = Instant.now() } - graphMethodCall(graph, g, startTime, stopTime, result, exception) + //graphMethodCall(graph, g, startTime, stopTime, result, exception) + graphMethodCallStop(gmcOut, stopTime, result, exception) + return gmcOut } diff --git a/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodBase.groovy b/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodBase.groovy index 33c9b8c..3b96515 100644 --- a/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodBase.groovy +++ b/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodBase.groovy @@ -40,6 +40,12 @@ class GraphMethodBase { /** The name of this graph method */ String name = this.class.name + /** + * Make the process vertex available in a non-thread-safe way for use by + * the execute methods. + */ + protected Vertex processVertex + /////////////////////////////////////////////////////////////////////////// // ACCESSORS @@ -143,6 +149,110 @@ class GraphMethodBase { // CALL /////////////////////////////////////////////////////////////////////////// + /** + * Creates a graph method call object and instantiates the representation + * in the graph. + */ + protected GraphMethodCall graphMethodCallStart( + Graph graph, + GraphTraversalSource g, + Instant startTime + ) { + log.trace "graphMethodCallStart startTime:${startTime}" + + assert graph != null + assert g != null + assert startTime != null + + // compute a hash to record in the process vertex + String argsHash = CoreUtil.argumentsUniquifier(this.arguments) + + // grab the defs + def pcvDef = getProcessClassVertexDef() + def pvDef = getProcessVertexDef() + + // assert that the process def has the required properties + pvDef.propertyDefs.addAll(Core.VX.GRAPH_PROCESS.propertyDefs) + + // create the process vertex + Vertex procV = pvDef.instance().withProperties( + Core.PX.NAME, getName(), + Core.PX.ARGUMENTS_HASH, argsHash, + Core.PX.START_TIME, startTime.toEpochMilli() + ).create(graph) + + // the process vertex is an instance of the process class + Base.EX.IS_INSTANCE_OF.instance() + .from(procV) + .to(pcvDef.vertex) + .create() + + // ensure that the process class is a subclass of GRAPH_PROCESS_CLASS + // it is troubling that this happens every time a graph method is called + if ( + pcvDef.label != Core.VX.GRAPH_PROCESS_CLASS.label || + pcvDef.nameSpace != Core.VX.GRAPH_PROCESS_CLASS.nameSpace + ) { + Base.EX.IS_SUBCLASS_OF.instance() + .from(pcvDef.vertex) + .to(Core.VX.GRAPH_PROCESS_CLASS.vertex) + .ensure(g) + } + + // set the internal process vertex + this.processVertex = procV + + // construct and return a graph method call + GraphMethodCall gmc = new GraphMethodCall( + arguments: this.arguments, + processVertex: procV + ) + + gmc + } + + + /** + * + * + */ + protected void graphMethodCallStop( + GraphMethodCall gmc, + Instant stopTime, + Map result, + Exception exception + ) { + log.trace "graphMethodCallStop gmc:${gmc}" + log.trace "graphMethodCallStop stopTime:${stopTime}" + log.trace "graphMethodCallStop result:${result}" + log.trace "graphMethodCallStop exception:${exception}" + + assert gmc != null + assert gmc.processVertex + assert stopTime != null + + def procV = gmc.processVertex + + Core.PX.STOP_TIME.set( + procV, + stopTime.toEpochMilli() + ) + + if (exception != null) { + try { + String msg = exception.message ?: "${exception.class}" + Core.PX.EXCEPTION_MESSAGE.set(procV, msg) + } catch (Exception e) { + log.warn "could not set exception message of process vertex ${procV} ${e.message}" + } + throw exception + } + + if (result != null) gmc.result = result + if (exception != null) gmc.exception = exception + } + + /** * Creates a graph method call object and instantiates the representation * in the graph. @@ -213,6 +323,7 @@ class GraphMethodBase { result: result, processVertex: procV ) + if (exception != null) gmc.exception = exception gmc } diff --git a/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodCall.groovy b/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodCall.groovy index ce12b1b..5edb884 100644 --- a/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodCall.groovy +++ b/app/carnival-core/src/main/groovy/carnival/core/graph/GraphMethodCall.groovy @@ -31,6 +31,12 @@ class GraphMethodCall { */ Vertex processVertex + + /** + * An exception if one was thrown. + */ + Exception exception + /** * Return the arguments used by this method. diff --git a/app/carnival-core/src/test/groovy/carnival/core/graph/GraphMethodSpec.groovy b/app/carnival-core/src/test/groovy/carnival/core/graph/GraphMethodSpec.groovy index ae8be15..14f4c60 100644 --- a/app/carnival-core/src/test/groovy/carnival/core/graph/GraphMethodSpec.groovy +++ b/app/carnival-core/src/test/groovy/carnival/core/graph/GraphMethodSpec.groovy @@ -2,6 +2,7 @@ package carnival.core.graph +import groovy.util.logging.Slf4j import spock.lang.Specification import spock.lang.Unroll import spock.lang.Shared @@ -17,6 +18,7 @@ import carnival.core.Core +@Slf4j public class GraphMethodSpec extends Specification { @@ -53,6 +55,12 @@ public class GraphMethodSpec extends Specification { } } + static class TestGraphMethodCheckProcessVertex extends GraphMethod { + public void execute(Graph graph, GraphTraversalSource g) { + if (!processVertex) throw new Exception('no process vertex') + } + } + static class TestGraphMethodArguments extends GraphMethod { public void execute(Graph graph, GraphTraversalSource g) { def args = arguments @@ -105,6 +113,18 @@ public class GraphMethodSpec extends Specification { noExceptionThrown() } + + void "processVertex is set"() { + when: + def gm = new TestGraphMethodCheckProcessVertex() + def gmc = gm.call(graph, g) + + then: + gm.processVertex + gmc.processVertex + !gmc.exception + } + void "name override"() { when: