OTX-Runtime Converter  
Sample Program

The console application demonstrates the main functionality of the OTX-Runtime Converter API. It can be used as a reference and the source code below is like a reference guide about the proper programming of the API.

When the OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication is started without arguments, the usage of API will be exposed.

Important: the target framework of OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication is Net6.0 so that it can work on both Windows and Linux.

Sample Commands

Suppose the current directory is installation location of OTX-Runtime Converter API.

OS Command
Window OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication "D:\RoutineControl_TestFunctions.ptx" -out:"D:\out\RoutineControl_TestFunctions.ptx" -validate -diagPort:8888 -importDVGs:"D:\DiagValidationGroups1.xml" -dbPort:1984
Linux ./OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication.exe "/home/emotive/RoutineControl_TestFunctions.ptx" -out:"/home/emotive/out/RoutineControl_TestFunctions.ptx" -validate -diagPort:8888 -importDVGs:"D:\DiagValidationGroups1.xml" -dbPort:1984

Code Example

Code snippet of the OTX-Runtime Converter API Sample Program.

4 using System;
5 using System.IO;
6 using System.Linq;
7 
8 namespace OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication
9 {
10  class Program
11  {
12  static void Main(string[] args)
13  {
14  LicenseManager.SetLicenseKey(Constants.LICENSE);
15  IRuntimeConverter runtimeConverter = null;
16  Arguments arguments;
17  string message;
18  if (!Arguments.ReadArguments(args, out arguments, out message))
19  {
20  System.Console.WriteLine(message);
21  return;
22  }
23 
24  Config.TraceLevel = arguments.TraceLevel;
25  if (!string.IsNullOrEmpty(arguments.OutputFolder))
26  {
27  Config.OutputFolder = arguments.OutputFolder;
28  }
29 
30  if (Config.TraceLevel != TraceLevels.OFF)
31  {
32  Console.WriteLine("*** Tracer was setup");
33  Console.WriteLine("- Output folder".PadRight(20) + Config.OutputFolder);
34  Console.WriteLine("- Trace folder".PadRight(20) + Config.TraceFolder);
35  Console.WriteLine("- Trace level".PadRight(20) + Config.TraceLevel);
36  }
37 
38  if (arguments.DiagPort != 0)
39  {
40  runtimeConverter = RuntimeConverterFactory.CreateSocketRuntimeConverter(arguments.DiagPort);
41  Console.WriteLine("Diag port is " + arguments.DiagPort);
42  }
43  else if (!string.IsNullOrEmpty(arguments.DiagPipe))
44  {
45  runtimeConverter = RuntimeConverterFactory.CreatePipeRuntimeConverter(arguments.DiagPipe);
46  Console.WriteLine("Diag pipe is " + arguments.DiagPipe);
47  }
48  else
49  {
50  runtimeConverter = RuntimeConverterFactory.CreateSocketRuntimeConverter(8888);
51  Console.WriteLine("Diag port is 8888");
52  }
53 
54  runtimeConverter.XmlDbPort = arguments.XmlDbPort;
55  Console.WriteLine("XmlDb port is " + runtimeConverter.XmlDbPort);
56 
57  if (!string.IsNullOrEmpty(arguments.ImportDVGs))
58  {
59  Console.WriteLine();
60  Console.WriteLine("Importing DiagValidationGroups from {0}", GetAbsolutePath(arguments.ImportDVGs));
61  runtimeConverter.DiagValidationGroups = new DiagValidationGroups();
62  runtimeConverter.DiagValidationGroups.Import(GetAbsolutePath(arguments.ImportDVGs));
63  Console.WriteLine("All DiagValidationGroups are imported");
64  }
65 
66  Console.WriteLine();
67  if (arguments.Type == Arguments.Types.ptx)
68  {
69  Console.WriteLine("Loading ptx {0}", GetAbsolutePath(arguments.InputPath));
70  runtimeConverter.LoadPtx(GetAbsolutePath(arguments.InputPath), arguments.Password);
71  }
72  else if (arguments.Type == Arguments.Types.ppx)
73  {
74  Console.WriteLine("Loading ppx {0}", GetAbsolutePath(arguments.InputPath));
75  runtimeConverter.LoadPpx(GetAbsolutePath(arguments.InputPath), arguments.Password);
76  }
77  else
78  {
79  Console.WriteLine("Loading project {0}", GetAbsolutePath(arguments.InputPath));
80  runtimeConverter.LoadProject(GetAbsolutePath(arguments.InputPath));
81  }
82 
83  if (arguments.Validate)
84  {
85  Console.WriteLine();
86  Console.WriteLine("Validating ...");
87  IError[] errors = runtimeConverter.Validate();
88  if (errors.Length > 0)
89  {
90  Console.WriteLine();
91  Console.WriteLine("*** {0} Error(s):", errors.Count(err => err.SeverityType == SeverityTypes.Critical));
92  foreach (IError error in errors.Where(err => err.SeverityType == SeverityTypes.Critical))
93  {
94  WriteError(error);
95  }
96 
97  Console.WriteLine();
98  Console.WriteLine("*** {0} Warning(s):", errors.Count(err => err.SeverityType == SeverityTypes.Warning));
99  foreach (IError error in errors.Where(err => err.SeverityType == SeverityTypes.Warning))
100  {
101  WriteError(error);
102  }
103 
104  Console.WriteLine();
105  Console.WriteLine("*** {0} Deprecation(s):", errors.Count(err => err.SeverityType == SeverityTypes.Deprecation));
106  foreach (IError error in errors.Where(err => err.SeverityType == SeverityTypes.Deprecation))
107  {
108  WriteError(error);
109  }
110 
111  Console.WriteLine();
112  Console.WriteLine("*** {0} Specification(s):", errors.Count(err => err.SeverityType == SeverityTypes.Specification));
113  foreach (IError error in errors.Where(err => err.SeverityType == SeverityTypes.Specification))
114  {
115  WriteError(error);
116  }
117 
118  if (errors.FirstOrDefault(err => err.SeverityType == SeverityTypes.Critical) != null)
119  {
120  Console.WriteLine();
121  Console.WriteLine("*** The {0} cannot be compiled because it has {1} error(s) and {2} warning(s).", arguments.Type, errors.Count(err => err.SeverityType == SeverityTypes.Critical), errors.Count(err => err.SeverityType == SeverityTypes.Warning));
122  return;
123  }
124  }
125  }
126 
127  if (File.Exists(arguments.OutputFile))
128  {
129  string selection = string.Empty;
130  while (selection.Trim() != "y" && selection.Trim() != "n")
131  {
132  Console.WriteLine();
133  Console.Write("Override existing file {0}? y(yes)/n(no): ", arguments.OutputFile);
134  selection = Console.ReadLine();
135  }
136 
137  if (selection == "n")
138  {
139  return;
140  }
141  }
142 
143  Console.WriteLine();
144  Console.WriteLine("Compiling ...");
145  runtimeConverter.Compile(arguments.OutputFile);
146  if (File.Exists(arguments.OutputFile))
147  {
148  Console.WriteLine("{0} is written successfully.", arguments.OutputFile);
149  }
150  }
151 
152  private static string GetAbsolutePath(string path)
153  {
154  return Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), path);
155  }
156 
157  private static void WriteError(IError error)
158  {
159  Console.Write("<SeverityType>");
160  Console.Write(error.SeverityType);
161  Console.Write("</SeverityType>");
162  Console.Write("<Description>");
163  Console.Write(error.Description);
164  Console.Write("</Description>");
165  Console.Write("<ScopeId>");
166  Console.Write(error.ScopeId);
167  Console.Write("</ScopeId>");
168  Console.Write("<ScopeName>");
169  Console.Write(error.ScopeName);
170  Console.Write("</ScopeName>");
171  Console.Write("<ElementType>");
172  Console.Write(error.ElementType);
173  Console.Write("</ElementType>");
174  Console.Write("<ErrorCode>");
175  Console.Write(error.ErrorCode);
176  Console.Write("</ErrorCode>");
177  Console.Write("<Procedure>");
178  Console.Write(error.Procedure);
179  Console.Write("</Procedure>");
180  Console.Write("<Document>");
181  Console.Write(error.Document);
182  Console.Write("</Document>");
183  Console.Write("<Package>");
184  Console.Write(error.Package);
185  Console.Write("</Package>");
186  Console.Write("<Project>");
187  Console.WriteLine(error.Project);
188  Console.Write("</Project>");
189  Console.WriteLine();
190  }
191  }
192 }
Namespace containing all objects related to licensing
Definition: LicenseManager.cs:16
Namespace containing all objects related to DiagValidationGroups.
Definition: Api2ModelConverter.cs:6
Namespace containing all objects for validation.
Definition: IError.cs:11
Namespace containing the programming interface for for validation and compilation of PTX,...
Definition: Config.cs:8
TraceLevels
Enumeration of TraceLevels
Definition: TraceLevels.cs:13
Namespace containing all objects for validation and compilation of PTX, PPX, PROJECT
Definition: Config.cs:8
Namespace containing all objects which are standardized according to ISO 13209 (OTX)
Namespace containing all objects related to testing inside automotive industry
1 //-----------------------------------------------------------------------
2 // <copyright file="Arguments.cs">
3 // Copyright (c). All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
6 
7 namespace OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication
8 {
10  using System;
11  using System.Collections.Generic;
12  using System.Linq;
13  using System.Text;
14 
18  public class Arguments
19  {
20  public enum Types
21  {
22  ptx,
23  ppx,
24  project
25  }
26 
27  private const string UnrecognizeOption = "Unrecognized option: ";
28  private const string optionType = "-type";
29  private const string optionPassword = "-password";
30  private const string optionValidate = "-validate";
31  private const string optionOut = "-out";
32  private const string optionImportDVGs = "-importDVGs";
33  private const string optionDiagPort = "-diagPort";
34  private const string optionDiagPipe = "-diagPipe";
35  private const string optionTraceLevel = "-traceLevel";
36  private const string optionOutputFolder = "-outputFolder";
37  private const string optionDbPort = "-dbPort";
38 
39  #region Fields
40 
41  #endregion
42 
43  #region ctors
44  public Arguments()
45  {
46  this.TraceLevel = TraceLevels.ERROR;
47  }
48 
49  #endregion
50 
51  #region Properties
52  public string InputPath { get; private set; }
53  public Types Type { get; private set; }
54  public string Password { get; private set; }
55  public bool Validate { get; private set; }
56  public string OutputFile { get; private set; }
57  public string ImportDVGs { get; private set; }
58  public ushort DiagPort { get; private set; }
59  public string DiagPipe { get; private set; }
60  public TraceLevels TraceLevel { get; private set; }
61  public string OutputFolder { get; private set; }
62  public ushort XmlDbPort { get; private set; }
63  #endregion
64 
65  #region Methods
66  public static bool ReadArguments(string[] args, out Arguments arguments, out string message)
67  {
68  message = "";
69  arguments = new Arguments();
70  arguments.Type = Types.ptx;
71  arguments.DiagPort = 8888;
72  arguments.XmlDbPort = 1984;
73  if (args.Length == 0)
74  {
75  message = PrintUsage();
76  return false;
77  }
78 
79  foreach (string arg in args)
80  {
81  if (arg.StartsWith("-"))//option
82  {
83  if (!ProcessOption(arg, arguments, out message))
84  {
85  return false;
86  }
87  }
88  else
89  {
90  if (string.IsNullOrEmpty(arguments.InputPath))
91  {
92  arguments.InputPath = arg;
93  }
94  else
95  {
96  message = string.Format("More than 1 input path: {0}, {1}.", arguments.InputPath, arg);
97  return false;
98  }
99  }
100  }
101 
102  if (string.IsNullOrEmpty(arguments.InputPath))
103  {
104  message = PrintUsage();
105  return false;
106  }
107 
108  if (string.IsNullOrEmpty(arguments.OutputFile))
109  {
110  if ((arguments.Type == Types.ptx || arguments.Type == Types.ppx))
111  {
112  arguments.OutputFile = arguments.InputPath;
113  }
114  else
115  {
116  message = "The -out:<file> is mandatory if -type:project is set.";
117  return false;
118  }
119  }
120 
121  return true;
122  }
123 
124  private static bool ProcessOption(string value, Arguments arguments, out string error)
125  {
126  error = "";
127  if (!value.StartsWith("-"))
128  {
129  throw new ArgumentException(value + " is not an option.");
130  }
131 
132  List<string> arr = value.Split(':').ToList();
133  string optionName = arr[0];
134  arr.RemoveAt(0);
135  string optionValue = string.Join(":", arr);
136  switch (optionName)
137  {
138  case optionType:
139  if (optionValue == "ptx")
140  {
141  arguments.Type = Types.ptx;
142  }
143  else if (optionValue == "ppx")
144  {
145  arguments.Type = Types.ppx;
146  }
147  else if (optionValue == "project")
148  {
149  arguments.Type = Types.project;
150  }
151  else
152  {
153  error = UnrecognizeOption + value + ".";
154  return false;
155  }
156 
157  break;
158  case optionPassword:
159  arguments.Password = optionValue;
160  break;
161  case optionValidate:
162  if (!string.IsNullOrEmpty(optionValue))
163  {
164  error = UnrecognizeOption + value + ".";
165  return false;
166  }
167 
168  arguments.Validate = true;
169  break;
170  case optionOut:
171  arguments.OutputFile = optionValue;
172  break;
173  case optionImportDVGs:
174  arguments.ImportDVGs = optionValue;
175  break;
176  case optionDiagPort:
177  ushort diagPort = 0;
178  if (ushort.TryParse(optionValue, out diagPort))
179  {
180  arguments.DiagPort = diagPort;
181  }
182  else
183  {
184  error = UnrecognizeOption + value + ".";
185  return false;
186  }
187 
188  break;
189  case optionDiagPipe:
190  arguments.DiagPipe = optionValue;
191  arguments.DiagPort = 0;
192  break;
193  case optionTraceLevel:
194  TraceLevels traceLevel;
195  if (Enum.TryParse(optionValue.ToUpper(), out traceLevel))
196  {
197  arguments.TraceLevel = traceLevel;
198  }
199  else
200  {
201  error = UnrecognizeOption + value + ".";
202  return false;
203  }
204  break;
205  case optionOutputFolder:
206  arguments.OutputFolder = optionValue;
207  break;
208  case optionDbPort:
209  ushort dbPort = 0;
210  if (ushort.TryParse(optionValue, out dbPort))
211  {
212  arguments.XmlDbPort = dbPort;
213  }
214  else
215  {
216  error = UnrecognizeOption + value + ".";
217  return false;
218  }
219  break;
220  default:
221  error = UnrecognizeOption + value + ".";
222  return false;
223  }
224 
225  return true;
226  }
227 
228  private static bool ParseDictionary(string str, out Dictionary<string, string> dictionary)
229  {
230  dictionary = new Dictionary<string, string>();
231  str = str.Trim();
232  if (str.StartsWith("{") && str.EndsWith("}"))
233  {
234  str = str.Substring(1, str.Length - 2);
235  string[] arr = str.Split(',');
236  foreach (string pair in arr)
237  {
238  string[] nameAndValue = pair.Split('=');
239  if (nameAndValue.Length != 2)
240  {
241  return false;
242  }
243 
244  string name = nameAndValue[0];
245  string value = nameAndValue[1];
246  if (dictionary.ContainsKey(arr[0]))
247  {
248  return false;
249  }
250 
251  dictionary.Add(name, value);
252  }
253 
254  return true;
255  }
256 
257  return false;
258  }
259 
260  private static string PrintUsage()
261  {
262  StringBuilder builder = new StringBuilder();
263  int count = 35;
264  builder.AppendLine(string.Format("RuntimeConverter version {0}", Config.Version));
265  builder.AppendLine("Usage: OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication [options] <path>");
266  builder.AppendLine();
267  builder.AppendLine("<path>".PadRight(count) + "ptx file, ppx file or project directory");
268  builder.AppendLine();
269  builder.AppendLine("options".PadLeft(count));
270  builder.AppendLine(string.Format("{0}:ptx", optionType).PadRight(count) + "Load a ptx file (default)");
271  builder.AppendLine(string.Format("{0}:ppx", optionType).PadRight(count) + "Load a ppx file");
272  builder.AppendLine(string.Format("{0}:project", optionType).PadRight(count) + "Load a project directory containing project file (*.otfPrj)");
273  builder.AppendLine(string.Format("{0}:<password>", optionPassword).PadRight(count) + "Specify password to decrypt the input ptx or ppx");
274  builder.AppendLine(string.Format("{0}", optionValidate).PadRight(count) + "Validate before compiling. If an error occurs, compiling will be canceled");
275  builder.AppendLine(string.Format("{0}:<file>", optionOut).PadRight(count) + "Specify output file");
276  builder.AppendLine(new string(' ', count) + string.Format("If {0}:ptx or {0}:ppx is set and this option is ommitted, output file will be the same as input file", optionType));
277  builder.AppendLine(new string(' ', count) + string.Format("If {0}:project is set, this option is mandatory", optionType));
278  builder.AppendLine(string.Format("{0}:<file>", optionImportDVGs).PadRight(count) + "Import a DiagValidationGroups file (xml)");
279  builder.AppendLine(string.Format("{0}:<port>", optionDiagPort).PadRight(count) + "Specify diag port. Default is 8888");
280  builder.AppendLine(string.Format("{0}:<pipe name>", optionDiagPipe).PadRight(count) + "Specify diag pipe name");
281  builder.AppendLine(string.Format("{0}:<db port>", optionDbPort).PadRight(count) + "Specify XmlDb port. Default is 1984");
282  builder.AppendLine(string.Format("{0}:<trace level>", optionTraceLevel).PadRight(count) + "Specify trace level: ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL or OFF (default)");
283  builder.AppendLine(string.Format("{0}:<folder>", optionOutputFolder).PadRight(count) + "Specify a folder where BaseX databases, extracted files, trace files are put in");
284  return builder.ToString();
285  }
286  #endregion
287  }
288 }