| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using Itinero.Logging; |
| | 5 | | using Neo.IronLua; |
| | 6 | |
|
| | 7 | | namespace Itinero.Profiles.Lua; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Represents a dynamic routing profile that is based on a lua function. |
| | 11 | | /// </summary> |
| | 12 | | public class LuaProfile : Profile |
| | 13 | | { |
| 1 | 14 | | private static readonly global::Neo.IronLua.Lua _lua = new(); |
| 4 | 15 | | private readonly dynamic _env = _lua.CreateEnvironment(); |
| | 16 | |
|
| | 17 | | private readonly bool _hasTurnFactor; |
| | 18 | |
|
| 4 | 19 | | private LuaProfile(LuaChunk chunk) |
| 4 | 20 | | { |
| 4 | 21 | | _env.dochunk(chunk); |
| 4 | 22 | | this.Name = _env.name; |
| 4 | 23 | | _hasTurnFactor = _env.turn_cost_factor != null; |
| 4 | 24 | | if (!_hasTurnFactor) |
| 1 | 25 | | { |
| 1 | 26 | | Logger.Log("LuaProfile Turnfactor", TraceEventType.Verbose, |
| 1 | 27 | | "The profile " + this.Name + " doesn't have a turn_cost_factor defined"); |
| 1 | 28 | | } |
| 4 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc /> |
| 1 | 32 | | public override string Name { get; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Loads a profile from a lua script file. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="path">The path to the lua file.</param> |
| | 38 | | /// <returns>The profile.</returns> |
| | 39 | | public static Profile LoadFromFile(string path) |
| 0 | 40 | | { |
| 0 | 41 | | var chunk = _lua.CompileChunk(path, new LuaCompileOptions()); |
| 0 | 42 | | return new LuaProfile(chunk); |
| 0 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Loads profile from a raw lua script. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="script">The script.</param> |
| | 49 | | /// <param name="name">The name of the script.</param> |
| | 50 | | /// <returns>The profile.</returns> |
| | 51 | | public static Profile Load(string script, string name) |
| 4 | 52 | | { |
| 4 | 53 | | var chunk = _lua.CompileChunk(script, name, new LuaCompileOptions()); |
| 4 | 54 | | return new LuaProfile(chunk); |
| 4 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <inheritdoc /> |
| | 58 | | public override EdgeFactor Factor(IEnumerable<(string key, string value)> attributes) |
| 3 | 59 | | { |
| 3 | 60 | | var attributesTable = new LuaTable(); |
| 3 | 61 | | var resultTable = new LuaTable(); |
| 17 | 62 | | foreach (var (k, v) in attributes) |
| 4 | 63 | | { |
| 4 | 64 | | attributesTable[k] = v; |
| 4 | 65 | | } |
| | 66 | |
|
| 3 | 67 | | _env.factor(attributesTable, resultTable); |
| | 68 | |
|
| 3 | 69 | | var forward = resultTable.GetDouble("forward") ?? 0; |
| 3 | 70 | | var backward = resultTable.GetDouble("backward") ?? 0; |
| | 71 | |
|
| 3 | 72 | | var speedForward = resultTable.GetDouble("forward_speed"); |
| 3 | 73 | | if (speedForward == null) |
| 3 | 74 | | { |
| | 75 | | // when forward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s) |
| 3 | 76 | | speedForward = 0; |
| 3 | 77 | | if (forward > 0) |
| 2 | 78 | | { // convert to m/s. |
| 2 | 79 | | speedForward = 1.0 / forward; |
| 2 | 80 | | } |
| 3 | 81 | | } |
| | 82 | | else |
| 0 | 83 | | { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s. |
| 0 | 84 | | speedForward /= 3.6; |
| 0 | 85 | | } |
| | 86 | |
|
| 3 | 87 | | var speedBackward = resultTable.GetDouble("backward_speed"); |
| 3 | 88 | | if (speedBackward == null) |
| 3 | 89 | | { |
| | 90 | | // when backward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s) |
| 3 | 91 | | speedBackward = 0; |
| 3 | 92 | | if (backward > 0) |
| 1 | 93 | | { // convert to m/s. |
| 1 | 94 | | speedBackward = 1.0 / backward; |
| 1 | 95 | | } |
| 3 | 96 | | } |
| | 97 | | else |
| 0 | 98 | | { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s. |
| 0 | 99 | | speedBackward /= 3.6; |
| 0 | 100 | | } |
| | 101 | |
|
| 3 | 102 | | var canstop = resultTable.GetBoolean("canstop") ?? (backward > 0 || forward > 0); |
| | 103 | |
|
| 3 | 104 | | return new EdgeFactor( |
| 3 | 105 | | (uint)(forward * 100), |
| 3 | 106 | | (uint)(backward * 100), |
| 3 | 107 | | (ushort)(speedForward * 100), |
| 3 | 108 | | (ushort)(speedBackward * 100), |
| 3 | 109 | | canstop |
| 3 | 110 | | ); |
| 3 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <inheritdoc /> |
| | 114 | | public override TurnCostFactor TurnCostFactor(IEnumerable<(string key, string value)> attributes) |
| 2 | 115 | | { |
| 2 | 116 | | if (!_hasTurnFactor || !attributes.Any()) |
| 1 | 117 | | { |
| 1 | 118 | | return Profiles.TurnCostFactor.Empty; |
| | 119 | | } |
| | 120 | |
|
| 1 | 121 | | var attributesTable = new LuaTable(); |
| 1 | 122 | | var resultTable = new LuaTable(); |
| 7 | 123 | | foreach (var (k, v) in attributes) |
| 2 | 124 | | { |
| 2 | 125 | | attributesTable[k] = v; |
| 2 | 126 | | } |
| | 127 | |
|
| 1 | 128 | | _env.turn_cost_factor(attributesTable, resultTable); |
| | 129 | |
|
| 1 | 130 | | var factor = resultTable.GetDouble("factor") ?? 0; |
| | 131 | |
|
| 1 | 132 | | var turnCostFactor = Profiles.TurnCostFactor.Empty; |
| 1 | 133 | | if (factor < 0) |
| 1 | 134 | | { |
| 1 | 135 | | turnCostFactor = Profiles.TurnCostFactor.Binary; |
| 1 | 136 | | } |
| 0 | 137 | | else if (factor > 0) |
| 0 | 138 | | { |
| 0 | 139 | | turnCostFactor = new TurnCostFactor((uint)(factor * 10)); |
| 0 | 140 | | } |
| 1 | 141 | | return turnCostFactor; |
| 2 | 142 | | } |
| | 143 | | } |