示例#1
0
        public static HttpStatusCode Post(GremlinScript script)
        {
            string response;
            var    status = Rest.HttpRest.Post(ConnectionManager.Connection().GremlinUrl, script.GetScript(), out response);

            return(status);
        }
示例#2
0
        public void FilterClause()
        {
            GremlinScript script = new GremlinScript();
            script.Append("g.v(0)")
                .Filter(it => it.GetProperty("MyProp") == "SomeValue");

            Assert.IsTrue(script.ToString() == "g.v(0).filter{it.getProperty('MyProp') == 'SomeValue'}");
        }
示例#3
0
        public void FilterClauseCompareToIgnoreCase2()
        {
            GremlinScript script = new GremlinScript();
            script.Append("g.v(0)")
                .Filter(it => it.GetProperty("MyProp").CompareToIgnoreCase(it.GetProperty("CompareProperty").Name()));

            Assert.IsTrue(script.ToString() == "g.v(0).filter{it.getProperty('MyProp').compareToIgnoreCase(it.getProperty('CompareProperty').name)}");
        }
示例#4
0
        public void FilterClauseChainCommands()
        {
            GremlinScript script = new GremlinScript();
            script.Append("g.v(0)")
                .Filter(it => it.GetProperty("MyProp").ToLowerCase().Contains("ContainThis"));

            Assert.IsTrue(script.ToString() == "g.v(0).filter{it.getProperty('MyProp').toLowerCase().contains('ContainThis')}");
        }
示例#5
0
        public static GremlinScript RelationshipIndexLookup(this GremlinScript query, IEnumerable <KeyValuePair <string, string> > propertyKeyValue)
        {
            var sb = new StringBuilder();

            foreach (var kvp in propertyKeyValue)
            {
                sb.AppendFormat("{0}'{1}':'{2}'", sb.Length > 0 ? "," : "", kvp.Key, kvp.Value);
            }

            return(query.Append("g.E[[{0}]]", sb.ToString()));
        }
示例#6
0
        public static GremlinScript In(this GremlinScript query, params string[] relationshipName)
        {
            if (relationshipName.Count() == 1)
            {
                return(query.Append(".in({0})", relationshipName[0]));
            }

            // Can not send more then one element via parameters
            var relationships = relationshipName.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}'{0}'", item, sb.Length == 0 ? string.Empty : ","));

            return(query.Append(string.Format(".in({0})", relationships)));
        }
示例#7
0
        public static GremlinScript NodeIndexLookup(this GremlinScript query, IEnumerable <KeyValuePair <string, object> > propertyKeyValue)
        {
            var sb = new StringBuilder();

            foreach (var kvp in propertyKeyValue)
            {
                if (kvp.Value is string)
                {
                    sb.AppendFormat("{0}'{1}':'{2}'", sb.Length > 0 ? "," : "", kvp.Key, kvp.Value);
                }
                else if (kvp.Value is bool)
                {
                    sb.AppendFormat("{0}'{1}':{2}", sb.Length > 0 ? "," : "", kvp.Key, (bool)kvp.Value ? "true" : "false");
                }
                else
                {
                    sb.AppendFormat("{0}'{1}':{2}", sb.Length > 0 ? "," : "", kvp.Key, kvp.Value);
                }
            }

            return(query.Append("g.V[[{0}]]", sb.ToString()));
        }
示例#8
0
        public static IEnumerable <T> Post <T>(ConnectionElement connection, GremlinScript script) where T : IGraphObject
        {
            var typeParameterType = typeof(T);

            string response;
            var    status = Rest.HttpRest.Post(connection.GremlinUrl, script.GetScript(), out response);

            if (typeParameterType == typeof(Node))
            {
                return((IEnumerable <T>)RestNodeStore.ParseNodeJson(response));
            }

            if (typeParameterType == typeof(Relationship))
            {
                return((IEnumerable <T>)RestRelationshipStore.ParseRelationshipJson(response));
            }

            if (typeParameterType == typeof(Path))
            {
                return((IEnumerable <T>)Path.ParseJson(response));
            }

            throw new Exception("Return type " + typeParameterType + " not implemented");
        }
示例#9
0
 public static GremlinScript gE(this GremlinScript query, Relationship relationship)
 {
     return(gE(query, relationship.Id));
 }
示例#10
0
 public static GremlinScript gE(this GremlinScript query, IEnumerable <long> Ids)
 {
     return(query.Append("g.e({0})", string.Join(",", Ids)));
 }
示例#11
0
 public static GremlinScript ToPipe(this GremlinScript query)
 {
     return(query.Append("._()"));
 }
示例#12
0
        public void Table()
        {
            GremlinScript script = new GremlinScript();
            script.NewTable("t")
                .NodeIndexLookup(new Dictionary<string, object>() { { NodeProperty.FirstName.ToString() , "Jack" }, { NodeProperty.LastName.ToString(), "Shaw" } })
                .Filter(it => it.GetProperty(NodeProperty.UID.ToString()) == "jshaw")
                .As("UserNode")
                .OutE(RelationshipType.Likes.ToString())
                .As("LikeRel")
                .InV()
                .As("FriendNode")
                .Table("t", "UserNode", "LikeRel", "FriendNode")
                .Append("{{it}}{{it.getProperty('{0}')}}{{it.getProperty('{1}')}} >> -1; t", RelationshipProperty.Date.ToString(), NodeProperty.FirstName.ToString());

            Assert.IsTrue(script.ToString() == "t = new Table();g.V[['FirstName':'Jack','LastName':'Shaw']].filter{it.getProperty('UID') == 'jshaw'}.as('UserNode').outE('Likes').as('LikeRel').inV().as('FriendNode').table(t, ['UserNode','LikeRel','FriendNode']){it}{it.getProperty('Date')}{it.getProperty('FirstName')} >> -1; t");
        }
示例#13
0
        public static DataTable GetTable(ConnectionElement connection, GremlinScript script)
        {
            string response;
            var    status = Rest.HttpRest.Post(connection.GremlinUrl, script.GetScript(), out response);

            var joResponse = JObject.Parse(response);
            var jaColumns  = (JArray)joResponse["columns"];
            var jaData     = (JArray)joResponse["data"];

            var dt = new DataTable();

            var initColumns = true;
            var colIndex    = 0;

            foreach (JArray jRow in jaData)
            {
                var row = new List <object>();
                foreach (var jCol in jRow)
                {
                    switch (jCol.Type)
                    {
                    case JTokenType.String:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Object:
                        if (jCol["self"] == null)
                        {
                            row.Add(jCol.ToString());

                            if (initColumns)
                            {
                                dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                                colIndex++;
                            }
                        }
                        else
                        {
                            string   self      = jCol["self"].ToString();
                            string[] selfArray = self.Split('/');
                            if (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "node")
                            {
                                row.Add(RestNodeStore.CreateNodeFromJson((JObject)jCol));

                                if (initColumns)
                                {
                                    dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(Node));
                                    colIndex++;
                                }
                            }
                            else if (selfArray.Length > 2 && selfArray[selfArray.Length - 2] == "relationship")
                            {
                                row.Add(RestRelationshipStore.CreateRelationshipFromJson((JObject)jCol));

                                if (initColumns)
                                {
                                    dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(Relationship));
                                    colIndex++;
                                }
                            }
                            else
                            {
                                // Not a Node or Relationship - return as string
                                row.Add(jCol.ToString());

                                if (initColumns)
                                {
                                    dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                                    colIndex++;
                                }
                            }
                        }
                        break;

                    case JTokenType.Integer:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(int));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Float:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(float));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Date:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(DateTime));
                            colIndex++;
                        }
                        break;

                    case JTokenType.Boolean:
                        row.Add(jCol.ToString());
                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(bool));
                            colIndex++;
                        }
                        break;

                    default:
                        row.Add(jCol.ToString());

                        if (initColumns)
                        {
                            dt.Columns.Add(jaColumns[colIndex].ToString(), typeof(string));
                            colIndex++;
                        }
                        break;
                    }
                }

                initColumns = false;
                var dtRow = dt.NewRow();
                dtRow.ItemArray = row.ToArray();
                dt.Rows.Add(dtRow);
            }

            return(dt);
        }
示例#14
0
 public static GremlinScript Aggregate(this GremlinScript query, string variable)
 {
     return(query.Append(".aggregate({0})", variable));
 }
示例#15
0
 public static GremlinScript In(this GremlinScript query, params Enum[] relationshipName)
 {
     return(In(query, relationshipName.Select(r => r.ToString()).ToArray()));
 }
示例#16
0
 public static GremlinScript Table(this GremlinScript query, string name)
 {
     return(query.Append(string.Format(".table({0})", name)));
 }
示例#17
0
 public static GremlinScript Array <T>(this GremlinScript query, params T[] items)
 {
     return(query.Append(string.Format("[{0}]",
                                       items.Aggregate(new StringBuilder(), (sb, item) => sb.AppendFormat("{1}{2}{0}{2}", item, sb.Length > 0 ? "," : string.Empty, typeof(T) == typeof(string) ? "'" : string.Empty)))));
 }
示例#18
0
 public static GremlinScript gV(this GremlinScript query, Node node)
 {
     return(gV(query, node.Id));
 }
示例#19
0
 public static GremlinScript Retain(this GremlinScript query, string variable)
 {
     return(query.Append(".retain({0})", variable));
 }
示例#20
0
 public static GremlinScript Except(this GremlinScript query, string variable)
 {
     return(query.Append(".except({0})", variable));
 }
示例#21
0
 public static GremlinScript gE(this GremlinScript query, IEnumerable <Relationship> relationships)
 {
     return(gE(query, relationships.Select(s => s.Id)));
 }
示例#22
0
 public static GremlinScript In(this GremlinScript query)
 {
     return(query.Append(".in()"));
 }
示例#23
0
 public static GremlinScript Unique(this GremlinScript query)
 {
     return(query.Append(".unique()"));
 }
示例#24
0
        public static GremlinScript Table(this GremlinScript query, string name, params string[] columnNames)
        {
            string names = string.Join(",", columnNames.Select(r => string.Concat("'", r, "'")));

            return(query.Append(string.Format(".table({0}, [{1}])", name, names)));
        }
示例#25
0
 public static GremlinScript As(this GremlinScript query, string label)
 {
     return(query.Append(".as({0})", label));
 }
示例#26
0
 public static IEnumerable <T> Post <T>(GremlinScript script) where T : IGraphObject
 {
     return(Post <T>(ConnectionManager.Connection(), script));
 }
示例#27
0
 public static GremlinScript gV(this GremlinScript query, IEnumerable <Node> nodes)
 {
     return(gV(query, nodes.Select(s => s.Id)));
 }
示例#28
0
 public static DataTable GetTable(GremlinScript script)
 {
     return(GetTable(ConnectionManager.Connection(), script));
 }
示例#29
0
        public void FilterClauseCompound()
        {
            GremlinScript script = new GremlinScript();
            script.Append("g.v(0)")
                .Filter(it => it.GetProperty("MyProp") && !it.GetProperty("NotProp") || it.GetProperty("OrProp").ToLowerCase() == "lower Value");

            Assert.IsTrue(script.ToString() == "g.v(0).filter{it.getProperty('MyProp') && !it.getProperty('NotProp') || it.getProperty('OrProp').toLowerCase() == 'lower Value'}");
        }
示例#30
0
        static void Main(string[] args)
        {
            //var dnode = Node.GetNode(81);

            //var node = Node.CreateNode("MyType");
            //node.Properties.SetProperty("myDate", new DateTime(2011, 12, 15));

            //node.SaveProperties();

            //Neo4jRestNet.CleanDbPlugin.CleanDbPlugin.CleanDb();

            // Get Root Node from graphDB
            var rootNode = Node.GetRootNode();

            // Create a User Node with no Properties
            var nodeUser = Node.CreateNode(NodeType.User.ToString());

            // Create a User Node with Properties
            var prop = new Properties();
            prop.SetProperty(NodeProperty.FirstName.ToString(), "Joe");
            prop.SetProperty(NodeProperty.LastName.ToString(), "Smith");

            var nodeUserWithName = Node.CreateNode(NodeType.User.ToString(), prop);

            // Create Relationships to Nodes
            rootNode.CreateRelationshipTo(nodeUser, RelationshipType.Likes.ToString());
            rootNode.CreateRelationshipTo(nodeUserWithName, RelationshipType.Likes.ToString());

            // Create Relationship with Properties
            var relProp = new Properties();
            relProp.SetProperty(RelationshipProperty.Name.ToString(), "MyRelationship");
            relProp.SetProperty("CustomRelProp", "CustomPropValue");

            nodeUserWithName.CreateRelationshipTo(nodeUser, RelationshipType.Knows.ToString(), relProp);

            // Get Id From Node
            var id = rootNode.Id;
            var geid = rootNode.EncryptedId;

            // Gremlin

            // Same as above
            var sameLikeNodes = Gremlin.Post<Node>(new GremlinScript(rootNode).Out(RelationshipType.Likes.ToString()));

            // More Gremlin example
            var script = new GremlinScript(rootNode);
            script.OutE()
                .InV()
                .OutE()
                .Filter("it.getProperty('{0}') == '{1}'", RelationshipProperty.Name, "MyRelationship");

            var myRelationship = Gremlin.Post<Relationship>(script);

            // More Gremlin example
            var script1 = new GremlinScript(rootNode);
            script1.OutE()
                .InV()
                .OutE()
                .Filter(it => it.GetProperty(RelationshipProperty.Name.ToString()) == "MyRelationship");

            IEnumerable<Relationship> myRelationship1 = Gremlin.Post<Relationship>(script1);

            // Gremlin returning a datatable
            var tblScript = new GremlinScript();
            tblScript.NewTable("t")
                .gV(rootNode)
                .Out(RelationshipType.Likes.ToString())
                .As("Like")
                .Table("t", "Like")
                .Append(" >> -1; t;");

            DataTable dt = Gremlin.GetTable(tblScript);

            // Basic Cypher query
            var c1 = new Cypher();
            c1.Start(s => s.Node("A", 0));
            c1.Return( r => r.Node("A"));

            DataTable tbl = c1.Post();

            // Cypher with Match clause
            var c2 = new Cypher();
            c2.Start(s => s.Node("A", 0));
            c2.Match(m => m.Node("A").To("r", "Likes").Node("B"));
            c2.Return(r => r.Node("A").Relationship("r").Node("B"));

            tbl = c2.Post();

            // Cypher with multi start and return optional property
            var c3 = new Cypher();
            c3.Start(s => s.Node("A", 0, 1));
            c3.Match(m => m.Node("A").Any("r", "Likes").Node("C"));
            //	c3.Return(r => r.Node("C").Node("C").Property("Name?"));

            tbl = c3.Post();

            // Multi Start
            var c4 = new Cypher();
            c4.Start(s => s.Node("A", 0).Node("B",1));
            c4.Return(r => r.Node("A").Node("B"));

            tbl = c4.Post();

            // Cypher with Where clause
            var c5 = new Cypher();
            c5.Start(s => s.Node("A", 0, 1));
            c5.Where(w => w.Node("A").Property("Age?") < 30 && w.Node("A").Property("Name?") == "Tobias" || !(w.Node("A").Property("Name?") == "Tobias"));
            c5.Return(r => r.Node("A"));

            tbl = c5.Post();

            // Alt Syntax
            var c6 = new Cypher();
            c6.Start(s =>	{
                                s.Node("A", 0);
                                s.Node("B", 1);
                                return s;
                            });

            c6.Return(r =>	{
                                r.Node("A");
                                r.Node("B");
                                return r;
                            });

            tbl = c6.Post();

            // Alt Syntax
            var c7 = new Cypher();
            c7.Start(s => s.Node("MyNode", "Index-Name", "QueryString"));
            c7.Start(s => s.Node("A", 0));
            c7.Start(s => s.Node("B", 1));

            c7.Return(r => r.Node("A"));
            c7.Return(r => r.Node("B"));

            tbl = c7.Post();

            // Test Cypher Dates
            var date = new DateTime(2011, 12, 15);

            //// Basic Cypher query
            //var c1 = new Cypher();
            //c1.Start(s => s.Node("A", 81));
            //c1.Where(w => w.Node("A").Property("myDate") < date.AddDays(2)
            //    && w.Node("A").Property("myDate") == new DateTime(2011, 12, 15));

            //c1.Return(r => r.Node("A"));

            //tbl = c1.Post();
        }
示例#31
0
 public static GremlinScript Each(this GremlinScript query, string each, params object[] args)
 {
     return(query.Append(string.Format(".each{{{0}}}", string.Format(each, args))));
 }
示例#32
0
 public static GremlinScript NewTable(this GremlinScript query, string name)
 {
     return(query.Append(string.Format("{0} = new Table();", name)));
 }
示例#33
0
 public static GremlinScript ToList(this GremlinScript query)
 {
     return(query.Append(".toList()"));
 }
示例#34
0
 public static GremlinScript NewArray(this GremlinScript query, string name)
 {
     return(query.Append(string.Format("{0} = [];", name)));
 }
示例#35
0
 public static GremlinScript gE(this GremlinScript query, long Id)
 {
     return(query.Append("g.e({0})", Id));
 }
示例#36
0
        public void FilterClauseEqualBool()
        {
            GremlinScript script = new GremlinScript();
            script.Append("g.v(0)")
                .Filter(it => it.GetProperty("MyProp") == true);

            Assert.IsTrue(script.ToString() == "g.v(0).filter{it.getProperty('MyProp') == true}");
        }
示例#37
0
 public static GremlinScript Reverse(this GremlinScript query)
 {
     return(query.Append(".reverse()"));
 }