dimanche 28 juin 2015

if-elseif-else statements in iron-python for .Net

I am trying to integrate the iron-python as rule engine in my .Net project. I want to know is it possible to execute the expressions like this:

if(c.agents > 100) then 10 elseif (c.agents < 50) then 2 else 0)

I tried the following code but result is always null.

               // string expression = if(c.agents > 100) then 10  elseif (c.agents < 50) then 2 else 0)
                dynamic config = new ExpandoObject();
                var configDictionary = config as IDictionary<string, Object>;
                configDictionary.Add("agents", 100);
                var engine = Python.CreateEngine();

                var statements = new StringBuilder();
                statements.AppendLine("if c.agents > 100: 10");
                statements.AppendLine("elif c.agents < 50: 5");
                statements.AppendLine("else: 0");

                engine.CreateScriptSourceFromString(statements.ToString(), SourceCodeKind.Statements);

                var scope = engine.CreateScope();
                scope.SetVariable("c", config);

                var result = engine.Execute(statements.ToString(), scope);

Can someone guide me how to write such conditional statements in iron-python way.

Aucun commentaire:

Enregistrer un commentaire