Exemplo n.º 1
0
        public static Dictionary <string, (List <Type> Types, string inFunction)> UsedParameters(
            this ProfileMetaData profile, Context context)
        {
            var parameters = new Dictionary <string, (List <Type> Types, string usageLocation)>();


            void AddParams(IExpression e, string inFunction)
            {
                var parms = e.UsedParameters();

                foreach (var param in parms)
                {
                    if (parameters.TryGetValue(param.ParamName, out var typesOldUsage))
                    {
                        var(types, oldUsage) = typesOldUsage;
                        var unified = types.SpecializeTo(param.Types);
                        if (unified == null)
                        {
                            throw new ArgumentException("Inconsistent parameter usage: the paremeter " +
                                                        param.ParamName + " is used\n" +
                                                        $"   in {oldUsage} as {string.Join(",", types)}\n" +
                                                        $"   in {inFunction} as {string.Join(",", param.Types)}\n" +
                                                        $"which can not be unified");
                        }
                    }
                    else
                    {
                        parameters[param.ParamName] = (param.Types.ToList(), inFunction);
                    }
                }
            }

            AddParams(profile.Access, "profile definition for " + profile.Name + ".access");
            AddParams(profile.Oneway, "profile definition for " + profile.Name + ".oneway");
            AddParams(profile.Speed, "profile definition for " + profile.Name + ".speed");

            foreach (var(key, expr) in profile.Priority)
            {
                AddParams(new Parameter(key), profile.Name + ".priority.lefthand");
                AddParams(expr, profile.Name + ".priority");
            }

            var calledFunctions = profile.CalledFunctionsRecursive(context).Values
                                  .SelectMany(ls => ls).ToHashSet();

            foreach (var calledFunction in calledFunctions)
            {
                var func = context.GetFunction(calledFunction);
                if (func is AspectMetadata meta && meta.ProfileInternal)
                {
                    continue;
                }

                AddParams(func, "function " + calledFunction);
            }


            return(parameters);
        }
Exemplo n.º 2
0
        public string ToLua()
        {
            _skeleton.AddDep("spoken_instructions");

            var(membershipFunction, extraKeys)       = GenerateMembershipPreprocessor();
            var(profileOverview, behaviourFunctions) = GenerateProfileFunctions();
            var mainFunction = GenerateMainProfileFunction();
            var tests        = new LuaTestPrinter(_skeleton, new List <string> {
                "unitTest", "unitTestProfile"
            }).GenerateFullTestSuite(_profileTests, _aspectTestSuites);


            var keys = _profile.AllExpressions(_context).PossibleTags().Keys
                       .Concat(extraKeys)
                       .Select(key => "\"" + key + "\"")
                       .ToHashSet();

            var header = new List <string>
            {
                $"-- Itinero 1.0-profile, generated by AspectedRouting.",
                $"name = \"{_profile.Name}\"",
                "normalize = false",
                "vehicle_types = {" + string.Join(", ", _profile.VehicleTyps.Select(s => "\"" + s + "\"")) + "}",
                // meta_whitelist is defined in the profile file, these are tags that are included in the generated route, but are not relevant for determining weights
                "meta_whitelist = {\n"
                + string.Join("\n    , ", _profile.Metadata.Select(s => "\"" + s + "\""))
                + " }",
                "profile_whitelist = {\n      " + string.Join("\n    , ", keys) + "\n    }",
                "",
                "",
                "",
                profileOverview,
                "",
                _parameterPrinter.GenerateDefaultParameters()
            };


            // Add the aspect functions to the skeleton
            var usedFunctions = _profile.CalledFunctionsRecursive(_context).Values.SelectMany(v => v).ToHashSet();

            foreach (var functionName in usedFunctions)
            {
                _skeleton.AddFunction(_context.GetAspect(functionName));
            }


            // The dependencies should be generated after all the other functions are generated, to make sure all are added
            var dependencies = _skeleton.GenerateDependencies();
            var functions    = _skeleton.GenerateFunctions();

            var allCode = new List <List <string> >
            {
                header,
                behaviourFunctions,
                mainFunction.InList(),
                membershipFunction.InList(),
                "---------------------- ASPECTS ----------------------".InList(),
                functions,
                "---------------------- UTILS ------------------------".InList(),
                dependencies,
                _skeleton.GenerateConstants().ToList(),
                "----------------------- TESTS ------------------------".InList(),
                tests.InList(),
                GenerateLegacyTail().InList()
            };


            return(string.Join("\n\n\n", allCode.Select(code => string.Join("\n", code))));
        }