Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2015
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace LightingSchemeAnalyzer
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var calls = new StringBuilder();
  15.             var dic = new Dictionary<string, string>();
  16.             var map = new Dictionary<string, string>();
  17.             var inputList = new List<string>();
  18.             string s = null;
  19.             int stateBit = 0;
  20.             while ((s = Console.ReadLine()) != null)
  21.             {
  22.                 if (s.Contains("end")) break;
  23.                 if (s.Length < 1) continue;
  24.                 var cols = s.Split(new char[] {'\t'}, StringSplitOptions.RemoveEmptyEntries);
  25.                 if (cols.Length > 1)
  26.                 {
  27.                     inputList.Add(cols[0]);
  28.                     calls.AppendFormat("{0}(button := {1}, sceneCount := {2}, lightsOn => state.{3});\r\n", cols[0], NameToButtons(cols[0]), cols.Length - 1, stateBit);
  29.                     stateBit++;
  30.                 }
  31.                 for (int i = 1; i < cols.Length; i++)
  32.                 {
  33.                     var outputs = new Regex("[a-z][0-9]+").Matches(cols[i]);
  34.                     foreach (var match in outputs)
  35.                     {
  36.                         var cur = string.Empty;
  37.                         dic.TryGetValue(match.ToString(), out cur);
  38.                         if (cur == null || !cur.Contains(cols[0]))
  39.                         {
  40.                             dic[match.ToString()] = cur + " " + cols[0];
  41.                         }
  42.                     }
  43.                     foreach (var match in outputs)
  44.                     {
  45.                         var cur = string.Empty;
  46.                         map.TryGetValue(match.ToString(), out cur);
  47.                         map[match.ToString()] = cur + " (" + cols[0] + ".sceneIdx = " + (i - 1).ToString() + " AND " + cols[0] + ".lightsOn) OR";
  48.                     }
  49.                 }
  50.             }
  51.             foreach (var input in inputList)
  52.             {
  53.                 Console.WriteLine(input + ",");
  54.             }
  55.             foreach (var entry in dic)
  56.             {
  57.                 Console.WriteLine("{0}\t{1}", entry.Key, entry.Value);
  58.             }
  59.             Console.WriteLine(calls);
  60.             foreach (var entry in map)
  61.             {
  62.                 string line = (entry.Value + ";").Replace(" OR;", ";");
  63.                 string num = new Regex("[0-9]*").Match(entry.Key).NextMatch().Value;
  64.                 if (entry.Key[0] == 'p')
  65.                 {
  66.                     Console.WriteLine("{0} :={1}", "DO" + num, line);
  67.                 }
  68.                 else
  69.                 {
  70.                     Console.WriteLine("{0} :={1}", "MO[" + num + "]", line);
  71.                 }
  72.             }
  73.         }
  74.  
  75.         static string NameToButtons(string name)
  76.         {
  77.             return "DI" + new Regex("[0-9]+.*").Match(name).Value.Replace("_", " OR DI");
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement