Using Literal Values in an Action Invocation
The action invocation in the RuleAttribute attribute is used to establish the correspondence between the Cord action declaration signature and the potentially different rule method signature. If an action invocation argument is a literal value (for example, a number) it is not mapped to a rule method parameter.
During exploration, the action invocation for the rule is consumed by the Cord interpreter. Spec Explorer explores the rule method only when the action parameter matches the literal value in the action invocation.
Supported Literal Types
The Cord syntax supports the following types of literals as action invocation arguments:
Boolean literals, either true or false in C#.
Integer literals, such as
-5
or41
.Real literals, such as
-1
or5.33
.Character literals, such as
'a'
or'B'
.String literals, such as
"Kim"
.Null literals, such as
(string)null
or(Set<int>)null
.Modeling collection literals, such as
Map<int,char>{1->'a',2->'b'}
.
Note
The action invocation for a rule is provided as a string. To specify a string literal in a rule declaration in C#, use the backslash (\) to escape each quotation mark (") around the literal value.
Spec Explorer supports as action parameters the three immutable collection types Map, Set, and Sequence in the Microsoft.Modeling namespace.
Examples
The following examples use conditions to ensure that only one rule is enabled for any given input parameter.
Example 1
The following example declares a GetQualifiedMinimum
action in Cord script, and then shows C# rule definitions that model the action. The action returns the minimum value from a set of positive integers, or an error value, if the set is invalid or not supported. In this example, Spec Explorer would examine one of the four rules, based on the value of the input parameter, numbers.
If the input parameter is null, Spec Explorer examines the
GetQualifiedMinimumNull
method and sets the return parameter to-2
.If the input parameter is the empty set, Spec Explorer examines the
GetQualifiedMinimumEmpty
method and sets the return parameter to0
.If the input parameter is a set that contains any non-positive numbers, Spec Explorer examines the
GetQualifiedMinimumError
method and sets the return parameter to-1
.Otherwise, Spec Explorer examines the
GetQualifiedMinimum
method and sets the return parameter to the return value of the method.
The following Cord script shows the action declaration.
action abstract static int Adapter.GetQualifiedMinimum(Set<int> numbers);
The follow C# code shows corresponding rule definitions.
[Rule(Action = "GetQualifiedMinimum((Set<int>)null) / -2")]
static void GetQualifiedMinimumNull() { }
[Rule(Action = "GetQualifiedMinimum(Set<int>{}) / 0")]
static void GetQualifiedMinimumEmpty() { }
[Rule(Action = "GetQualifiedMinimum(numbers) / -1")]
static void GetQualifiedMinimumError(Set<int> numbers)
{
Condition.IsNotNull(numbers);
Condition.Exists(numbers, n => n <= 0);
}
[Rule(Action = "GetQualifiedMinimum(numbers) / result")]
static int GetQualifiedMinimum(Set<int> numbers)
{
Condition.IsNotNull(numbers);
Condition.IsTrue(numbers.Count > 0);
Condition.All(numbers, n => n > 0);
return numbers.Min();
}
Example 2
The following example declares a GetFirstCharacterAndLength
action in Cord script, and then shows C# rule definitions that model the action. The action returns the first character of a string and also sets an output parameter to the input string's length. In this example, Spec Explorer would examine one of the three rules, based on the value of the input parameter, name.
If the input parameter is null, Spec Explorer examines the
GetFirstCharacterAndLengthForNull
method and sets the return parameter to null and the output parameter to-1
.If the input parameter is the empty string, Spec Explorer examines the
GetFirstCharacterAndLengthForEmpty
method and sets the return parameter to null and the output parameter to0
.Otherwise, Spec Explorer examines the
GetFirstCharacterAndLength
method.
The following Cord script shows the action declaration.
action abstract static string Adapter.GetFirstCharacterAndLength(string name, out int length);
The follow C# code shows corresponding rule definitions.
[Rule(Action = "GetFirstCharacterAndLength((string)null , out -1) / (string)null")]
static void GetFirstCharacterAndLengthForNull() { }
[Rule(Action = "GetFirstCharacterAndLength(\"\", out 0) / (string)null")]
static void GetFirstCharacterAndLengthForEmpty() { }
[Rule(Action = "GetFirstCharacterAndLength(name, out length) / result")]
static string GetFirstCharacterAndLength(string name, out int length)
{
Condition.IsNotNull(name);
Condition.IsTrue(name.Length > 0);
length = name.Length;
return name.Substring(0, 1);
}
Example 3
The following example declares a GetFirstName
action in Cord script, and then shows C# rule definitions that model the action. The action returns the first name from a sequence of names. In this example, Spec Explorer would examine one of the three rules, based on the value of the input parameter, names.
If the input parameter is null, Spec Explorer examines the
GetFirstNameNull
method and sets the return parameter to null.If the input parameter is the empty sequence, Spec Explorer examines the
GetFirstNameEmpty
method and sets the return parameter to null.Otherwise, Spec Explorer examines the
GetFirstName
method.
The following Cord script shows the action declaration.
action abstract static string Adapter.GetFirstName(Sequence<string> names);
The follow C# code shows corresponding rule definitions.
[Rule(Action = "GetFirstName((Sequence<string>)null)/ (string)null")]
static void GetFirstNameNull() { }
[Rule(Action = "GetFirstName(Sequence<string>{})/ (string)null")]
static void GetFirstNameEmpty() { }
[Rule(Action = "GetFirstName(names)/ result")]
static string GetFirstName(Sequence<string> names)
{
Condition.IsNotNull(names);
Condition.IsTrue(names.Count > 0);
return names[0];
}
Example 4
The following example declares a GetAbsoluteValue
action in Cord script, and then shows C# rule definitions that model the action. The action returns the absolute value of the input value. In this example, Spec Explorer would examine one of the two rules, based on the value of the input parameter, number.
If the input parameter is a non-negative value, Spec Explorer examines the
GetAbsoluteValuePos
method and sets the return parameter to the input parameter.If the input parameter is a negative value, Spec Explorer examines the
GetAbsoluteValueNeg
method and sets the return parameter to the method's return value.
The following Cord script shows the action declaration.
action abstract static int Adapter.GetAbsoluteValue(int number);
The follow C# code shows corresponding rule definitions.
[Rule(Action = "GetAbsoluteValue(number) / number")]
static void GetAbsoluteValuePos(int number)
{
Condition.IsTrue(number >= 0);
}
[Rule(Action = "GetAbsoluteValue(number) / result")]
static int GetAbsoluteValueNeg(int number)
{
Condition.IsTrue(number < 0);
return -number;
}