OTX-Runtime for DotNet  
OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter Class Reference

Converts OTX Value to Literal in OTL notation and vice versa. More...

Static Public Member Functions

static object String2Value (string literal, string targetType)
 Converts a Literal into a value of the given data type. More...
 
static string Value2String (object value)
 Converts a value from an element of the API to a Literal in OTL notation. More...
 

Detailed Description

Converts OTX Value to Literal in OTL notation and vice versa.

Static class for parameter value conversion of different data types available at the API

Member Function Documentation

◆ String2Value()

static object OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value ( string  literal,
string  targetType 
)
inlinestatic

Converts a Literal into a value of the given data type.

This method converts a Literal in OTL notation into a value of the given data type. The Literal for the data type is formated in OTL notation and is therefore independent from the underlying software platform like C++, DotNet or Java. The string of the data type can be get via the DataType method or property of the related OTX element, e.g. ProcedureParameter.

Parameters
stringLiteralString literal formatted in OTL notation, see OTX Data Types Supported at the API.
targetDataTypeOTL notation of the data type which the string literal will be converted to. To get the data type the property DataType of the related element can be used, e.g. ProcedureParameter. Examples: "String", "Integer", "Boolean", "List<String>", "Map<String, String>". If the data type is defined in the OTX, targetDataType must contain the full path name. Example: "StructurePackage1.Document1.StructureSignature1<String, Integer, Float>", "EnumerationElementPackage1.Document1.EnumerationSignature1", ...

///

Returns
An OTX value object.
Exceptions
System.InvalidCastExceptionThrown if literal string is null, or if targetType is null or empty.
Exceptions.InvalidLiteralExceptionThrown if the given string literal cannot be converted into the given data type.

Example

String2Value("1", "Integer");
String2Value("\"Hello World!\"", "String");
String2Value("true", "Boolean");
String2Value("1.23", "Float");
String2Value("&123456789ABCDEF0", "ByteField");
String2Value("{\"Value1\", \"Value2\", \"Value3\"}", "List<String>");
String2Value("{\"Key1\":\"Value1\", \"Key2\":\"Value2\", \"Key3\":\"Value3\"}", "Map<String, String>");
String2Value("{StringElement=\"Hello\", IntegerElement=123, FloatElement=457.0}", "StructurePackage1.Document1.StructureSignature1<String, Integer, Float>"); // structure defined in OTX
String2Value("IntegerElement", "EnumerationElementPackage1.Document1.EnumerationSignature1"); // enumeration defined in OTX
String2Value("{\"Value1\", \"Value2\", \"Value3\"}", parameter.DataType);
static object String2Value(string literal, string targetType)
Converts a Literal into a value of the given data type.
Definition: ValueConverter.cs:55

◆ Value2String()

static string OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.Value2String ( object  value)
inlinestatic

Converts a value from an element of the API to a Literal in OTL notation.

This method converts a value from an element of the API like a ProcedureParameter to a Literal. The Literal is formated in OTL notation and is therefore independent from the underlying software platform like C++, DotNet or Java, see OTX Data Types Supported at the API.

Parameters
valueAn arbitrary value of an OTX object e.g. ProcedureParameter.
Returns
String literal in OTL notation.
Exceptions
System.InvalidCastExceptionThrow if value is null.
Exceptions.InvalidDataTypeExceptionThrow if the value's datatype is not supported in the api.

Example

// Pseudo-Code example to convert values to string and vice versa
// ==============================================================
// Please note that the exact syntax of C++, DotNet and Java is different!
void main()
{
// ...
// Set and Get datatype List
// Assume the parameter[0] is a ProcedureInParameter of data type List of String (List<String>)
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[0], "{\"Value1\", \"Value2\", \"Value3\"}");
// ...
// ...
// Set and Get datatype Map
// Assume the parameter[1] is a ProcedureInParameter of data type Map with key of type String and value of type String (Map<String, String>)
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[1], "{\"Key1\":\"Value1\", \"Key2\":\"Value2\", \"Key3\":\"Value3\"}");
// ...
// ...
// Set and Get datatype ByteField
// Assume the parameter[2] is a ProcedureInParameter of data type ByteField
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[2], "&121212");
// ...
// Set and Get datatype ResourceLocation
// Assume the parameter[3] is a ProcedureInParameter of data type ResourceLocation
String newlyAddedListStringLiteral = SetParameterValue(procedure.Parameters[3], "@ResourceLocation: \"ID_1\"/\"path/to/resource/file\"");
// ...
}
// Set value to a parameter using string literal
// and Get the newly added value back as a string literal
String SetParameterValue(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureParameter parameter, String stringLiteral)
{
if (parameter is null)
{
return String.Empty;
}
try
{
// The string literal will be converted to an OTX Data Types Supported at the API
Object value = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(stringLiteral, parameter.DataType);
{
(parameter as OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter).Value = value;
}
{
(parameter as OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInOutParameter).Value = value;
}
// Get value from the parameter and convert it back to a string literal
// This should be nearly the same as the new string literal set to the Parameter
}
{
throw e;
}
}
OpenTestSystem.Otx.Runtime.Api.DataTypes.Structure CreatePersonStructureWithValueConverter()
{
// Structure: Person{Name, Age}
// Name: String
// Age: Integer
// Structure Person is locate inside Document1 in Package1
String structureDataType = "Package1.Document1.Person<String, Integer>";
String structureLiteral = "{Name=\"Chris\",Age=25}";
// Use ValueConverter.String2Value to convert the string literal
// then cast the result from Object back to OpenTestSystem.Otx.Runtime.Api.DataTypes.Structure
return person1;
}
void ChangePersonStructure(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
// Structure: Person{Name, Age}
// Name: String
// Age: Integer
// Structure Person is locate inside Document1 in Package1
// Assume that the given inParameter datatype is Person Structure
// and default value is Person{Name="Chris",Age=25}
// Get StrutureElements from Structure chris
// This Collection should contains only 2 values (Name and Age StructureElement)
ReadOnlyCollection<OpenTestSystem.Otx.Runtime.Api.DataTypes.StructureElement> chrisInfoCollection = chris.Elements;
String newNameLiteral = "\"Christopher\"";
String newAgeLiteral = "40";
foreach (OpenTestSystem.Otx.Runtime.Api.DataTypes.StructureElement info in chrisInfoCollection)
{
// Create new StructureElement Name
if (info.Name == "Name")
{
Object newNameValue = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(newNameLiteral, info.Type);
info.Name
, newNameValue
, info.Type
);
newChrisInfoCollection.Add(nameStructureElement);
}
// Create new StructureElement Age
else if (info.Name == "Age")
{
Object newAgeValue = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(newAgeLiteral, info.Type);
info.Name
, newAgeValue
, info.Type
);
newChrisInfoCollection.Add(ageStructureElement);
}
}
// Create new Chris Structure with the new Information
chris.Signature
, newChrisInfoCollection
);
}
OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement CreateColorEnumerationWithValueConverter()
{
// EnumerationSignature name: Color
// EnumerationElements in Color: Red, Green, Blue
// EnumerationSignature Color is locate inside Document1 in Package1
String enumerationDataType = "Package1.Document1.Color";
String enumerationLiteral = "Blue";
// Use ValueConverter.String2Value to convert the string literal
// then cast the result from Object back to OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement
// This will only work after the Otx Runtime Api loaded a PTX contains EnumerationSignature Color like above
OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement color1 = OpenTestSystem.Otx.Runtime.Api.DataTypes.ValueConverter.String2Value(enumerationLiteral, enumerationDataType) as OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement; // On success, this will return Color EnumerationElement Blue
return color1;
}
void ChangeColorEnumerationElement(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
// EnumerationSignature name: Color
// EnumerationElements in Color: Red, Green, Blue
// EnumerationSignature Color is locate inside Document1 in Package1
// Assume that the given inParameter datatype is EnumerationElement from Color EnumerationSignature
// and the default Color is Red
OpenTestSystem.Otx.Runtime.Api.Otx.Signatures.IEnumerationSignature colorEnumeration = redColor.Enumeration;
// Get all Color EnumerationElements
List<OpenTestSystem.Otx.Runtime.Api.DataTypes.EnumerationElement> colors = colorEnumeration.Elements;
// Get Blue Color EnumerationElement
{
if (color.Name == "Blue")
{
blueColor = color;
break;
}
}
// Set Blue Color into inParameter
inParameter.Value = blueColor;
}
void ChangeLocationPropertyOfResourceLocation(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
// Assume that the given inParameter datatype is ResourceLocation
// and the default ID is "ID_1" and Location is "current/path/to/resource"
// Update ResourceLocation
// Since this is a reference to the ResourceLocaiton inside inParameter
// , the ResourceLocation's Location should also be updated, inParameter.Value does not need to be set again
resourceLocation.Location = "new/path/to/resource";
}
void CreateNewResourceLocationWithResourceLocationConstructor(OpenTestSystem.Otx.Runtime.Api.Otx.IProcedureInParameter inParameter)
{
String id = "Logo";
String location = "/home/chris/pictures/myLogo.ico";
// Assume that the given inParameter datatype is ResourceLocation
inParameter.Value = logoResource;
}
Represents an element of an OTX Enumeration
Definition: EnumerationElement.cs:17
long Value
Gets the value of the enumeration element
Definition: EnumerationElement.cs:79
Represents the data type ResourceLocation
Definition: ResourceLocation.cs:7
string Location
Gets default resource location
Definition: ResourceLocation.cs:34
Represents OTX StructureElement DataType.
Definition: StructureElement.cs:7
Represents OTX Structure DataType.
Definition: Structure.cs:13
Converts OTX Value to Literal in OTL notation and vice versa.
Definition: ValueConverter.cs:22
static string Value2String(object value)
Converts a value from an element of the API to a Literal in OTL notation.
Definition: ValueConverter.cs:229
Thrown when a literal string cannot be converted to OTX Object.
Definition: InvalidLiteralException.cs:14
Represents class for InOutParameter of a Procedure. The parameter is identical to a related parameter...
Definition: IProcedureInOutParameter.cs:8
Represents class for InParameter of a Procedure. The parameter is identical to a related parameter in...
Definition: IProcedureInParameter.cs:8
Represents base class for InParameter, OutParameter and InOutParameter of a Procedure....
Definition: IProcedureParameter.cs:8
Represents an OTX Enumeration Signature.
Definition: IEnumerationSignature.cs:14
Namespace which contains all supported data types
Definition: BlackBox.cs:5
Namespace containing exceptions
Definition: ConnectionStateException.cs:7
Namespace for browsing at OTX signatures.
Definition: IConstructorSignature.cs:10
Namespace for browsing at OTX data structure.
Definition: IContextVariable.cs:5
Namespace containing the programming interface for browsing and execution of OTX procedures in own ap...
Definition: ApiConstants.cs:5
Namespace containing all objects for browsing and execution of OTX procedures
Definition: ApiConstants.cs:5
Namespace containing all objects which are standardized according to ISO 13209 (OTX)
Namespace containing all objects related to testing inside automotive industry

The documentation for this class was generated from the following file: