Skip to content

Commit

Permalink
For #127
Browse files Browse the repository at this point in the history
  • Loading branch information
augustearth committed Nov 12, 2024
1 parent f613fb5 commit ad5dee4
Show file tree
Hide file tree
Showing 39 changed files with 2,184 additions and 695 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ subprojects {
events 'standard_out'
}

/*
I think all this is a relic of the past when Carnival was an app and
and no longer appropriate.
if (System.getProperty('carnival.home')) {
systemProperty('carnival.home', System.getProperty('carnival.home'))
systemProperty('logback.configurationFile', System.getProperty('carnival.home') + '/config/logback.xml')
Expand All @@ -272,6 +276,7 @@ subprojects {
ant.echo "WARNING: Logback configuration file not set. Use the -D command line argument like -Dlogback.configurationFile=/path/to/file.xml. Or, set the environment variable CARNIVAL_LOGBACK_CONFIG to /path/to/file.xml. See documentation."
}
}
*/
}
}

Expand Down
3 changes: 3 additions & 0 deletions app/carnival-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ dependencies {

// reflections for dymamic class instantiation
implementation 'org.reflections:reflections:0.10.2'

implementation('org.janusgraph:janusgraph-core:1.1.0-20240927-111508.73232fe')
implementation('org.janusgraph:janusgraph-berkeleyje:1.1.0-20240927-111508.73232fe')
}


Expand Down
108 changes: 106 additions & 2 deletions app/carnival-core/src/main/groovy/carnival/core/Carnival.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import carnival.core.graph.GraphValidator
import carnival.core.graph.GraphValidationError
import carnival.core.graph.DefaultGraphValidator
import carnival.core.graph.EdgeConstraint
import carnival.core.graph.PropertyConstraint
import carnival.core.graph.VertexConstraint
import carnival.core.util.DuplicateModelException

Expand Down Expand Up @@ -104,7 +105,7 @@ abstract class Carnival implements GremlinTrait {
*/
public void initialize(Graph graph, GraphTraversalSource g) {
log.info "Carnival initialize graph:$graph g:$g"
[Base.EX, Core.EX, Core.VX].each {
[Base.PX, Base.EX, Core.PX, Core.VX, Core.EX].each {
addModel(graph, g, it)
}
}
Expand Down Expand Up @@ -253,6 +254,9 @@ abstract class Carnival implements GremlinTrait {
* @param defClass The element definition class.
*/
public void addModel(Class<ElementDefinition> defClass) {

log.debug "\n\ndefClass:${defClass}\n\n"

assert defClass
withGremlin { graph, g ->
addModel(graph, g, defClass)
Expand All @@ -276,8 +280,8 @@ abstract class Carnival implements GremlinTrait {
def defInterfaces = defClass.getInterfaces()
if (defInterfaces.contains(VertexDefinition)) addVertexModel(graph, g, defClass)
else if (defInterfaces.contains(EdgeDefinition)) addEdgeModel(graph, g, defClass)
else if (defInterfaces.contains(PropertyDefinition)) addPropertyModel(graph, g, defClass)
else throw new RuntimeException("unrecognized definition class: $defClass")

}


Expand Down Expand Up @@ -326,6 +330,29 @@ abstract class Carnival implements GremlinTrait {
}


/**
* Add the edge models in the provided property definition class to this
* Carnival using the provided graph and graph traversal source. This is an
* internal method and not expected to be called by client code.
* @param graph A gremlin graph.
* @param g A graph traversal source.
* @param defClass A property definition class.
*/
public void addPropertyModel(Graph graph, GraphTraversalSource g, Class<PropertyDefinition> defClass) {
assert graph
assert g
assert defClass

log.debug "\n\naddPropertyModel defClass:${defClass}\n\n"

Set<PropertyConstraint> propertyConstraints = findNewPropertyConstraints(defClass)
propertyConstraints.each { pc ->
addConstraint(pc)
}
}



///////////////////////////////////////////////////////////////////////////
// GRAPH CONSTRAINTS - VERTEX
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -493,6 +520,83 @@ abstract class Carnival implements GremlinTrait {
it.label == ec.label && it.nameSpace == ec.nameSpace
}
}



///////////////////////////////////////////////////////////////////////////
// GRAPH CONSTRAINTS - PROPERTIES
///////////////////////////////////////////////////////////////////////////

/**
* Add the provided property constraint to this carnival.
* @param propertyConst The property constraint to add.
*/
public void addConstraint(PropertyConstraint propertyConst) {
log.trace "addConstraint propertyConst: ${propertyConst.label} $propertyConst"

log.trace "adding property definition to graph schema ${propertyConst.label} ${propertyConst}"
graphSchema.propertyConstraints.add(propertyConst)
}


/**
* Find property constraints in an property definition class that are not already
* present in this Carnival.
* @param propertyDefClass The property definition class.
* @return A collection of property constraints.
*/
public Collection<PropertyConstraint> findNewPropertyConstraints(Class<PropertyDefinition> propertyDefClass) {
assert propertyDefClass
Set<Class<PropertyDefinition>> edcs = new HashSet<Class<PropertyDefinition>>()
edcs.add(propertyDefClass)
findNewPropertyConstraints(edcs)
}


/**
* Find property constraints in a provided set of property definition classes that
* are not already present in this Carnival.
* @param propertyDefClasses A set of property definition classes.
* @return A collection of property constraints.
*/
public Collection<PropertyConstraint> findNewPropertyConstraints(Set<Class<PropertyDefinition>> propertyDefClasses) {
assert propertyDefClasses

Set<PropertyConstraint> newConstraints = new HashSet<PropertyConstraint>()

propertyDefClasses.each { Class edc ->
log.trace "findNewPropertyConstraints edc: $edc"

edc.values().each { PropertyDefinition edef ->
log.trace "findNewPropertyConstraints edef: $edef"

PropertyConstraint ec = PropertyConstraint.create(edef)
def exists = existsInGraphSchema(ec)
if (!exists) {
log.trace "new property constraint ${ec.label} ${ec}"
newConstraints.add(ec)
}
if (exists && !ignoreDuplicateModels) throw new DuplicateModelException(
"property constraint already exists: ${ec}"
)
}
}

return newConstraints
}


/**
* Return true if the provided property constraint exists in the graph
* schema.
* @param vc The property constraint
* @return True if the property constraint exists
*/
boolean existsInGraphSchema(PropertyConstraint ec) {
graphSchema.propertyConstraints.find {
it.label == ec.label && it.nameSpace == ec.nameSpace
}
}



Expand Down
Loading

0 comments on commit ad5dee4

Please sign in to comment.