Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rofr
Created December 1, 2017 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rofr/6c75fe686b74c6758ab7f13af4b8b987 to your computer and use it in GitHub Desktop.
Save rofr/6c75fe686b74c6758ab7f13af4b8b987 to your computer and use it in GitHub Desktop.
Memstate movie model - strongly typed graphs
//define actor node type
class Actor {
String Name;
Set<Role> Movies;
}
//define movie node type
class Movie {
String Name;
Set<Role> Roles;
}
/* A relationship modeled as a class */
class Role {
String Name;
Actor Actor;
Movie Movie;
}
class Model {
Set<Actor> Actors;
Set<Movie> Movies;
}
/* populate the graph */
// create nodes
var matrix = new Movie("Matrix");
var keanu = new Actor("Keanu Reeves");
//create an edge with properties
//notice that we can navigate from the edge to each node
var role = new Role {
Name = "Neo",
Actor = keanu,
Movie = matrix
};
//add the edge to each node
keanu.Roles.Add(role);
matrix.Roles.Add(role);
//set up the root graph
var model = new Model();
model.Actors.Add(keanu);
model.Movies.Add(matrix);
var movies = model.Movies.Where(m => m.Roles.Any(r => r.Actor == keanu)).Distinct();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment