< Summary

Information
Class: Intentum.Simulation.BehaviorSpaceSimulator
Assembly: Intentum.Simulation
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Simulation/BehaviorSpaceSimulator.cs
Line coverage
91%
Covered lines: 34
Uncovered lines: 3
Coverable lines: 37
Total lines: 65
Line coverage: 91.8%
Branch coverage
65%
Covered branches: 13
Total branches: 20
Branch coverage: 65%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
FromSequence(...)75%44100%
GenerateRandom(...)64.28%141486.36%
Hash(...)50%22100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Simulation/BehaviorSpaceSimulator.cs

#LineLine coverage
 1using Intentum.Core.Behavior;
 2
 3namespace Intentum.Simulation;
 4
 5/// <summary>
 6/// Default implementation of behavior space simulation.
 7/// </summary>
 8public sealed class BehaviorSpaceSimulator : IBehaviorSpaceSimulator
 9{
 10    /// <inheritdoc />
 11    public BehaviorSpace FromSequence(
 12        IReadOnlyList<(string Actor, string Action)> sequence,
 13        DateTimeOffset? baseTime = null)
 314    {
 315        var space = new BehaviorSpace();
 316        var time = baseTime ?? DateTimeOffset.UtcNow;
 1717        foreach (var (actor, action) in sequence)
 418        {
 419            space.Observe(new BehaviorEvent(actor, action, time));
 420            time = time.AddSeconds(1);
 421        }
 322        return space;
 323    }
 24
 25    /// <inheritdoc />
 26    public BehaviorSpace GenerateRandom(
 27        IReadOnlyList<string> actors,
 28        IReadOnlyList<string> actions,
 29        int eventCount,
 30        int? randomSeed = null)
 431    {
 432        if (actors is null || actors.Count == 0)
 033            throw new ArgumentException("At least one actor required.", nameof(actors));
 434        if (actions is null || actions.Count == 0)
 035            throw new ArgumentException("At least one action required.", nameof(actions));
 436        if (eventCount < 0)
 037            throw new ArgumentOutOfRangeException(nameof(eventCount), "Event count must be non-negative.");
 38
 439        var seed = randomSeed ?? Environment.TickCount;
 440        var space = new BehaviorSpace();
 441        var time = DateTimeOffset.UtcNow;
 442        var actorCount = actors.Count;
 443        var actionCount = actions.Count;
 44
 3445        for (var i = 0; i < eventCount; i++)
 1346        {
 1347            var actorIndex = Math.Abs(Hash(seed, i, 0)) % actorCount;
 1348            var actionIndex = Math.Abs(Hash(seed, i, 1)) % actionCount;
 1349            var timeDelta = 1 + (Math.Abs(Hash(seed, i, 2)) % 59);
 1350            space.Observe(new BehaviorEvent(actors[actorIndex], actions[actionIndex], time));
 1351            time = time.AddSeconds(timeDelta);
 1352        }
 53
 454        return space;
 455    }
 56
 57    private static int Hash(int seed, int index, int salt)
 3958    {
 59        unchecked
 3960        {
 3961            var h = (seed * 31 + index) * 31 + salt;
 3962            return h == int.MinValue ? 0 : h;
 63        }
 3964    }
 65}