Beispiel #1
0
        /// <summary>
        /// Adds the given node to the graph.
        /// PRECONDITION: The node must be valid
        /// </summary>
        /// <param name="node">The BasicNodeModel of the node to add to the graph</param>
        /// <returns>Returns true if the node was added to the graph successfully</returns>
        public bool AddNode(BasicNodeModel node)
        {
            // The total number of nodes that should be created
            int expectedNodeCount         = node.Relationships.Where(x => x.IsNewAddition).Count() + 1;
            int expectedRelationshipCount = node.Relationships.Count();
            int actualNodeCount           = 0;
            int actualRelationshipCount   = 0;
            // Get the statements that need to be run
            List <Statement> creationStatements = GenerateStatements(node);

            // Create the session
            using (ISession session = driver.Session())
            {
                // Add the source node
                session.WriteTransaction(action =>
                {
                    IStatementResult nodeResult = action.Run(new Statement($"CREATE (s:{node.GetNodeLabels()} $props)",
                                                                           new Dictionary <string, object> {
                        { "props", node.GetPropertyMap() }
                    }));
                    actualNodeCount += nodeResult.Summary.Counters.NodesCreated;
                    // Only continue if the node was added
                    if (actualNodeCount == 1)
                    {
                        // For each of the relationship statements,
                        foreach (Statement relStatement in creationStatements)
                        {
                            // Run the statement
                            IStatementResult relResult = action.Run(relStatement);
                            // Update the counts of the actual node and relationship values
                            actualNodeCount         += relResult.Summary.Counters.NodesCreated;
                            actualRelationshipCount += relResult.Summary.Counters.RelationshipsCreated;
                        }
                    }

                    // If the node and relationship counts do not equal the expected values,
                    if (actualNodeCount != expectedNodeCount || actualRelationshipCount != expectedRelationshipCount)
                    {
                        // This transaction has failed
                        action.Failure();
                    }
                    else
                    {
                        // This transaction has succeeded
                        action.Success();
                    }
                });
            }

            return(actualNodeCount == expectedNodeCount && actualRelationshipCount == expectedRelationshipCount);
        }
Beispiel #2
0
 private bool Update(ITransaction transaction, BasicNodeModel node)
 {
     return(transaction.Run(BuildNodeCreationQuery(node), new { props = node.GetPropertyMap() }).Summary.Counters.NodesCreated > 0);
 }
Beispiel #3
0
        /// <summary>
        /// Adds the given node to the graph.
        /// PRECONDITION: The node must be valid
        /// </summary>
        /// <param name="node">The BasicNodeModel of the node to add to the graph</param>
        /// <returns>Returns true if the node was added to the graph successfully</returns>
        public bool AddNode(BasicNodeModel node)
        {
            INode sessionResult;

            // Create the session
            using (ISession session = driver.Session())
            {
                // Run the statement in a transaction
                sessionResult = session.WriteTransaction(action =>
                {
                    // Create the node
                    IStatementResult result = action.Run(BuildNodeCreationQuery(node), new { props = node.GetPropertyMap() });
                    return(result.Single()[0].As <INode>());
                });
            }

            Program.LogInColor($"Neo4j: Added {node.CommonName} with id {node.Id} to graph.", ConsoleColor.DarkGreen);
            return(sessionResult != null);
        }