Skip to content

Commit

Permalink
processVertex
Browse files Browse the repository at this point in the history
  • Loading branch information
augustearth committed Jul 3, 2024
1 parent 0cc047e commit 53faf77
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,23 @@ 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
} finally {
stopTime = Instant.now()
}

graphMethodCall(graph, g, startTime, stopTime, result, exception)
//graphMethodCall(graph, g, startTime, stopTime, result, exception)
graphMethodCallStop(gmcOut, stopTime, result, exception)
return gmcOut
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -213,6 +323,7 @@ class GraphMethodBase {
result: result,
processVertex: procV
)
if (exception != null) gmc.exception = exception

gmc
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class GraphMethodCall {
*/
Vertex processVertex


/**
* An exception if one was thrown.
*/
Exception exception


/**
* Return the arguments used by this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +18,7 @@ import carnival.core.Core



@Slf4j
public class GraphMethodSpec extends Specification {


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 53faf77

Please sign in to comment.