Skip to content

Instantly share code, notes, and snippets.

@lonniev
Created September 1, 2022 12:22
Show Gist options
  • Save lonniev/183b02eefc014b0154ef8535f6585786 to your computer and use it in GitHub Desktop.
Save lonniev/183b02eefc014b0154ef8535f6585786 to your computer and use it in GitHub Desktop.
Perform a Gremlin Query that finds Parts that have related Requirements and which groups by the Part
g.V().has( 'ArtifactType', 'name', 'Part' )
.match(
__.as( 't' ).in( 'hasType' ).as( 'p' ),
__.as( 't' ).out( 'ownedBy' ).as( 'r' ),
__.as( 'r' ).out( 'hasType' ).filter{ it.get().value( 'name' ).contains( 'Windchill' ) }.as( 'rt' ),
__.as( 'p' ).both( 'Relation' ).as( 'o' ),
__.as( 'o' ).out( 'hasType' ).filter{ it.get().value( 'name' ) ==~ /(?i).*?((requirement)|(task)).*/ }
)
.group()
.by( select( 'p' ) ).unfold().project( 'part', 'requirements' )
.by( select(keys).map { it.get().value( 'name' ) } )
.by( select(values).unfold().select( 'o' ).map { it.get().value( 'name' ) }.fold() )
@lonniev
Copy link
Author

lonniev commented Sep 1, 2022

As the query leaves the match() step, it will have a Traversal that has vertices and edges which are selectable with the matching p, r, rt, and o keys. We only are interested in the pairs (p,o).

We group the match results by their p element (the Part).

We unfold the group result to get back a stream of pairs (key = p, value = the match resultset).

We want to project results into two sets: part and requirement.

For the part, we select the key of the grouped match result and map the Part to its name.

For the requirement, we select the value of the grouped match result, unfold it, select the 'o' entry, and map that vertex to its name. We fold back the unfolded, now subsampled and mapped, requirements value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment