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

[LG] support custom function #3434

Merged
merged 1 commit into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 15 additions & 4 deletions libraries/Microsoft.Bot.Builder.LanguageGeneration/LGParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using AdaptiveExpressions;
using Antlr4.Runtime;

namespace Microsoft.Bot.Builder.LanguageGeneration
Expand Down Expand Up @@ -33,13 +34,17 @@ public static class LGParser
/// </summary>
/// <param name="filePath"> absolut path of a LG file.</param>
/// <param name="importResolver">resolver to resolve LG import id to template text.</param>
/// <param name="expressionEngine">expressionEngine Expression engine for evaluating expressions.</param>
/// <returns>new <see cref="LGFile"/> entity.</returns>
public static LGFile ParseFile(string filePath, ImportResolverDelegate importResolver = null)
public static LGFile ParseFile(
string filePath,
ImportResolverDelegate importResolver = null,
ExpressionEngine expressionEngine = null)
{
var fullPath = Path.GetFullPath(filePath.NormalizePath());
var content = File.ReadAllText(fullPath);

return ParseText(content, fullPath, importResolver);
return ParseText(content, fullPath, importResolver, expressionEngine);
}

/// <summary>
Expand All @@ -48,11 +53,17 @@ public static LGFile ParseFile(string filePath, ImportResolverDelegate importRes
/// <param name="content">Text content contains lg templates.</param>
/// <param name="id">id is the identifier of content. If importResolver is null, id must be a full path string. </param>
/// <param name="importResolver">resolver to resolve LG import id to template text.</param>
/// <param name="expressionEngine">expressionEngine Expression engine for evaluating expressions.</param>
/// <returns>new <see cref="LGFile"/> entity.</returns>
public static LGFile ParseText(string content, string id = "", ImportResolverDelegate importResolver = null)
public static LGFile ParseText(
string content,
string id = "",
ImportResolverDelegate importResolver = null,
ExpressionEngine expressionEngine = null)
{
importResolver = importResolver ?? DefaultFileResolver;
var lgFile = new LGFile(content: content, id: id, importResolver: importResolver);
var lgFile = new LGFile(content: content, id: id, importResolver: importResolver, expressionEngine: expressionEngine);

var diagnostics = new List<Diagnostic>();
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ internal class StaticChecker : LGFileParserBaseVisitor<List<Diagnostic>>
/// </summary>
/// <param name="lgFile">the lgFile wihch would be checked.</param>
/// <param name="expressionEngine">Init expression engine.</param>
public StaticChecker(LGFile lgFile, ExpressionEngine expressionEngine = null)
public StaticChecker(LGFile lgFile)
{
this.lgFile = lgFile;
baseExpressionEngine = expressionEngine ?? new ExpressionEngine();
baseExpressionEngine = lgFile.ExpressionEngine;
}

// Create a property because we want this to be lazy loaded
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# template
- ${custom(1, 2)}
20 changes: 20 additions & 0 deletions tests/Microsoft.Bot.Builder.LanguageGeneration.Tests/LGFileTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AdaptiveExpressions;
using AdaptiveExpressions.Memory;
using Microsoft.Bot.Builder.LanguageGeneration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
Expand Down Expand Up @@ -984,6 +985,25 @@ public void TestInlineEvaluate()
Assert.IsTrue(exception.Message.Contains("it's not a built-in function or a customized function"));
}

[TestMethod]
public void TestCustomFunction()
{
var engine = new ExpressionEngine((string func) =>
{
if (func == "custom")
{
return ExpressionFunctions.Numeric("custom", (args) => args[0] + args[1]);
}
else
{
return ExpressionFunctions.Lookup(func);
}
});
var lgFile = LGParser.ParseFile(GetExampleFilePath("CustomFunction.lg"), null, engine);
var evaled = lgFile.EvaluateTemplate("template");
Assert.AreEqual(3, evaled);
}

public class LoopClass
{
public string Name { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<None Remove="Examples\StringInterpolation.lg" />
<None Remove="Examples\ReExecute.lg" />
<None Remove="Examples\NullTolerant.lg" />
<None Remove="Examples\CustomFunction.lg" />
<None Remove="ExceptionExamples\ConditionFormatError.lg" />
<None Remove="ExceptionExamples\DuplicatedTemplates.lg" />
<None Remove="ExceptionExamples\EmptyLGFile.lg" />
Expand Down Expand Up @@ -160,6 +161,10 @@
<Content Include="Examples\NullTolerant.lg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Examples\CustomFunction.lg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

<Content Include="ExceptionExamples\ConditionFormatError.lg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down