< Summary

Class:Itinero.Instructions.Generators.LinearInstructionListGenerator
Assembly:Itinero.Instructions
File(s):/home/runner/work/routing2/routing2/src/Itinero.Instructions/Generators/LinearInstructionListGenerator.cs
Covered lines:32
Uncovered lines:1
Coverable lines:33
Total lines:72
Line coverage:96.9% (32 of 33)
Covered branches:7
Total branches:8
Branch coverage:87.5% (7 of 8)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)100%1100%
GenerateInstructions(...)100%4100%
ConstructNext(...)75%490%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.Instructions/Generators/LinearInstructionListGenerator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using Itinero.Instructions.Types;
 4using Itinero.Routes;
 5
 6namespace Itinero.Instructions.Generators;
 7
 8/// <summary>
 9/// Constructs instructions using the instruction constructors and adds an end-instruction.
 10/// </summary>
 11/// <remarks>
 12/// Given a list of instruction constructors, this will construct a route in the following way:
 13///  - Try to generate a base instruction with the first instruction in the list. If this fails, try the next one
 14///  - Check the shape-index of the generated instruction and move the index of the route forward
 15///  - Try to generate a new instruction as described above
 16///
 17///  This means that the list of 'baseInstructionConstructors' should go from "very specialized" to "very generic", e.g.
 18///  the roundabout-instruction-constructor should be at the first position as that instruction will only trigger in spe
 19///  whereas the 'follow the road/go left/go right' instruction will always trigger but is not very informative.
 20///
 21/// An 'end'-instruction will always an automatically be appended. Adding the 'endInstructionGenerator' via the construc
 22/// _two_ end instructions
 23/// </remarks>
 24public class LinearInstructionListGenerator : IInstructionListGenerator
 25{
 26    private readonly IReadOnlyList<Types.Generators.IInstructionGenerator> _constructors;
 27
 828    internal LinearInstructionListGenerator(IReadOnlyList<Types.Generators.IInstructionGenerator> constructors)
 829    {
 830        _constructors = constructors;
 831    }
 32
 33    /// <inhertdoc/>
 34    public IReadOnlyList<BaseInstruction> GenerateInstructions(Route route)
 835    {
 836        var indexedRoute = new IndexedRoute(route);
 837        var instructions = new List<BaseInstruction>();
 38
 839        var currentIndex = 0;
 3540        while (currentIndex < indexedRoute.Last)
 2741        {
 2742            var instruction = this.ConstructNext(indexedRoute, currentIndex);
 2743            instructions.Add(instruction);
 2744            if (instruction.ShapeIndexEnd == currentIndex)
 745            {
 746                currentIndex++;
 747            }
 48            else
 2049            {
 2050                currentIndex = instruction.ShapeIndexEnd;
 2051            }
 2752        }
 53
 854        instructions.Add(new EndInstruction(indexedRoute));
 55
 856        return instructions;
 857    }
 58
 59    private BaseInstruction ConstructNext(IndexedRoute r, int currentOffset)
 2760    {
 16261        foreach (var constructor in _constructors)
 5462        {
 5463            var instruction = constructor.Generate(r, currentOffset);
 5464            if (instruction != null)
 2765            {
 2766                return instruction;
 67            }
 2768        }
 69
 070        throw new Exception("Could not generate instruction for offset " + currentOffset);
 2771    }
 72}