Специализированное администрирование > Настройка бизнес-объектов > Администрирование правил инициализации объектов > Understanding Object Initialization Rules > Defining the Content of XML Documents used for Object Initialization Rules > Variables
  
Variables
You can choose to use variables when creating object initialization rules. A variable is defined using the VarDef tag and then referenced using the VarRef tag. The main reason to use a variable is to have the ability to generate a value one time and then use the generated value multiple times within the rules you have set up or to simplify complex rules.
The variable definitions that are available when you are referencing a variable (using the VarRef tag) are determined by the object type and the context hierarchies that are in place. The value for a variable is not calculated until a rule element is processed that references the variable. Additionally, a value in a variable definition cannot be derived from the calculation of another value in the rule set being evaluated.
The following XML sample uses the VarDef tag to generate a number and then uses the VarRef tags to set both the name and number of a part to the generated number:
<AttributeValues objType="wt.part.WTPart">

<VarDef id="GeneratedNumber" algorithm=
”com.ptc.windchill.enterprise.revisionControlled.server.impl.NumberGenerator”>
<Arg>{GEN:wt.enterprise.SequenceGenerator:WTPARTID_seq:10:0}</Arg>
</VarDef>

<AttrValue id="name" algorithm=”wt.rule.algorithm.StringConstant”>
<VarRef id="GeneratedNumber"/>
</AttrValue>

<AttrValue id="number" algorithm=”wt.rule.algorithm.StringConstant”>
<VarRef id="GeneratedNumber"/>
</AttrValue>

<AttrConstraint id="name"
algorithm="com.ptc.core.rule.server.impl.GatherAttributeConstraints">
<Value algorithm="com.ptc.core.rule.server.impl.GetServerAssignedConstraint"/>
</AttrConstraint>

<AttrConstraint id="number"
algorithm="com.ptc.core.rule.server.impl.GatherAttributeConstraints">
<Value algorithm="com.ptc.core.rule.server.impl.GetServerAssignedConstraint"/>
<Value algorithm="com.ptc.core.rule.server.impl.GetImmutableConstraint"/>
</AttrConstraint>

</AttributeValues>
In this sample XML, the GeneratedNumber variable is set using the number generator algorithm. Then the value of the variable is used to set the value of both the name and number attributes.
* 
This example assumes that the constraints listed in the example are in place; these constraints are not the out-of-the box constraints (in which name is required in the interface and therefore is not generated).
The following is an XML sample that generates a number for a part and then defines the GeneratedNumber variable, setting its value equal to the value of the number attribute. Then, the name attribute is set to the value in the GeneratedNumber variable:
<AttributeValues objType="wt.part.WTPart">

<AttrValue id="number" algorithm=
"com.ptc.windchill.enterprise.revisionControlled.server.impl.NumberGenerator">
<Arg>{GEN:wt.enterprise.SequenceGenerator:WTPARTID_seq:10:0}</Arg>
</AttrValue>

<VarDef id="GeneratedNumber" algorithm="wt.rule.algorithm.StringConstant">
< Attr id="number">
</VarDef>

<AttrValue id="name" algorithm=”wt.rule.algorithm.StringConstant”>
<VarRef id="GeneratedNumber"/>
</AttrValue>
.
.
.
</AttributeValues>
This sample fails to set the name and number attributes to the same value because the value of the variable being defined cannot be derived from the calculation of another value in the rule set that is being evaluated. In this case, the value of the number attribute identified in the VarDef tag comes from a previously cached number attribute value (if there is one cached) and not from the value set by the AttrValue tag specified for the number attribute in the same rule.