From 8e55bd9840160bca9ca518d6368ac2e3a8d9ace5 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Tue, 26 Jan 2021 23:16:40 +0100 Subject: [PATCH] Fix output for itinero 2.0 --- AspectedRouting.sln.DotSettings.user | 4 +- .../IO/LuaSkeleton/LuaTestPrinter.cs | 17 ++++--- .../IO/itinero2/LuaPrinter2.MainFunction.cs | 44 ++++++++++++------- AspectedRouting/IO/itinero2/LuaPrinter2.cs | 10 +++-- 4 files changed, 48 insertions(+), 27 deletions(-) diff --git a/AspectedRouting.sln.DotSettings.user b/AspectedRouting.sln.DotSettings.user index 74e9617..130e071 100644 --- a/AspectedRouting.sln.DotSettings.user +++ b/AspectedRouting.sln.DotSettings.user @@ -1,7 +1,5 @@  - <SessionState ContinuousTestingMode="0" Name="ParseFunction_Duration_TotalMinutes" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> - <Project Location="/home/pietervdvn/werk/AspectedRouting/AspectedRouting.Test" Presentation="&lt;AspectedRouting.Test&gt;" /> -</SessionState> + <SessionState ContinuousTestingMode="0" IsActive="True" Name="Integration_TestExamples" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> <TestAncestor> <TestId>xUnit::A1309041-8AAE-42D7-A886-94C9FFC6A28C::.NETCoreApp,Version=v3.1::AspectedRouting.Test.ExamplesTest.Integration_TestExamples</TestId> diff --git a/AspectedRouting/IO/LuaSkeleton/LuaTestPrinter.cs b/AspectedRouting/IO/LuaSkeleton/LuaTestPrinter.cs index fe5b283..e8b3a48 100644 --- a/AspectedRouting/IO/LuaSkeleton/LuaTestPrinter.cs +++ b/AspectedRouting/IO/LuaSkeleton/LuaTestPrinter.cs @@ -15,7 +15,7 @@ namespace AspectedRouting.IO.itinero1 } - public string GenerateFullTestSuite(List profileTests, List aspectTests) + public string GenerateFullTestSuite(List profileTests, List aspectTests, bool invertPriority = false) { _skeleton.AddDep("inv"); @@ -33,7 +33,7 @@ namespace AspectedRouting.IO.itinero1 string.Join("\n\n", profileTests .Where(x => x != null) .Select( - GenerateProfileTestSuite + t => GenerateProfileTestSuite(t, invertPriority) )); return string.Join("\n\n\n", new List @@ -47,15 +47,15 @@ namespace AspectedRouting.IO.itinero1 } - private string GenerateProfileTestSuite(BehaviourTestSuite testSuite) + private string GenerateProfileTestSuite(BehaviourTestSuite testSuite, bool invertPriority) { return string.Join("\n", - testSuite.Tests.Select((test, i) => GenerateProfileUnitTestCall(testSuite, i, test.Item1, test.tags)) + testSuite.Tests.Select((test, i) => GenerateProfileUnitTestCall(testSuite, i, test.Item1, test.tags, invertPriority)) .ToList()); } private string GenerateProfileUnitTestCall(BehaviourTestSuite testSuite, int index, ProfileResult expected, - Dictionary tags) + Dictionary tags, bool invertPriority) { _skeleton.AddDep("debug_table"); var parameters = new Dictionary(); @@ -88,13 +88,18 @@ namespace AspectedRouting.IO.itinero1 tags.Remove("#" + paramName); } + var expectedPriority = "" + expected.Priority; + if (invertPriority) { + expectedPriority = $"inv({expectedPriority})"; + } + // Generates something like: // function unit_test_profile(profile_function, profile_name, index, expected, tags) return $"unit_test_profile(behaviour_{testSuite.Profile.Name.AsLuaIdentifier()}_{testSuite.BehaviourName.AsLuaIdentifier()}, " + $"\"{testSuite.BehaviourName}\", " + $"{index}, " + - $"{{access = \"{D(expected.Access)}\", speed = {expected.Speed}, oneway = \"{D(expected.Oneway)}\", priority = {expected.Priority} }}, " + + $"{{access = \"{D(expected.Access)}\", speed = {expected.Speed}, oneway = \"{D(expected.Oneway)}\", priority = {expectedPriority} }}, " + tags.ToLuaTable() + ")"; } diff --git a/AspectedRouting/IO/itinero2/LuaPrinter2.MainFunction.cs b/AspectedRouting/IO/itinero2/LuaPrinter2.MainFunction.cs index 92202d4..6d46a1b 100644 --- a/AspectedRouting/IO/itinero2/LuaPrinter2.MainFunction.cs +++ b/AspectedRouting/IO/itinero2/LuaPrinter2.MainFunction.cs @@ -26,8 +26,8 @@ namespace AspectedRouting.IO.itinero2 foreach (var (paramName, expr) in _profile.Priority) { - var paramExpr = parameters[paramName].Evaluate(_context); - if (!(paramExpr is double weight)) + var weightExpr = parameters[paramName].Evaluate(_context); + if (!(weightExpr is double weight)) { continue; } @@ -37,11 +37,20 @@ namespace AspectedRouting.IO.itinero2 continue; } - var expression = _profile.Priority[paramName]; - var exprInLua = _skeleton.ToLua(expression); - var subs = new Curry(Typs.Tags, new Var(("a"))).UnificationTable(expression.Types.First()); - if (subs != null && subs.TryGetValue("$a", out var resultType) && - (resultType.Equals(Typs.Bool) || resultType.Equals(Typs.String))) + // The expression might still have multiple typings, + // which take inputs different from 'Tags', so we specialize the expr first + var exprSpecialized = expr; + var resultType = expr.Types.First(); + if (exprSpecialized.Types.Count() >=2) { + exprSpecialized = expr.Specialize(new Curry(Typs.Tags, new Var("a"))); + if (exprSpecialized == null) { + throw new Exception("Could not specialize expression to type tags -> $a"); + } + resultType = (exprSpecialized.Types.First() as Curry).ResultType; + } + + var exprInLua = _skeleton.ToLua(exprSpecialized); + if (resultType.Equals(Typs.Bool) || resultType.Equals(Typs.String)) { _skeleton.AddDep("parse"); exprInLua = "parse(" + exprInLua + ")"; @@ -78,7 +87,7 @@ namespace AspectedRouting.IO.itinero2 var code = new List { "--[[", - _profile.Behaviours[_behaviourName]["description"].Evaluate(_context).ToString(), + "Calculate the actual factor.forward and factor.backward for a segment with the given properties", "]]", "function factor(tags, result)", " ", @@ -97,15 +106,20 @@ namespace AspectedRouting.IO.itinero2 "", " local oneway = " + _skeleton.ToLua(_profile.Oneway), " tags.oneway = oneway", - " -- forward calculation", + " -- An aspect describing oneway should give either 'both', 'against' or 'width'", + + "", + "", + " -- forward calculation. We set the meta tag '_direction' to 'width' to indicate that we are going forward. The other functions will pick this up", " tags[\"_direction\"] = \"with\"", " local access_forward = " + _skeleton.ToLua(_profile.Access), " if(oneway == \"against\") then", + " -- no 'oneway=both' or 'oneway=with', so we can only go back over this segment", + " -- we overwrite the 'access_forward'-value with no; whatever it was...", " access_forward = \"no\"", " end", - " if(access_forward ~= nil and access_forward ~= \"no\") then", - " tags.access = access_forward", + " tags.access = access_forward -- might be relevant, e.g. for 'access=dismount' for bicycles", " result.forward_speed = " + _skeleton.ToLua(_profile.Speed).Indent(), " tags.speed = result.forward_speed", " local priority = calculate_priority(parameters, tags, result, access_forward, oneway, result.forward_speed)", @@ -117,15 +131,15 @@ namespace AspectedRouting.IO.itinero2 " end", "", " -- backward calculation", - " tags[\"_direction\"] = \"against\"", + " tags[\"_direction\"] = \"against\" -- indicate the backward direction to priority calculation", " local access_backward = " + _skeleton.ToLua(_profile.Access), - "", " if(oneway == \"with\") then", + " -- no 'oneway=both' or 'oneway=against', so we can only go forward over this segment", + " -- we overwrite the 'access_forward'-value with no; whatever it was...", " access_backward = \"no\"", " end", - "", " if(access_backward ~= nil and access_backward ~= \"no\") then", - " tags.access = access_backward" + + " tags.access = access_backward", " result.backward_speed = " + _skeleton.ToLua(_profile.Speed).Indent(), " tags.speed = result.backward_speed", " local priority = calculate_priority(parameters, tags, result, access_backward, oneway, result.backward_speed)", diff --git a/AspectedRouting/IO/itinero2/LuaPrinter2.cs b/AspectedRouting/IO/itinero2/LuaPrinter2.cs index 8256833..0bec370 100644 --- a/AspectedRouting/IO/itinero2/LuaPrinter2.cs +++ b/AspectedRouting/IO/itinero2/LuaPrinter2.cs @@ -55,16 +55,20 @@ namespace AspectedRouting.IO.itinero2 public string ToLua() { + var profileDescr = _profile.Behaviours[_behaviourName]["description"].Evaluate(_context).ToString(); var header = new List { $"name = \"{_profile.Name}.{_behaviourName}\"", $"generationDate = \"{_lastChangeTime:s}\"", - $"description = \"{_profile.Description}\"" + $"description = \"{profileDescr} ({_profile.Description})\"" }; - var tests = new LuaTestPrinter(_skeleton, new List() {"unitTestProfile2"}).GenerateFullTestSuite( - _behaviourTestSuite.ToList(), new List()); + var tests = new LuaTestPrinter(_skeleton, + new List() {"unitTestProfile2"}).GenerateFullTestSuite( + _behaviourTestSuite.ToList(), + new List(), + true); var all = new List { header.Lined(),