Skip to content

Commit

Permalink
Verify output, varBindings, and annIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmaxwell3 committed Feb 7, 2025
1 parent f7d284d commit 82b15ac
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/SIL.Machine/FeatureModel/VariableBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ bool ICollection<KeyValuePair<string, SimpleFeatureValue>>.IsReadOnly
get { return false; }
}

public bool ValueEquals(VariableBindings other)
{
if (Count != other.Count)
return false;
foreach (string key in _varBindings.Keys)
{
if (!TryGetValue(key, out SimpleFeatureValue value))
return false;
if (!value.ValueEquals(_varBindings[key]))
return false;
}
return true;
}

public VariableBindings Clone()
{
return new VariableBindings(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ ISet<int> initAnns
ti = CopyInstance(inst);
if (inst.VariableBindings != null)
ti.VariableBindings = inst.VariableBindings.Clone();
RecordCommands(inst, null, null, new Register<TOffset>(), new Register<TOffset>(), ti);
}

ExecuteOutputs(arc.Outputs, ti.Output, ti.Mappings, ti.Queue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ ISet<int> initAnns
if (inst.VariableBindings != null && varBindings == null)
varBindings = inst.VariableBindings.Clone();
ti.VariableBindings = varBindings;
RecordCommands(inst, null, null, new Register<TOffset>(), new Register<TOffset>(), ti);
}

ti.Visited.Add(arc.Target);
Expand Down Expand Up @@ -98,6 +99,7 @@ ISet<int> initAnns
NondeterministicFsaTraversalInstance<TData, TOffset> ti = isInstReusable
? inst
: CopyInstance(inst);
RecordCommands(inst, null, null, new Register<TOffset>(), new Register<TOffset>(), ti);

foreach (
NondeterministicFsaTraversalInstance<TData, TOffset> newInst in Advance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ ISet<int> initAnns
if (inst.VariableBindings != null && varBindings == null)
varBindings = inst.VariableBindings.Clone();
ti.VariableBindings = varBindings;
RecordCommands(inst, null, null, new Register<TOffset>(), new Register<TOffset>(), ti);
}

if (arc.Outputs.Count == 1)
Expand Down Expand Up @@ -111,6 +112,7 @@ ISet<int> initAnns
NondeterministicFstTraversalInstance<TData, TOffset> ti = isInstReusable
? inst
: CopyInstance(inst);
RecordCommands(inst, null, null, new Register<TOffset>(), new Register<TOffset>(), ti);

if (arc.Outputs.Count == 1)
{
Expand Down
2 changes: 2 additions & 0 deletions src/SIL.Machine/FiniteState/TraversalInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public virtual void CopyTo(TraversalInstance<TData, TOffset> other)
if (_priorities != null)
other.Priorities.AddRange(_priorities);
Array.Copy(_registers, other.Registers, _registers.Length);
if (VariableBindings != null)
other.VariableBindings = VariableBindings.Clone();
}

public virtual void Clear()
Expand Down
17 changes: 14 additions & 3 deletions src/SIL.Machine/FiniteState/TraversalMethodBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ protected void RecordCommands(
TInst target
)
{
if (source == target)
return;
var commandUpdate = new CommandUpdate(source, arc, cmds, start, end);
if (!_commandUpdates.ContainsKey(target))
_commandUpdates[target] = new List<CommandUpdate>();
Expand Down Expand Up @@ -185,12 +187,15 @@ TInst inst
ExecuteCommands(matchRegisters, arc.Target.Finishers, new Register<TOffset>(), new Register<TOffset>());
TInst finalInst = CreateInstance();
RecordCommands(inst, null, arc.Target.Finishers, new Register<TOffset>(), new Register<TOffset>(), finalInst);
if (!arc.Target.IsAccepting)
if (true)
{
var outputs = GetOutputs(finalInst);
Debug.Assert(_fst.RegistersEqualityComparer.Equals(outputs[0].Registers, matchRegisters), "registers didn't match");
if (output != null)
Debug.Assert(outputs[0].Output.ToString().Equals(output.ToString()), "output didn't match");
if (varBindings != null)
Debug.Assert(varBindings.ValueEquals(inst.VariableBindings), "varBindings didn't match");
Debug.Assert(annIndex.Equals(inst.AnnotationIndex), "annIndex didn't match");
}
if (arc.Target.AcceptInfos.Count > 0)
{
Expand Down Expand Up @@ -289,7 +294,8 @@ private IList<TraverseOutput> GetOutputs(TInst inst)
foreach(TraverseOutput output in GetOutputs(update.Source))
{
var newOutput = new TraverseOutput(output);
ExecuteCommands(newOutput.Registers, update.Cmds, update.Start, update.End);
if (update.Cmds != null)
ExecuteCommands(newOutput.Registers, update.Cmds, update.Start, update.End);
if (update.Arc != null)
{
for (int j = 0; j < update.Arc.Input.EnqueueCount; j++)
Expand All @@ -307,6 +313,7 @@ private IList<TraverseOutput> GetOutputs(TInst inst)
{
Annotation<TOffset> inputAnn = newOutput.Queue.Dequeue();
outputAnn = output.Mappings[inputAnn];
outputAnn = outputAnn.Clone();
}
prevNewAnn = outputAction.UpdateOutput(newOutput.Output, outputAnn, Fst.Operations);
}
Expand Down Expand Up @@ -431,6 +438,7 @@ protected IEnumerable<TInst> Advance(
{
TInst ti = CopyInstance(inst);
ti.AnnotationIndex = i;
RecordCommands(inst, null, null, new Register<TOffset>(), new Register<TOffset>(), ti);
foreach (TInst ni in Advance(ti, varBindings, arc, curResults, true))
{
yield return ni;
Expand All @@ -456,6 +464,7 @@ protected IEnumerable<TInst> Advance(
);
if (!optional || _endAnchor)
{
inst.AnnotationIndex = nextIndex;
CheckAccepting(
nextIndex,
inst.Registers,
Expand All @@ -477,6 +486,7 @@ protected IEnumerable<TInst> Advance(
ni.AnnotationIndex = curIndex;
if (varBindings != null)
inst.VariableBindings = cloneOutputs ? varBindings.Clone() : varBindings;
RecordCommands(inst, null, null, new Register<TOffset>(), new Register<TOffset>(), ni);
yield return ni;
cloneOutputs = true;
first = false;
Expand All @@ -498,11 +508,12 @@ protected IEnumerable<TInst> Advance(
new Register<TOffset>(nextOffset, nextStart),
new Register<TOffset>(end, false)
);
CheckAccepting(nextIndex, inst.Registers, inst.Output, varBindings, arc, curResults, inst.Priorities, inst);

inst.State = arc.Target;
inst.AnnotationIndex = nextIndex;
inst.VariableBindings = varBindings;
CheckAccepting(nextIndex, inst.Registers, inst.Output, varBindings, arc, curResults, inst.Priorities, inst);

yield return inst;
}
}
Expand Down

0 comments on commit 82b15ac

Please sign in to comment.