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.
Suppose the current directory is installation location of OTX-Runtime Converter API.
Code snippet of the OTX-Runtime Converter API Sample Program.
8 namespace OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication
12 static void Main(
string[] args)
14 LicenseManager.SetLicenseKey(Constants.LICENSE);
15 IRuntimeConverter runtimeConverter =
null;
18 if (!Arguments.ReadArguments(args, out arguments, out message))
20 System.Console.WriteLine(message);
24 Config.TraceLevel = arguments.TraceLevel;
25 if (!
string.IsNullOrEmpty(arguments.OutputFolder))
27 Config.OutputFolder = arguments.OutputFolder;
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);
38 if (arguments.DiagPort != 0)
40 runtimeConverter = RuntimeConverterFactory.CreateSocketRuntimeConverter(arguments.DiagPort);
41 Console.WriteLine(
"Diag port is " + arguments.DiagPort);
43 else if (!
string.IsNullOrEmpty(arguments.DiagPipe))
45 runtimeConverter = RuntimeConverterFactory.CreatePipeRuntimeConverter(arguments.DiagPipe);
46 Console.WriteLine(
"Diag pipe is " + arguments.DiagPipe);
50 runtimeConverter = RuntimeConverterFactory.CreateSocketRuntimeConverter(8888);
51 Console.WriteLine(
"Diag port is 8888");
54 runtimeConverter.XmlDbPort = arguments.XmlDbPort;
55 Console.WriteLine(
"XmlDb port is " + runtimeConverter.XmlDbPort);
57 if (!
string.IsNullOrEmpty(arguments.ImportDVGs))
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");
67 if (arguments.Type == Arguments.Types.ptx)
69 Console.WriteLine(
"Loading ptx {0}", GetAbsolutePath(arguments.InputPath));
70 runtimeConverter.LoadPtx(GetAbsolutePath(arguments.InputPath), arguments.Password);
72 else if (arguments.Type == Arguments.Types.ppx)
74 Console.WriteLine(
"Loading ppx {0}", GetAbsolutePath(arguments.InputPath));
75 runtimeConverter.LoadPpx(GetAbsolutePath(arguments.InputPath), arguments.Password);
79 Console.WriteLine(
"Loading project {0}", GetAbsolutePath(arguments.InputPath));
80 runtimeConverter.LoadProject(GetAbsolutePath(arguments.InputPath));
83 if (arguments.Validate)
86 Console.WriteLine(
"Validating ...");
87 IError[] errors = runtimeConverter.Validate();
88 if (errors.Length > 0)
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))
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))
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))
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))
118 if (errors.FirstOrDefault(err => err.SeverityType ==
SeverityTypes.Critical) !=
null)
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));
127 if (File.Exists(arguments.OutputFile))
129 string selection =
string.Empty;
130 while (selection.Trim() !=
"y" && selection.Trim() !=
"n")
133 Console.Write(
"Override existing file {0}? y(yes)/n(no): ", arguments.OutputFile);
134 selection = Console.ReadLine();
137 if (selection ==
"n")
144 Console.WriteLine(
"Compiling ...");
145 runtimeConverter.Compile(arguments.OutputFile);
146 if (File.Exists(arguments.OutputFile))
148 Console.WriteLine(
"{0} is written successfully.", arguments.OutputFile);
152 private static string GetAbsolutePath(
string path)
154 return Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), path);
157 private static void WriteError(IError error)
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>");
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
7 namespace OpenTestSystem.Otx.RuntimeConverter.Api.ReferenceApplication
11 using System.Collections.
Generic;
18 public class Arguments
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";
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; }
61 public string OutputFolder {
get;
private set; }
62 public ushort XmlDbPort {
get;
private set; }
66 public static bool ReadArguments(
string[] args, out Arguments arguments, out
string message)
69 arguments =
new Arguments();
70 arguments.Type = Types.ptx;
71 arguments.DiagPort = 8888;
72 arguments.XmlDbPort = 1984;
75 message = PrintUsage();
79 foreach (
string arg
in args)
81 if (arg.StartsWith(
"-"))
83 if (!ProcessOption(arg, arguments, out message))
90 if (
string.IsNullOrEmpty(arguments.InputPath))
92 arguments.InputPath = arg;
96 message =
string.Format(
"More than 1 input path: {0}, {1}.", arguments.InputPath, arg);
102 if (
string.IsNullOrEmpty(arguments.InputPath))
104 message = PrintUsage();
108 if (
string.IsNullOrEmpty(arguments.OutputFile))
110 if ((arguments.Type == Types.ptx || arguments.Type == Types.ppx))
112 arguments.OutputFile = arguments.InputPath;
116 message =
"The -out:<file> is mandatory if -type:project is set.";
124 private static bool ProcessOption(
string value, Arguments arguments, out
string error)
127 if (!value.StartsWith(
"-"))
129 throw new ArgumentException(value +
" is not an option.");
132 List<string> arr = value.Split(
':').ToList();
133 string optionName = arr[0];
135 string optionValue =
string.Join(
":", arr);
139 if (optionValue ==
"ptx")
141 arguments.Type = Types.ptx;
143 else if (optionValue ==
"ppx")
145 arguments.Type = Types.ppx;
147 else if (optionValue ==
"project")
149 arguments.Type = Types.project;
153 error = UnrecognizeOption + value +
".";
159 arguments.Password = optionValue;
162 if (!
string.IsNullOrEmpty(optionValue))
164 error = UnrecognizeOption + value +
".";
168 arguments.Validate =
true;
171 arguments.OutputFile = optionValue;
173 case optionImportDVGs:
174 arguments.ImportDVGs = optionValue;
178 if (ushort.TryParse(optionValue, out diagPort))
180 arguments.DiagPort = diagPort;
184 error = UnrecognizeOption + value +
".";
190 arguments.DiagPipe = optionValue;
191 arguments.DiagPort = 0;
193 case optionTraceLevel:
195 if (Enum.TryParse(optionValue.ToUpper(), out traceLevel))
197 arguments.TraceLevel = traceLevel;
201 error = UnrecognizeOption + value +
".";
205 case optionOutputFolder:
206 arguments.OutputFolder = optionValue;
210 if (ushort.TryParse(optionValue, out dbPort))
212 arguments.XmlDbPort = dbPort;
216 error = UnrecognizeOption + value +
".";
221 error = UnrecognizeOption + value +
".";
228 private static bool ParseDictionary(
string str, out Dictionary<string, string> dictionary)
230 dictionary =
new Dictionary<string, string>();
232 if (str.StartsWith(
"{") && str.EndsWith(
"}"))
234 str = str.Substring(1, str.Length - 2);
235 string[] arr = str.Split(
',');
236 foreach (
string pair
in arr)
238 string[] nameAndValue = pair.Split(
'=');
239 if (nameAndValue.Length != 2)
244 string name = nameAndValue[0];
245 string value = nameAndValue[1];
246 if (dictionary.ContainsKey(arr[0]))
251 dictionary.Add(name, value);
260 private static string PrintUsage()
262 StringBuilder builder =
new StringBuilder();
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();