ULSAlgorithms 1.1.0-g3e5595996d
High-performance exact and heuristic algorithms for uncapacitated lot sizing
Loading...
Searching...
No Matches
XpressRuntimeLocator.cs
Go to the documentation of this file.
1using System.Reflection;
2using System.Runtime.Loader;
3
5
6/// <summary>
7/// Locates and loads the optional FICO Xpress Optimizer managed assembly.
8/// </summary>
9public static class XpressRuntimeLocator
10{
11 /// <summary>
12 /// Resolves the Optimizer.dll path without loading it.
13 /// </summary>
14 public static string ResolveAssemblyPath()
15 {
16 string[] explicitVariables =
17 [
18 "ULSALGORITHMS_XPRESS_OPTIMIZER_ASSEMBLY",
19 "LOTSIZING_XPRESS_OPTIMIZER_ASSEMBLY"
20 ];
21
22 foreach (string variableName in explicitVariables)
23 {
24 string explicitPath =
25 Environment.GetEnvironmentVariable(
26 variableName)?
27 .Trim()
28 .Trim('"') ??
29 string.Empty;
30
31 if (File.Exists(explicitPath))
32 {
33 return Path.GetFullPath(explicitPath);
34 }
35 }
36
37 string xpressDirectory =
38 Environment.GetEnvironmentVariable(
39 "XPRESSDIR")?.Trim() ??
40 string.Empty;
41
42 if (!string.IsNullOrWhiteSpace(xpressDirectory))
43 {
44 string[] candidates =
45 [
46 Path.Combine(
47 xpressDirectory,
48 "bin",
49 "Optimizer.dll"),
50 Path.Combine(
51 xpressDirectory,
52 "lib",
53 "Optimizer.dll"),
54 Path.Combine(
55 xpressDirectory,
56 "bin",
57 "dotnet",
58 "Optimizer.dll"),
59 Path.Combine(
60 xpressDirectory,
61 "lib",
62 "dotnet",
63 "Optimizer.dll"),
64 Path.Combine(
65 xpressDirectory,
66 "Optimizer.dll")
67 ];
68
69 foreach (string candidate in candidates)
70 {
71 if (File.Exists(candidate))
72 {
73 return Path.GetFullPath(candidate);
74 }
75 }
76 }
77
78 return string.Empty;
79 }
80
81 /// <summary>
82 /// Loads an already loaded Optimizer assembly, an explicitly located
83 /// assembly, or an assembly resolvable by normal .NET probing.
84 /// </summary>
85 public static Assembly? TryLoad(
86 out string resolvedPath)
87 {
88 resolvedPath = string.Empty;
89
90 Assembly? loaded =
91 AppDomain.CurrentDomain
92 .GetAssemblies()
93 .FirstOrDefault(
94 assembly =>
95 string.Equals(
96 assembly.GetName().Name,
97 "Optimizer",
98 StringComparison.OrdinalIgnoreCase));
99
100 if (loaded is not null)
101 {
102 resolvedPath =
103 string.IsNullOrWhiteSpace(loaded.Location)
104 ? "already loaded"
105 : loaded.Location;
106
107 return loaded;
108 }
109
110 string path =
112
113 if (!string.IsNullOrWhiteSpace(path))
114 {
115 resolvedPath = path;
116
117 return AssemblyLoadContext.Default
118 .LoadFromAssemblyPath(path);
119 }
120
121 try
122 {
123 Assembly assembly =
124 Assembly.Load("Optimizer");
125
126 resolvedPath =
127 string.IsNullOrWhiteSpace(assembly.Location)
128 ? "assembly probing"
129 : assembly.Location;
130
131 return assembly;
132 }
133 catch
134 {
135 return null;
136 }
137 }
138}
Locates and loads the optional FICO Xpress Optimizer managed assembly.
static ? Assembly TryLoad(out string resolvedPath)
Loads an already loaded Optimizer assembly, an explicitly located assembly, or an assembly resolvable...
static string ResolveAssemblyPath()
Resolves the Optimizer.dll path without loading it.