Typesystem and profile updates

This commit is contained in:
Pieter Vander Vennet 2020-06-16 17:37:32 +02:00
parent 537fb2c613
commit 173756818e
17 changed files with 189 additions and 69 deletions

View file

@ -322,9 +322,19 @@ namespace AspectedRouting.Test
new Curry(Typs.String, Typs.String));
var f1 = f.Specialize(strstrstr);
Assert.Equal(new[] {strstrb, strstrstr}, f1.Types);
Assert.Equal(new[] {strstrb, strstrstr}, f1.Types);
}
[Fact]
public void SpecializeToCommonType()
{
var p0 = Funcs.Parse;
var p1 = Funcs.Const.Apply(new Constant(1.0));
var exprs = new[] {p0, p1};
var newTypes = exprs.SpecializeToCommonTypes(out var specializedExpressions);
Assert.Single(newTypes);
}
}
}

View file

@ -1,13 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using AspectedRouting.Language;
using AspectedRouting.Language.Functions;
using Xunit;
namespace AspectedRouting.Test
{
public class TestAnalysis
{
}
}

View file

@ -0,0 +1,9 @@
<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/=af5d2251_002D713d_002D473f_002D9157_002D89c9d06216e5/@EntryIndexedValue">&lt;SessionState ContinuousTestingIsOn="False" ContinuousTestingMode="0" FrameworkVersion="{x:Null}" IsLocked="False" Name="All tests from &amp;lt;AspectedRouting.Test&amp;gt;" PlatformMonoPreference="{x:Null}" PlatformType="{x:Null}" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&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/=bad1d05d_002Df895_002D4e7d_002Da6e2_002Dede32f077b26/@EntryIndexedValue">&lt;SessionState ContinuousTestingIsOn="False" ContinuousTestingMode="0" FrameworkVersion="{x:Null}" IsLocked="False" Name="SpecializeToCommonType" PlatformMonoPreference="{x:Null}" PlatformType="{x:Null}" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
&lt;TestAncestor&gt;
&lt;TestId&gt;xUnit::A1309041-8AAE-42D7-A886-94C9FFC6A28C::.NETCoreApp,Version=v3.1::AspectedRouting.Test.FunctionsTest.SpecializeToCommonType&lt;/TestId&gt;
&lt;/TestAncestor&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>

View file

@ -27,7 +27,7 @@ namespace AspectedRouting.IO.itinero1
"end"
};
return string.Join("\n",impl);
return string.Join("\n", impl);
}
/// <summary>
@ -56,7 +56,11 @@ namespace AspectedRouting.IO.itinero1
continue;
}
impl += $" parameters.{paramName.TrimStart('#').AsLuaIdentifier()} = {_skeleton.ToLua(value)}\n";
var paramNameTrimmed = paramName.TrimStart('#').AsLuaIdentifier();
if (!string.IsNullOrEmpty(paramNameTrimmed))
{
impl += $" parameters.{paramNameTrimmed} = {_skeleton.ToLua(value)}\n";
}
}
return impl;

View file

@ -1,4 +1,7 @@
function as_number(a)
function atleast(a, b)
if(a <= b) then
return "yes"
end
return "no"
end

View file

View file

@ -30,7 +30,13 @@ function unit_test_profile(profile_function, profile_name, index, expected, tags
end
local actualOneway = result.direction;
local actualOneway = result.direction
if(actualOneway == nil) then
print("Fail: result.direction is nil")
profile_failed = true;
end
if (result.direction == 0) then
actualOneway = "both"
elseif (result.direction == 1) then
@ -40,7 +46,7 @@ function unit_test_profile(profile_function, profile_name, index, expected, tags
end
if (expected.oneway ~= actualOneway) then
print("Test " .. tostring(index) .. " failed for " .. profile_name .. ".oneway: expected " .. expected.direction .. " but got " .. actualOneway)
print("Test " .. tostring(index) .. " failed for " .. profile_name .. ".oneway: expected " .. expected.oneway .. " but got " .. actualOneway)
failed_profile_tests = true
profile_failed = true
end

View file

@ -237,7 +237,7 @@ namespace AspectedRouting.Language
var unused = defaultParameters.Except(usedParameters);
if (unused.Any())
{
throw new ArgumentException("A default value is set for parameter, but it is unused: " +
Console.WriteLine("[WARNING] A default value is set for parameter, but it is unused: " +
string.Join(", ", unused));
}

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AspectedRouting.Language.Expression;
@ -32,6 +33,7 @@ namespace AspectedRouting.Language
public static readonly Function Multiply = new Multiply();
public static readonly Function AtLeast = new AtLeast();
public static readonly Function FirstOf = new FirstMatchOf();
public static readonly Function MustMatch = new MustMatch();
@ -79,13 +81,35 @@ namespace AspectedRouting.Language
return null;
}
e = e.SpecializeToSmallestType();
if (e.Types.Count() == 0)
{
throw new Exception("Expression has no valid types:\n" + e);
}
var eSmallest = e.SpecializeToSmallestType();
if (eSmallest == null || eSmallest.Types.Count() == 0)
{
throw new Exception("Could not specialize " + e);
}
// TODO FIX THIS so that it works
// An argument 'optimizes' it's types from 'string -> bool' to 'string -> string'
e = e.Optimize();
//
e.SanityCheck();
return e;
try
{
var eOpt = eSmallest.Optimize(); if (eOpt == null || eOpt.Types.Count() == 0)
{
throw new Exception("Could not optimize " + eSmallest);
}
eOpt.SanityCheck();
return eOpt;
}
catch
{
Console.WriteLine("Optimalization failed for "+eSmallest);
return eSmallest;
}
}
public static IExpression SpecializeToSmallestType(this IExpression e)

View file

@ -0,0 +1,84 @@
using System;
using System.Collections.Generic;
using AspectedRouting.Language.Expression;
using AspectedRouting.Language.Typ;
using Type = AspectedRouting.Language.Typ.Type;
namespace AspectedRouting.Language.Functions
{
public class AtLeast : Function
{
public override string Description { get; } =
"Returns 'yes' if the second argument is bigger then the first argument. (Works great in combination with $dot)";
public override List<string> ArgNames { get; } = new List<string> {"minimum", "f", "then","else"};
private static Type a = new Var("a");
private static Type b = new Var("b");
public AtLeast() : base("atleast", true,
new[]
{
Curry.ConstructFrom(a,
Typs.Double,
new Curry(b,Typs.Double),
a, a, b),
})
{
}
private AtLeast(IEnumerable<Type> types) : base("atleast", types)
{
}
public override IExpression Specialize(IEnumerable<Type> allowedTypes)
{
var unified = Types.SpecializeTo(allowedTypes);
if (unified == null)
{
return null;
}
return new AtLeast(unified);
}
public override object Evaluate(Context c, params IExpression[] arguments)
{
var minimum = arguments[0].Evaluate(c);
var f = (IExpression) arguments[1].Evaluate(c);
var then = arguments[2].Evaluate(c);
var @else = arguments[3].Evaluate(c);
var x = arguments[4];
var arg1 = f.Evaluate(c, x);
if (minimum == null || arg1 == null)
{
return null;
}
if (minimum is int i0)
{
minimum = (double) i0;
}
if (arg1 is int i1)
{
arg1 = (double) i1;
}
if (minimum is double d0 && arg1 is double d1)
{
if (d0 <= d1)
{
return then;
}
return @else;
}
throw new InvalidCastException("One of the arguments is not a number: "+minimum+", "+arg1);
}
}
}

View file

@ -39,8 +39,8 @@
"pedestrian": "dismount",
"corridor": "dismount",
"path": "yes",
"primary": "no",
"primary_link": "no",
"primary": "yes",
"primary_link": "yes",
"secondary": "yes",
"secondary_link": "yes",
"tertiary": "yes",

View file

@ -9,7 +9,7 @@
"#": "access=no and access 'destination' implies this access for cars too and is a bonus!",
"no": 1.5,
"destination": 1.4,
"dismount": 0.01,
"dismount": 0.2,
"designated": 1.5
},
"motor_vehicle": {
@ -33,7 +33,7 @@
},
"highway": {
"cycleway": 1.0,
"primary": 0.3,
"primary": 0.1,
"secondary": 0.4,
"tertiary": 0.5,
"unclassified": 0.8,
@ -44,31 +44,28 @@
"path": 0.9
},
"cycleway": {
"yes": 0.95,
"#": "TODO: the implied value for quite a bit of roads (residential, unclassified) is 'no', so this is a bit weird",
"no": 0.5,
"lane": 1,
"shared": 0.8,
"shared_lane": 0.8,
"share_busway": 0.9,
"#": "A distinct cycleway _always_ makes the road safer for cyclists, even if it is but a small lane. The default assumption is 'no', no cycleway, in which case the safety feeling is not improved",
"yes": 1.15,
"lane": 1.15,
"shared": 1.03,
"shared_lane": 1.03,
"share_busway": 1.05,
"track": 1.5
},
"cycleway:left": {
"yes": 0.95,
"no": 0.5,
"lane": 1,
"shared": 0.8,
"shared_lane": 0.8,
"share_busway": 0.9,
"yes": 1.15,
"lane": 1.15,
"shared": 1.03,
"shared_lane": 1.03,
"share_busway": 1.05,
"track": 1.5
},
"cycleway:right": {
"yes": 0.95,
"no": 0.5,
"lane": 1,
"shared": 0.8,
"shared_lane": 0.8,
"share_busway": 0.9,
"yes": 1.15,
"lane": 1.15,
"shared": 1.03,
"shared_lane": 1.03,
"share_busway": 1.05,
"track": 1.5
}
}

View file

@ -15,15 +15,7 @@
"status",
"network"
],
"access": {
"$ifDotted": {
"$dot": {"$notEq": "no"},
"f": "$bicycle.legal_access"
},
"then": "$bicycle.legal_access",
"else": "$bicycle.network_is_bicycle_network"
},
"access": "$bicycle.legal_access",
"oneway": "$bicycle.oneway",
"speed": {
"$min": [
@ -46,7 +38,8 @@
"#bicycleNetworkScore": "$bicycle.network_is_bicycle_network",
"#timeNeeded": "$speed",
"#distance": "$distance",
"#trespassingPenalty":"$clean_permission_score"
"#trespassingPenalty": "$clean_permission_score",
"#leastSafetyPenalty": 0
},
"defaults": {
"#defaultSpeed": 15,
@ -60,7 +53,10 @@
"#cycleHighwayNetworkScore": 0,
"#nodeNetworkScore": 0,
"#bicycleNetworkScore": 0,
"#trespassingPenalty": 15
"#trespassingPenalty": 15,
"#": "This is not a priority weight, but rather a kind of access restriction. If 'leastafety' is not met, a huge penalty is applied, namely #leastSafetyPenalty. Note: 0.1 is the safety level of a primary without cycle highway",
"#leastSafetyRequired": 0.11,
"#leastSafetyPenalty": 2
},
"behaviours": {
"fastest": {

View file

@ -1,7 +1,7 @@
access,oneway,speed,priority,highway,bicycle,surface,cycleway:left,oneway,oneway:bicycle,access,maxspeed,junction
no,both,0,0,,,,,,,,,
designated,both,15,15,cycleway,,,,,,,,
no,both,0,0,primary,,,,,,,,
yes,both,15,15,primary,,,,,,,,
yes,both,15,15,primary,yes,,,,,,,
yes,both,15,15,residential,,,,,,,,
yes,both,13.5,13.5,residential,yes,sett,,,,,,

1 access oneway speed priority highway bicycle surface cycleway:left oneway oneway:bicycle access maxspeed junction
2 no both 0 0
3 designated both 15 15 cycleway
4 no yes both 0 15 0 15 primary
5 yes both 15 15 primary yes
6 yes both 15 15 residential
7 yes both 13.5 13.5 residential yes sett

View file

@ -12,7 +12,7 @@ designated,residential,designated,,,,
designated,motorway,designated,,,,
no,residential,use_sidepath,,,,
yes,residential,,no,yes,,
no,primary,,,,,
yes,primary,,,,,
yes,primary,,,,yes,
yes,primary,,,,,yes
yes,secondary,,,,track,

1 expected highway bicycle access anyways:access cycleway:right cycleway:right
12 designated motorway designated
13 no residential use_sidepath
14 yes residential no yes
15 no yes primary
16 yes primary yes
17 yes primary yes
18 yes secondary track

View file

@ -1,8 +1,8 @@
expected,highway,cycleway,cyclestreet,foot,bicycle,cycleway:right,access
0.15,primary,no,,,,,
0.285,primary,yes,,,,,
0.45,primary,track,,,,,
0.4,secondary,lane,,,,,
0.1,primary,no,,,,,
0.115,primary,yes,,,,,
0.15,primary,track,,,,,
0.46,secondary,lane,,,,,
0.9,residential,,,,,,
1.5,cycleway,,,,,,designated
1.35,residential,,yes,,,,
@ -10,4 +10,4 @@ expected,highway,cycleway,cyclestreet,foot,bicycle,cycleway:right,access
0.9025,footway,,,designated,,,
1.2825,path,,,designated,designated,,
1.35,path,,,,designated,,
0.4,secondary,,,,,lane,
0.46,secondary,,,,,lane,

1 expected highway cycleway cyclestreet foot bicycle cycleway:right access
2 0.15 0.1 primary no
3 0.285 0.115 primary yes
4 0.45 0.15 primary track
5 0.4 0.46 secondary lane
6 0.9 residential
7 1.5 cycleway designated
8 1.35 residential yes
10 0.9025 footway designated
11 1.2825 path designated designated
12 1.35 path designated
13 0.4 0.46 secondary lane

View file

@ -2,4 +2,4 @@ access,oneway,speed,priority,highway,surface
no,both,0,0,,
designated,both,15,1,cycleway,
yes,both,5.25,1,path,ground
no,both,15,0,primary,
yes,both,15,1,primary,

1 access oneway speed priority highway surface
2 no both 0 0
3 designated both 15 1 cycleway
4 yes both 5.25 1 path ground
5 no yes both 15 0 1 primary