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:

  1. Load all ACTED_IN and filter via blueprint41 expression.

    ACTED_IN.Where(alias => alias.Person(actor));
    
  2. Load all ACTED_IN via actor only.

    actor.ActedMovieRelations();
    
  3. Load all ACTED_IN via actor and filter via blueprint41 expression.

    actor.ActedMoviesWhere(alias => alias.roles == new string[] { "Thomas Anderson" });
    
  4. Load all ACTED_IN via actor and json notation.

    actor.ActedMoviesWhere(roles: new string[] { "Thomas Anderson" });
    

Updating a Relationship property

To update relationship values, use the Assign(jsonNotation) method.

  1. To update the ACTED_IN relationship property, create the static method UpdateRelationships. In the method, create a transaction and get the Movie record by calling the Movie.LoadBytitle(string) and get Actor by calling the Person.LoadByname(string) method. Change the value of the ACTED_IN relationship roles property:

    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_IN relationship where the roles are "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();
        }
    }
    
  2. In the Main method, call the UpdateRelationships method after the UpdateMovie method and before the GetMoviesAndTheActors method.

    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();
    }
    
  3. Press F5 to run the "MovieGraph" application. You've successfully updated the ACTED_IN relationship.

Get Relationships By Property Value

  1. To get the ACTED_IN relationships by property value, create the static method GetRelationshipsByPropertyValue. In the method, create a transaction and get the ACTED_IN relationship by calling the ACTED_IN.Where(jsonNotation) or the ACTED_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}' }})");
            }
        }
    }
    
  2. In the Main method, call the GetRelationshipsByPropertyValue method after the GetMoviesAndTheActors method.

    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();
    }
    
  3. Press F5 to run the "MovieGraph" application.

image

Get Actors from a relationship and Add to a Movie

  1. To get the actors from ACTED_IN relationship and add to a new Movie, create the static method GetActorsFromRelationshipAndAddToMovie. In the method, create a transaction and get the Movie record by calling the Movie.LoadBytitle(string) method and set movieTitle parameter value to "The Matrix Reloaded". After getting the record, load ACTED_IN relationship by calling the method matrix.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();
        }
    }
    
  2. In the Main method, call the GetActorsFromRelationshipAndAddToMovie method after the UpdateRelationships method and before the GetMoviesAndTheActors method.

    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();
    }
    
  3. Press F5 to run the "MovieGraph" application.

image

image

🎉 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.

Next : Blueprint41 Query