public void TestSingleElement()
        {
            IntervalSet s         = IntervalSet.Of(99);
            String      expecting = "99";

            Assert.AreEqual(s.ToString(), expecting);
        }
        public void TestIsolatedElements()
        {
            IntervalSet s = new IntervalSet();

            s.Add(1);
            s.Add('z');
            s.Add('\uFFF0');
            String expecting = "{1, 122, 65520}";

            Assert.AreEqual(s.ToString(), expecting);
        }
        public void TestMergeOfRangesAndSingleValuesReverse()
        {
            IntervalSet s = IntervalSet.Of(43, 65534);

            s.Add(42);
            s.Add(0, 41);
            String expecting = "{0..65534}";
            String result    = s.ToString();

            Assert.AreEqual(expecting, result);
        }
        public void TestRmSingleElement()
        {
            IntervalSet s = IntervalSet.Of(1, 10);

            s.Add(-3, -3);
            s.Remove(-3);
            String expecting = "{1..10}";
            String result    = s.ToString();

            Assert.AreEqual(expecting, result);
        }
        public void TestRmRightSide()
        {
            IntervalSet s = IntervalSet.Of(1, 10);

            s.Add(-3, -3);
            s.Remove(10);
            String expecting = "{-3, 1..9}";
            String result    = s.ToString();

            Assert.AreEqual(expecting, result);
        }
        public void TestRmMiddleRange()
        {
            IntervalSet s = IntervalSet.Of(1, 10);

            s.Add(-3, -3);
            s.Remove(5);
            String expecting = "{-3, 1..4, 6..10}";
            String result    = s.ToString();

            Assert.AreEqual(expecting, result);
        }
        public void TestMixedRangesAndElements()
        {
            IntervalSet s = new IntervalSet();

            s.Add(1);
            s.Add('a', 'z');
            s.Add('0', '9');
            String expecting = "{1, 48..57, 97..122}";

            Assert.AreEqual(s.ToString(), expecting);
        }
        public void TestMergeWithDoubleOverlap()
        {
            IntervalSet s = IntervalSet.Of(1, 10);

            s.Add(20, 30);
            s.Add(5, 25); // overlaps two!
            String expecting = "{1..30}";
            String result    = s.ToString();

            Assert.AreEqual(expecting, result);
        }
        /// <summary>
        /// This method is called to report a syntax error which requires the
        /// insertion of a missing token into the input stream.
        /// </summary>
        /// <remarks>
        /// This method is called to report a syntax error which requires the
        /// insertion of a missing token into the input stream. At the time this
        /// method is called, the missing token has not yet been inserted. When this
        /// method returns,
        /// <paramref name="recognizer"/>
        /// is in error recovery mode.
        /// <p>This method is called when
        /// <see cref="SingleTokenInsertion(Parser)"/>
        /// identifies
        /// single-token insertion as a viable recovery strategy for a mismatched
        /// input error.</p>
        /// <p>The default implementation simply returns if the handler is already in
        /// error recovery mode. Otherwise, it calls
        /// <see cref="BeginErrorCondition(Parser)"/>
        /// to
        /// enter error recovery mode, followed by calling
        /// <see cref="Parser.NotifyErrorListeners(string)"/>
        /// .</p>
        /// </remarks>
        /// <param name="recognizer">the parser instance</param>
        protected internal virtual void ReportMissingToken(Parser recognizer)
        {
            if (InErrorRecoveryMode(recognizer))
            {
                return;
            }
            BeginErrorCondition(recognizer);
            IToken      t         = recognizer.CurrentToken;
            IntervalSet expecting = GetExpectedTokens(recognizer);
            string      msg       = "missing " + expecting.ToString(recognizer.Vocabulary) + " at " + GetTokenErrorDisplay(t);

            recognizer.NotifyErrorListeners(t, msg, null);
        }
        public void TestMergeWhereAdditionMergesThreeExistingIntervals()
        {
            IntervalSet s = new IntervalSet();

            s.Add(0);
            s.Add(3);
            s.Add(5);
            s.Add(0, 7);
            String expecting = "{0..7}";
            String result    = s.ToString();

            Assert.AreEqual(expecting, result);
        }
Exemple #11
0
        protected override void ReportUnwantedToken(Parser recognizer)
        {
            if (InErrorRecoveryMode(recognizer))
            {
                return;
            }
            BeginErrorCondition(recognizer);
            IToken      t         = recognizer.CurrentToken;
            string      tokenName = GetTokenErrorDisplay(t);
            IntervalSet expecting = GetExpectedTokens(recognizer);
            string      msg       = "无效的输入 " + tokenName + " expecting " + expecting.ToString(recognizer.Vocabulary);

            recognizer.NotifyErrorListeners(t, msg, null);
        }
        public void TestMergeWhereAdditionMergesTwoExistingIntervals()
        {
            // 42, 10, {0..9, 11..41, 43..65534}
            IntervalSet s = IntervalSet.Of(42);

            s.Add(10);
            s.Add(0, 9);
            s.Add(43, 65534);
            s.Add(11, 41);
            String expecting = "{0..65534}";
            String result    = s.ToString();

            Assert.AreEqual(expecting, result);
        }
Exemple #13
0
        protected internal override void ReportUnwantedToken(Parser recognizer)
        {
            if (InErrorRecoveryMode(recognizer))
            {
                return;
            }
            BeginErrorCondition(recognizer);
            IToken      t         = recognizer.CurrentToken;
            string      tokenName = GetTokenErrorDisplay(t);
            IntervalSet expecting = GetExpectedTokens(recognizer);
            string      msg;

            if (expecting.Count <= 4)
            {
                msg = "unexpected input " + tokenName + " expecting " + expecting.ToString(recognizer.Vocabulary);
            }
            else
            {
                msg = "unexpected input " + tokenName;
                string missing = null;
                // unexpected closing tokens often indicate a missing opening token
                switch (t.Type)
                {
                case XSharpParser.RPAREN:
                    missing = "'('";
                    break;

                case XSharpParser.RCURLY:
                    missing = "'{'";
                    break;

                case XSharpParser.RBRKT:
                    missing = "'['";
                    break;

                case XSharpParser.EOS:
                    missing = " closing ')' or '}'";
                    break;
                }
                if (!String.IsNullOrEmpty(missing))
                {
                    msg += Missing(missing);
                }
            }
            recognizer.NotifyErrorListeners(t, msg, null);
        }
Exemple #14
0
        protected override void ReportMissingToken([NotNull] Parser recognizer)
        {
            if (InErrorRecoveryMode(recognizer))
            {
                return;
            }
            BeginErrorCondition(recognizer);
            IToken               t         = recognizer.CurrentToken;
            IntervalSet          expecting = GetExpectedTokens(recognizer);
            string               msg       = "Expected {f:Red}<" + expecting.ToString(recognizer.Vocabulary).Trim('\'') + ">{r} at '{f:Red}" + GetTokenErrorDisplay(t).Trim('\'') + "{r}'";
            NoViableAltException e         = new NoViableAltException(recognizer)
            {
                HelpLink = "505"
            };

            NotifyErrorListeners(recognizer, msg, e);
        }
Exemple #15
0
        protected internal override void ReportInputMismatch(Parser recognizer, InputMismatchException e)
        {
            IntervalSet expecting = GetExpectedTokens(recognizer);
            string      msg;

            if (expecting.Count <= 4)
            {
                msg = "mismatched input " + GetTokenErrorDisplay(e.OffendingToken) + " expecting " + expecting.ToString(recognizer.Vocabulary);
            }
            else
            {
                msg = "mismatched input " + GetTokenErrorDisplay(e.OffendingToken);
                if (e.OffendingToken.Type == XSharpParser.EOS)
                {
                    msg += Missing("closing ')' or '}'");
                }
            }
            NotifyErrorListeners(recognizer, msg, e);
        }
Exemple #16
0
        /// <summary>
        /// This method is called to report a syntax error which requires the removal
        /// of a token from the input stream.
        /// </summary>
        /// <remarks>
        /// This method is called to report a syntax error which requires the removal
        /// of a token from the input stream. At the time this method is called, the
        /// erroneous symbol is current
        /// <code>LT(1)</code>
        /// symbol and has not yet been
        /// removed from the input stream. When this method returns,
        /// <code>recognizer</code>
        /// is in error recovery mode.
        /// <p>This method is called when
        /// <see cref="SingleTokenDeletion(Parser)"/>
        /// identifies
        /// single-token deletion as a viable recovery strategy for a mismatched
        /// input error.</p>
        /// <p>The default implementation simply returns if the handler is already in
        /// error recovery mode. Otherwise, it calls
        /// <see cref="BeginErrorCondition(Parser)"/>
        /// to
        /// enter error recovery mode, followed by calling
        /// <see cref="Parser.NotifyErrorListeners(string)"/>
        /// .</p>
        /// </remarks>
        /// <param name="recognizer">the parser instance</param>
        protected internal virtual void ReportUnwantedToken(Parser recognizer)
        {
            if (InErrorRecoveryMode(recognizer))
            {
                return;
            }
            BeginErrorCondition(recognizer);
            IToken      t         = recognizer.CurrentToken;
            string      tokenName = GetTokenErrorDisplay(t);
            IntervalSet expecting = GetExpectedTokens(recognizer);
            string      msg       = "extraneous input " + tokenName + " expecting " + expecting.ToString(recognizer.TokenNames);

            recognizer.NotifyErrorListeners(t, msg, null);
        }
Exemple #17
0
 public override string ToString()
 {
     return(set.ToString());
 }
Exemple #18
0
 private void AssertEqual(string expected, IntervalSet <int> intervalSet)
 {
     Assert.Equal(expected, intervalSet.ToString());
 }
        /// <summary>
        /// Method to be invoked recursively to build the whole tree
        /// </summary>
        /// <param name="nodeCollection"></param>
        /// <param name="pictureBox"></param>
        /// <param name="label"></param>
        protected void buildSubTree(FuzzyRelation subrelation, TreeNodeCollection nodeCollection, PictureBox pictureBox, Label label)
        {
            TreeNode tnThis;


            if (subrelation is FuzzySet)
            {
                FuzzySet fs = (FuzzySet)subrelation;
                tnThis = new TreeNode();
                if (!String.IsNullOrEmpty(fs.Caption))
                {
                    tnThis.Text = fs.Caption;
                }
                else
                {
                    tnThis.Text = "Fuzzy Set";
                }
                tnThis.ImageKey         = "fuzzySet";
                tnThis.SelectedImageKey = "fuzzySet";

                TreeNode tnDimType = new TreeNode("Type: " + (fs.Dimensions[0] is IContinuousDimension ? "Continuous" : "Discrete"));
                tnDimType.ImageKey         = "dimensionType";
                tnDimType.SelectedImageKey = "dimensionType";
                tnThis.Nodes.Add(tnDimType);
            }
            else
            {
                NodeFuzzyRelation nfr = (NodeFuzzyRelation)subrelation;
                tnThis                  = new TreeNode("Multidimensional Relation");
                tnThis.ImageKey         = "nodeFuzzyRelation";
                tnThis.SelectedImageKey = "nodeFuzzyRelation";

                TreeNode tnSubrelations = new TreeNode(nfr.Operator.Caption);
                tnSubrelations.ImageKey         = "subrelations";
                tnSubrelations.SelectedImageKey = "subrelations";
                tnSubrelations.ForeColor        = OperatorFontColor;
                tnThis.Nodes.Add(tnSubrelations);

                //Find all operands. Several commutative operands of same type from different nested levels will be displayed together
                List <FuzzyRelation> nestedSubrelations = new List <FuzzyRelation>();
                findNestedOperands(nfr, nestedSubrelations);

                foreach (FuzzyRelation nestedSubrelation in nestedSubrelations)
                {
                    buildSubTree(nestedSubrelation, tnSubrelations.Nodes, pictureBox, label);
                }
            }

            #region Dimensions

            TreeNode tnDimensions = new TreeNode("Dimension" + ((subrelation.Dimensions.Count() > 1) ? "s" : ""));
            tnDimensions.ImageKey         = "dimensions";
            tnDimensions.SelectedImageKey = "dimensions";
            tnThis.Nodes.Add(tnDimensions);

            foreach (IDimension dimension in subrelation.Dimensions)
            {
                bool  blnKnown      = _inputs.ContainsKey(dimension);
                bool  blnContinuous = dimension is IContinuousDimension;
                Color fontColor;

                string strDimCaption = String.IsNullOrEmpty(dimension.Name) ? "Dimension" : dimension.Name;

                if (blnKnown)
                {
                    if (blnContinuous)
                    {
                        strDimCaption += String.Format("={0:F5} {1}", _inputs[dimension], ((IContinuousDimension)dimension).Unit);
                    }
                    else
                    {
                        IDiscreteDimension discreteDim = (IDiscreteDimension)dimension;
                        if (discreteDim.DefaultSet != null)
                        {
                            strDimCaption += "=" + discreteDim.DefaultSet.GetMember(_inputs[dimension]).Caption;
                        }
                        else
                        {
                            strDimCaption += String.Format("=#{0:F0}", _inputs[dimension]);
                        }
                    }
                    fontColor = SpecifiedDimensionFontColor;
                }
                else
                {
                    fontColor = UnspecifiedDimensionFontColor;
                }

                if (dimension == _variableDimension)
                {
                    fontColor = VariableDimensionFontColor;
                }

                string imageKey = String.Format("dimension{0}{1}", blnContinuous ? "Continuous" : "Discrete", blnKnown ? "Known" : "Unknown");

                TreeNode tnDimension = new TreeNode(strDimCaption);
                tnDimension.ImageKey         = imageKey;
                tnDimension.SelectedImageKey = imageKey;
                tnDimension.ForeColor        = fontColor;
                addToolTip(tnDimension, dimension.Description);

                tnDimensions.Nodes.Add(tnDimension);
            }
            #endregion

            #region Function
            if (allInputDimensionsAvailable(subrelation))
            {
                IDimension realVariableDimension;
                if (subrelation.Dimensions.Count() == 1)
                {
                    realVariableDimension = subrelation.Dimensions[0];
                }
                else
                {
                    realVariableDimension = _variableDimension;
                }

                Dictionary <IDimension, decimal> copyInputs = new Dictionary <IDimension, decimal>(_inputs);

                foreach (KeyValuePair <IDimension, decimal> item in _inputs)
                {
                    if (!subrelation.Dimensions.Contains(item.Key))
                    {
                        copyInputs.Remove(item.Key);
                    }
                }

                if (copyInputs.ContainsKey(realVariableDimension))
                {
                    copyInputs.Remove(realVariableDimension);
                }

                if (subrelation.Dimensions.Count() > copyInputs.Count())
                {
                    IntervalSet intervals = subrelation.GetFunction(copyInputs);

                    string strIntervals = intervals.ToString();

                    string[] arrLines = strIntervals.Split(new char[] { '\n' });

                    TreeNode tnFunction = new TreeNode("Function");
                    tnFunction.ImageKey         = "function";
                    tnFunction.SelectedImageKey = "function";
                    foreach (string line in arrLines)
                    {
                        if (!String.IsNullOrWhiteSpace(line))
                        {
                            TreeNode tnLine = new TreeNode(line);
                            tnLine.ImageKey         = "spacer";
                            tnLine.SelectedImageKey = "spacer";
                            tnFunction.Nodes.Add(tnLine);
                        }
                    }

                    tnThis.Nodes.Add(tnFunction);
                }
            }

            #endregion

            tnThis.ForeColor = MainNodeFontColor;
            tnThis.Tag       = subrelation;
            nodeCollection.Add(tnThis);
        }