From 802b0690b7f675486aef9df2d5d2be9db9912969 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Thu, 28 May 2020 19:00:15 +0200 Subject: [PATCH] Add output of all tags --- AspectedRouting/Language/Analysis.cs | 32 ++++++++++++++++++- .../Language/Expression/ProfileMetaData.cs | 29 ++++++++++++++++- .../Language/Functions/FirstMatchOf.cs | 3 +- AspectedRouting/Program.cs | 17 ++++++++++ 4 files changed, 78 insertions(+), 3 deletions(-) diff --git a/AspectedRouting/Language/Analysis.cs b/AspectedRouting/Language/Analysis.cs index e91ec64..73c9781 100644 --- a/AspectedRouting/Language/Analysis.cs +++ b/AspectedRouting/Language/Analysis.cs @@ -398,6 +398,36 @@ namespace AspectedRouting.Language }); } + + public static Dictionary> PossibleTags(this IEnumerable exprs) + { + var usedTags = new Dictionary>(); + foreach (var expr in exprs) + { + + var possible = expr.PossibleTags(); + if (possible == null) + { + continue; + } + foreach (var (key, values) in possible) + { + if (!usedTags.TryGetValue(key, out var collection)) + { + collection = new HashSet(); + usedTags[key] = collection; + } + + foreach (var v in values) + { + collection.Add(v); + } + } + } + + return usedTags; + } + /// /// Returns which tags are used in this calculation /// @@ -406,7 +436,6 @@ namespace AspectedRouting.Language /// A dictionary containing all possible values. An entry with an empty list indicates a wildcard public static Dictionary> PossibleTags(this IExpression e) { - var result = new Dictionary>(); var mappings = new List(); e.Visit(x => { @@ -437,6 +466,7 @@ namespace AspectedRouting.Language // Visit will have the main mapping at the first position var rootMapping = mappings[0]; + var result = new Dictionary>(); foreach (var (key, expr) in rootMapping.StringToResultFunctions) { diff --git a/AspectedRouting/Language/Expression/ProfileMetaData.cs b/AspectedRouting/Language/Expression/ProfileMetaData.cs index a8daade..5fc74b4 100644 --- a/AspectedRouting/Language/Expression/ProfileMetaData.cs +++ b/AspectedRouting/Language/Expression/ProfileMetaData.cs @@ -44,6 +44,32 @@ namespace AspectedRouting.Language.Expression Behaviours = behaviours; } + public List AllExpressions(Context ctx) + { + var l = new List {Access, Oneway, Speed}; + l.AddRange(DefaultParameters.Values); + l.AddRange(Behaviours.Values.SelectMany(b => b.Values)); + l.AddRange(Priority.Values); + + + var allExpr = new List(); + allExpr.AddRange(l); + foreach (var e in l) + { + e.Visit(expression => + { + if (expression is FunctionCall fc) + { + var called = ctx.GetFunction(fc.CalledFunctionName); + allExpr.Add(called); + } + return true; + }); + } + + return allExpr; + } + public ProfileResult Run(Context c, string behaviour, Dictionary tags) { @@ -52,6 +78,7 @@ namespace AspectedRouting.Language.Expression throw new ArgumentException( $"Profile {Name} does not contain the behaviour {behaviour}\nTry one of {string.Join(",", Behaviours.Keys)}"); } + var parameters = new Dictionary(); foreach (var (k, v) in DefaultParameters) @@ -65,7 +92,7 @@ namespace AspectedRouting.Language.Expression } c = c.WithParameters(parameters); - + tags = new Dictionary(tags); var canAccess = Access.Run(c, tags); tags["access"] = "" + canAccess; diff --git a/AspectedRouting/Language/Functions/FirstMatchOf.cs b/AspectedRouting/Language/Functions/FirstMatchOf.cs index 0fb306b..38e5df5 100644 --- a/AspectedRouting/Language/Functions/FirstMatchOf.cs +++ b/AspectedRouting/Language/Functions/FirstMatchOf.cs @@ -8,7 +8,8 @@ namespace AspectedRouting.Language.Functions { public class FirstMatchOf : Function { - public override string Description { get; } = "Parses a string into a numerical value"; + public override string Description { get; } = "This higherorder function takes a list of keys, a mapping (function over tags) and a collection of tags. It will try the function for the first key (and it's respective value). If the function fails (it gives null), it'll try the next key.\n\n" + + "E.g. `$firstMatchOf ['maxspeed','highway'] {'maxspeed' --> $parse, 'highway' --> {residential --> 30, tertiary --> 50}}` applied on `{maxspeed=70, highway=tertiary}` will yield `70` as that is the first key in the list; `{highway=residential}` will yield `30`."; public override List ArgNames { get; } = new List {"s"}; public FirstMatchOf() : base("firstMatchOf", true, diff --git a/AspectedRouting/Program.cs b/AspectedRouting/Program.cs index 0e2ac53..2c726ef 100644 --- a/AspectedRouting/Program.cs +++ b/AspectedRouting/Program.cs @@ -254,7 +254,24 @@ namespace AspectedRouting test.Run(context); } + + Console.WriteLine("\n\n\n---------- " + profile.Name + " --------------"); + foreach (var (key, values) in profile.AllExpressions(context).PossibleTags()) + { + var vs = "*"; + if (values.Any()) + { + vs = string.Join(", ", values); + } + + Console.WriteLine(key + ": " + vs); + } + + Console.WriteLine("\n\n\n------------------------"); + var luaPrinter = GenerateLua(context, aspects, profile, profileTests); + + File.WriteAllText(outputDir + "/" + profile.Name + ".lua", luaPrinter.ToLua()); }