| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using Itinero.Logging; |
| | | 5 | | using MoonSharp.Interpreter; |
| | | 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 | | { |
| | | 14 | | private readonly Script _script; |
| | 7 | 15 | | private readonly object _lock = new(); |
| | | 16 | | |
| | | 17 | | private readonly bool _hasTurnFactor; |
| | | 18 | | |
| | 7 | 19 | | private LuaProfile(Script script) |
| | 7 | 20 | | { |
| | 7 | 21 | | _script = script; |
| | 7 | 22 | | this.Name = _script.Globals.Get("name").String; |
| | 7 | 23 | | _hasTurnFactor = _script.Globals.Get("turn_cost_factor").Type == DataType.Function; |
| | 7 | 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 | | } |
| | 7 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | 12105 | 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 script = new Script(); |
| | 0 | 42 | | script.DoFile(path); |
| | 0 | 43 | | return new LuaProfile(script); |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Loads profile from a raw lua script. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="script">The script.</param> |
| | | 50 | | /// <param name="name">The name of the script.</param> |
| | | 51 | | /// <returns>The profile.</returns> |
| | | 52 | | public static Profile Load(string script, string name) |
| | 7 | 53 | | { |
| | 7 | 54 | | var s = new Script(); |
| | 7 | 55 | | s.DoString(script); |
| | 7 | 56 | | return new LuaProfile(s); |
| | 7 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public override EdgeFactor Factor(IEnumerable<(string key, string value)> attributes) |
| | 1785 | 61 | | { |
| | 1785 | 62 | | lock (_lock) |
| | 1785 | 63 | | { |
| | 1785 | 64 | | var attributesTable = new Table(_script); |
| | 1785 | 65 | | var resultTable = new Table(_script); |
| | 28879 | 66 | | foreach (var (k, v) in attributes) |
| | 11762 | 67 | | { |
| | 11762 | 68 | | attributesTable[k] = v; |
| | 11762 | 69 | | } |
| | | 70 | | |
| | 1785 | 71 | | _script.Call(_script.Globals.Get("factor"), attributesTable, resultTable); |
| | | 72 | | |
| | 1785 | 73 | | var forward = resultTable.GetDouble("forward") ?? 0; |
| | 1785 | 74 | | var backward = resultTable.GetDouble("backward") ?? 0; |
| | | 75 | | |
| | 1785 | 76 | | var speedForward = resultTable.GetDouble("forward_speed"); |
| | 1785 | 77 | | if (speedForward == null) |
| | 1178 | 78 | | { |
| | | 79 | | // when forward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s) |
| | 1178 | 80 | | speedForward = 0; |
| | 1178 | 81 | | if (forward > 0) |
| | 765 | 82 | | { // convert to m/s. |
| | 765 | 83 | | speedForward = 1.0 / forward; |
| | 765 | 84 | | } |
| | 1178 | 85 | | } |
| | | 86 | | else |
| | 607 | 87 | | { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s. |
| | 607 | 88 | | speedForward /= 3.6; |
| | 607 | 89 | | } |
| | | 90 | | |
| | 1785 | 91 | | var speedBackward = resultTable.GetDouble("backward_speed"); |
| | 1785 | 92 | | if (speedBackward == null) |
| | 1178 | 93 | | { |
| | | 94 | | // when backward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s) |
| | 1178 | 95 | | speedBackward = 0; |
| | 1178 | 96 | | if (backward > 0) |
| | 528 | 97 | | { // convert to m/s. |
| | 528 | 98 | | speedBackward = 1.0 / backward; |
| | 528 | 99 | | } |
| | 1178 | 100 | | } |
| | | 101 | | else |
| | 607 | 102 | | { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s. |
| | 607 | 103 | | speedBackward /= 3.6; |
| | 607 | 104 | | } |
| | | 105 | | |
| | 1785 | 106 | | var canstop = resultTable.GetBoolean("canstop") ?? (backward > 0 || forward > 0); |
| | | 107 | | |
| | 1785 | 108 | | return new EdgeFactor( |
| | 1785 | 109 | | (uint)(forward * 100), |
| | 1785 | 110 | | (uint)(backward * 100), |
| | 1785 | 111 | | (ushort)(speedForward * 100), |
| | 1785 | 112 | | (ushort)(speedBackward * 100), |
| | 1785 | 113 | | canstop |
| | 1785 | 114 | | ); |
| | | 115 | | } |
| | 1785 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <inheritdoc /> |
| | | 119 | | public override TurnCostFactor TurnCostFactor(IEnumerable<(string key, string value)> attributes) |
| | 29 | 120 | | { |
| | 29 | 121 | | if (!_hasTurnFactor || !attributes.Any()) |
| | 1 | 122 | | { |
| | 1 | 123 | | return Profiles.TurnCostFactor.Empty; |
| | | 124 | | } |
| | | 125 | | |
| | 28 | 126 | | lock (_lock) |
| | 28 | 127 | | { |
| | 28 | 128 | | var attributesTable = new Table(_script); |
| | 28 | 129 | | var resultTable = new Table(_script); |
| | 188 | 130 | | foreach (var (k, v) in attributes) |
| | 52 | 131 | | { |
| | 52 | 132 | | attributesTable[k] = v; |
| | 52 | 133 | | } |
| | | 134 | | |
| | 28 | 135 | | _script.Call(_script.Globals.Get("turn_cost_factor"), attributesTable, resultTable); |
| | | 136 | | |
| | 28 | 137 | | var factor = resultTable.GetDouble("factor") ?? 0; |
| | | 138 | | |
| | 28 | 139 | | var turnCostFactor = Profiles.TurnCostFactor.Empty; |
| | 28 | 140 | | if (factor < 0) |
| | 8 | 141 | | { |
| | 8 | 142 | | turnCostFactor = Profiles.TurnCostFactor.Binary; |
| | 8 | 143 | | } |
| | 20 | 144 | | else if (factor > 0) |
| | 0 | 145 | | { |
| | 0 | 146 | | turnCostFactor = new TurnCostFactor((uint)(factor * 10)); |
| | 0 | 147 | | } |
| | 28 | 148 | | return turnCostFactor; |
| | | 149 | | } |
| | 29 | 150 | | } |
| | | 151 | | } |