-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathProgram.cs
65 lines (56 loc) · 2.33 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using CommandLine;
using SC.ObjectModel;
using SC.ObjectModel.IO;
using System;
using System.IO;
using SC.CLI;
using SC.ObjectModel.Additionals;
using SC.ObjectModel.Configuration;
using SC.ObjectModel.IO.Json;
namespace SC.CLI
{
class Options
{
[Option('i', "input", Required = false, HelpText = "Input file to process")]
public string Input { get; set; }
[Option('o', "output", Required = false, HelpText = "Output file to write to")]
public string Output { get; set; }
}
class Program
{
private static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(Execute);
}
private static void Execute(Options opts)
{
// Read the content fully first (either from file or from stdin)
var content = string.IsNullOrWhiteSpace(opts.Input) ? Console.In.ReadToEnd() : File.ReadAllText(opts.Input);
// Try to parse the input as a calculation
var instance = JsonIO.From<JsonCalculation>(content);
// If nothing useful was found, try to parse it as an unnested instance (without a configuration)
if (instance == null || instance.Instance == null && instance.Configuration == null)
{
var inst = JsonIO.From<JsonInstance>(content);
if (inst != null)
instance = new JsonCalculation() { Instance = inst };
}
// If still nothing useful was found, abort
if (instance == null || instance.Instance == null)
{
Console.WriteLine("No 'instance' found in input file.");
return;
}
// >> Run calculation
Action<string> logger = string.IsNullOrWhiteSpace(opts.Output) ? null : Console.Write;
instance.Configuration ??= new Configuration(MethodType.ExtremePointInsertion, true);
var result = Executor.Execute(Instance.FromJsonInstance(instance.Instance), instance.Configuration, logger);
// Output result
if (string.IsNullOrWhiteSpace(opts.Output))
Console.WriteLine(JsonIO.To(result.Solution.ToJsonSolution()));
else
File.WriteAllText(opts.Output, JsonIO.To(result.Solution.ToJsonSolution()));
}
}
}