Skip to content

Commit

Permalink
Fix synchronization bug; Code cleanup & More
Browse files Browse the repository at this point in the history
+ The 'SkillsCore' and 'CognitionCore' tasks now run synchronously. There was a bug where the program was showing the Cognition results before they actually happend.
  • Loading branch information
Mei Fagundes committed Aug 29, 2018
1 parent a3238a7 commit f1ae540
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 50 deletions.
15 changes: 15 additions & 0 deletions CodeSnippets/ReachUtility.snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>ReachUtility</Title>
<Author>Mei</Author>
<Shortcut>ru</Shortcut>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[Utilities.ReachUtility(System.Reflection.MethodBase.GetCurrentMethod())]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
15 changes: 7 additions & 8 deletions Cognition/CognitionCore.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;

namespace POLARIS.Cognition {
Expand All @@ -11,16 +9,17 @@ public static class CognitionCore {
/// Fetches and executes the 'Cognize' Methods from all the Classes inside the 'Cognition' Namespace
/// </summary>
/// <param name="dialog"></param>
public static void Fetch(Dialog dialog) {
public static void FetchCognition(Dialog dialog) {

Type[] classTypes = POLARIS.Utilities.GetTypesInNamespace(Assembly.GetExecutingAssembly(), "POLARIS.Cognition");

// If this throws a 'NullReferenceException' just change the FOR condition to 'i < classTypes.Length - 1' to make it not trigger the '<>c__DisplayClass1_0' Class

for (int i = 0; i < classTypes.Length; i++) {
if (classTypes[i].Name != "CognitionCore") {
// The StartsWith("<>") is in there to avoid calling the '<>c__DisplayClass1_...' class from the Debugger, if this happens an 'NullReferenceException' will be thrown
if (classTypes[i].Name != "CognitionCore" && !classTypes[i].Name.StartsWith("<>")) {

MethodInfo classMethod = classTypes[i].GetMethod("Cognize");
//Task.Factory.StartNew(() => classMethod.Invoke(null, new object[] { dialog }));
classMethod.Invoke(null, new object[] { dialog });
Task cognitionTask = new Task(() => classMethod.Invoke(null, new object[] { dialog }));
cognitionTask.RunSynchronously();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Cognition/Question.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void Cognize(Dialog dialog) {
if (!dialog.IsVerbsEmpty && !dialog.IsPronounsEmpty) {

// isQuestion if the Pronoun is immediately after the Verb. || Example: "Do you know if..."
if (dialog.PronounsIndex[0] > dialog.VerbsIndex[0] && dialog.PronounsIndex[0] - dialog.VerbsIndex[0] <= 1 && !dialog.IsNounsEmpty && dialog.NounsIndex[0] == dialog.PronounsIndex[0] + 1) {
if (dialog.PronounsIndex[0] > dialog.VerbsIndex[0] && dialog.PronounsIndex[0] - dialog.VerbsIndex[0] <= 1 && !dialog.IsNounsEmpty) {
dialog.IsQuestion = true;
return;
}
Expand Down
25 changes: 9 additions & 16 deletions Dialog.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace POLARIS {
public class Dialog {
Expand All @@ -12,11 +10,11 @@ public class Dialog {
public Vocabulary vocabulary;

public List<String> Phrase { get; set; } = new List<String>();
public List<Int32> VerbsIndex { get; set; } = new List<Int32>();
public List<Int32> PronounsIndex { get; set; } = new List<Int32>();
public List<Int32> AdverbsIndex { get; set; } = new List<Int32>();
public List<Int32> SkillsIndex { get; set; } = new List<Int32>();
public List<Int32> NounsIndex { get; set; } = new List<Int32>();
public List<UInt16> VerbsIndex { get; set; } = new List<UInt16>();
public List<UInt16> PronounsIndex { get; set; } = new List<UInt16>();
public List<UInt16> AdverbsIndex { get; set; } = new List<UInt16>();
public List<UInt16> SkillsIndex { get; set; } = new List<UInt16>();
public List<UInt16> NounsIndex { get; set; } = new List<UInt16>();

public Boolean IsVerbsEmpty { get; set; }
public Boolean IsPronounsEmpty { get; set; }
Expand All @@ -32,13 +30,8 @@ public Dialog(String input, Vocabulary vocabularyIn) {

this.vocabulary = vocabularyIn;

String[] Aux;
Aux = input.ToLower().Split(' ');

// Copying input from Aux to the List 'phrase'
for (int i = 0; i < Aux.Length; i++) {
Phrase.Add(Aux[i]);
}
Phrase = input.ToLower().Split(' ').ToList();
Phrase.RemoveAll(String.IsNullOrEmpty);

// Isolating ponctuation mark as a last String
String lastString = Phrase[Phrase.Count - 1].Substring(Phrase[Phrase.Count - 1].Length - 1);
Expand All @@ -64,9 +57,9 @@ public Dialog() { }
/// <param name="VocabularyFile"></param>
/// <param name="Indexes"></param>
/// <returns></returns>
private Boolean IndexVocabulary(List<String> VocabularyFile, List<Int32> Indexes) {
private Boolean IndexVocabulary(List<String> VocabularyFile, List<UInt16> Indexes) {

for (int i = 0; i < Phrase.Count; i++) {
for (UInt16 i = 0; i < Phrase.Count; i++) {
foreach (String currentFile in VocabularyFile) {
if (Phrase[i] == currentFile) {
Indexes.Add(i);
Expand Down
3 changes: 2 additions & 1 deletion POLARIS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>1</ApplicationRevision>
<ApplicationRevision>4</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
Expand Down Expand Up @@ -102,6 +102,7 @@
<None Include="POLARIS_TemporaryKey.pfx" />
<None Include="Resources\Icon.ico" />
<None Include="Design\Icon.ico" />
<None Include="CodeSnippets\ReachUtility.snippet" />
<Content Include="Vocabulary\Skills.txt" />
<Content Include="Vocabulary\Adverbs.txt" />
<Content Include="Vocabulary\Nouns.txt" />
Expand Down
3 changes: 2 additions & 1 deletion PolarisCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public static void Main(string[] args) {
dialog.Debug();

Console.WriteLine("Type something: ");

input = Console.ReadLine();
Console.Clear();
}
}

public static void MainPipeline(Dialog dialog) {

Task cognitionCoreTask = new Task(() => Cognition.CognitionCore.Fetch(dialog));
Task cognitionCoreTask = new Task(() => Cognition.CognitionCore.FetchCognition(dialog));
cognitionCoreTask.RunSynchronously();

if (!dialog.IsSkillsEmpty) {
Expand Down
13 changes: 8 additions & 5 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System.Reflection;
using System.Resources;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("POLARIS")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyTitle("Made with love by Mei ❤")]
[assembly: AssemblyDescription("https://github.com/LouchDaishiteru/Polaris")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("POLARIS")]
Expand All @@ -32,5 +33,7 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.*")]
[assembly: NeutralResourcesLanguage("en-US")]

4 changes: 3 additions & 1 deletion Skills/SkillsCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static void FetchSkill(Dialog dialog) {
return;
}

if (classType.Name != "SkillsCore") {
// The StartsWith("<>") is in there to avoid calling the '<>c__DisplayClass1_...' class from the Debugger, if this happens an 'NullReferenceException' will be thrown
if (classType.Name != "SkillsCore" && !classType.Name.StartsWith("<>")) {

MethodInfo classMethod = classType.GetMethod("Execute");
Task.Factory.StartNew(() => classMethod.Invoke(null, new object[] { dialog }));
}
Expand Down
5 changes: 4 additions & 1 deletion Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;

Expand All @@ -18,5 +17,9 @@ public static Type[] GetTypesInNamespace(Assembly assembly, string nameSpace) {
.Where(t => String.Equals(t.Namespace, nameSpace, StringComparison.Ordinal))
.ToArray();
}

public static void ReachUtility(MethodBase methodBase) {
Console.WriteLine("Reached Method '" + methodBase.Name + "' from Class '" + methodBase.DeclaringType.Name + "'");
}
}
}
30 changes: 14 additions & 16 deletions Vocabulary.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace POLARIS {
public class Vocabulary {

private List<String> verbs;
private List<String> skills;
private List<String> pronouns;
private List<String> adverbs;
private List<String> nouns;
/// <summary>
/// Model Class that loads the vocabulary from Resources
/// </summary>
public class Vocabulary {

public List<String> Verbs { get; set; } = new List<String>();
public List<String> Skills { get; set; } = new List<String>();
public List<String> Pronouns { get; set; } = new List<String>();
public List<String> Adverbs { get; set; } = new List<String>();
public List<String> Nouns { get; set; } = new List<String>();

/// <summary>
/// Loads the vocabulary from Resources once
/// </summary>
public Vocabulary() {

Verbs = Convert(POLARIS.Properties.Resources.Verbs);
Expand All @@ -28,7 +32,7 @@ public Vocabulary() {
/// </summary>
/// <param name="stringToConvert"></param>
/// <returns></returns>
public static List<String> Convert(String stringToConvert) {
private static List<String> Convert(String stringToConvert) {
List<String> temp = stringToConvert.Split(' ', '\n', '\r', (char)0x2028, (char)0x2029).ToList();
temp.RemoveAll(String.IsNullOrEmpty);
return temp;
Expand All @@ -37,11 +41,5 @@ public static List<String> Convert(String stringToConvert) {
public void Debug() {
Console.WriteLine("\nVocabulary Debug ->\n Nº of Verbs: " + Verbs.Count + "\n Nº of Pronouns: " + Pronouns.Count + "\n Nº of Adverbs: " + Adverbs.Count + "\n");
}

public List<String> Verbs { get => verbs; set => verbs = value; }
public List<String> Skills { get => skills; set => skills = value; }
public List<String> Pronouns { get => pronouns; set => pronouns = value; }
public List<String> Adverbs { get => adverbs; set => adverbs = value; }
public List<String> Nouns { get => nouns; set => nouns = value; }
}
}

0 comments on commit f1ae540

Please sign in to comment.