Esempio n. 1
0
        //public async Task CreateIndices()
        //{
        //    string[] queries = {
        //        "CREATE INDEX ON :Movie(title)",
        //        "CREATE INDEX ON :Movie(id)",
        //        "CREATE INDEX ON :Person(id)",
        //        "CREATE INDEX ON :Person(name)",
        //        "CREATE INDEX ON :Genre(name)"
        //    };

        //    using (var session = driver.Session())
        //    {
        //        foreach(var query in queries)
        //        {
        //            session.Run(query);
        //        }
        //    }
        //}

        public async Task CreateRelationships_Service(IList <ServiceInformation> metadatas)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $metadatas AS metadata")
                            // Find the Service:
                            .AppendLine("MERGE (m:Service { name: metadata.service.name, traceid: metadata.service.traceid })")
                            .AppendLine("SET m = metadata.service")
                            // Create FromService Relationships:
                            .AppendLine("WITH metadata, m")
                            .AppendLine("UNWIND metadata.fromservices AS fromservice")
                            .AppendLine("MERGE (a:Service { name: fromservice.name, traceid: fromservice.traceid })")
                            .AppendLine("SET a = fromservice")
                            .AppendLine("MERGE (a)-[r:calls {traceid: metadata.service.traceid, endpoint: metadata.service.endpoint}]->(m)")
                            // Create ToService Relationships:
                            .AppendLine("WITH metadata, m")
                            .AppendLine("UNWIND metadata.toservices AS toservice")
                            .AppendLine("MERGE (c:Service { name: toservice.name, traceid: toservice.traceid })")
                            .AppendLine("SET c = toservice")
                            .AppendLine("MERGE (m)-[r:calls {traceid: toservice.traceid, endpoint: toservice.endpoint}]->(c)")
                            .ToString();

            using (var session = driver.Session())
            {
                session.Run(cypher, new Dictionary <string, object>()
                {
                    { "metadatas", ParameterSerializer.ToDictionary(metadatas) }
                });
            }
        }
Esempio n. 2
0
        public void CreateReasons(IList <Reason> reasons)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {reasons} AS reason")
                            .AppendLine("MERGE (r:Reason {code: reason.code})")
                            .AppendLine("SET r = reason")
                            .ToString();

            using (var session = driver.Session())
            {
                session.Run(cypher, new Dictionary <string, object>()
                {
                    { "reasons", ParameterSerializer.ToDictionary(reasons) }
                });
            }
        }
Esempio n. 3
0
        public void CreateAirports(IList <AirportInformation> airports)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {airports} AS row")
                            // Add the Country:
                            .AppendLine("MERGE (country:Country { name: row.country.name })")
                            .AppendLine("SET country = row.country")
                            .AppendLine()
                            // Add the City:
                            .AppendLine("WITH country, row")
                            .AppendLine("MERGE (city:City { name: row.city.name })")
                            .AppendLine("SET city = row.city")
                            .AppendLine("MERGE (city)-[:IN_COUNTRY]->(country)")
                            .AppendLine()
                            // Add the Optional State:
                            .AppendLine("WITH city, country, row")
                            .AppendLine("FOREACH (s IN (CASE row.state.name WHEN \"\" THEN [] ELSE [row.state.name] END) |")
                            .AppendLine("   MERGE (state:State {name: row.state.name})")
                            .AppendLine("   MERGE (state)-[:IN_COUNTRY]->(country)")
                            .AppendLine("   MERGE (city)-[:IN_STATE]->(state)")
                            .AppendLine(")")
                            .AppendLine()
                            // Add the Airport:
                            .AppendLine("WITH city, row")
                            .AppendLine("MERGE (airport:Airport {airport_id: row.airport.airport_id})")
                            .AppendLine("SET airport = row.airport")
                            .AppendLine("MERGE (airport)-[r:IN_CITY]->(city)")
                            .AppendLine()
                            .ToString();

            using (var session = driver.Session())
            {
                var result = session.WriteTransaction(tx => tx.Run(cypher, new Dictionary <string, object>()
                {
                    { "airports", ParameterSerializer.ToDictionary(airports) }
                }));

                // Get the Summary for Diagnostics:
                var summary = result.Consume();

                Console.WriteLine($"[{DateTime.Now}] [Airports] #NodesCreated: {summary.Counters.NodesCreated}, #RelationshipsCreated: {summary.Counters.RelationshipsCreated}");
            }
        }
Esempio n. 4
0
        public void CreateCarriers(IList <Carrier> carriers)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {carriers} AS carrier")
                            .AppendLine("MERGE (c:Carrier {code: carrier.code})")
                            .AppendLine("SET c = carrier")
                            .ToString();

            using (var session = driver.Session())
            {
                var result = session.WriteTransaction(tx => tx.Run(cypher, new Dictionary <string, object>()
                {
                    { "carriers", ParameterSerializer.ToDictionary(carriers) }
                }));

                // Get the Summary for Diagnostics:
                var summary = result.Consume();

                Console.WriteLine($"[{DateTime.Now}] [Carriers] #NodesCreated: {summary.Counters.NodesCreated}, #RelationshipsCreated: {summary.Counters.RelationshipsCreated}");
            }
        }
        public async Task CreateLocalWeatherDataAsync(IList <LocalWeatherData> localWeatherDatas)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {localWeatherDatas} AS row")
                            .AppendLine("MATCH (station:Station {identifier: row.station_identifier})")
                            .AppendLine("CREATE (localWeatherData:LocalWeatherData {code: carrier.code})")
                            .AppendLine("   (station)-[:MEASURED]->(localWeatherData)")
                            .AppendLine("SET localWeatherData = row")
                            .ToString();

            using (var session = driver.Session())
            {
                var result = await session.WriteTransactionAsync(tx => tx.RunAsync(cypher, new Dictionary <string, object>()
                {
                    { "localWeatherDatas", ParameterSerializer.ToDictionary(localWeatherDatas) }
                }));

                // Get the Summary for Diagnostics:
                var summary = await result.ConsumeAsync();

                Console.WriteLine($"[{DateTime.Now}] [Carriers] #NodesCreated: {summary.Counters.NodesCreated}, #RelationshipsCreated: {summary.Counters.RelationshipsCreated}");
            }
        }
Esempio n. 6
0
        public void CreateFlights(IList <FlightInformation> flights)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {flights} AS row")
                            // Get the Airports of this Flight:
                            .AppendLine("MATCH (oAirport:Airport {airport_id: row.origin})")
                            .AppendLine("MATCH (dAirport:Airport {airport_id: row.destination})")
                            // Create the Flight Item:
                            .AppendLine("CREATE (f:Flight {flight_number: row.flight_number}),")
                            // Relate Flight to Origin Airport:
                            .AppendLine("   (f)-[:ORIGIN {taxi_time: row.taxi_out, dep_delay: row.departure_delay}]->(oAirport),")
                            .AppendLine("   (f)-[:DESTINATION {taxi_time: row.taxi_in, arr_delay: row.arrival_delay}]->(dAirport)")
                            .AppendLine()
                            // Set Flight Details:
                            .AppendLine("SET f.year = row.year,")
                            .AppendLine("    f.month = row.month,")
                            .AppendLine("    f.day = row.day_of_month,")
                            .AppendLine("    f.weekday = row.day_of_week")
                            .AppendLine()
                            // Add Carrier Information:
                            .AppendLine("WITH row, f")
                            .AppendLine("MATCH (car:Carrier {code: row.carrier})")
                            .AppendLine("CREATE (f)-[:CARRIER]->(car)")
                            .AppendLine()
                            // Add Cancellation Information:
                            .AppendLine("WITH row, f")
                            .AppendLine("OPTIONAL MATCH (r:Reason {code: row.cancellation_code})")
                            .AppendLine("FOREACH (o IN CASE WHEN r IS NOT NULL THEN [r] ELSE [] END |")
                            .AppendLine("   CREATE (f) -[:CANCELLED_BY]->(r)")
                            .AppendLine(")")
                            .AppendLine()
                            // Add Delay Information:
                            .AppendLine("WITH row, f")
                            .AppendLine("UNWIND (CASE row.delays WHEN [] THEN [null] else row.delays END) as delay")
                            .AppendLine("OPTIONAL MATCH (r:Reason {code: delay.reason})")
                            .AppendLine("FOREACH (o IN CASE WHEN r IS NOT NULL THEN [r] ELSE [] END |")
                            .AppendLine("   CREATE (f)-[fd:DELAYED_BY]->(r)")
                            .AppendLine("   SET fd.delay = delay.duration")
                            .AppendLine(")")
                            .AppendLine()
                            // Add Aircraft Information:
                            .AppendLine("WITH row, f")
                            .AppendLine("FOREACH(a IN (CASE row.tail_number WHEN \"\" THEN [] ELSE [row.tail_number] END) |")
                            .AppendLine("   MERGE (craft:Aircraft {tail_num: a})")
                            .AppendLine("   CREATE (f)-[:AIRCRAFT]->(craft)")
                            .AppendLine(")")
                            .ToString();

            using (var session = driver.Session())
            {
                // Insert in a Transaction:
                var result = session.WriteTransaction(tx => tx.Run(cypher, new Dictionary <string, object>()
                {
                    { "flights", ParameterSerializer.ToDictionary(flights) }
                }));

                // Get the Summary for Diagnostics:
                var summary = result.Consume();

                Console.WriteLine($"[{DateTime.Now}] [Flights] #NodesCreated: {summary.Counters.NodesCreated}, #RelationshipsCreated: {summary.Counters.RelationshipsCreated}");
            }
        }
        public async Task CreateStationsAsync(IList <Station> stations)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {stations} AS row")
                            .AppendLine("MERGE (state:State {name: row.State")
                            .AppendLine("MERGE (station:Station {identifier: row.identifier}),")
                            .AppendLine("   (station)-[:IN_STATE]->(state)")
                            .AppendLine("SET state = row")
                            .ToString();

            using (var session = driver.Session())
            {
                await session.RunAsync(cypher, new Dictionary <string, object>() { { "stations", ParameterSerializer.ToDictionary(stations) } });
            }
        }
Esempio n. 8
0
        public async Task <List <Like> > UsersLikes(IList <User> users)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $users AS user")
                            .AppendLine("MATCH (u:User{id: user.id})")
                            .AppendLine("MATCH (u)-[:LIKE]->(l:Like)")
                            .AppendLine("RETURN l.name")
                            .ToString();
            var         session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            List <Like> myLikes = new List <Like>();

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "users", ParameterSerializer.ToDictionary(users) } });

                System.Console.WriteLine("****user's likes:");
                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        Like auxLike = new Like
                        {
                            Name = item.Value.ToString()
                        };
                        System.Console.WriteLine(item.Value.ToString());
                        myLikes.Add(auxLike);
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(myLikes);
        }
Esempio n. 9
0
        public async Task CreateMovies(IList <Movie> movies)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {movies} AS movie")
                            .AppendLine("MERGE (m:Movie {id: movie.id})")
                            .AppendLine("SET m = movie")
                            .ToString();

            using (var session = driver.Session())
            {
                await session.RunAsync(cypher, new Dictionary <string, object>() { { "movies", ParameterSerializer.ToDictionary(movies) } });
            }
        }
Esempio n. 10
0
        public async Task CreatePersons(IList <Person> persons)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {persons} AS person")
                            .AppendLine("MERGE (p:Person {name: person.name})")
                            .AppendLine("SET p = person")
                            .ToString();

            using (var session = driver.Session())
            {
                await session.RunAsync(cypher, new Dictionary <string, object>() { { "persons", ParameterSerializer.ToDictionary(persons) } });
            }
        }
Esempio n. 11
0
        //Crear nodo
        private async Task newReportNodo(IList <Report> reports)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $reports AS report")
                            .AppendLine("CREATE(:Report{id: report.id, commentary: report.commentary})")
                            .ToString();
            var session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "reports", ParameterSerializer.ToDictionary(reports) } });
            }
            finally { await session.CloseAsync(); }
        }
Esempio n. 12
0
        //Crea la relacion de sugerencia de usuario
        private async Task CreateRelationshipSuggestion(IList <SuggestionInfo> suggestionMetadata)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $suggestionMetadata AS suggestionMetadata")
                            // find Suggestion
                            .AppendLine("MATCH (s:Suggestion { id: suggestionMetadata.suggestion.id})")
                            // suggested user
                            .AppendLine("WITH suggestionMetadata, s")
                            .AppendLine("UNWIND suggestionMetadata.suggestedUser AS suggestedUser")
                            .AppendLine("MATCH (u:User { id: suggestedUser.id})")
                            .AppendLine("MERGE (s)-[r:SUGGEST]->(u)")
                            .ToString();
            var session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "suggestionMetadata", ParameterSerializer.ToDictionary(suggestionMetadata) } });
            }
            finally { await session.CloseAsync(); }
        }
Esempio n. 13
0
        //Busca los usuarios que va a sugerir
        private async Task <List <User> > searchUserToSuggest(IList <User> users)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $users AS user")
                            //Trae usuario
                            .AppendLine("MATCH (mu:User { id: user.id})")
                            //Identifica los gustos del usuario
                            .AppendLine("MATCH (mu)--(l:Like)")
                            //Identifica los susuarios que tenga gustos como el objetivo
                            .AppendLine("MATCH (l)--(u:User)")
                            //Rechaza todos los usuarios con los que ya tenga relación y autocontenencia
                            .AppendLine("WHERE  NOT ((mu)-->(u)) AND NOT(mu.id = u.id)")
                            //Rechaza si ya tienen una sugerencia
                            .AppendLine("AND NOT((mu)-[:GET]-(:Suggestion)-[:SUGGEST]-(u))")
                            .AppendLine("RETURN u.id, u.name")
                            .ToString();
            var         session   = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            List <User> listUsers = new List <User>();

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "users", ParameterSerializer.ToDictionary(users) } });

                while (await reader.FetchAsync())
                {
                    int    count = 0;
                    string id    = "";
                    // Each current read in buffer can be reached via Current
                    foreach (var item in reader.Current.Values)
                    {
                        if ((count % 2 != 0))
                        {
                            User aux = new User
                            {
                                Id   = id,
                                Name = item.Value.ToString()
                            };
                            listUsers.Add(aux);
                        }
                        else
                        {
                            id = item.Value.ToString();
                        }
                        System.Console.WriteLine(item.Value);
                        count++;
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(listUsers);
        }
Esempio n. 14
0
        /*DB operations*/
        //Crea sugerencias de usuario en la base de datos
        private async Task CreateSuggestion(IList <Suggestion> suggestions)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $suggestions AS suggestion")
                            .AppendLine("CREATE(:Suggestion{id: suggestion.id, isActive: suggestion.isActive})")
                            .ToString();
            var session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));

            try
            {
                await session.RunAsync(cypher, new Dictionary <string, object>() { { "suggestions", ParameterSerializer.ToDictionary(suggestions) } });
            }
            finally { await session.CloseAsync(); }
        }
Esempio n. 15
0
        //Consultada sugerencias de un usuario
        public async Task <List <SuggestionInfo> > getSuggestion(IList <User> users)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $users AS user")
                            //Identifica usuario
                            .AppendLine("MATCH (myuser:User {id: user.id})")
                            //Traer sugerencias que tiene el usuario
                            .AppendLine("MATCH ((myuser)-[:GET]-(s:Suggestion)-[:SUGGEST]-(u:User))")
                            //Filtrar que no esten desactivados
                            .AppendLine("WHERE NOT(s.isActive = false)")
                            .AppendLine("RETURN u.id, u.name, s.id, s.isActive")
                            .ToString();
            var session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            List <SuggestionInfo> mySuggInfoList = new List <SuggestionInfo>();

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "users", ParameterSerializer.ToDictionary(users) } });

                while (await reader.FetchAsync())
                {
                    int      count = 0;
                    string[] aux   = new string[3];
                    foreach (var item in reader.Current.Values)
                    {
                        if (count == 3)
                        {
                            User myuser = new User {
                                Id   = aux[0],
                                Name = aux[1]
                            };
                            Suggestion mySugg = new Suggestion {
                                Id       = aux[2],
                                IsActive = (bool)item.Value
                            };
                            SuggestionInfo SuggInfo = new SuggestionInfo
                            {
                                Suggestion    = mySugg,
                                SuggestedUser = myuser
                            };
                            mySuggInfoList.Add(SuggInfo);
                        }
                        else
                        {
                            aux[count] = item.Value.ToString();
                        }
                        count++;
                    }
                }
            }
            finally { await session.CloseAsync(); }
            return(mySuggInfoList);
        }
Esempio n. 16
0
        //Pasa el estado de activado a desactivado
        public async Task <string> ChangeIsActive(IList <Suggestion> suggestions)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $suggestions AS suggestion")
                            .AppendLine("MATCH (s:Suggestion {id: suggestion.id})")
                            .AppendLine("SET s.isActive = false")
                            .AppendLine("RETURN s.id, s.isActive")
                            .ToString();
            var    session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            string ans     = "";

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "suggestions", ParameterSerializer.ToDictionary(suggestions) } });

                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        ans += item.Value.ToString() + " ";
                    }
                }
            }
            finally { await session.CloseAsync(); }
            return(ans);
        }
Esempio n. 17
0
        public async Task <string> CreateRelationGatherUser(IList <UserInfo> userMetadata)
        {
            var    session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            string cypher  = new StringBuilder()
                             .AppendLine("UNWIND $userMetadata AS userMetadata")
                             //Find User
                             .AppendLine("MATCH (u:User { id: userMetadata.user.id })")
                             // User GATHER
                             .AppendLine("WITH userMetadata, u")
                             .AppendLine("UNWIND userMetadata.gathers AS gather")
                             .AppendLine("MATCH (g:User { id: gather.id})")
                             .AppendLine("MERGE (u)-[r:GATHER]-(g)")
                             .AppendLine("RETURN u.name, type(r), g.name")
                             .ToString();
            string ans = "";

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "userMetadata", ParameterSerializer.ToDictionary(userMetadata) } });

                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        ans += item.Value.ToString() + " ";
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(ans);
        }
Esempio n. 18
0
        /*DB operations*/

        public async Task <string> CreateUser(IList <User> users)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $users AS user")
                            .AppendLine("CREATE(u:User{id: user.id, name: user.name})")
                            .AppendLine("RETURN u.id")
                            .ToString();
            var    session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            string ans     = "";

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "users", ParameterSerializer.ToDictionary(users) } });

                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        ans += item.Value.ToString();
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(ans);
        }
        public async Task <List <bool> > ExistCategory(IList <Category> categories)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $categories AS category")
                            .AppendLine("OPTIONAL MATCH (c:Category{name: category.name})")
                            .AppendLine("RETURN c.name")
                            .ToString();
            var         session  = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            List <bool> boolList = new List <bool>();

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "categories", ParameterSerializer.ToDictionary(categories) } });

                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        if (item.Value == null)
                        {
                            boolList.Add(false);
                        }
                        else
                        {
                            boolList.Add(true);
                        }
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(boolList);
        }
        public async Task <List <Like> > LikeByCategory(IList <Category> categories)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $categories AS category")
                            .AppendLine("MATCH (c:Category{name: category.name})")
                            .AppendLine("MATCH (c)<-[:IS]-(l:Like)")
                            .AppendLine("RETURN l.name")
                            .ToString();
            var         session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            List <Like> myLikes = new List <Like>();

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "categories", ParameterSerializer.ToDictionary(categories) } });

                System.Console.WriteLine("****Likes by category:");
                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        Like auxLike = new Like
                        {
                            Name = item.Value.ToString()
                        };
                        System.Console.WriteLine(item.Value.ToString());
                        myLikes.Add(auxLike);
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(myLikes);
        }
Esempio n. 21
0
        //Crear relaciones
        private async Task <string> reportRelationship(IList <ReportInfo> reportInfos)
        {
            System.Console.WriteLine(reportInfos.Count);
            string cypher = new StringBuilder()
                            //Etiqueta todo
                            .AppendLine("UNWIND $reportInfos AS reportInfo")
                            //Encontrar usuarios
                            .AppendLine("MATCH (us:User { id: reportInfo.user.id})")
                            .AppendLine("MATCH (usr:User { id: reportInfo.userReported.id})")
                            .AppendLine("MATCH (r:Report { id: reportInfo.report.id})")
                            //Relacion
                            .AppendLine("WITH us, usr,r")
                            .AppendLine("MERGE (us)-[re:DOES_REPORT]->(r)")
                            .AppendLine("MERGE (r)-[rep:REPORTS]->(usr)")
                            .AppendLine("RETURN us.name, type(re), r.id, type(rep), usr.name")
                            .ToString();
            var    session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            string ans     = "";

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "reportInfos", ParameterSerializer.ToDictionary(reportInfos) } });

                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        System.Console.WriteLine(item.Value.ToString());
                        ans += item.Value.ToString() + " ";
                    }
                }
            }
            finally { await session.CloseAsync(); }
            return(ans);
        }
        /*DB operations*/

        public async Task <string> CreateCategory(IList <Category> categories)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $categories AS category")
                            .AppendLine("CREATE(c:Category{name: category.name})")
                            .AppendLine("RETURN c.name")
                            .ToString();
            var    session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            string ans     = "";

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "categories", ParameterSerializer.ToDictionary(categories) } });

                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        ans += item.Value.ToString() + " ";
                    }
                }
            }
            finally { await session.CloseAsync(); }
            return(ans);
        }
Esempio n. 23
0
        public async Task CreateGenres(IList <Genre> genres)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {genres} AS genre")
                            .AppendLine("MERGE (g:Genre {name: genre.name})")
                            .AppendLine("SET g = genre")
                            .ToString();

            using (var session = driver.Session())
            {
                await session.RunAsync(cypher, new Dictionary <string, object>() { { "genres", ParameterSerializer.ToDictionary(genres) } });
            }
        }
        public async Task <List <User> > GetUsers(IList <Category> categories)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $categories AS category")
                            .AppendLine("MATCH (c:Category { name: category.name })")
                            .AppendLine("MATCH (c)<-[:IS]-(l:Like)")
                            .AppendLine("WITH c,l")
                            .AppendLine("MATCH (l)<-[:LIKE]-(u:User)")
                            .AppendLine("RETURN u.id, u.name")
                            .ToString();
            var         session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            List <User> users   = new List <User>();

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "categories", ParameterSerializer.ToDictionary(categories) } });

                while (await reader.FetchAsync())
                {
                    int    count = 0;
                    string id    = "";
                    // Each current read in buffer can be reached via Current
                    foreach (var item in reader.Current.Values)
                    {
                        if ((count % 2 != 0))
                        {
                            User aux = new User
                            {
                                Id   = id,
                                Name = item.Value.ToString()
                            };
                            users.Add(aux);
                        }
                        else
                        {
                            id = item.Value.ToString();
                        }
                        System.Console.WriteLine(item.Value);
                        count++;
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(users);
        }
Esempio n. 25
0
        public async Task CreateRelationships(IList <MovieInformation> metadatas)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND {metadatas} AS metadata")
                            // Find the Movie:
                            .AppendLine("MATCH (m:Movie { title: metadata.movie.title })")
                            // Create Cast Relationships:
                            .AppendLine("UNWIND metadata.cast AS actor")
                            .AppendLine("MATCH (a:Person { name: actor.name })")
                            .AppendLine("MERGE (a)-[r:ACTED_IN]->(m)")
                            // Create Director Relationship:
                            .AppendLine("WITH metadata, m")
                            .AppendLine("MATCH (d:Person { name: metadata.director.name })")
                            .AppendLine("MERGE (d)-[r:DIRECTED]->(m)")
                            // Add Genres:
                            .AppendLine("WITH metadata, m")
                            .AppendLine("UNWIND metadata.genres AS genre")
                            .AppendLine("MATCH (g:Genre { name: genre.name})")
                            .AppendLine("MERGE (m)-[r:GENRE]->(g)")
                            .ToString();


            using (var session = driver.Session())
            {
                await session.RunAsync(cypher, new Dictionary <string, object>() { { "metadatas", ParameterSerializer.ToDictionary(metadatas) } });
            }
        }
Esempio n. 26
0
        public async Task <string> CreateRelationshipLike(IList <LikeInfo> likeMetadata)
        {
            string cypher = new StringBuilder()
                            .AppendLine("UNWIND $likeMetadata AS likeMetadata")
                            //find like
                            .AppendLine("MATCH (l:Like { name: likeMetadata.like.name })")
                            //category
                            .AppendLine("WITH likeMetadata, l")
                            .AppendLine("UNWIND likeMetadata.category AS category")
                            .AppendLine("MATCH (c:Category { name: category.name})")
                            .AppendLine("MERGE (l)-[r:IS]->(c)")
                            .AppendLine("RETURN l.name, type(r), c.name")
                            .ToString();
            var    session = client.GetDriver().AsyncSession(o => o.WithDatabase("neo4j"));
            string ans     = "";

            try
            {
                var reader = await session.RunAsync(cypher, new Dictionary <string, object>() { { "likeMetadata", ParameterSerializer.ToDictionary(likeMetadata) } });

                while (await reader.FetchAsync())
                {
                    foreach (var item in reader.Current.Values)
                    {
                        ans += item.Value.ToString() + " ";
                    }
                }
            }
            finally
            {
                await session.CloseAsync();
            }
            return(ans);
        }