Add output of all tags

This commit is contained in:
Pieter Vander Vennet 2020-05-28 19:00:15 +02:00
parent ab03f6f359
commit 802b0690b7
4 changed files with 78 additions and 3 deletions

View file

@ -398,6 +398,36 @@ namespace AspectedRouting.Language
});
}
public static Dictionary<string, HashSet<string>> PossibleTags(this IEnumerable<IExpression> exprs)
{
var usedTags = new Dictionary<string, HashSet<string>>();
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<string>();
usedTags[key] = collection;
}
foreach (var v in values)
{
collection.Add(v);
}
}
}
return usedTags;
}
/// <summary>
/// Returns which tags are used in this calculation
///
@ -406,7 +436,6 @@ namespace AspectedRouting.Language
/// <returns>A dictionary containing all possible values. An entry with an empty list indicates a wildcard</returns>
public static Dictionary<string, List<string>> PossibleTags(this IExpression e)
{
var result = new Dictionary<string, List<string>>();
var mappings = new List<Mapping>();
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<string, List<string>>();
foreach (var (key, expr) in rootMapping.StringToResultFunctions)
{

View file

@ -44,6 +44,32 @@ namespace AspectedRouting.Language.Expression
Behaviours = behaviours;
}
public List<IExpression> AllExpressions(Context ctx)
{
var l = new List<IExpression> {Access, Oneway, Speed};
l.AddRange(DefaultParameters.Values);
l.AddRange(Behaviours.Values.SelectMany(b => b.Values));
l.AddRange(Priority.Values);
var allExpr = new List<IExpression>();
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<string, string> 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<string, IExpression>();
foreach (var (k, v) in DefaultParameters)
@ -65,7 +92,7 @@ namespace AspectedRouting.Language.Expression
}
c = c.WithParameters(parameters);
tags = new Dictionary<string, string>(tags);
var canAccess = Access.Run(c, tags);
tags["access"] = "" + canAccess;

View file

@ -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<string> ArgNames { get; } = new List<string> {"s"};
public FirstMatchOf() : base("firstMatchOf", true,

View file

@ -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());
}