Generator クラス

このクラスは、データ ジェネレータの抽象基本クラスです。

名前空間 :  Microsoft.Data.Schema.DataGenerator
アセンブリ :  Microsoft.Data.Schema (Microsoft.Data.Schema.dll 内)

構文

'宣言
<GeneratorAttribute(GetType(DefaultGeneratorDesigner))> _
<CLSCompliantAttribute(True)> _
Public MustInherit Class Generator _
    Implements IGenerator, IDisposable, IExtension
'使用
Dim instance As Generator
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public abstract class Generator : IGenerator, 
    IDisposable, IExtension
[GeneratorAttribute(typeof(DefaultGeneratorDesigner))]
[CLSCompliantAttribute(true)]
public ref class Generator abstract : IGenerator, 
    IDisposable, IExtension
public abstract class Generator implements IGenerator, IDisposable, IExtension

解説

標準のデータ ジェネレータだけでは不十分な場合、カスタム データ ジェネレータを作成できます。カスタム データ ジェネレータを作成するには、IGenerator を実装するクラスまたは Generator を継承するクラスを作成する必要があります。クラスをデータ ジェネレータとして指定するには、そのクラスを GeneratorAttribute で装飾します。

カスタム データ ジェネレータ用のカスタム デザイナを作成することも、DefaultGeneratorDesigner を使用することもできます。

基本クラスの実装では、OutputAttribute でマークされたパブリック プロパティに基づいて出力が構築されます。入力は、InputAttribute を使って設定されます。属性でマークされたプロパティを使用することにより、厳密に型指定された入力値と出力値を単純な方法で宣言できます。

標準のデータ ジェネレータでは、特殊な CHECK 制約を満たすデータは生成できません。 たとえば、2 つの異なる日付範囲があり、日付がそのいずれかの範囲内にあることを要求する CHECK 制約が存在するとします。 この場合、標準の DateTime ジェネレータを使用することはできません。この例では、こうした制約を満たすことができるカスタム データ ジェネレータを作成します。 このジェネレータは、2 つの異なる範囲を入力として受け取り、その 2 つの範囲のいずれかに該当する日付をランダムに生成します。 詳細については、「チュートリアル : CHECK 制約のカスタム データ ジェネレータの作成」を参照してください。

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TeamSystem.Data.DataGenerator;

namespace GeneratorDateRangesCS
{
    public class GeneratorDateRangesCS : Generator
    {
        DateTime mRange1Min;
        DateTime mRange1Max;

        DateTime mRange2Min;
        DateTime mRange2Max;

        [Input]
        public DateTime Range1Min
        {
            set {mRange1Min = value;}
        }

        [Input]
        public DateTime Range1Max
        {
            set {mRange1Max = value;}
        }

        [Input]
        public DateTime Range2Min
        {
            set {mRange2Min = value;}
        }

        [Input]
        public DateTime Range2Max
        {
            set {mRange2Max = value;}
        }


        DateTime mRandomDate;

        [Output]
        public DateTime RandomDate
        {
            get {return mRandomDate;}
        }


        Random mRandom;
        Random mRandomRange;

        protected override void OnInitialize(GeneratorInit initInfo)
        {
            mRandom = new Random(this.Seed);       //deterministic
            mRandomRange = new Random(this.Seed);  //deterministic

            //mRandom = new Random();                //non-deterministic
            //mRandomRange = new Random();           //non-deterministic

            base.OnInitialize(initInfo);
        }


        protected override void OnGenerateNextValues()
        {
            DateTime min;
            DateTime max;

            //Generate a random date from either range 1 or range 2.
            //Randomly select either range 1 or range 2 by randomly 
            //generating an odd or an even random number.
            //------------------------------------------------------------
            if (mRandomRange.Next() % 2 == 0)  //check for odd or even
            {
                min = mRange1Min;
                max = mRange1Max;
            }
            else
            {
                min = mRange2Min;
                max = mRange2Max;
            }

            //The formula for creating a random number in a specific range is:
            //start of range + (size of range * random number between 0 and 1)

            //size of range
            TimeSpan range = max - min;

            //(size of range * random number between 0 and 1)
            TimeSpan randomNumber = new TimeSpan((long)(range.Ticks * mRandom.NextDouble()));

            //start of range + (size of range * random number between 0 and 1)
            mRandomDate = min + randomNumber;
        }
    }//end class
}//end namespace
Imports Microsoft.VisualStudio.TeamSystem.Data.DataGenerator

Public Class GeneratorDateRangesVB
    Inherits Generator

    Dim mRange1Min As Date
    Dim mRange1Max As Date

    Dim mRange2Min As Date
    Dim mRange2Max As Date

    <Input()> _
    Public WriteOnly Property Range1Min() As Date
        Set(ByVal value As Date)
            mRange1Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range1Max() As Date
        Set(ByVal value As Date)
            mRange1Max = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Min() As Date
        Set(ByVal value As Date)
            mRange2Min = value
        End Set
    End Property

    <Input()> _
    Public WriteOnly Property Range2Max() As Date
        Set(ByVal value As Date)
            mRange2Max = value
        End Set
    End Property


    Dim mRandomDate As Date

    <Output()> _
    Public ReadOnly Property RandomDate() As Date
        Get
            Return mRandomDate
        End Get
    End Property


    Dim mRandom As Random
    Dim mRandomRange As Random

    Protected Overrides Sub OnInitialize(ByVal initInfo As GeneratorInit)

        mRandom = New Random(Me.Seed)       'deterministic
        mRandomRange = New Random(Me.Seed)  'deterministic

        'mRandom = New Random()              'non-deterministic
        'mRandomRange = New Random()         'non-deterministic

        MyBase.OnInitialize(initInfo)
    End Sub


    Protected Overrides Sub OnGenerateNextValues()

        Dim min As Date
        Dim max As Date

        'Generate a random date from either range 1 or range 2.
        'Randomly select either range 1 or range 2 by randomly 
        'generating an odd or an even random number.
        '------------------------------------------------------------
        If mRandomRange.Next() Mod 2 = 0 Then  'check for odd or even
            min = mRange1Min
            max = mRange1Max
        Else
            min = mRange2Min
            max = mRange2Max
        End If

        'The formula for creating a random number in a specific range is:
        'start of range + (size of range * random number between 0 and 1)

        'size of range
        Dim range As TimeSpan = max - min

        '(size of range * random number between 0 and 1)
        Dim randomNumber As TimeSpan = New TimeSpan(CLng(range.Ticks * mRandom.NextDouble()))

        'start of range + (size of range * random number between 0 and 1)
        mRandomDate = min + randomNumber

    End Sub
End Class

継承階層

System.Object
  Microsoft.Data.Schema.DataGenerator.Generator

スレッド セーフ

この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。 インスタンス メンバの場合は、スレッド セーフであるとは限りません。

参照

参照

Generator メンバ

Microsoft.Data.Schema.DataGenerator 名前空間

GeneratorAttribute

GeneratorInit

IGenerator

その他の技術情報

カスタム データ ジェネレータの作成

データ ジェネレータ機能拡張の概要

列へのデータ生成の詳細の指定