Skip to content

Commit

Permalink
client // handle missing "dzn" section in results
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjrawlings committed Jun 19, 2024
1 parent cd19792 commit eaf980a
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 121 deletions.
2 changes: 1 addition & 1 deletion MiniZinc.Net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniZinc.Toolkit", "src\Min
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniZinc.Core", "src\MiniZinc.Core\MiniZinc.Core.csproj", "{31818B09-AFB2-4F57-8951-B8F450824427}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniZinc.Models", "src\MiniZinc.Models\MiniZinc.Models.csproj", "{A27BCE82-317A-4A05-A343-6F0609BC0EDD}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniZinc.Compiler", "src\MiniZinc.Compiler\MiniZinc.Compiler.csproj", "{A27BCE82-317A-4A05-A343-6F0609BC0EDD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
11 changes: 6 additions & 5 deletions build/Make/MakeClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ dotnet run --project ./build/Make/Make.csproj --make-client-tests
Write(solution.Command);
Write(solution.Status);
WriteSection();
var ok = false;
var ok = statuses.Length == 0;
foreach (var status in statuses){
if (solution.Status == status){
ok = true;
break;
}
}
ok.Should().BeTrue($"{solution.Status} - {solution.Text}");
ok.Should().BeTrue($"{solution.Status} - {solution.Error}");
return solution;
"""
);
Expand Down Expand Up @@ -253,14 +253,15 @@ private void MakeErrorTest(string testName, TestCase testCase)
return;
}

Var("solution", "await Solve(model, options, SolveStatus.Error)");
Var("solution", "await Solve(model, options)");
WriteLn("solution.IsError.Should().BeTrue();");
if (testCase.ErrorRegex is { } regex)
{
regex = regex.Replace("\\", "");
WriteLn($"solution.Text.Should().MatchRegex(\"{regex}\");");
WriteLn($"solution.Error.Should().MatchRegex(\"{regex}\");");
}
else if (testCase.ErrorMessage is { } error)
WriteLn($"solution.Text.Should().Be(\"{error}\");");
WriteLn($"solution.Error.Should().Be(\"{error}\");");
}

private void MakeUnsatisfiableTest(string testName, TestCase testCase)
Expand Down
2 changes: 1 addition & 1 deletion src/MiniZinc.Client/MiniZinc.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<ItemGroup>
<ProjectReference Include="..\MiniZinc.Core\MiniZinc.Core.csproj" />
<ProjectReference Include="..\MiniZinc.Models\MiniZinc.Models.csproj" />
<ProjectReference Include="..\MiniZinc.Compiler\MiniZinc.Compiler.csproj" />
<ProjectReference Include="..\MiniZinc.Parser\MiniZinc.Parser.csproj" />
<ProjectReference Include="..\MiniZinc.Command\MiniZinc.Command.csproj" />
</ItemGroup>
Expand Down
18 changes: 13 additions & 5 deletions src/MiniZinc.Client/SolveResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ public abstract record SolveResult<T>
/// </summary>
public required SolveStatus Status { get; init; }

/// <summary>
/// Text content of the message
/// </summary>
public string Text { get; init; } = string.Empty;

/// <summary>
/// Time period from the last iteration to this one
/// </summary>
Expand Down Expand Up @@ -116,6 +111,19 @@ public abstract record SolveResult<T>
/// </summary>
public required double? RelativeIterationGap { get; init; }

public bool IsSuccess => !IsError;

public bool IsError =>
Status switch
{
SolveStatus.Error => true,
SolveStatus.AssertionError => true,
SolveStatus.EvaluationError => true,
SolveStatus.SyntaxError => true,
SolveStatus.TypeError => true,
_ => false
};

/// <summary>
/// Get the solution assigned to the given variable
/// </summary>
Expand Down
61 changes: 42 additions & 19 deletions src/MiniZinc.Client/SolverProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,28 +228,43 @@ private void OnSolutionOutput(SolutionOutput o)
{
_iteration++;
_solveStatus = SolveStatus.Satisfied;
// TODO - is this always the case?
var dzn = o.Output["dzn"].ToString();
var parsed = Parser.ParseString(dzn!);
_data = new Dictionary<string, SyntaxNode>();
parsed.EnsureOk();
foreach (var node in parsed.SyntaxNode.Nodes)
if (o.Sections is not { } sections)
return;
string? dzn = null;
foreach (var section in sections)
{
if (node is not AssignmentSyntax assign)
throw new Exception();
var name = assign.Identifier.ToString();
var value = assign.Expr;
_data[name] = value;
if (section is "dzn")
dzn = o.Output[section].ToString();
}
_data.TryGetValue("_objective", out var objectiveNode);
IntOrFloat? objective = objectiveNode switch

if (dzn is not null)
{
null => null,
IntLiteralSyntax i => IntOrFloat.Int(i),
FloatLiteralSyntax f => IntOrFloat.Float((float)f.Value),
var other => throw new Exception(other.GetType().FullName)
};
_current = CreateResult(objectiveValue: objective);
var parsed = Parser.ParseString(dzn!);
_data = new Dictionary<string, SyntaxNode>();
parsed.EnsureOk();
foreach (var node in parsed.SyntaxNode.Nodes)
{
if (node is not AssignmentSyntax assign)
throw new Exception();
var name = assign.Identifier.ToString();
var value = assign.Expr;
_data[name] = value;
}

_data.TryGetValue("_objective", out var objectiveNode);
IntOrFloat? objective = objectiveNode switch
{
null => null,
IntLiteralSyntax i => IntOrFloat.Int(i),
FloatLiteralSyntax f => IntOrFloat.Float((float)f.Value),
var other => throw new Exception(other.GetType().FullName)
};
_current = CreateResult(objectiveValue: objective);
}
else
{
_current = CreateResult();
}
_channel.Writer.TryWrite(_current);
}

Expand All @@ -260,6 +275,14 @@ private void OnWarningOutput(WarningOutput o)

private void OnErrorOutput(ErrorOutput o)
{
_solveStatus = o.Kind switch
{
"SyntaxError" => SolveStatus.SyntaxError,
"TypeError" => SolveStatus.TypeError,
"AssertionError" => SolveStatus.AssertionError,
"EvaluationError" => SolveStatus.EvaluationError,
_ => SolveStatus.Error
};
_current = CreateResult(error: o.Message);
_channel.Writer.TryWrite(_current);
}
Expand Down
Loading

0 comments on commit eaf980a

Please sign in to comment.