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