Blueprint41 Relationships - circles-arrows/blueprint41 GitHub Wiki
Blueprint41 Relationships
The following steps will guide you on how to load and update an instance of ACTED_IN relationship.
Load Relationships
There are 4 options on how to load the ACTED_IN relationship:
-
Load all
ACTED_INand filter via blueprint41 expression.ACTED_IN.Where(alias => alias.Person(actor)); -
Load all
ACTED_INvia actor only.actor.ActedMovieRelations(); -
Load all
ACTED_INvia actor and filter via blueprint41 expression.actor.ActedMoviesWhere(alias => alias.roles == new string[] { "Thomas Anderson" }); -
Load all
ACTED_INvia actor and json notation.actor.ActedMoviesWhere(roles: new string[] { "Thomas Anderson" });
Updating a Relationship property
To update relationship values, use the Assign(jsonNotation) method.
-
To update the
ACTED_INrelationship property, create the static methodUpdateRelationships. In the method, create a transaction and get theMovierecord by calling theMovie.LoadBytitle(string)and getActorby calling thePerson.LoadByname(string)method. Change the value of theACTED_INrelationshiprolesproperty:Property Value roles "Thomas Anderson", "Neo", "The One" For loading relationship, we can choose either option 3 or 4, because we only need to update the
ACTED_INrelationship where therolesare"Thomas Anderson".private static void UpdateRelationships() { using (Transaction.Begin(true)) { var actor = Person.LoadByname("Keanu Reeves"); // Option 3 - Load ACTED_IN via actor and blueprint41 expression and assign new value via json notation. //actor.ActedMoviesWhere(alias => alias.roles == new string[] { "Thomas Anderson" }) // .Assign(roles: new string[] { "Thomas Anderson", "Neo", "The One" }); // Option 4 - Load ACTED_IN via actor and json notation and assign new values via json notation. actor.ActedMoviesWhere(roles: new string[] { "Thomas Anderson" }) .Assign(roles: new string[] { "Thomas Anderson", "Neo", "The One" }); Transaction.Commit(); } } -
In the
Mainmethod, call theUpdateRelationshipsmethod after theUpdateMoviemethod and before theGetMoviesAndTheActorsmethod.static void Main(string[] args) { Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]"); PersistenceProvider.CurrentPersistenceProvider = provider; Datastore model = new Datastore(); model.Execute(true); //CreateMoviesAndActors(); //DeleteMovie("Cloud Atlas"); //UpdateMovie(); UpdateRelationships(); GetMoviesAndTheActors(); Console.ReadKey(); } -
Press F5 to run the "MovieGraph" application. You've successfully updated the
ACTED_INrelationship.
Get Relationships By Property Value
-
To get the
ACTED_INrelationships by property value, create the static methodGetRelationshipsByPropertyValue. In the method, create a transaction and get theACTED_INrelationship by calling theACTED_IN.Where(jsonNotation)or theACTED_IN.Where(expression).private static void GetRelationshipsByPropertyValue() { using (Transaction.Begin()) { Console.WriteLine($"\n Get relationship with roles: \"Thomas Anderson\", \"Neo\", \"The One\" "); foreach (var actedIn in ACTED_IN.Where(roles: new string[] { "Thomas Anderson", "Neo", "The One" })) { Console.WriteLine($" (in:Person {{ name: '{actedIn.Person.name}' }})-[rel:ACTED_IN {{ roles: [{string.Join(", ", actedIn.roles.Select(item => "'" + item + "'"))}] }}]-(out:Movie {{ title: '{actedIn.Movie.title}' }})"); } Console.WriteLine($"\n Get relationship with roles: \"Neo\" "); foreach (var actedIn in ACTED_IN.Where(rel => rel.roles.Any("Neo"))) { Console.WriteLine($" (in:Person {{ name: '{actedIn.Person.name}' }})-[rel:ACTED_IN {{ roles: [{string.Join(", ", actedIn.roles.Select(item => "'" + item + "'"))}] }}]-(out:Movie {{ title: '{actedIn.Movie.title}' }})"); } } } -
In the
Mainmethod, call theGetRelationshipsByPropertyValuemethod after theGetMoviesAndTheActorsmethod.static void Main(string[] args) { Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]"); PersistenceProvider.CurrentPersistenceProvider = provider; Datastore model = new Datastore(); model.Execute(true); //CreateMoviesAndActors(); //DeleteMovie("Cloud Atlas"); //UpdateMovie(); //UpdateRelationships(); //GetMoviesAndTheActors(); GetRelationshipsByPropertyValue(); Console.ReadKey(); } -
Press F5 to run the "MovieGraph" application.
Get Actors from a relationship and Add to a Movie
-
To get the actors from
ACTED_INrelationship and add to a newMovie, create the static methodGetActorsFromRelationshipAndAddToMovie. In the method, create a transaction and get theMovierecord by calling theMovie.LoadBytitle(string)method and setmovieTitleparameter value to"The Matrix Reloaded". After getting the record, loadACTED_INrelationship by calling the methodmatrix.ActorsWhere(jsonNotation).private static void GetActorsFromRelationshipAndAddToMovie() { using (Transaction.Begin(true)) { var matrix = Movie.LoadBytitle("The Matrix Reloaded"); var acted_in = matrix.ActorsWhere(roles: new string[] { "Morpheus" }).First(); var laurence = acted_in.Person; acted_in = matrix.ActorsWhere(roles: new string[] { "Thomas Anderson", "Neo", "The One" }).First(); var keanu = acted_in.Person; Movie johnWick2 = new Movie() { title = "John Wick: Chapter 2", tagline = "The Only Way Out Is Back In", released = 2017 }; johnWick2.AddActor(keanu, roles: new string[] { "John Wick" }); johnWick2.AddActor(laurence, roles: new string[] { "Bowery King" }); Transaction.Commit(); } } -
In the
Mainmethod, call theGetActorsFromRelationshipAndAddToMoviemethod after theUpdateRelationshipsmethod and before theGetMoviesAndTheActorsmethod.static void Main(string[] args) { Driver.Neo4jPersistenceProvider provider = new Driver.Neo4jPersistenceProvider("bolt://localhost:7687", "[USERNAME_HERE]", "[PASSWORD_HERE]"); PersistenceProvider.CurrentPersistenceProvider = provider; Datastore model = new Datastore(); model.Execute(true); //CreateMoviesAndActors(); //DeleteMovie("Cloud Atlas"); //UpdateMovie(); //UpdateRelationships(); GetActorsFromRelationshipAndAddToMovie(); GetMoviesAndTheActors(); //GetRelationshipsByPropertyValue(); Console.ReadKey(); } -
Press F5 to run the "MovieGraph" application.
🎉 You've successfully load and updated an ACTED_IN relationship. Next topic, we will change the way on how we get the Movie records by using Blueprint41 query.