< Summary

Class:Itinero.Profiles.Lua.LuaProfile
Assembly:Itinero.Profiles.Lua
File(s):/home/runner/work/routing2/routing2/src/Itinero.Profiles.Lua/LuaProfile.cs
Covered lines:85
Uncovered lines:8
Coverable lines:93
Total lines:151
Line coverage:91.3% (85 of 93)
Covered branches:28
Total branches:34
Branch coverage:82.3% (28 of 34)
Tag:251_23667616543

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%2100%
get_Name()100%1100%
LoadFromFile(...)100%10%
Load(...)100%1100%
Factor(...)81.81%22100%
TurnCostFactor(...)80%1088%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.Profiles.Lua/LuaProfile.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using Itinero.Logging;
 5using MoonSharp.Interpreter;
 6
 7namespace Itinero.Profiles.Lua;
 8
 9/// <summary>
 10/// Represents a dynamic routing profile that is based on a lua function.
 11/// </summary>
 12public class LuaProfile : Profile
 13{
 14    private readonly Script _script;
 715    private readonly object _lock = new();
 16
 17    private readonly bool _hasTurnFactor;
 18
 719    private LuaProfile(Script script)
 720    {
 721        _script = script;
 722        this.Name = _script.Globals.Get("name").String;
 723        _hasTurnFactor = _script.Globals.Get("turn_cost_factor").Type == DataType.Function;
 724        if (!_hasTurnFactor)
 125        {
 126            Logger.Log("LuaProfile Turnfactor", TraceEventType.Verbose,
 127                "The profile " + this.Name + " doesn't have a turn_cost_factor defined");
 128        }
 729    }
 30
 31    /// <inheritdoc />
 1210532    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)
 040    {
 041        var script = new Script();
 042        script.DoFile(path);
 043        return new LuaProfile(script);
 044    }
 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)
 753    {
 754        var s = new Script();
 755        s.DoString(script);
 756        return new LuaProfile(s);
 757    }
 58
 59    /// <inheritdoc />
 60    public override EdgeFactor Factor(IEnumerable<(string key, string value)> attributes)
 178561    {
 178562        lock (_lock)
 178563        {
 178564            var attributesTable = new Table(_script);
 178565            var resultTable = new Table(_script);
 2887966            foreach (var (k, v) in attributes)
 1176267            {
 1176268                attributesTable[k] = v;
 1176269            }
 70
 178571            _script.Call(_script.Globals.Get("factor"), attributesTable, resultTable);
 72
 178573            var forward = resultTable.GetDouble("forward") ?? 0;
 178574            var backward = resultTable.GetDouble("backward") ?? 0;
 75
 178576            var speedForward = resultTable.GetDouble("forward_speed");
 178577            if (speedForward == null)
 117878            {
 79                // when forward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s)
 117880                speedForward = 0;
 117881                if (forward > 0)
 76582                { // convert to m/s.
 76583                    speedForward = 1.0 / forward;
 76584                }
 117885            }
 86            else
 60787            { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s.
 60788                speedForward /= 3.6;
 60789            }
 90
 178591            var speedBackward = resultTable.GetDouble("backward_speed");
 178592            if (speedBackward == null)
 117893            {
 94                // when backward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s)
 117895                speedBackward = 0;
 117896                if (backward > 0)
 52897                { // convert to m/s.
 52898                    speedBackward = 1.0 / backward;
 52899                }
 1178100            }
 101            else
 607102            { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s.
 607103                speedBackward /= 3.6;
 607104            }
 105
 1785106            var canstop = resultTable.GetBoolean("canstop") ?? (backward > 0 || forward > 0);
 107
 1785108            return new EdgeFactor(
 1785109                (uint)(forward * 100),
 1785110                (uint)(backward * 100),
 1785111                (ushort)(speedForward * 100),
 1785112                (ushort)(speedBackward * 100),
 1785113                canstop
 1785114            );
 115        }
 1785116    }
 117
 118    /// <inheritdoc />
 119    public override TurnCostFactor TurnCostFactor(IEnumerable<(string key, string value)> attributes)
 29120    {
 29121        if (!_hasTurnFactor || !attributes.Any())
 1122        {
 1123            return Profiles.TurnCostFactor.Empty;
 124        }
 125
 28126        lock (_lock)
 28127        {
 28128            var attributesTable = new Table(_script);
 28129            var resultTable = new Table(_script);
 188130            foreach (var (k, v) in attributes)
 52131            {
 52132                attributesTable[k] = v;
 52133            }
 134
 28135            _script.Call(_script.Globals.Get("turn_cost_factor"), attributesTable, resultTable);
 136
 28137            var factor = resultTable.GetDouble("factor") ?? 0;
 138
 28139            var turnCostFactor = Profiles.TurnCostFactor.Empty;
 28140            if (factor < 0)
 8141            {
 8142                turnCostFactor = Profiles.TurnCostFactor.Binary;
 8143            }
 20144            else if (factor > 0)
 0145            {
 0146                turnCostFactor = new TurnCostFactor((uint)(factor * 10));
 0147            }
 28148            return turnCostFactor;
 149        }
 29150    }
 151}