Skip to content

Commit

Permalink
Merge pull request #1 from NaXzyu/11-implement-basic-unit-tests-for-s…
Browse files Browse the repository at this point in the history
…ocraticagent-and-lexer-classes

11 implement basic unit tests for socraticagent and lexer classes
  • Loading branch information
p3nGu1nZz authored Apr 8, 2024
2 parents ba442f5 + 1611668 commit 2e6e5f6
Show file tree
Hide file tree
Showing 46 changed files with 1,750 additions and 52 deletions.
5 changes: 5 additions & 0 deletions Assets/CommandTerminal/CommandArg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ public struct CommandArg
{
private string _value;

public CommandArg(string value)
{
_value = value;
}

public string Value
{
get { return _value; }
Expand Down
9 changes: 9 additions & 0 deletions Assets/CommandTerminal/CommandBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ private void ProcessLogLine(LogItem item, int maxChars, ref List<string> lines,
}
}

public string GetLastLog()
{
if (_Logs.Count == 0)
{
return string.Empty;
}
return _Logs[_Logs.Count - 1].Message;
}

public void Reset()
{
_Logs.Clear();
Expand Down
2 changes: 1 addition & 1 deletion Assets/CommandTerminal/CommandShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Run(string line)

while (_remaining != "")
{
var _arg = CommandUtils.EatArgument(ref _remaining);
var _arg = CommandUtils.ParseCommand(ref _remaining);

if (_arg.String != "")
{
Expand Down
44 changes: 39 additions & 5 deletions Assets/CommandTerminal/CommandUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,55 @@ public static Dictionary<string, MethodInfo> CacheCommandMethods()
return methodDictionary;
}

public static CommandArg EatArgument(ref string s)
public static CommandArg ParseCommand(ref string s)
{
var arg = new CommandArg();
int space_index = s.IndexOf(' ');
if (space_index >= 0)
s = s.Trim();

char[] quoteChars = { '\"', '\'' };

foreach (var quoteChar in quoteChars)
{
if (s.StartsWith(quoteChar.ToString()))
{
int quoteIndex = FindClosingQuote(s, quoteChar);
if (quoteIndex >= 0)
{
arg.String = UnescapedQuotes(s.Substring(1, quoteIndex - 1), quoteChar);
s = s.Substring(quoteIndex + 1).Trim();
return arg;
}
}
}

int spaceIndex = s.IndexOf(' ');
if (spaceIndex >= 0)
{
arg.String = s.Substring(0, space_index);
s = s.Substring(space_index + 1);
arg.String = s.Substring(0, spaceIndex);
s = s.Substring(spaceIndex + 1).Trim();
}
else
{
arg.String = s;
s = "";
}

return arg;
}

public static int FindClosingQuote(string s, char quoteChar)
{
int quoteIndex = s.IndexOf(quoteChar, 1);
while (quoteIndex > 0 && s[quoteIndex - 1] == '\\')
{
quoteIndex = s.IndexOf(quoteChar, quoteIndex + 1);
}
return quoteIndex;
}

public static string UnescapedQuotes(string s, char quoteChar)
{
return s.Replace("\\" + quoteChar, quoteChar.ToString());
}
}
}
2 changes: 1 addition & 1 deletion Assets/CommandTerminal/Terminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ void Awake()

void Initialize()
{
Settings = new CommandSettings();
Commands = new Commands();
Settings = new CommandSettings();
Buffer = new CommandBuffer(Settings.BufferSize);
Shell = new CommandShell();
History = new CommandHistory();
Expand Down
8 changes: 8 additions & 0 deletions Assets/Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Editor/Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Assets/Editor/Tests/EditorTests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "EditorTests",
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
]
}
7 changes: 7 additions & 0 deletions Assets/Editor/Tests/EditorTests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions Assets/Editor/Tests/NewEditorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

namespace DialogosEngine.Tests
{
public class NewEditorTests
{
[Test]
public void NewEditorTestsSimplePasses()
{
// TODO Use the Assert class to test conditions
}

[UnityTest]
public IEnumerator NewEditorTestsWithEnumeratorPasses()
{
// TODO Use the Assert class to test conditions.
yield return null;
}
}
}
2 changes: 2 additions & 0 deletions Assets/Editor/Tests/NewEditorTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/Resources/Prefabs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
33 changes: 33 additions & 0 deletions Assets/Resources/Prefabs/EchoAgent.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &7291126267894192705
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1189533486204883621}
m_Layer: 0
m_Name: EchoAgent
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1189533486204883621
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7291126267894192705}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
7 changes: 7 additions & 0 deletions Assets/Resources/Prefabs/EchoAgent.prefab.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 7 additions & 1 deletion Assets/Resources/bootstrap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
# System Commands
Register { Register, 0, -1, "Registers a new command in the terminal."}
Register { Unregister, 1, 1, "Unregisters an existing command from the terminal."}
Register { clear, 0, 1, "Clears the console and archives the log lines."}
Register { print, 1, 1, "Prints a message to the terminal."}
Register { echo, 1, 1, "Echo's a message to the terminal."}

# Help Command
Register { Help, 0, -1, "Displays help information for all commands."}

# New Command
# Build Command
Register { Build, 0, -1, "Performs a Build using local Unity installation."}

# Prefab Command
Register { Prefab, 2, 2, "Load Create Destroy Manage prefabs."}
40 changes: 40 additions & 0 deletions Assets/Scripts/Agents/AgentUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using UnityEngine;

namespace DialogosEngine
{
public static class AgentUtils
{
public const string k_EndOfSequence = "<eos>";

public static float CalculateEchoReward(string expectedString, string guessedString)
{
// Validate input strings
if (string.IsNullOrEmpty(expectedString) || string.IsNullOrEmpty(guessedString))
{
throw new ArgumentException("Expected and guessed strings must not be null or empty.");
}
if (!expectedString.EndsWith(k_EndOfSequence))
{
throw new ArgumentException("Expected string must end with '<eos>'.");
}

// Calculate Levenshtein distance
int levenshteinDistance = Lexer.LevenshteinDistance(expectedString, guessedString);
float maxStringLength = Mathf.Max(expectedString.Length, guessedString.Length);
float similarityScore = 1f - (float)levenshteinDistance / maxStringLength;
similarityScore = (similarityScore * 2f) - 1f; // Normalize to range [-1, 1]

// Calculate length match score
float lengthDifference = Mathf.Abs(expectedString.Length - guessedString.Length);
float lengthMatchScore = 1f - Mathf.Min(2f * lengthDifference / maxStringLength, 1f);
lengthMatchScore = (lengthMatchScore * 2f) - 1f; // Normalize to range [-1, 1]

// Combine similarity and length match scores
float combinedScore = (0.5f * similarityScore) + (0.5f * lengthMatchScore);

// Ensure the final score is within the range [-1, 1]
return Mathf.Clamp(combinedScore, -1f, 1f);
}
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Agents/AgentUtils.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions Assets/Scripts/Agents/EchoAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using CommandTerminal;
using Unity.MLAgents;
using Unity.MLAgents.Actuators;
using Unity.MLAgents.Sensors;

namespace DialogosEngine
{
public class EchoAgent : Agent
{
string _CachedString;

public override void OnEpisodeBegin()
{
Terminal.Instance.Buffer.Reset();
}

public void FixedUpdate()
{
string expectedString = GetExpectedString();
if (_CachedString != null)
{
float reward = AgentUtils.CalculateEchoReward(expectedString, _CachedString);

if (_CachedString.EndsWith(AgentUtils.k_EndOfSequence))
{
_CachedString = _CachedString.Replace(AgentUtils.k_EndOfSequence, "");
Terminal.Instance.Shell.Run(_CachedString);
}

SetReward(reward);
_CachedString = null;
}
}

public override void CollectObservations(VectorSensor sensor)
{
string _buffer = Terminal.Instance.Buffer.GetLastLog();
float[] _vectorizedBuffer = Lexer.VectorizeUTF8(_buffer);
foreach (var obs in _vectorizedBuffer)
{
sensor.AddObservation(obs);
}
}

public override void OnActionReceived(ActionBuffers actions)
{
float[] _actionArray = actions.ContinuousActions.Array;
_CachedString = Lexer.QuantizeUTF8(_actionArray);
}

private string GetExpectedString()
{
return "echo hello <eos>"; // Testing
}
}
}
2 changes: 2 additions & 0 deletions Assets/Scripts/Agents/EchoAgent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2e6e5f6

Please sign in to comment.