Fix output for itinero 2.0

This commit is contained in:
Pieter Vander Vennet 2021-01-26 23:16:40 +01:00
parent 60c3bec1e0
commit 8e55bd9840
4 changed files with 48 additions and 27 deletions

View file

@ -1,7 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=46c4a96e_002D61f1_002D4d55_002Da7ff_002D649a683870dc/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="ParseFunction_Duration_TotalMinutes" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Project Location="/home/pietervdvn/werk/AspectedRouting/AspectedRouting.Test" Presentation="&amp;lt;AspectedRouting.Test&amp;gt;" /&gt;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=a6a74f48_002D8456_002D43c7_002Dbbee_002Dd3da33a8a4be/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Integration_TestExamples" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;xUnit::A1309041-8AAE-42D7-A886-94C9FFC6A28C::.NETCoreApp,Version=v3.1::AspectedRouting.Test.ExamplesTest.Integration_TestExamples&lt;/TestId&gt;

View file

@ -15,7 +15,7 @@ namespace AspectedRouting.IO.itinero1
}
public string GenerateFullTestSuite(List<BehaviourTestSuite> profileTests, List<AspectTestSuite> aspectTests)
public string GenerateFullTestSuite(List<BehaviourTestSuite> profileTests, List<AspectTestSuite> 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<string>
@ -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<string, string> tags)
Dictionary<string, string> tags, bool invertPriority)
{
_skeleton.AddDep("debug_table");
var parameters = new Dictionary<string, string>();
@ -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() +
")";
}

View file

@ -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<string>
{
"--[[",
_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)",

View file

@ -55,16 +55,20 @@ namespace AspectedRouting.IO.itinero2
public string ToLua()
{
var profileDescr = _profile.Behaviours[_behaviourName]["description"].Evaluate(_context).ToString();
var header =
new List<string>
{
$"name = \"{_profile.Name}.{_behaviourName}\"",
$"generationDate = \"{_lastChangeTime:s}\"",
$"description = \"{_profile.Description}\""
$"description = \"{profileDescr} ({_profile.Description})\""
};
var tests = new LuaTestPrinter(_skeleton, new List<string>() {"unitTestProfile2"}).GenerateFullTestSuite(
_behaviourTestSuite.ToList(), new List<AspectTestSuite>());
var tests = new LuaTestPrinter(_skeleton,
new List<string>() {"unitTestProfile2"}).GenerateFullTestSuite(
_behaviourTestSuite.ToList(),
new List<AspectTestSuite>(),
true);
var all = new List<string>
{
header.Lined(),