示例#1
0
        public void Test()
        {
            var compiler = new FormulaCompiler();
            var f        = compiler.Compile <int>(@"5 + 5 {}");

            f().ShouldEqual(10);
        }
示例#2
0
        public static BusinessRule <T, object> AddRuleWithParam <T>(this T item, string formula)
            where T : EntityBase <T>
        {
            var index    = formula.IndexOf("=");
            var left     = formula.Substring(0, index);
            var right    = formula.Substring(index + 1);
            var compiler = new FormulaCompiler()
                           .WithType <T>().WithType <User>();

            var get = compiler.NewLambda()
                      .WithThis <T>()
                      .Returns <object>()
                      .CompileToExpression(left);

            var func = compiler.NewLambda()
                       .WithThis <T>()
                       .WithParam <User>("User")
                       .Returns <object>()
                       .CompileToExpression(right);
            var f = func.Compile();
            var r = new RuleFactory <T>();

            var rule = r.CreateBusinessRule(e => f(e, User.Current), get)
                       .WithModeAtStartup(BusinessRuleStartupMode.None)
                       .AnalyzeDependencies(func);

            item.Extensions.Rules.Add(rule);
            return(rule);
        }
示例#3
0
        public void Complex()
        {
            var compiler = new FormulaCompiler()
                           .WithMath().WithInfo(FormulaCompilerBuildInfoLevels.None);

            TestTime.Measure(1000, () => compiler.Compile <int>("2+2*2 + max(4,5)"));
        }
示例#4
0
        public void Test4()
        {
            var compiler = new FormulaCompiler();
            var f        = compiler.Compile <int>(@"55 + 5;");

            f().ShouldEqual(60);
        }
示例#5
0
        public void PerformanceTest()
        {
            var compiler = new FormulaCompiler();

            compiler.WithInfo(FormulaCompilerBuildInfoLevels.Warning);
            var s = Stopwatch.StartNew();

            for (int i = 0; i < 1e4; i++)
            {
                var f = compiler.Compile <int>("2*5");
            }
            s.Stop();
            Debug.WriteLine("Compile time: " + s.Elapsed);

            var func = compiler.Compile <int>("2*5");

            s = Stopwatch.StartNew();

            for (int i = 0; i < 1e6; i++)
            {
                var res = func();
            }
            s.Stop();
            Debug.WriteLine("Compile time: " + s.Elapsed);
        }
示例#6
0
        public void Test5()
        {
            var compiler = new FormulaCompiler();
            var f        = compiler.Compile <int>(@"5 {5}");

            f().ShouldEqual(5);
        }
示例#7
0
 public void Init()
 {
     _compiler = new FormulaCompiler()
                 .WithMath()
                 .WithDate()
                 .WithType <This>()
                 .WithType <string>();
 }
示例#8
0
        public void ProcessorCount()
        {
            var compiler = new FormulaCompiler()
                           .WithInfo(FormulaCompilerBuildInfoLevels.None)
                           .WithSystem();

            TestTime.Measure(1000, () => compiler.Compile <int>("Environment.ProcessorCount"));
        }
示例#9
0
        public void FlatTypeProviderTest()
        {
            var compiler = new FormulaCompiler()
                           .WithFlatType(typeof(Math));

            var f = compiler.Compile <int>("max(4,2)");

            f().ShouldEqual(4);
        }
示例#10
0
        public void Multiline()
        {
            var compiler = new FormulaCompiler();
            var f        = compiler.Compile <int>(@"5 +
4 + 2
-2");

            f().ShouldEqual(9);
        }
示例#11
0
        public void Simple()
        {
            var compiler = new FormulaCompiler()
                           .WithInfo(FormulaCompilerBuildInfoLevels.None);

            compiler.Compile <int>("5+4");

            TestTime.Measure(1000, () => compiler.Compile <int>("5+4"));
        }
示例#12
0
        public void Simple()
        {
            var compiler = new FormulaCompiler().WithSystem();

            var f = compiler.Compile <Func <string, int, int> >("(string x, int y) => 5 + x.Length + y");

            f.ShouldNotBeNull();

            f()("abc", 2).ShouldEqual(10);
        }
示例#13
0
        //[ExpectedException(typeof(ParameterNotFoundException))]
        public void NonExistingParam()
        {
            var compiler = new FormulaCompiler();

            compiler.NewLambda()
            .Returns <int>()
            .Compile("@Param");

            compiler.BuildInfo.Errors.Count().ShouldBeGreaterThan(0);
        }
示例#14
0
        public void Linq()
        {
            var compiler = new FormulaCompiler()
                           .WithInfo(FormulaCompilerBuildInfoLevels.None)
                           .WithLinq();
            var lambda = compiler.NewLambda()
                         .WithParam <IEnumerable <int> >("input")
                         .Returns <int>();

            TestTime.Measure(1000, () => lambda.Compile("input.Max()"));
        }
示例#15
0
        //[ExpectedException(typeof(MethodNotFoundException))]
        public void SecondLevelParam()
        {
            var compiler = new FormulaCompiler();

            compiler.NewLambda()
            .WithParam <int>("Param")
            .Returns <int>()
            .Compile("@Param.@abc");

            compiler.BuildInfo.Errors.Count().ShouldBeGreaterThan(0);
        }
示例#16
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _func = null;

            var compiler = new FormulaCompiler();

            if (Custom.IsChecked ?? false)
            {
                compiler.
                WithMethod("rand", () => new Random(Environment.TickCount).Next())
                .WithMethod("rand", (int max) => new Random(Environment.TickCount).Next(max))
                .WithMethod("p", (int p) => 5);
            }
            if (Math.IsChecked ?? false)
            {
                compiler.WithMath();
            }
            if (Date.IsChecked ?? false)
            {
                compiler.WithDate();
            }
            if (All.IsChecked ?? false)
            {
                var a = new AssemblyCallProvider()
                        .AddAssembly("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
                        .AddAssembly("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
                        .AddAssembly("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
                        .Using(Using.Text.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries))
                        .WithExtensionMethods();
                compiler.With(a);
            }

            if (SampleEntity.IsChecked ?? false)
            {
                compiler.WithType <SampleEntity>();
                var f = compiler.NewLambda()
                        .WithParam <SampleEntity>("s")
                        .Returns <object>()
                        .Compile(Formula.Text);
                if (f != null)
                {
                    _func = () => f(new SampleEntity());
                }
            }
            else
            {
                _func = compiler.Compile <object>(Formula.Text);
            }

            Status.Text = compiler.BuildInfo.Message;

            Button_Click_1(null, null);
        }
示例#17
0
        public void ParamTest()
        {
            var compiler = new FormulaCompiler();

            var f = compiler.NewLambda().
                    WithParam <int>("Param1").
                    WithParam <int>("Param2").
                    Returns <int>().
                    Compile("Param1 * Param2");

            f(2, 3).ShouldEqual(6);
        }
示例#18
0
        public void Where()
        {
            var compiler = new FormulaCompiler().WithLinq();

            var f = compiler.NewLambda()
                    .WithParam <IEnumerable <int> >("data")
                    .Returns <double>()
                    .Compile("data.Where((int e)=> e < 5).Where((int x)=>x>2).Average()");

            f.ShouldNotBeNull();
            f(Enumerable.Range(0, 10)).ShouldEqual(3.5d);
        }
示例#19
0
        private static FormulaCompiler CreateCompiler(IEntityExtensions extensions)
        {
            Type thisType = extensions.Target.GetType();

            var compiler = new FormulaCompiler();

            compiler.With(new TypeCallProvider(thisType));

            compiler.WithDynamicEntity(thisType, extensions.DynamicProperties.AllowDynamicProperties,
                                       extensions.DynamicProperties.PropertiesMetadata);

            return(compiler);
        }
示例#20
0
    public void EditFormulaField(Formula f, int i)
    {
        string name      = "formulae.formula." + i;
        bool   priorWrap = EditorStyles.textField.wordWrap;

        EditorStyles.textField.wordWrap = true;
        GUI.SetNextControlName("" + fdb.GetInstanceID() + "." + i);
        EditorGUI.BeginChangeCheck();
        f.text = EditorGUILayout.TextArea(f.text, GUILayout.Height(32), GUILayout.Width(Screen.width - 24)).RemoveControlCharacters();
        if (EditorGUI.EndChangeCheck() ||
            (GUI.GetNameOfFocusedControl() != name && lastFocusedControl == name))
        {
            // Debug.Log("compile "+f.text);
            FormulaCompiler.CompileInPlace(f);
        }
        GUI.SetNextControlName("");
        EditorStyles.textField.wordWrap = priorWrap;
        if (f.compilationError != null && f.compilationError.Length > 0)
        {
            EditorGUILayout.HelpBox(f.compilationError, MessageType.Error);
        }
    }
示例#21
0
 internal Lambda(FormulaCompiler compiler)
 {
     Definition = new LambdaTypeDefinition();
     Compiler   = compiler;
 }
示例#22
0
        static void Main(string[] args)
        {
            var directoryInfo     = new DirectoryInfo(@"..\..\..\PhysicsFormulae.Formulae");
            var formulaFiles      = directoryInfo.GetFiles("*.formula");
            var constantFiles     = directoryInfo.GetFiles("*.constant");
            var referenceFiles    = directoryInfo.GetFiles("*.reference");
            var formulaSetFiles   = directoryInfo.GetFiles("*.formulaset");
            var formulaSheetFiles = directoryInfo.GetFiles("*.formulasheet");

            var excludedWordsFile = directoryInfo.GetFiles("ExcludedWords.txt").First();
            var keyPhrasesFile    = directoryInfo.GetFiles("KeyPhrases.txt").First();

            var excludedWords = File.ReadAllLines(excludedWordsFile.FullName).Where(l => l != "").Select(l => l.Trim());
            var keyPhrases    = File.ReadAllLines(keyPhrasesFile.FullName).Where(l => l != "").Select(l => l.Trim());

            var autotagger = new Autotagger(excludedWords, keyPhrases);

            var formulaCompiler      = new FormulaCompiler(autotagger);
            var constantCompiler     = new ConstantCompiler(autotagger);
            var referenceCompiler    = new ReferenceCompiler(autotagger);
            var formulaSetCompiler   = new FormulaSetCompiler(autotagger);
            var formulaSheetCompiler = new FormulaSheetCompiler(autotagger);

            var formulae      = new List <Formula>();
            var constants     = new List <Constant>();
            var references    = new List <Reference>();
            var formulaSets   = new List <FormulaSet>();
            var formulaSheets = new List <FormulaSheet>();

            foreach (var file in referenceFiles)
            {
                var lines     = File.ReadAllLines(file.FullName);
                var reference = referenceCompiler.CompileReference(lines);
                references.Add(reference);

                Console.WriteLine(reference.CitationKey);
            }

            foreach (var file in formulaFiles)
            {
                var lines   = File.ReadAllLines(file.FullName);
                var formula = formulaCompiler.CompileFormula(lines, references);
                formulae.Add(formula);

                Console.WriteLine(formula.Reference);
            }

            foreach (var file in formulaSetFiles)
            {
                var lines      = File.ReadAllLines(file.FullName);
                var formulaSet = formulaSetCompiler.CompileFormulaSet(lines, formulae);
                formulaSets.Add(formulaSet);

                Console.WriteLine(formulaSet.Reference);
            }

            foreach (var file in formulaSheetFiles)
            {
                var lines        = File.ReadAllLines(file.FullName);
                var formulaSheet = formulaSheetCompiler.CompileFormulaSheet(lines, formulae);
                formulaSheets.Add(formulaSheet);

                Console.WriteLine(formulaSheet.Reference);
            }

            foreach (var file in constantFiles)
            {
                var lines    = File.ReadAllLines(file.FullName);
                var constant = constantCompiler.CompileConstant(lines);
                constants.Add(constant);

                Console.WriteLine(constant.Reference);
            }

            foreach (var constant in constants)
            {
                foreach (var formula in formulae)
                {
                    if (formula.Identifiers.Any(i => i.Reference == constant.Reference))
                    {
                        constant.UsedInFormulae.Add(formula.Reference);
                    }
                }
            }

            var curricula = new Dictionary <string, Curriculum>();

            foreach (var formula in formulae)
            {
                foreach (var curriculum in formula.Curricula)
                {
                    if (!curricula.ContainsKey(curriculum))
                    {
                        curricula[curriculum]      = new Curriculum();
                        curricula[curriculum].Name = curriculum;
                    }

                    curricula[curriculum].Formulae.Add(formula.Reference);
                }
            }

            var model = new Model();

            model.Formulae      = formulae;
            model.Constants     = constants;
            model.References    = references;
            model.FormulaSets   = formulaSets;
            model.FormulaSheets = formulaSheets;
            model.Curricula     = curricula.Select(c => c.Value).ToList();

            var outputLocations = new List <string>()
            {
                @"..\..\..\PhysicsFormulae.Formulae\Compiled.json", @"..\..\..\PhysicsFormulae.WebApplication\formulae.json"
            };

            var serializer = new JsonSerializer();

            serializer.Formatting = Newtonsoft.Json.Formatting.Indented;

            foreach (var outputLocation in outputLocations)
            {
                using (var streamWriter = new StreamWriter(outputLocation))
                    using (var jsonTextWriter = new JsonTextWriter(streamWriter))
                    {
                        serializer.Serialize(jsonTextWriter, model);
                    }
            }

            MakeFormulaImages(formulae);

            var sitemap = new Sitemap();

            foreach (var formula in formulae)
            {
                sitemap.AddURL("http://www.physicsformulae.com/#/formula/" + formula.URLReference);
            }

            foreach (var formulaSet in formulaSets)
            {
                sitemap.AddURL("http://www.physicsformulae.com/#/formula-set/" + formulaSet.URLReference);
            }

            foreach (var constant in constants)
            {
                sitemap.AddURL("http://www.physicsformulae.com/#/constant/" + constant.URLReference, "0.9");
            }

            sitemap.Save(@"..\..\..\PhysicsFormulae.WebApplication\sitemap.xml");
        }
 public static FormulaCompiler WithDynamicEntity(this FormulaCompiler compiler,
                                                 Type thisType, bool allowDynamicProperties, IEnumerable <DynamicPropertyMetadata> properties)
 {
     return(compiler.With(new DynamicPropertyProvider(thisType, allowDynamicProperties, properties)));
 }
 public CompilerTests()
 {
     _autotagger      = new Autotagger(new List <string>(), new List <string>());
     _formulaCompiler = new FormulaCompiler(_autotagger);
 }