/// <summary>
 /// Adds an expression node to the given tree.
 /// </summary>
 /// <param name="root">Root node of expression tree</param>
 /// <param name="node">Node to add</param>
 /// <param name="conjunction">Conjunction used to join with other nodes</param>
 /// <returns>The new root node</returns>
 internal static INode?AddNode(INode?root, INode?node, ConjunctionType conjunction, bool group = false)
 {
     if (node != null)
     {
         node.Grouped = group;
         if (root != null)
         {
             root = new InternalNode
             {
                 LeftChild   = root,
                 RightChild  = node,
                 Conjunction = conjunction
             }
         }
         ;
         else
         {
             root = node;
         }
     }
     return(root);
 }
 /// <summary>
 /// Creates an expression node and adds it to the give tree.
 /// </summary>
 /// <param name="root">Root node of expression tree.</param>
 /// <param name="term">Term for this node.</param>
 /// <param name="termForm">Indicates form of this term.</param>
 /// <param name="termExclude">Indicates if this is an excluded term.</param>
 /// <param name="conjunction">Conjunction used to join with other nodes.</param>
 /// <returns>The new root node.</returns>
 internal INode?AddNode(INode?root, string term, TermForm termForm, bool termExclude, ConjunctionType conjunction)
 {
     if (term.Length > 0 && !IsStopWord(term))
     {
         INode node = new TerminalNode
         {
             Term     = term,
             TermForm = termForm,
             Exclude  = termExclude
         };
         root = AddNode(root, node, conjunction);
     }
     return(root);
 }
        /// <summary>
        /// Parses a query segment and converts it to an expression
        /// tree.
        /// </summary>
        /// <param name="query">Query segment to be converted.</param>
        /// <param name="defaultConjunction">Implicit conjunction type.</param>
        /// <returns>Root node of expression tree.</returns>
        internal INode?ParseNode(string?query, ConjunctionType defaultConjunction)
        {
            ConjunctionType conjunction = defaultConjunction;
            TermForm        termForm    = TermForm.Inflectional;
            bool            termExclude = false;
            bool            resetState  = true;
            INode?          root        = null;
            INode?          node;
            string          term;

            ParsingHelper parser = new ParsingHelper(query);

            while (!parser.EndOfText)
            {
                if (resetState)
                {
                    // Reset modifiers
                    conjunction = defaultConjunction;
                    termForm    = TermForm.Inflectional;
                    termExclude = false;
                    resetState  = false;
                }

                parser.SkipWhitespace();
                if (parser.EndOfText)
                {
                    break;
                }

                char ch = parser.Peek();
                if (Punctuation.Contains(ch))
                {
                    switch (ch)
                    {
                    case '"':
                    case '\'':
                        termForm = TermForm.Literal;
                        parser.MoveAhead();
                        term       = parser.ParseWhile(c => c != ch);
                        root       = AddNode(root, term.Trim(), termForm, termExclude, conjunction);
                        resetState = true;
                        break;

                    case '(':
                        // Parse parentheses block
                        term       = ExtractBlock(parser, '(', ')');
                        node       = ParseNode(term, defaultConjunction);
                        root       = AddNode(root, node, conjunction, true);
                        resetState = true;
                        break;

                    case '<':
                        // Parse angle brackets block
                        term       = ExtractBlock(parser, '<', '>');
                        node       = ParseNode(term, ConjunctionType.Near);
                        root       = AddNode(root, node, conjunction);
                        resetState = true;
                        break;

                    case '-':
                        // Match when next term is not present
                        termExclude = true;
                        break;

                    case '+':
                        // Match next term exactly
                        termForm = TermForm.Literal;
                        break;

                    case '~':
                        // Match synonyms of next term
                        termForm = TermForm.Thesaurus;
                        break;

                    default:
                        break;
                    }
                    // Advance to next character
                    parser.MoveAhead();
                }
                else
                {
                    // Parse this query term
                    term = parser.ParseWhile(c => !Punctuation.Contains(c) && !char.IsWhiteSpace(c));

                    // Allow trailing wildcard
                    if (parser.Peek() == '*')
                    {
                        term += parser.Peek();
                        parser.MoveAhead();
                        termForm = TermForm.Literal;
                    }

                    // Interpret term
                    StringComparer comparer = StringComparer.OrdinalIgnoreCase;
                    if (comparer.Compare(term, "AND") == 0)
                    {
                        conjunction = ConjunctionType.And;
                    }
                    else if (comparer.Compare(term, "OR") == 0)
                    {
                        conjunction = ConjunctionType.Or;
                    }
                    else if (comparer.Compare(term, "NEAR") == 0)
                    {
                        conjunction = ConjunctionType.Near;
                    }
                    else if (comparer.Compare(term, "NOT") == 0)
                    {
                        termExclude = true;
                    }
                    else
                    {
                        root       = AddNode(root, term, termForm, termExclude, conjunction);
                        resetState = true;
                    }
                }
            }
            return(root);
        }
Example #4
0
        public static List<Condition> DeSerializeCondition(string EvalString)
        {
            List<Condition> cons = new List<Condition>();
            try
            {
                if (!string.IsNullOrWhiteSpace(EvalString))
                {
                    var typeSplits = new ConjunctionType[] {
                        ConjunctionType.AND,
                        ConjunctionType.OR,
                        ConjunctionType.FENCEIN,
                        ConjunctionType.FENCEOUT,
                        ConjunctionType.FENCEBOTHINOUT
                    };

                    foreach (var item in typeSplits)
                    {
                        EvalString = EvalString.Replace(item.ToString(), "<<<" + Convert.ToInt32(item).ToString() + ">>>" + item.ToString());
                    }

                    var strSplits = new string[] {
                        ConjunctionType.AND.ToString(),
                        ConjunctionType.OR.ToString(),
                        ConjunctionType.FENCEIN.ToString(),
                        ConjunctionType.FENCEOUT.ToString(),
                        ConjunctionType.FENCEBOTHINOUT.ToString()
                    };

                    //({Speed} >= [10]) AND ({Acc}) AND ({Acc})`
                    //({Speed} >= [10]) <0>AND ({Acc}) <0>AND ({Acc})`
                    //({Speed} >= [10]) <0>, ({Acc}) <0>, ({Acc})`
                    //({Speed} >= [10])

                    foreach (var spStr in EvalString.Split(strSplits, StringSplitOptions.RemoveEmptyEntries))
                    {
                        var Conjunction = ConjunctionType.NONE;
                        int cTIndex = spStr.IndexOf("<<<");
                        if (cTIndex > -1)
                        {
                            Conjunction = (ConjunctionType)Convert.ToInt32(spStr.Substring(cTIndex + 3).Replace(">>>", string.Empty));
                        }
                        var spStrs = spStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (spStrs.Length >= 3)
                        {
                            cons.Add(new Condition()
                            {
                                Conjunction = Conjunction,
                                Operand = spStrs[0],
                                Operator = spStrs[1],
                                Value = spStrs[2]
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return cons;
        }
 public ConjunctionRelObject(WorldObject objective, ConjunctionType type) : base(objective)
 {
     this.Type = type;
 }
Example #6
0
        public static List<Condition> DeSerializeCondition(string EvalString)
        {
            List<Condition> cons = new List<Condition>();
            try
            {
                if (!string.IsNullOrWhiteSpace(EvalString))
                {
                    var typeSplits = new ConjunctionType[] {
                        ConjunctionType.AND,
                        ConjunctionType.OR,
                        ConjunctionType.FENCEIN,
                        ConjunctionType.FENCEOUT,
                        ConjunctionType.FENCEBOTHINOUT
                    };

                    foreach (var item in typeSplits)
                    {
                        EvalString = EvalString.Replace(item.ToString(), "<<<" + Convert.ToInt32(item).ToString() + ">>>" + item.ToString());
                    }

                    var strSplits = new string[] {
                        ConjunctionType.AND.ToString(),
                        ConjunctionType.OR.ToString(),
                        ConjunctionType.FENCEIN.ToString(),
                        ConjunctionType.FENCEOUT.ToString(),
                        ConjunctionType.FENCEBOTHINOUT.ToString()
                    };

                    //({Speed} >= [10]) AND ({Acc}) AND ({Acc})`
                    //({Speed} >= [10]) <0>AND ({Acc}) <0>AND ({Acc})`
                    //({Speed} >= [10]) <0>, ({Acc}) <0>, ({Acc})`
                    //({Speed} >= [10])

                    foreach (var spStr in EvalString.Split(strSplits, StringSplitOptions.RemoveEmptyEntries))
                    {
                        var Conjunction = ConjunctionType.NONE;
                        int cTIndex = spStr.IndexOf("<<<");
                        if (cTIndex > -1)
                        {
                            Conjunction = (ConjunctionType)Convert.ToInt32(spStr.Substring(cTIndex + 3).Replace(">>>", string.Empty));
                        }
                        var spStrs = spStr.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (spStrs.Length >= 3)
                        {
                            cons.Add(new Condition()
                            {
                                Conjunction = Conjunction,
                                Operand = spStrs[0],
                                Operator = spStrs[1],
                                Value = spStrs[2]
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {

            }

            return cons;
        }
Example #7
0
 public Conjunction(string text, ConjunctionType conjunctionType)
 {
     this.Text            = text;
     this.ConjunctionType = conjunctionType;
 }