public GremlinChooseOp(Predicate predicate, GraphTraversal2 trueChoice, GraphTraversal2 falseChoice)
 {
     Predicate            = predicate;
     TrueChoiceTraversal  = trueChoice;
     FalseChocieTraversal = falseChoice;
     Type       = ChooseType.Predicate;
     OptionDict = new Dictionary <object, GraphTraversal2>();
 }
 public override void ModulateBy(string key)
 {
     if (Scope == GremlinKeyword.Scope.local)
     {
         throw new SyntaxErrorException("Dedup(local) can't be modulated by by()");
     }
     ByTraversal = GraphTraversal2.__().Values(key);
 }
 public override void ModulateBy(GraphTraversal2 traversal)
 {
     if (Scope == GremlinKeyword.Scope.local)
     {
         throw new SyntaxErrorException("Dedup(local) can't be modulated by by()");
     }
     ByTraversal = traversal;
 }
Beispiel #4
0
 public GraphTraversal2 Choose(GraphTraversal2 traversalPredicate, GraphTraversal2 trueChoice, GraphTraversal2 falseChoice = null)
 {
     if (falseChoice == null)
     {
         falseChoice = __();
     }
     this.AddOperator(new GremlinChooseOp(traversalPredicate, trueChoice, falseChoice));
     return(this);
 }
Beispiel #5
0
 public override void ModulateBy()
 {
     if (Scope == GremlinKeyword.Scope.global)
     {
         ByModulatingMap.Add(new Tuple <object, IComparer>(GraphTraversal2.__(), new IncrOrder()));
     }
     else
     {
         ByModulatingMap.Add(new Tuple <object, IComparer>("", new IncrOrder()));
     }
 }
Beispiel #6
0
        internal override GremlinToSqlContext GetContext()
        {
            GremlinToSqlContext inputContext = GetInputContext();

            if (inputContext.PivotVariable == null)
            {
                throw new QueryCompilationException("The PivotVariable can't be null.");
            }

            if (this.ByModulatingList.Count == 0)
            {
                this.ByModulatingList.Add(new Tuple <GraphTraversal2, IComparer>(GraphTraversal2.__(), new IncrOrder()));
            }

            var newByModulatingList = new List <Tuple <GremlinToSqlContext, IComparer> >();

            foreach (var pair in this.ByModulatingList)
            {
                GraphTraversal2     traversal = pair.Item1;
                GremlinToSqlContext context   = null;

                //g.V().groupCount().order(Local).by(Keys) or g.V().groupCount().order(Local).by(__.select(Keys))
                if (traversal.TranslationOpList.Count >= 2 && traversal.TranslationOpList[1] is GremlinSelectColumnOp)
                {
                    //FROM selectColumn(C._value, "Keys"/"Values")
                    GremlinToSqlContext           newContext = new GremlinToSqlContext();
                    GremlinOrderLocalInitVariable initVar    = new GremlinOrderLocalInitVariable();
                    newContext.VariableList.Add(initVar);
                    newContext.SetPivotVariable(initVar);

                    traversal.GetStartOp().InheritedContextFromParent(newContext);
                    context = traversal.GetEndOp().GetContext();
                }
                else
                {
                    //FROM decompose1(C._value)
                    GremlinToSqlContext       newContext = new GremlinToSqlContext();
                    GremlinDecompose1Variable decompose1 = new GremlinDecompose1Variable(inputContext.PivotVariable);
                    newContext.VariableList.Add(decompose1);
                    newContext.TableReferences.Add(decompose1);
                    newContext.SetPivotVariable(decompose1);

                    traversal.GetStartOp().InheritedContextFromParent(newContext);
                    context = traversal.GetEndOp().GetContext();
                }

                newByModulatingList.Add(new Tuple <GremlinToSqlContext, IComparer>(context, pair.Item2));
            }

            inputContext.PivotVariable.OrderLocal(inputContext, newByModulatingList);

            return(inputContext);
        }
Beispiel #7
0
 public GraphTraversal2 Repeat(GraphTraversal2 repeatTraversal)
 {
     if (GetEndOp() is GremlinRepeatOp)
     {
         (GetEndOp() as GremlinRepeatOp).RepeatTraversal = repeatTraversal;
     }
     else
     {
         AddGremlinOperator(new GremlinRepeatOp(repeatTraversal));
     }
     return(this);
 }
Beispiel #8
0
 public GraphTraversal2 Option(object pickToken, GraphTraversal2 traversalOption)
 {
     if (LastGremlinTranslationOp is GremlinChooseOp)
     {
         (LastGremlinTranslationOp as GremlinChooseOp).OptionDict[pickToken] = traversalOption;
         return(this);
     }
     else
     {
         throw new Exception("Option step only can follow by choose step.");
     }
 }
Beispiel #9
0
        public GraphTraversal2 From(GraphTraversal2 fromVertexTraversal)
        {
            GremlinAddEOp addEOp = GetEndOp() as GremlinAddEOp;

            if (addEOp != null)
            {
                addEOp.FromVertexTraversal = fromVertexTraversal;
            }
            else
            {
                throw new SyntaxErrorException($"{GetEndOp()} cannot be cast to GremlinAddEOp");
            }
            return(this);
        }
Beispiel #10
0
        public GraphTraversal2 From(string fromLabel)
        {
            GremlinAddEOp addEOp = GetEndOp() as GremlinAddEOp;

            if (addEOp != null)
            {
                addEOp.FromVertexTraversal = GraphTraversal2.__().Select(fromLabel);
            }
            else
            {
                throw new SyntaxErrorException($"{GetEndOp()} cannot be cast to GremlinAddEOp");
            }
            return(this);
        }
Beispiel #11
0
 public GraphTraversal2 Until(GraphTraversal2 untilTraversal)
 {
     if (GetEndOp() is GremlinRepeatOp)
     {
         (GetEndOp() as GremlinRepeatOp).TerminationTraversal = untilTraversal;
     }
     else
     {
         AddGremlinOperator(new GremlinRepeatOp());
         (GetEndOp() as GremlinRepeatOp).TerminationTraversal = untilTraversal;
         (GetEndOp() as GremlinRepeatOp).StartFromContext     = true;
     }
     return(this);
 }
        internal void ConfigureStartAndEndSteps(GraphTraversal2 whereTraversal)
        {
            if (whereTraversal.TranslationOpList.Count >= 2)
            {
                //__.as()
                GremlinAsOp asOp = whereTraversal.TranslationOpList[1] as GremlinAsOp;
                if (asOp != null)
                {
                    whereTraversal.TranslationOpList.RemoveAt(1); //remove as-step
                    whereTraversal.InsertOperator(1, new GremlinSelectOp(GremlinKeyword.Pop.Last, asOp.Labels.First()));
                }

                //__.Or()
                GremlinOrOp orOp = whereTraversal.TranslationOpList[1] as GremlinOrOp;
                if (orOp != null)
                {
                    foreach (var traversal in orOp.OrTraversals)
                    {
                        ConfigureStartAndEndSteps(traversal);
                    }
                }

                //__.And()
                GremlinAndOp andOp = whereTraversal.TranslationOpList[1] as GremlinAndOp;
                if (andOp != null)
                {
                    foreach (var traversal in andOp.AndTraversals)
                    {
                        ConfigureStartAndEndSteps(traversal);
                    }
                }

                //__.Not()
                GremlinNotOp notOp = whereTraversal.TranslationOpList[1] as GremlinNotOp;
                if (notOp != null)
                {
                    ConfigureStartAndEndSteps(notOp.NotTraversal);
                }
            }

            var lastOp = WhereTraversal.GetEndOp() as GremlinAsOp;

            if (lastOp != null)
            {
                string label = lastOp.Labels.First();
                whereTraversal.TranslationOpList.Remove(whereTraversal.GetEndOp()); //remove the last as-step
                whereTraversal.AddOperator(new GremlinWherePredicateOp(Predicate.eq(label)));
            }
        }
Beispiel #13
0
 public override void ModulateBy(GremlinKeyword.Column column)
 {
     if (GroupBy == null)
     {
         GroupBy = GraphTraversal2.__().Select(column);
     }
     else if (ProjectBy == null)
     {
         ProjectBy = GraphTraversal2.__().Select(column);
     }
     else
     {
         throw new QueryCompilationException("The key and value traversals for group()-step have already been set");
     }
 }
Beispiel #14
0
 public override void ModulateBy(string key)
 {
     if (GroupBy == null)
     {
         GroupBy = GraphTraversal2.__().Values(key);
     }
     else if (ProjectBy == null)
     {
         ProjectBy = GraphTraversal2.__().Values(key);
     }
     else
     {
         throw new QueryCompilationException("The key and value traversals for group()-step have already been set");
     }
 }
Beispiel #15
0
 public override void ModulateBy(GraphTraversal2 traversal)
 {
     if (GroupBy == null)
     {
         GroupBy = traversal;
     }
     else if (ProjectBy == null)
     {
         ProjectBy = traversal;
         IsProjectingACollection = false;
     }
     else
     {
         throw new QueryCompilationException("The key and value traversals for group()-step have already been set");
     }
 }
Beispiel #16
0
 public GraphTraversal2 Emit(GraphTraversal2 emitTraversal)
 {
     if (GetEndOp() is GremlinRepeatOp)
     {
         (GetEndOp() as GremlinRepeatOp).IsEmit        = true;
         (GetEndOp() as GremlinRepeatOp).EmitTraversal = emitTraversal;
     }
     else
     {
         AddGremlinOperator(new GremlinRepeatOp());
         (GetEndOp() as GremlinRepeatOp).IsEmit        = true;
         (GetEndOp() as GremlinRepeatOp).EmitTraversal = emitTraversal;
         (GetEndOp() as GremlinRepeatOp).EmitContext   = true;
     }
     return(this);
 }
Beispiel #17
0
 public GraphTraversal2 Repeat(GraphTraversal2 repeatTraversal)
 {
     if (GetEndOp() is GremlinRepeatOp)
     {
         //until().repeat()
         //emit().repeat()
         (GetEndOp() as GremlinRepeatOp).RepeatTraversal = repeatTraversal;
     }
     else
     {
         //repeat().until()
         //repeat().emit()
         this.AddOperator(new GremlinRepeatOp(repeatTraversal));
     }
     return(this);
 }
Beispiel #18
0
        public override void ModulateBy(GraphTraversal2 traversal, GremlinKeyword.Order order)
        {
            switch (order)
            {
            case GremlinKeyword.Order.Incr:
                ByModulatingMap.Add(new Tuple <object, IComparer>(traversal, new IncrOrder()));
                break;

            case GremlinKeyword.Order.Decr:
                ByModulatingMap.Add(new Tuple <object, IComparer>(traversal, new DecrOrder()));
                break;

            case GremlinKeyword.Order.Shuffle:
                ByModulatingMap.Add(new Tuple <object, IComparer>(traversal, new ShuffleOrder()));
                break;
            }
        }
        public virtual void ModulateBy(GraphTraversal2 traversal, GremlinKeyword.Order order)
        {
            switch (order)
            {
            case GremlinKeyword.Order.Incr:
                ModulateBy(traversal, new IncrOrder());
                break;

            case GremlinKeyword.Order.Decr:
                ModulateBy(traversal, new DecrOrder());
                break;

            case GremlinKeyword.Order.Shuffle:
                ModulateBy(traversal, new ShuffleOrder());
                break;
            }
        }
Beispiel #20
0
        /// <summary>
        /// Only valid for VertexProperty
        /// </summary>
        internal virtual void HasKeyOrValue(GremlinToSqlContext currentContext, GremlinHasType hasType, List <object> valuesOrPredicates)
        {
            GraphTraversal2 traversal2 = hasType == GremlinHasType.HasKey ? GraphTraversal2.__().Key() : GraphTraversal2.__().Value();

            traversal2.GetStartOp().InheritedVariableFromParent(currentContext);
            GremlinToSqlContext existContext = traversal2.GetEndOp().GetContext();

            List <WBooleanExpression> booleanExprList         = new List <WBooleanExpression>();
            GremlinVariableProperty   defaultVariableProperty = existContext.PivotVariable.DefaultProjection();

            foreach (var valuesOrPredicate in valuesOrPredicates)
            {
                booleanExprList.Add(CreateBooleanExpression(defaultVariableProperty, valuesOrPredicate));
            }
            existContext.AddPredicate(SqlUtil.ConcatBooleanExprWithOr(booleanExprList));

            currentContext.AddPredicate(SqlUtil.GetExistPredicate(existContext.ToSelectQueryBlock()));
        }
Beispiel #21
0
        internal override GremlinToSqlContext GetContext()
        {
            GremlinToSqlContext inputContext = GetInputContext();

            if (inputContext.PivotVariable == null)
            {
                throw new QueryCompilationException("The PivotVariable can't be null.");
            }

            if (ByList.Count == 0)
            {
                ByList.Add(GraphTraversal2.__());
            }

            inputContext.PivotVariable.Path(inputContext, ByList);

            return(inputContext);
        }
Beispiel #22
0
        public GraphTraversal2 Until(GraphTraversal2 untilTraversal)
        {
            GremlinRepeatOp lastOp = GetEndOp() as GremlinRepeatOp;

            if (lastOp != null)
            {
                lastOp.TerminationTraversal = untilTraversal;
            }
            else
            {
                this.AddOperator(new GremlinRepeatOp()
                {
                    TerminationTraversal = untilTraversal,
                    StartFromContext     = true
                });
            }
            return(this);
        }
Beispiel #23
0
        internal override GremlinToSqlContext GetContext()
        {
            GremlinToSqlContext inputContext = GetInputContext();

            if (inputContext.PivotVariable == null)
            {
                throw new QueryCompilationException("The PivotVariable can't be null.");
            }

            if (ByModulatingMap.Count == 0)
            {
                if (Scope == GremlinKeyword.Scope.global)
                {
                    ByModulatingMap.Add(new Tuple <object, IComparer>(GraphTraversal2.__(), new IncrOrder()));
                }
                else
                {
                    ByModulatingMap.Add(new Tuple <object, IComparer>("", new IncrOrder()));
                }
            }

            var newByModulatingMap = new List <Tuple <object, IComparer> >();

            foreach (var pair in ByModulatingMap)
            {
                if (pair.Item1 is GraphTraversal2)
                {
                    ((GraphTraversal2)pair.Item1).GetStartOp().InheritedVariableFromParent(inputContext);
                    newByModulatingMap.Add(new Tuple <object, IComparer>(((GraphTraversal2)pair.Item1).GetEndOp().GetContext(), pair.Item2));
                }
                else if (pair.Item1 is GremlinKeyword.Column || pair.Item1 == "")
                {
                    newByModulatingMap.Add(new Tuple <object, IComparer>(pair.Item1, pair.Item2));
                }
                else
                {
                    throw new ArgumentException();
                }
            }

            inputContext.PivotVariable.Order(inputContext, newByModulatingMap, Scope);

            return(inputContext);
        }
Beispiel #24
0
 public GraphTraversal2 And(params GraphTraversal2[] andTraversals)
 {
     if (andTraversals.Length == 0)
     {
         //Infix And step
         GraphTraversal2 firstTraversal   = GraphTraversal2.__();
         GraphTraversal2 sencondTraversal = GraphTraversal2.__();
         for (var i = 1; i < this.TranslationOpList.Count; i++) //reserve the first op as the input
         {
             firstTraversal.AddOperator(this.TranslationOpList[i].Copy());
         }
         this.TranslationOpList.RemoveRange(1, this.TranslationOpList.Count - 1);
         this.AddOperator(new GremlinAndInfixOp(firstTraversal, sencondTraversal));
     }
     else
     {
         this.AddOperator(new GremlinAndOp(andTraversals));
     }
     return(this);
 }
Beispiel #25
0
        public GraphTraversal2 Emit(GraphTraversal2 emitTraversal)
        {
            GremlinRepeatOp lastOp = GetEndOp() as GremlinRepeatOp;

            if (lastOp != null)
            {
                lastOp.IsEmit        = true;
                lastOp.EmitTraversal = emitTraversal;
            }
            else
            {
                this.AddOperator(new GremlinRepeatOp()
                {
                    EmitTraversal = emitTraversal,
                    IsEmit        = true,
                    EmitContext   = true
                });
            }
            return(this);
        }
Beispiel #26
0
 public GraphTraversal2 Or(params GraphTraversal2[] orTraversals)
 {
     if (orTraversals.Length == 0)
     {
         //Infix And step
         GraphTraversal2 firstTraversal  = GraphTraversal2.__();
         GraphTraversal2 secondTraversal = GraphTraversal2.__();
         for (var i = 1; i < this.TranslationOpList.Count; i++)
         {
             firstTraversal.AddOperator(this.TranslationOpList[i].Copy());
         }
         this.TranslationOpList.RemoveRange(1, this.TranslationOpList.Count - 1);
         this.AddOperator(new GremlinOrInfixOp(firstTraversal, secondTraversal));
     }
     else
     {
         this.AddOperator(new GremlinOrOp(orTraversals));
     }
     return(this);
 }
Beispiel #27
0
        internal virtual GremlinSelectVariable GetSelectVar(GremlinToSqlContext currentContext, GremlinKeyword.Pop pop, List <string> selectKeys, List <GraphTraversal2> byList = null)
        {
            //TODO: refactor
            if (byList == null)
            {
                byList = new List <GraphTraversal2>()
                {
                    GraphTraversal2.__()
                };
            }
            List <GremlinToSqlContext> byContexts          = new List <GremlinToSqlContext>();
            List <GremlinVariable>     steps               = currentContext.GetGlobalPathStepList();
            List <GremlinVariable>     sideEffectVariables = currentContext.GetSideEffectVariables();

            GremlinGlobalPathVariable pathVariable = new GremlinGlobalPathVariable(steps);

            currentContext.VariableList.Add(pathVariable);
            currentContext.TableReferences.Add(pathVariable);

            foreach (var by in byList)
            {
                GremlinToSqlContext       newContext = new GremlinToSqlContext();
                GremlinDecompose1Variable decompose1 = new GremlinDecompose1Variable(pathVariable);
                newContext.VariableList.Add(decompose1);
                newContext.TableReferences.Add(decompose1);
                newContext.SetPivotVariable(decompose1);

                by.GetStartOp().InheritedContextFromParent(newContext);
                byContexts.Add(by.GetEndOp().GetContext());
            }

            GremlinSelectVariable newVariable = new GremlinSelectVariable(this, pathVariable, sideEffectVariables, pop, selectKeys, byContexts);

            currentContext.VariableList.Add(newVariable);
            currentContext.TableReferences.Add(newVariable);

            return(newVariable);
        }
Beispiel #28
0
        public GraphTraversal2 Option(object pickToken, GraphTraversal2 traversalOption)
        {
            if (!(GremlinUtil.IsNumber(pickToken) || pickToken is string || pickToken is GremlinKeyword.Pick || pickToken is bool))
            {
                throw new ArgumentException();
            }
            var op = GetEndOp() as GremlinChooseOp;

            if (op != null)
            {
                if (op.Options.ContainsKey(pickToken))
                {
                    throw new SyntaxErrorException(
                              $"Choose step can only have one traversal per pick token: {pickToken}");
                }
                op.Options[pickToken] = traversalOption;
                return(this);
            }
            else
            {
                throw new Exception("Option step only can follow by choose step.");
            }
        }
 public override void ModulateBy(GraphTraversal2 traversal)
 {
     ByTraversal = traversal;
 }
 public GremlinAggregateOp(string sideEffectKey)
 {
     SideEffectKey = sideEffectKey;
     ByTraversal   = GraphTraversal2.__();
 }