Пример #1
0
 protected internal override void AddTo(Pick.Target target)
 {
     for (int i = 0; i < items.Length; ++i)
     {
         items[i].AddTo(target);
     }
 }
Пример #2
0
 /*
  * private Repeat(int minCount, int maxCount, int itemWeight, Pick item)
  * { super(item); weightedIndex = new
  * WeightedIndex(minCount).add(maxCount-minCount+1, itemWeight); }
  */
 /*
  * private Repeat(int minCount, int maxCount, Object item) { this.item =
  * convert(item); weightedIndex = new
  * WeightedIndex(minCount).add(maxCount-minCount+1, 1); }
  */
 protected internal override void AddTo(Pick.Target target)
 {
     // int count ;
     for (int i = weightedIndex.ToIndex(target.NextDouble()); i > 0; --i)
     {
         item.AddTo(target);
     }
 }
Пример #3
0
 public static Pick.Target  Make(Pick pick_0, Random random_1, Quoter quoter_2)
 {
     Pick.Target result = new Pick.Target();
     result.pick   = pick_0;
     result.random = random_1;
     result.quoter = quoter_2;
     return(result);
 }
Пример #4
0
        /*
         * + "weight = integer '%';" +
         * "range = '{' integer (',' integer?)? '}' weight*;" + "quote = '@';" +
         * "star = '*' weight*;" + "plus = '+' weight*;" + "maybe = '?' weight?;" +
         * "quantifier = range | star | maybe | plus;" +
         * "core = string | unicodeSet | '(' alternation ')';" +
         * "sequence = (core quantifier*)+;" +
         * "alternation = sequence (weight? ('|' sequence weight?)+)?;" +
         * "rule = string '=' alternation;";
         *
         *
         * Match 0 or more times + Match 1 or more times ? Match 1 or 0 times {n}
         * Match exactly n times {n,} Match at least n times {n,m} Match at least n
         * but not more than m times
         */

        public BNF(Random random_0, Quoter quoter_1)
        {
            this.map       = new Hashtable();
            this.variables = new HashedSet();
            this.pick      = null;
            this.target    = null;
            this.maxRepeat = 99;
            this.random    = random_0;
            this.quoter    = quoter_1;
            t = new Tokenizer();
        }
Пример #5
0
            protected internal override void AddTo(Pick.Target target)
            {
                // get contents into separate buffer
                addBuffer.CopyState(target);
                addBuffer.Clear();
                item.AddTo(addBuffer);
                String newValue = addBuffer.Get();

                if (IBM.ICU.Charset.Pick.DEBUG)
                {
                    System.Console.Out.WriteLine("Old: " + lastValue + ", New:" + newValue);
                }

                // if not first one, merge with old
                if (lastValue != null)
                {
                    mergeBuffer.Length = 0;
                    int lastIndex = 0;
                    int newIndex  = 0;
                    // the new length is a random value between old and new.
                    int newLenLimit = (int)IBM.ICU.Charset.Pick.PickMthd(target.random, lastValue.Length,
                                                                         newValue.Length);

                    while (mergeBuffer.Length < newLenLimit &&
                           newIndex < newValue.Length &&
                           lastIndex < lastValue.Length)
                    {
                        int c = choice.ToIndex(target.NextDouble());
                        if (c == COPY_NEW || c == COPY_BOTH || c == SKIP)
                        {
                            newIndex = IBM.ICU.Charset.Pick.GetChar(newValue, newIndex, mergeBuffer,
                                                                    c < LEAST_SKIP);
                            if (mergeBuffer.Length >= newLenLimit)
                            {
                                break;
                            }
                        }
                        if (c == COPY_LAST || c == COPY_BOTH || c == SKIP)
                        {
                            lastIndex = IBM.ICU.Charset.Pick.GetChar(lastValue, lastIndex, mergeBuffer,
                                                                     c < LEAST_SKIP);
                        }
                    }
                    newValue = mergeBuffer.ToString();
                }
                lastValue = newValue;
                target.Append(newValue);
                if (IBM.ICU.Charset.Pick.DEBUG)
                {
                    System.Console.Out.WriteLine("Result: " + newValue);
                }
            }
Пример #6
0
        public BNF Complete()
        {
            // check that the rules match the variables, except for $root in rules
            ILOG.J2CsMapping.Collections.ISet ruleSet = new ILOG.J2CsMapping.Collections.ListSet(map.Keys);
            // add also
            ILOG.J2CsMapping.Collections.Generics.Collections.Add(variables, "$root");
            ILOG.J2CsMapping.Collections.Generics.Collections.AddAll(t.GetLookedUpItems(), variables);
            if (!ruleSet.Equals(variables))
            {
                String msg = ShowDiff(variables, ruleSet);
                if (msg.Length != 0)
                {
                    msg = "Error: Missing definitions for: " + msg;
                }
                String temp = ShowDiff(ruleSet, variables);
                if (temp.Length != 0)
                {
                    temp = "Warning: Defined but not used: " + temp;
                }
                if (msg.Length == 0)
                {
                    msg = temp;
                }
                else if (temp.Length != 0)
                {
                    msg = msg + "; " + temp;
                }
                Error(msg);
            }

            if (!ruleSet.Equals(variables))
            {
                String msg_0 = ShowDiff(variables, ruleSet);
                if (msg_0.Length != 0)
                {
                    msg_0 = "Missing definitions for: " + msg_0;
                }
                String temp_1 = ShowDiff(ruleSet, variables);
                if (temp_1.Length != 0)
                {
                    temp_1 = "Defined but not used: " + temp_1;
                }
                if (msg_0.Length == 0)
                {
                    msg_0 = temp_1;
                }
                else if (temp_1.Length != 0)
                {
                    msg_0 = msg_0 + "; " + temp_1;
                }
                Error(msg_0);
            }

            // replace variables by definitions
            IIterator it = new ILOG.J2CsMapping.Collections.IteratorAdapter(ruleSet.GetEnumerator());

            while (it.HasNext())
            {
                String    key        = (String)it.Next();
                Pick      expression = (Pick)ILOG.J2CsMapping.Collections.Collections.Get(map, key);
                IIterator it2        = new ILOG.J2CsMapping.Collections.IteratorAdapter(ruleSet.GetEnumerator());
                if (false && key.Equals("$crlf"))
                {
                    System.Console.Out.WriteLine("debug");
                }
                while (it2.HasNext())
                {
                    Object key2 = it2.Next();
                    if (key.Equals(key2))
                    {
                        continue;
                    }
                    Pick expression2 = (Pick)ILOG.J2CsMapping.Collections.Collections.Get(map, key2);
                    expression2.Replace(key, expression);
                }
            }
            pick   = (Pick)ILOG.J2CsMapping.Collections.Collections.Get(map, "$root");
            target = IBM.ICU.Charset.Pick.Target.Make(pick, random, quoter);
            // TODO remove temp collections
            return(this);
        }
Пример #7
0
 protected internal override void AddTo(Pick.Target target)
 {
     target.Append(name);
 }
Пример #8
0
 protected internal override void AddTo(Pick.Target target)
 {
     target.quoter.SetQuoting(true);
     item.AddTo(target);
     target.quoter.SetQuoting(false);
 }
Пример #9
0
 public void CopyState(Pick.Target other)
 {
     random = other.random;
 }
Пример #10
0
 protected internal override void AddTo(Pick.Target target)
 {
     target.Append(source.CharAt(IBM.ICU.Charset.Pick.PickMthd(target.random, 0,
                                                               source.Size() - 1)));
 }
Пример #11
0
 protected internal override void AddTo(Pick.Target target)
 {
     items[weightedIndex.ToIndex(target.NextDouble())].AddTo(target);
 }
Пример #12
0
 protected abstract internal void AddTo(Pick.Target target);