C# Simulation of Intent Locking Protocols

The Main.cs code:

using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace NS_intentlocker
{

    #region "All Enums"
    public enum Status
    {
        Eligible,
        Running,
        Committed,
        Faulty
    }
    public enum BasesetNames
    {
        A, B, C, D, E, F, G, H, I, J
    }

    public enum BasesetTypes
    {
        S, X, IS, IX, SIX
    }

    public enum operations
    {
        R, A, S, M, D
    }
    #endregion

    #region "Custom class for list"
    
    //[Serializable]
    public class collections
    { 
       public int numbers { get; set; }
       public string operation_code { get; set; }
       public bool locks { get; set; }

       public collections(int numbers, string operation_code, bool locks = false)
       {
           this.numbers = numbers;
           this.operation_code = operation_code;
           this.locks = locks;
       }
    }

public class transcations
{
   public int ID { get; set; }
   public Status status{get;set;}
   public int extra_time_ticks {get; set;}
   public int operation_lines {get; set;}
   public int intent_locks { get; set; }
   public string BasesetName { get; set; }
   public string BasesetType { get; set; }
   public List<collections> cols { get; set; }

   //public int count { get; set; }

   public transcations() {

   }
    
}
    #endregion

    
    //##########################################################################################
    //##########################  Main Operation Class #########################################
    //##########################################################################################
public class Intent_Locker_implement{
    
   static void Main(string[] args)
   {
  
       List<transcations> tsc = new List<transcations>(); // declare the list by transcation type


       #region  "Read the data from script and save into the list"

      
       //****************************  Read the data from script and save into the list ***************************************

        List<string> tmp = new List<string>();

         try
        {
            using (StreamReader sr = new StreamReader("five.txt")) // here for change different txt file name
            {
                   String line;

                   //int test = 0;
                   int ids = 0;
                   while ((line = sr.ReadLine()) != null) 
			      {
                        if ((line = sr.ReadLine()) != "")  // filter & skip the empty line
                      {
                          string[] items = line.Split(new Char[] { ' ', ',', '.', ':' }); // split to the element

                          var t1 = new transcations(); // declare a new object
                          t1.ID = ids;
                          ++ids;
                          t1.status = Status.Eligible; // the default status set as Eligible
                          t1.extra_time_ticks = int.Parse(items[0]);
                          t1.operation_lines = int.Parse(items[1]);
                          t1.intent_locks = int.Parse(items[2]);
                          t1.BasesetName = items[3];
                          t1.BasesetType = items[4];


                          //********************* get the operation line record **************************
                          List<collections> operationList = new List<collections>(); // create the list of collections
                          for (int i = 0; i < int.Parse(items[1]); i++)
                          {
                              line = sr.ReadLine();
                              string[] items2 = line.Split(new Char[] { ' ', ',', '.', ':' });
                              collections cols = new collections(int.Parse(items2[0]), items2[1]);
                              //cols.numbers = int.Parse(items2[0]);
                              //cols.operation_code = items2[1];
                              operationList.Add(cols);
                          }
                          t1.cols = operationList;
                         //******************************************************************************

                          tsc.Add(t1); // add the transcation to the list tsc

                      }
                  }
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
       
        #endregion

       #region "testing part"
         //Console.WriteLine(tsc[1].cols[1].operation_code); // test read file successful :)
         //Console.WriteLine(tsc.Count());
         //Console.WriteLine(tsc[0].status);
         #endregion
         

       #region "start the Simulation of Intent Locking Protocols"
       
       //***************** Simulation of Intent Locking Protocols ***********************************
         int TranscationCount = tsc.Count(); // get the count of the transcations        

         int[] rollback = new int[TranscationCount]; // declare the arrays for save the count of rollback or completed
         int[] completed = new int[TranscationCount];
         for (int n = 0; n < TranscationCount; n++)
         {
             rollback[n] = 0;
             completed[n] = 0;
         }


         // start the tick
         for (int ticks = 0; ticks < 1000; ticks++)
         {
             
             bool[] _array = new bool[1000]; // declear an array of boolean, used for save operation status

             for (int n = 0; n < TranscationCount; n++)
             {

                 if (ticks >= tsc[n].extra_time_ticks && ticks < tsc[n].cols.Count() + tsc[n].extra_time_ticks)
                 {
                     tsc[n].status = Status.Running; // turn on the transcations

                     // turn on the operation
                     //Console.WriteLine(n + "/" + ticks);

                     //if (tsc[n].cols[ticks - tsc[n].extra_time_ticks].operation_code != "R")
                     //{
                         if (_array[tsc[n].cols[ticks - tsc[n].extra_time_ticks].numbers] == true) // if is true, that means this operation is using at moment, rollback!
                         {
                             //tsc[n].rollback = tsc[n].rollback++; // the rollback count of this transcation will be increase one
                             rollback[n] = rollback[n]+1;
                         }
                         else
                         {
                             _array[tsc[n].cols[ticks - tsc[n].extra_time_ticks].numbers] = true; //otherwise set it as true
                             //tsc[n].completed = tsc[n].completed++; // the complete count will increase one
                             completed[n] = completed[n]+1;
                         }
                     //}
                     
                     //

                 }

                 ****************** Verify the transcation committed or faulty ***************************************
                 if (ticks >= tsc[n].extra_time_ticks + tsc[n].cols.Count() && rollback[n] == 0)
                 {
                     tsc[n].status = Status.Committed;  //if rollback is 0, set status as commited
                 }
                 else if (rollback[n] > 0)
                 {
                     tsc[n].status = Status.Faulty; // if have any rollback, set status as faulty
                 }
                 ******************************************************************************************************


             }

             //************** output the result for each ticks *****************************
             Console.WriteLine("*********************************************");
             Console.WriteLine("Current ticks is: " + ticks);
             for (int n = 0; n < TranscationCount; n++)
             {
                 Console.WriteLine("Transcation[" + (n + 1) + "] status is :" + tsc[n].status);
                 Console.WriteLine("The Number of Rollback is:" + rollback[n]);
                 Console.WriteLine("The Number of Completed is:" + completed[n]);

             }
         }

       #endregion


       #region "Finally output"
         //************** Finally output *****************************
       // ouput on the screen
         Console.WriteLine("########################################");
         Console.WriteLine("####### Summary Output #########");
         ///Console.WriteLine("Current ticks is: " + ticks);
         for (int n = 0; n < TranscationCount; n++)
         {
             Console.WriteLine("Transcation[" + (n + 1) + "] status is :" + tsc[n].status);
             Console.WriteLine("The Number of Rollback is:" + rollback[n]);
             Console.WriteLine("The Number of Completed is:" + completed[n]);

         }

       // write in the log file
         using (StreamWriter sw = new StreamWriter("five.log"))
         {
             sw.WriteLine("########################################");
             sw.WriteLine("####### Summary Output #########");
             ///Console.WriteLine("Current ticks is: " + ticks);
             for (int n = 0; n < TranscationCount; n++)
             {
                 sw.WriteLine("Transcation[" + (n + 1) + "] status is :" + tsc[n].status);
                 sw.WriteLine("The Number of Rollback is:" + rollback[n]);
                 sw.WriteLine("The Number of Completed is:" + completed[n]);

             }
         }

       #endregion

   }

}
}




The Text script for read:



2 8 1 A SIX
044 R
045 R
046 R
047 R
047 A
046 S
045 S
044 A


3 8 1 A SIX
024 R
034 R
044 R
054 R
054 A
044 A
034 A
911 R

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Simscape 是一种多领域物理建模和仿真工具箱,专用于机电一体化系统的建模和仿真。机电一体化系统是由各种机械、电子、控制和信息处理组件组成的复杂系统。使用Simscape,我们可以更好地理解和分析这些系统的性能。 首先,通过使用Simscape,我们可以将机电一体化系统建模为网络连接的物理组件。这些组件可以是电动机、传感器、执行器、控制器等。通过使用库中提供的各种模型组件,我们能够准确地描述这些组件之间的物理关系和相互作用。这些模型可以是连续域的,也可以是离散域的。 其次,通过在模型中添加参数,我们可以对机电一体化系统进行参数化建模。这使得我们可以更好地理解系统的鲁棒性和性能特性。通过更改参数值,我们可以模拟系统在不同条件下的行为,从而优化设计和控制策略。 另外,Simscape 还提供了完整的仿真环境,可以对建模的机电一体化系统进行仿真分析。我们可以定义不同的输入条件和仿真时间,并观察系统的响应。通过仿真结果,我们可以评估系统的性能和效果,并对系统进行优化和改进。 总之,Simscape 是一种强大的工具,用于建模和仿真机电一体化系统。它提供了丰富的物理模型组件和参数化建模功能,使得我们能够更好地理解和分析这些系统的性能。通过使用Simscape,我们能够优化设计和控制策略,并最终提高机电一体化系统的性能和效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值