Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added all the templates #1015

Merged
merged 8 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
<ItemGroup>
<Compile Include="**\*.cs" Exclude="**/obj/**/*.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Templates\Templates.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
203 changes: 161 additions & 42 deletions src/Microsoft.InsightsGenerator/RulesEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,200 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Microsoft.InsightsGenerator
{
public class RulesEngine
{
public class ColumnHeaders
public static List<Template> Templates;

public static List<string> TopListHashHeaders = new List<string>{ "#top", "#average", "#averageSlice", "#topPerslice" , "#bottom"};
jiarima marked this conversation as resolved.
Show resolved Hide resolved

public RulesEngine()
{
public List<string> SingleHashValues { get; set; }
public List<string> DoubleHashValues { get; set; }
public string Template { get; set; }
Templates = GetTemplates();
}


public static ColumnHeaders TemplateParser(string templateFile)
public static ColumnHeaders TemplateParser(string templateContent)
{
StreamReader file = new StreamReader($"{templateFile}");
string line = null;
string templateText = null;

ColumnHeaders ch = new ColumnHeaders();
ch.SingleHashValues = new List<string>();
ch.DoubleHashValues = new List<string>();
while (!file.EndOfStream)
var processedText = Regex.Replace(templateContent, @",|\\n", "");
ch.Template = templateContent;

List<string> keyvalue = processedText.Split(' ').Select(s => s.Trim()).ToList();

foreach (string s in keyvalue)
{
//line = file.ReadLine().Replace("\\n",string.Empty);
line = Regex.Replace(file.ReadLine(), @",|\\n", "");
templateText = line;
ch.Template = templateText;
List<string> keyvalue = line.Split(' ').Select(s => s.Trim()).ToList();
foreach (string s in keyvalue)
if (s.StartsWith("#"))
{
if (s.StartsWith("#"))
string headers = s.Substring(1, s.Length - 1);
if (headers.StartsWith("#"))
{
string headers = s.Substring(1, s.Length - 1);
if (headers.StartsWith("#"))
{
ch.DoubleHashValues.Add(headers.Substring(1, headers.Length - 1));
}
else
{
ch.SingleHashValues.Add(headers);
}
ch.DoubleHashValues.Add(headers.Substring(1, headers.Length - 1));
}
if (s.Contains("tempId"))
else
{
Debug.WriteLine(s);
ch.SingleHashValues.Add(headers);
}
}

}

return ch;
}

public static Dictionary<int, string> RulesGeneratorFromTemplate()
/// <summary>
/// Find a matched template based on the single hash values
/// </summary>
/// <param name="singleHashHeaders"></param>
/// <returns></returns>
public static Template FindMatchedTemplate(List<List<string>> singleHashHeaders, DataArray columnInfo)
{
Dictionary<int, string> Rules_templateID = new Dictionary<int, string>();
string rules = null;
ColumnHeaders header = TemplateParser(@"template_16.txt");
return Rules_templateID;
if (Templates == null)
{
Templates = GetTemplates();
}
var headersWithSingleHash = GetTopHeadersWithHash(singleHashHeaders);

foreach (var template in Templates)
{
var columnHeaders = TemplateParser(template.Content);
var singleHash = columnHeaders.SingleHashValues;

if (singleHashHeaders.Count == singleHash.Count)
{
if (Enumerable.SequenceEqual(singleHash.OrderBy(s => s), headersWithSingleHash.OrderBy(s => s)))
{
// Replace # and ## values in template with actual values here befor return
ReplaceHashesInTemplate(singleHashHeaders, columnInfo, template);
return template;
}
}
}

// No matched Template found
return null;
}

public static string RulesChecking(string singleHashHeaders, string doublehashHeaders)
private static Template ReplaceHashesInTemplate(List<List<string>>singleHashList, DataArray columnInfo, Template template)
{
string template = null;
//if(singleHashHeaders!=null && doublehashHeaders!=null && RulesGeneratorFromInput(singleHashHeaders, doublehashHeaders).Equals(RulesGeneratorFromTemplate()){
//from dictionary mapping select template id
//}
StringBuilder modifiedTemp = new StringBuilder(template.Content);

// Replace single hash values
foreach (var line in singleHashList)
{
// Example of a single list (line):
// "top", "3", " input (value) %OfValue ", " input (value) %OfValue ", " input (value) %OfValue "
var headerInputs = line.ToArray();
string header = "#" + headerInputs[0];

if (TopListHashHeaders.Contains(header))
{
//First replace the header with the second value in the list

modifiedTemp.Replace(header, headerInputs[1]);
StringBuilder topListStr = new StringBuilder();
for (int i = 2; i < headerInputs.Length - 1; i++)
{
// Append all the rest of the elemet in the array separated by new line
topListStr.AppendLine(headerInputs[i]);
}
modifiedTemp.Replace("#toplist", topListStr.ToString());
}
else
{
modifiedTemp.Replace("#" + headerInputs[0], headerInputs[1]);
}
}

// Replace double hash values
var transformedColumnArray = columnInfo.TransformedColumnNames.ToArray();
var columnArray = columnInfo.ColumnNames.ToArray();

for (int p = 0; p < columnInfo.TransformedColumnNames.Length - 1; p++)
{
modifiedTemp.Replace("#" + transformedColumnArray[p], columnArray[p]);
}

template.Content = modifiedTemp.ToString();
return template;
}

private static List<string> GetTopHeadersWithHash(List<List<string>> singleHashHeaders)
{
var topHeaderList = new List<string>();
foreach (var list in singleHashHeaders)
{
topHeaderList.Add("#" + list.First());
if (TopListHashHeaders.Contains("#" + list.First()))
{
topHeaderList.Add("#" + "toplist");
}
}
return topHeaderList;
}

/// <summary>
/// Retrieve all the templates and template ids
/// </summary>
/// <returns>All the template & id comnbination </returns>
public static List<Template> GetTemplates()
{
var templateHolder = new List<Template>();

int templateId;
using (StreamReader streamReader = new StreamReader($"Templates/templates.txt", Encoding.UTF8))
{
int temId = 0;
var wholeText = streamReader.ReadToEnd();
var templateArray = wholeText.Split(new[] { "Template " }, StringSplitOptions.None).ToList();

foreach (var line in templateArray.Where(r => r != string.Empty))
{
var parts = line.Split(new[] { "\r\n" }, StringSplitOptions.None).ToList();

temId = int.Parse(parts[0]);
templateHolder.Add(new Template(temId, parts[1]));
}

return templateHolder;
}
}
}

/// <summary>
/// Template container to hold the template id + template body combination
/// </summary>
public class Template
{
public Template(int id, string content)
{
Id = id;
Content = content;
}

public int Id { get; set; }
public string Content { get; set; }
}

public class ColumnHeaders
{
public ColumnHeaders()
{
SingleHashValues = new List<string>();
DoubleHashValues = new List<string>();
Template = null;
}

public List<string> SingleHashValues { get; set; }
public List<string> DoubleHashValues { get; set; }
public string Template { get; set; }
}
}

54 changes: 54 additions & 0 deletions src/Microsoft.InsightsGenerator/Templates/Templates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
Template 16
For the #slices ##SlicePar_GG_1(s), the percentage of ##OutPar_N_C_1 on #time were \n #stHData\n this was compared with #Etime where #ESlices ##SlicePar_GG_1\n #EstHData \n.
Template 17
For the #slices ##SlicePar_GG_1(s), the percentage of ##OutPar_N_C_1 for each group during the week of #time was \n #stHData\n, compared to the #Etime where #ESlices ##SlicePar_GG_1\n #EstHData \n.
Template 21
The data pattern indicates that #st have #volumeType ##OutPar_N_C_1 \n.
Template 22
The largest increase of ##OutPar_N_C_1 was on #time when it increased #increasesize% above the average value.
Template 23
The largest decrease in ##OutPar_N_C_1 was on #time when it fell below #reducesize% of the average value.
Template 24
The data trend indicates a consistent #trend over time. \n
Template 1
For the #averageSlice ##slicer_0, the volume of each is: #averageSlicelist
Template 2
The ##InPar_GG_1 that had largest of each ##SlicePar_GG_1 are (only top #n ##InPar_GG_1) \n
Template 3
#input had #TPer% for ##SlicePar_GG_1 #Slice \n"
Template 4
The ##InPar_GG_1 that had the largest of each ##SlicePar_GG_1 are \n
Template 5
The ##InPar_GG_1 that had largest of each ##SlicePar_GG_1 are (only top #n ##InPar_GG_1) \n
Template 6
The largest and smallest ##SlicePar_GG_1 for the top #n ##InPar_GG_1 were are as follows \n
Template 7
#inp had #maxSlice the largest ##SlicePar_GG_1 #maxPer% and #minSlice the smallest ##SlicePar_GG_1 #minPer% \n
Template 8
#inp had a total of #OutPar_N_C_1 ##OutPar_N_C_1 that constitues #Per% \n \n
Template 9
There were #groups ##input_g_0 (s), the top #top highest total ##Output_0 were as follows:\n #toplist
Template 10
There were #groups ##InPar_GG_1(s), the total ##OutPar_N_C_1 were as follows:\n #st
Template 11
in the week #time the data contained #slices ##SlicePar_GG_1, the top #n ##SlicePar_GG_1 with highest ##OutPar_N_C_1 were as follows:\n #stHData this was compared with week #Etime with the following top #En of #Eslice ##SlicePar_GG_1\n #EstData\n
Template 12
in the week #time the data contained #slices ##SlicePar_GG_1, the top #n ##SlicePar_GG_1 with highest ##OutPar_N_C_1 were as follows:\n #stHData\n
Template 13
On day #time the data contained #slices ##SlicePar_GG_1, the top #n ##SlicePar_GG_1 with highest ##OutPar_N_C_1 were as follows:\n #stHData this was compared with day #Etime with the following top #n ##SlicePar_GG_1\n #EstHData\n
Template 14
in month #time the data contained #slices ##SlicePar_GG_1, the top #n ##SlicePar_GG_1 with highest ##OutPar_N_C_1 were as follows:\n #stData this was compared with month #Etime with the following top #n ##SlicePar_GG_1\n #EstData\n
Template 15
in month #time the data contained #slices ##SlicePar_GG_1, the top #n ##SlicePar_GG_1 with highest ##OutPar_N_C_1 were as follows:\n #stHData\n
Template 18
in the week #time the data contained #slices ##SlicePar_GG_1, the % of ##OutPar_N_C_1 for each ##SlicePar_GG_1 were as follows:\n #stHData this was compared with week #Etime with the following #En of #Eslice ##SlicePar_GG_1\n #EstHData \n
Template 19
in month #time the data contained #slices ##SlicePar_GG_1, the % of ##OutPar_N_C_1 for each ##SlicePar_GG_1 were as follows:\n #stData this was compared with month #Etime with the following #n ##SlicePar_GG_1\n #EstData\n
Template 20
in month #time the data contained #slices ##SlicePar_GG_1, the % of ##OutPar_N_C_1 for each ##SlicePar_GG_1 were as follows:\n #stData\n
Template 21
Looking at the pattern of the data it appears that #st has the #volumeType ##OutPar_N_C_1 \n
Template 22
The largest increase in ##OutPar_N_C_1 were observed on #time with an increase of #increasesize% of the avarage value of ##OutPar_N_C_1 \n
Template 23
The top #bottom lowest total ##Output_0 were as follows:\n #bottomlist
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@
<ApplicationIcon />
<StartupObject />
</PropertyGroup>
<ItemGroup>
<None Remove="TestTemplates\template_16.txt" />
</ItemGroup>
<ItemGroup>
<Content Include="TestTemplates\template_16.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Text.Encoding.CodePages" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
Expand Down
21 changes: 13 additions & 8 deletions test/Microsoft.InsightsGenerator.UnitTests/RulesEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
Expand All @@ -12,20 +11,26 @@
namespace Microsoft.InsightsGenerator.UnitTests
{
/// <summary>
/// DataTransformation tests
/// Rules Engine tests
/// </summary>
public class RulesEngineTests
{
[Fact]
public void TemplateParserTest()
{
ColumnHeaders header = TemplateParser(@"TestTemplates/template_16.txt");
var expectedSingleHashValues = new List<string>(new string[] {"slices", "time", "stHData", "Etime", "ESlices", "EstHData" });
var expectedDoubleHashValues = new List<string>(new string[] { "SlicePar_GG_1(s)", "OutPar_N_C_1", "SlicePar_GG_1" });
ColumnHeaders headerForTemp8 = TemplateParser(@"#inp had a total of #OutPar_N_C_1 ##OutPar_N_C_1 that constitues #Per% \n \n");
ColumnHeaders headerForTemp16 = TemplateParser(@"For the #slices ##SlicePar_GG_1(s), the percentage of ##OutPar_N_C_1 on #time were \n #stHData\n this was compared with #Etime where #ESlices ##SlicePar_GG_1\n #EstHData \n.");

Assert.True(Enumerable.SequenceEqual(expectedSingleHashValues, header.SingleHashValues));
Assert.True(Enumerable.SequenceEqual(expectedDoubleHashValues, header.DoubleHashValues));
var expectedSingleHashValuesForTemp16 = new List<string>(new string[] {"slices", "time", "stHData", "Etime", "ESlices", "EstHData" });
var expectedDoubleHashValuesForTemp16 = new List<string>(new string[] { "SlicePar_GG_1(s)", "OutPar_N_C_1", "SlicePar_GG_1" });

var expectedSingleHashValuesForTemp8 = new List<string>(new string[] { "inp", "OutPar_N_C_1", "Per%" });
var expectedDoubleHashValuesForTemp8 = new List<string>(new string[] { "OutPar_N_C_1" });

Assert.True(Enumerable.SequenceEqual(expectedSingleHashValuesForTemp8, headerForTemp8.SingleHashValues));
Assert.True(Enumerable.SequenceEqual(expectedDoubleHashValuesForTemp8, headerForTemp8.DoubleHashValues));
Assert.True(Enumerable.SequenceEqual(expectedSingleHashValuesForTemp16, headerForTemp16.SingleHashValues));
Assert.True(Enumerable.SequenceEqual(expectedDoubleHashValuesForTemp16, headerForTemp16.DoubleHashValues));
}
}
}

This file was deleted.