IToolS Blog

IToolS Scripting namespace

by itools@albertoschiassi.it on martedì 10 settembre 2013 15:17

All'interno del namespace IToolS.Data.Scripting e' presente la classe Script, mediante la quale e' possibile eseguire uno script passato in formato stringa.  

Lo script viene sostanzialmente "confezionato" all'interno di un metodo definito in una classe compilata in memoria, puo' avere parametri, utilizzare variabili passate per riferimento e deve necessariamente restituire un valore: 

Ipotizziamo che il mio script sia: 

  "System.Windows.Forms.MessageBox.Show("Hello!"); return 0;" 

la classe IToolS Script genera una classe compilata in memoria con al proprio interno un metodo "Execute" contenente lo script: 

public Object Execute(...)
{
System.Windows.Forms.MessageBox.Show("Hello!");
return 0;

Nell'esempio ripotato di seguito viene utilizzata la classe script dove sono presenti riferimenti a variabili IToolS ad un tipo di dato Int32 e viene passato un parametro al momento dell'invocazione:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using IToolS.Base;
using IToolS.Components.Communication;
using IToolS.Components.IOServers;
using IToolS.Data;
using IToolS.Data.Base;
using IToolS.Data.Scripting;
using IToolS.IOServers.Common;
 
namespace TestScript
{
   class Program
   {
      static void Main(string[] args)
      {
         IToolS.Components.ComponentBase.RaiseEventsOnMainThread = false;
 
         IOServer ioserver = new IToolS.Components.IOServers.IOServer() { Name = "Simulation" };
 
         Variable variable1 = new Variable() { VariableName = "variable1", Address = "0" };
         Variable variable2 = new Variable() { VariableName = "variable2", Address = "2" };
 
         Group group = new Group();
         group.Items.AddRange(new Variable[] { variable1, variable2 });
 
         Client client = new Client() { IOServer = ioserver, Group = group };
 
         int pippo = 12;
 
         List<ScriptVariable> scriptVariables = new List<ScriptVariable>();
         scriptVariables.Add(new ScriptVariable(variable1.VariableName, variable1, variable1.GetType()));
         scriptVariables.Add(new ScriptVariable(variable2.VariableName, variable2, variable2.GetType()));
         scriptVariables.Add(new ScriptVariable("pippo", pippo, typeof(Int32)));
 
 
         String expression = "if (((int?)variable1).HasValue && ((int?)variable2).HasValue)" +
                             "{ " +
                             "   return ((int?)variable1).Value + ((int?)variable2).Value + pippo + Convert.ToInt32(parameters[0]); " +
                             "}" +
                             "else" +
                             "{" +
                             "   return 0;" +
                             "}";
 
 
         IToolSejujshvqzz a = new IToolSejujshvqzz(variable1, variable2, pippo);
         a.Execute(100);
 
         using (Script script = new Script(expression, scriptVariables,
            new String[] { Utilities.ITOOLS_DLL_NAME, Utilities.ITOOLS_DATA_DLL_NAME, Utilities.ITOOLS_COMPONENTS_DLL_NAME }))
         {
            script.Compile();
 
            using (StringWriter sw = new StringWriter())
            {
               script.PrintCode(sw);
 
               Console.WriteLine(sw.ToString());
            }
 
            variable1.Changed += delegate(object sender, ChangedEventArgs e)
            {
               Console.WriteLine("variable name: {0}, value: {1}", ((Variable)sender).VariableName, e.NewValue);
               Console.WriteLine("script result: {0}", script.Execute(100));
            };
 
            variable2.Changed += delegate(object sender, ChangedEventArgs e)
            {
               Console.WriteLine("variable name: {0}, value: {1}", ((Variable)sender).VariableName, e.NewValue);
               Console.WriteLine("script result: {0}", script.Execute(200));
            };
 
            client.Start();
 
            Console.WriteLine("Press ENTER to exit");
            Console.ReadLine();
 
         }
 
      }
   }
}
TestScript.zip

Blogs Parent Separator IToolS Blog
Author

Tags