Example #1
0
        public static IEnumerable <TSLProtocol> GetDefaultNumberOfDistinctProtocols(this TSLGeneratorContext context)
        {
            var protocolNumber = DiscreteUniform.Sample(context.MasterRandom,
                                                        CommunicationInstanceSettings.MinProtocolNumber, CommunicationInstanceSettings.MaxProtocolNumber);

            return(context.GetRandomDistinctProtocols(protocolNumber));
        }
Example #2
0
        public static ITSLTopLevelElement GenerateProtocol(this TSLGeneratorContext context)
        {
            var protocol = context.GenerateRandomProtocol();

            context.Protocols.Add(protocol);
            return(protocol);
        }
 public static ITSLType GenerateEnumType(this TSLGeneratorContext context)
 {
     if (context.Enums.Count == 0)
     {
         return(null);
     }
     return(context.Enums.Choice(context.MasterRandom));
 }
 public static ITSLType GenerateStructType(this TSLGeneratorContext context)
 {
     if (context.StructsBeforeMaxDepth.Count == 0)
     {
         return(null);
     }
     return(context.StructsBeforeMaxDepth.Choice(context.MasterRandom));
 }
Example #5
0
        public static ITSLTopLevelElement GenerateModule(this TSLGeneratorContext context)
        {
            var name      = $"Module_{context.GeneratedElementCount + 1}";
            var protocols = context.GetDefaultNumberOfDistinctProtocols();
            var module    = new TSLModule(name, protocols);

            context.Modules.Add(module);
            return(module);
        }
Example #6
0
        public static ITSLTopLevelElement GenerateProxy(this TSLGeneratorContext context)
        {
            var name      = $"Proxy_{context.GeneratedElementCount + 1}";
            var protocols = context.GetDefaultNumberOfDistinctProtocols();
            var proxy     = new TSLProxy(name, protocols);

            context.Proxies.Add(proxy);
            return(proxy);
        }
Example #7
0
        public static ITSLTopLevelElement GenerateServer(this TSLGeneratorContext context)
        {
            var name      = $"Server_{context.GeneratedElementCount + 1}";
            var protocols = context.GetDefaultNumberOfDistinctProtocols();
            var server    = new TSLServer(name, protocols);

            context.Servers.Add(server);
            return(server);
        }
Example #8
0
        public static ITSLTopLevelElement GenerateCell(this TSLGeneratorContext context)
        {
            var name           = $"CellStruct_{context.TopLevelElementCount + 1}";
            var numberOfFields = DiscreteUniform.Sample(context.MasterRandom, StructSettings.MinFieldNumber, StructSettings.MaxFieldNumber);
            var fields         = context.RandomFields().Take(numberOfFields).ToArray();
            var result         = new TSLCell(name, fields);

            context.Cells.Add(result);
            return(result);
        }
 public R Generate(double typeSelector, TSLGeneratorContext context)
 {
     if (typeSelector - Probability > 0)
     {
         return(NextCombinator != null?NextCombinator.Generate(typeSelector - Probability, context) : default(R));
     }
     else
     {
         return(GenerateFunc(context));
     }
 }
Example #10
0
        public static ITSLTopLevelElement GenerateStruct(this TSLGeneratorContext context)
        {
            var name           = $"Struct_{context.TopLevelElementCount + 1}";
            var numberOfFields = DiscreteUniform.Sample(context.MasterRandom, StructSettings.MinFieldNumber, StructSettings.MaxFieldNumber);
            var fields         = context.RandomFields().Take(numberOfFields);
            // TODO(leasunhy): generate attributes
            var result = new TSLStruct(name, fields);

            context.AddStruct(result);
            return(result);
        }
Example #11
0
 public static IEnumerable <TSLField> RandomFields(this TSLGeneratorContext context)
 {
     while (true)
     {
         var optional = ContinuousUniform.Sample(context.MasterRandom, 0.0, 1.0) <
                        StructSettings.FieldProbabilities.OptionalFieldProbability;
         var name = $"field{context.GeneratedElementCount}";
         var type = context.GenerateRandomFieldType();
         context.GeneratedElementCount += 1;
         // TODO(leasunhy): generate attributes
         var field = new TSLField(type, name, optional, attributes: null);
         yield return(field);
     }
 }
        public static ITSLType GenerateArrayType(this TSLGeneratorContext context)
        {
            var elementType = context.GenerateRandomArrayElementType();
            var dimension   = DiscreteUniform.Sample(
                context.MasterRandom,
                ContainerProbabilities.Array.MinDimension,
                ContainerProbabilities.Array.MaxDimension);
            var dimensions =
                DiscreteUniform.Samples(context.MasterRandom,
                                        ContainerProbabilities.Array.MinDimensionLength,
                                        ContainerProbabilities.Array.MaxDimensionLength)
                .Take(dimension);

            return(new ArrayType(elementType, dimensions.ToArray()));
        }
Example #13
0
        public static IEnumerable <TSLProtocol> GetRandomDistinctProtocols(this TSLGeneratorContext context, int number)
        {
            if (context.Protocols.Count == 0)
            {
                return(Enumerable.Empty <TSLProtocol>());
            }
            if (number > context.Protocols.Count)
            {
                number = context.Protocols.Count;
            }
            var protocols = DiscreteUniform.Samples(context.MasterRandom, 0, context.Protocols.Count - 1)
                            .Take(number)
                            .Distinct()                 // may return fewer than number of protocols
                            .Select(i => context.Protocols[i]);

            return(protocols);
        }
Example #14
0
        public static ITSLTopLevelElement GenerateEnum(this TSLGeneratorContext context)
        {
            var name         = $"Enum{context.TopLevelElementCount + 1}";
            var memberNumber = DiscreteUniform.Sample(context.MasterRandom,
                                                      EnumSettings.MinEnumMemberNumber, EnumSettings.MaxEnumMemberNumber);
            var members = Enumerable.Range(0, memberNumber).Select(i => $"{name}_{i}").ToList();
            var values  =
                members
                .WithProbabilityThreshold(
                    EnumSettings.ValueSpecifiedProbability,
                    n => new KeyValuePair <string, int>(n, DiscreteUniform.Sample(context.MasterRandom, 0, byte.MaxValue)),
                    _ => ContinuousUniform.Sample(context.MasterRandom, 0.0, 1.0))
                .Where(p => p.Key != null)
                .ToImmutableDictionary();
            var result = new TSLEnum(name, members, values);

            context.Enums.Add(result);
            return(result);
        }
        public static TSLScript GetRandomTSLScript(int randomSeed, Random masterRandom)
        {
            // generate the top level elements = Struct | Cell | Enum | Protocol | Server | Proxy | Module
            var context = new TSLGeneratorContext(masterRandom);
            var topLevelElementNumber = DiscreteUniform.Sample(masterRandom, GeneralSettings.MinTopLevelElementNumber, GeneralSettings.MaxTopLevelElementNumber);

            // NOTE(leasunhy): to ensure the order of the types of the generated elements, we use (i/total) as typeSelector
            //var topLevelElementTypeDist = new ContinuousUniform(0.0, 1.0, new Random(masterRandom.Next()));
            //foreach (var typeSelector in topLevelElementTypeDist.Samples().Take(topLevelElementNumber))
            //    TopLevelElementGenerator(typeSelector, context, masterRandom);
            var topLevelElementGenerator = context.GetTopLevelElementGenerator();

            for (int i = 0; i < topLevelElementNumber; ++i)
            {
                topLevelElementGenerator((double)i / topLevelElementNumber);
            }

            Debug.Assert(topLevelElementNumber == context.TopLevelElementCount);
            var topLevelElements = context.GetAllTopLevelElements().ToImmutableArray();
            var script           = new TSLScript(context);

            script.RandomSeedForGeneration = randomSeed;
            return(script);
        }
 public static TSLProtocol GenerateHttpProtocol(this TSLGeneratorContext context) =>
 new TSLProtocol(name: $"HttpProtocol{context.TopLevelElementCount + 1}",
                 type: TSLProtocolType.Http,
                 requestType: HttpRequestTypeGenerator(context),
                 responseType: HttpResponseTypeGenerator(context));
 public static ITSLType GenerateRandomFixedLengthType(this TSLGeneratorContext context)
 => GenerateRandomFixedLengthTypeDelegate(context);
        public static ITSLType GenerateListType(this TSLGeneratorContext context)
        {
            var elementType = context.GenerateListElementType();

            return(new ListType(elementType));
        }
 public static ITSLType GenerateAtomType(this TSLGeneratorContext context)
 {
     // upper bound for DiscreteUniform.Sample() is inclusive
     return(context.Enums.Choice(context.MasterRandom));
 }
 public static ITSLType GenerateListElementType(this TSLGeneratorContext context) => GenerateListElementTypeDelegate(context);
 public static ITSLType GenerateRandomFieldType(this TSLGeneratorContext context) => GenerateRandomTypeDelegate(context);
 public static ITSLType GenerateFixedLengthArrayType(this TSLGeneratorContext context)
 => context.GenerateArrayType();
 public static ITSLType GenerateRandomArrayElementType(this TSLGeneratorContext context)
 => GenerateRandomArrayElementTypeDelegate(context);
        public R DefaultGenerate(TSLGeneratorContext context)
        {
            var typeSelector = ContinuousUniform.Sample(context.MasterRandom, 0.0, CumulativeProbability);

            return(Generate(typeSelector, context));
        }
 public static TSLProtocol GenerateRandomProtocol(this TSLGeneratorContext context)
 => ProtocolGenerator.DefaultGenerate(context);