< 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:74
Uncovered lines:14
Coverable lines:88
Total lines:143
Line coverage:84% (74 of 88)
Covered branches:38
Total branches:50
Branch coverage:76% (38 of 50)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.cctor()100%1100%
.ctor(...)100%14100%
get_Name()100%1100%
LoadFromFile(...)100%10%
Load(...)100%1100%
Factor(...)62.5%2486.04%
TurnCostFactor(...)75%1282.6%

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 Neo.IronLua;
 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{
 114    private static readonly global::Neo.IronLua.Lua _lua = new();
 415    private readonly dynamic _env = _lua.CreateEnvironment();
 16
 17    private readonly bool _hasTurnFactor;
 18
 419    private LuaProfile(LuaChunk chunk)
 420    {
 421        _env.dochunk(chunk);
 422        this.Name = _env.name;
 423        _hasTurnFactor = _env.turn_cost_factor != null;
 424        if (!_hasTurnFactor)
 125        {
 126            Logger.Log("LuaProfile Turnfactor", TraceEventType.Verbose,
 127                "The profile " + this.Name + " doesn't have a turn_cost_factor defined");
 128        }
 429    }
 30
 31    /// <inheritdoc />
 132    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 chunk = _lua.CompileChunk(path, new LuaCompileOptions());
 042        return new LuaProfile(chunk);
 043    }
 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)
 452    {
 453        var chunk = _lua.CompileChunk(script, name, new LuaCompileOptions());
 454        return new LuaProfile(chunk);
 455    }
 56
 57    /// <inheritdoc />
 58    public override EdgeFactor Factor(IEnumerable<(string key, string value)> attributes)
 359    {
 360        var attributesTable = new LuaTable();
 361        var resultTable = new LuaTable();
 1762        foreach (var (k, v) in attributes)
 463        {
 464            attributesTable[k] = v;
 465        }
 66
 367        _env.factor(attributesTable, resultTable);
 68
 369        var forward = resultTable.GetDouble("forward") ?? 0;
 370        var backward = resultTable.GetDouble("backward") ?? 0;
 71
 372        var speedForward = resultTable.GetDouble("forward_speed");
 373        if (speedForward == null)
 374        {
 75            // when forward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s)
 376            speedForward = 0;
 377            if (forward > 0)
 278            { // convert to m/s.
 279                speedForward = 1.0 / forward;
 280            }
 381        }
 82        else
 083        { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s.
 084            speedForward /= 3.6;
 085        }
 86
 387        var speedBackward = resultTable.GetDouble("backward_speed");
 388        if (speedBackward == null)
 389        {
 90            // when backward_speed isn't explicitly filled, the assumption is that factors are in 1/(m/s)
 391            speedBackward = 0;
 392            if (backward > 0)
 193            { // convert to m/s.
 194                speedBackward = 1.0 / backward;
 195            }
 396        }
 97        else
 098        { // when forward_speed is filled, it's assumed to be in km/h, it needs to be convert to m/s.
 099            speedBackward /= 3.6;
 0100        }
 101
 3102        var canstop = resultTable.GetBoolean("canstop") ?? (backward > 0 || forward > 0);
 103
 3104        return new EdgeFactor(
 3105            (uint)(forward * 100),
 3106            (uint)(backward * 100),
 3107            (ushort)(speedForward * 100),
 3108            (ushort)(speedBackward * 100),
 3109            canstop
 3110        );
 3111    }
 112
 113    /// <inheritdoc />
 114    public override TurnCostFactor TurnCostFactor(IEnumerable<(string key, string value)> attributes)
 2115    {
 2116        if (!_hasTurnFactor || !attributes.Any())
 1117        {
 1118            return Profiles.TurnCostFactor.Empty;
 119        }
 120
 1121        var attributesTable = new LuaTable();
 1122        var resultTable = new LuaTable();
 7123        foreach (var (k, v) in attributes)
 2124        {
 2125            attributesTable[k] = v;
 2126        }
 127
 1128        _env.turn_cost_factor(attributesTable, resultTable);
 129
 1130        var factor = resultTable.GetDouble("factor") ?? 0;
 131
 1132        var turnCostFactor = Profiles.TurnCostFactor.Empty;
 1133        if (factor < 0)
 1134        {
 1135            turnCostFactor = Profiles.TurnCostFactor.Binary;
 1136        }
 0137        else if (factor > 0)
 0138        {
 0139            turnCostFactor = new TurnCostFactor((uint)(factor * 10));
 0140        }
 1141        return turnCostFactor;
 2142    }
 143}