forked from memo1195/L5K
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTagSection.cs
130 lines (121 loc) · 5.28 KB
/
TagSection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace L5K
{
public class TagSection : L5Ksection, IAcquire
{
//TagSection should be implemented once the program section has been implemented to find all the necessary
//alarms
private HashSet<Tag> _needed;
private bool _localTagSection;
public TagSection(L5KReadStream readings)
: base(readings, readings.TagSecStart, readings.TagSecEnd)
{
_needed = new HashSet<Tag>();
_initializer = readings.TagInit;
_localTagSection = false;
//debbuging
//foreach(var line in _content)
//{
// Console.WriteLine(line);
//}
}
public TagSection(List<string> content)
: base(content)
{
//This constructor will be used with local tags only
_needed = new HashSet<Tag>();
//This gets all the lines in which a tag is delclared and adds it to a List of Tags
var tagIndexes = _content.FindAllIndex(x => x.StartsWith(" ") && !x.StartsWith(" "));
_localTagSection = true;
for (var i = 0; i < tagIndexes.Count; i++)
{
var index = tagIndexes[i];
if (i + 1 != tagIndexes.Count)
{
var length = tagIndexes[i + 1] - tagIndexes[i];
_needed.Add(new Tag(_content.GetRange(tagIndexes[index], ++length)));
}
else
{
var lastIndex = _content.FindIndex(x => x.Contains("END_" + content[content.Count - 1].Trim()));
var length = tagIndexes[i] - lastIndex;
_needed.Add(new Tag(_content.GetRange(tagIndexes[index], length)));
}
}
}
public Dictionary<string, string> GetGlobalTags()//used only with localtags
{
var output = new Dictionary<string, string>();
if (_localTagSection)
{
foreach (var tag in _needed)
{
//Gets the tag relation OriginalTagName and TagName and lists it to a dictionary, this way
//we can send this to the global tag section and build the _needed section with the correct names
if (tag.IsAlias)
{
var tagrelation = tag.GetTagRelation();
output.Add(tagrelation[0], tagrelation[1]);
}
}
return output;
}
else
throw new Exception("This method should only be used with local tags");
}
public void AddTags(List<Program> programs)
{
//This should have an algorithm to find all the tags in the programs, this to get the global tags
//local tags should be gathered with the constructor of Program(content)
//This method should read all the local tags in the program and get the ones that have an Alias
//Also this should compare the tag list I have in Rung class that lists all the tags found in logic
//This is done to discar missing any tags that are used as global 09/05/2020
if (!_localTagSection)
throw new Exception("This method should only be used with global tags");
foreach (var program in programs)
{
var tagsFromCode = program.GetTagsInCode();
foreach (var tag in tagsFromCode)
{
var tagIndex = _content.IndexOf(tag);
var length = _content.IndexOf(";", tagIndex) + 1 - tagIndex;
_needed.Add(new Tag(_content.GetRange(tagIndex, length)));
}
var tagsFromTagSection = program.GetTagsInSection();
foreach (var tag in tagsFromTagSection)
{
var tagIndex = _content.IndexOf(tag.Key);
var length = _content.IndexOf(";", tagIndex) + 1 - tagIndex;
_needed.Add(new Tag(tag.Value, _content.GetRange(tagIndex, length)));
}
}
}
public IEnumerable<IEnumerable<string>> SearchTags(Regex regex)
{
//used for search the whole content of a specific tag or group of tags
var output = new List<List<string>>();
var content = string.Join(" ", _content);
var matches = regex.Matches(content);
var tagNames = new List<string>();
foreach (Match match in matches)
{
tagNames.Add(match.Value);
}
foreach (var tag in tagNames)
{
var startIndex = _content.FindIndex(x=>x.Contains(tag));
var length = _content.FindIndex(startIndex, x => !x.Contains("COMMENT")) - startIndex;
output.Add(_content.GetRange(startIndex, length));
}
return output;
}
public override List<string> Acquire()
{
//Not sure if this is ok, maybe it will also be necessary to get the list of DTs
return _needed.Select(x => x.Name).ToList();
}
}
}