Skip to content

Commit 66263ab

Browse files
authored
LT-22313: Add FillInBlanks and ContainsBlank (#356)
* Make a minor change to force a new commit to nuget * Fix LT-22313: Add FillInBlanks * Add ContainsBlank * Change ShortName for blank values to include name of attribute * Undo edit to comment * Change blank values from name:??? to name:* in FsClosedValue
1 parent 5dcb85f commit 66263ab

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

src/SIL.LCModel/DomainImpl/OverridesCellar.cs

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,8 +2247,9 @@ public ITsString GetFeatureValueString(bool fLongForm)
22472247
var tisb = TsStringUtils.MakeIncStrBldr();
22482248
tisb.SetIntPropValues((int)FwTextPropType.ktptWs, (int)FwTextPropVar.ktpvDefault, Cache.DefaultUserWs);
22492249

2250-
var sFeature = GetFeatureString(fLongForm);
2251-
var sValue = GetValueString(fLongForm);
2250+
// ValueRA == null is an empty value that is filled in by FillInBlanks.
2251+
var sFeature = GetFeatureString(fLongForm || ValueRA == null);
2252+
var sValue = ValueRA == null ? "*" : GetValueString(fLongForm);
22522253
if ((!fLongForm) &&
22532254
(FeatureRA != null) &&
22542255
(FeatureRA.DisplayToRightOfValues))
@@ -3871,8 +3872,8 @@ from myitem in FeatureSpecsOC
38713872
else
38723873
{
38733874
if (myFeatureValues.First() is IFsComplexValue complex &&
3874-
spec is IFsComplexValue newComplexValue &&
3875-
complex.ValueOA is IFsFeatStruc fs)
3875+
spec is IFsComplexValue newComplexValue &&
3876+
complex.ValueOA is IFsFeatStruc fs)
38763877
{
38773878
fs.PriorityUnion(newComplexValue.ValueOA as IFsFeatStruc);
38783879
}
@@ -3885,6 +3886,87 @@ spec is IFsComplexValue newComplexValue &&
38853886
}
38863887

38873888
}
3889+
3890+
/// <summary>
3891+
/// Fill in the blanks.
3892+
/// Remove unfilled blanks and empty feature structures.
3893+
/// </summary>
3894+
/// <param name="fsValue">the values to fill with</param>
3895+
public IFsFeatStruc FillInBlanks(IFsFeatStruc fsValue)
3896+
{
3897+
foreach (IFsFeatureSpecification spec in FeatureSpecsOC.ToList())
3898+
{
3899+
// Find matching specification.
3900+
IFsFeatureSpecification spec2 = null;
3901+
if (fsValue != null)
3902+
{
3903+
foreach (var spec3 in fsValue.FeatureSpecsOC)
3904+
{
3905+
if (spec3.FeatureRA.Name == spec.FeatureRA.Name)
3906+
{
3907+
spec2 = spec3;
3908+
}
3909+
}
3910+
}
3911+
// Fill in blanks in spec.
3912+
if (spec is IFsClosedValue closed && closed != null && closed.ValueRA == null)
3913+
{
3914+
if (spec2 is IFsClosedValue closed2 && closed2 != null)
3915+
{
3916+
// Fill in blank.
3917+
closed.ValueRA = closed2.ValueRA;
3918+
3919+
}
3920+
if (closed.ValueRA == null)
3921+
{
3922+
// Remove unfilled blank.
3923+
FeatureSpecsOC.Remove(spec);
3924+
}
3925+
}
3926+
if (spec is IFsComplexValue complex && complex != null)
3927+
{
3928+
// Recursively fill in blanks.
3929+
if (complex.ValueOA is IFsFeatStruc fs)
3930+
{
3931+
IFsComplexValue complex2 = spec2 as IFsComplexValue;
3932+
if (fs.FillInBlanks(complex2?.ValueOA as IFsFeatStruc) == null)
3933+
{
3934+
// Remove empty feature structure.
3935+
FeatureSpecsOC.Remove(complex);
3936+
}
3937+
}
3938+
}
3939+
}
3940+
// See if removing blanks emptied the feature structure.
3941+
if (FeatureSpecsOC.Count == 0)
3942+
{
3943+
return null;
3944+
}
3945+
return this;
3946+
}
3947+
3948+
public bool ContainsBlank()
3949+
{
3950+
foreach (IFsFeatureSpecification spec in FeatureSpecsOC.ToList())
3951+
{
3952+
if (spec is IFsClosedValue closed && closed != null && closed.ValueRA == null)
3953+
{
3954+
return true;
3955+
}
3956+
if (spec is IFsComplexValue complex && complex != null)
3957+
{
3958+
if (complex.ValueOA is IFsFeatStruc fs)
3959+
{
3960+
if (fs.ContainsBlank())
3961+
{
3962+
return true;
3963+
}
3964+
}
3965+
}
3966+
}
3967+
return false;
3968+
}
3969+
38883970
protected override void RemoveObjectSideEffectsInternal(RemoveObjectEventArgs e)
38893971
{
38903972
switch (e.Flid)

src/SIL.LCModel/InterfaceAdditions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,19 @@ ITsString LongNameSortedTSS
23232323
/// </summary>
23242324
/// <param name="fsNew">the new feature structure</param>
23252325
void PriorityUnion(IFsFeatStruc fsNew);
2326+
2327+
/// <summary>
2328+
/// Fill in the blanks of this feature structure
2329+
/// using the given values.
2330+
/// Eliminates blanks with no values and returns null if there is nothing left.
2331+
/// </summary>
2332+
/// <param name="fsValues">feature structure with given values</param>
2333+
IFsFeatStruc FillInBlanks(IFsFeatStruc fsValues);
2334+
2335+
/// <summary>
2336+
/// Determine if this feature structure contains a blank.
2337+
/// </summary>
2338+
public bool ContainsBlank();
23262339
}
23272340

23282341
/// <summary>

tests/SIL.LCModel.Tests/DomainImpl/CellarTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,22 @@ public void AddClosedFeaturesToFeatureSystemAndThenToAFeatureStructure()
459459
Assert.AreEqual("Gen", cv.FeatureRA.Abbreviation.AnalysisDefaultWritingSystem.Text, "Expect to have Gen feature name");
460460
Assert.AreEqual("Neut", cv.ValueRA.Abbreviation.AnalysisDefaultWritingSystem.Text, "Expect to have Neut feature value");
461461
}
462+
463+
// Test FillInBlanks.
464+
IFsClosedValue closedValue = featStruct.FeatureSpecsOC.First() as IFsClosedValue;
465+
closedValue.ValueRA = null;
466+
Assert.AreEqual("Gen:*", featStruct.ShortName);
467+
Assert.IsTrue(featStruct.ContainsBlank());
468+
Assert.IsFalse(featStrucGenNeut.ContainsBlank());
469+
featStruct = featStruct.FillInBlanks(featStrucGenNeut);
470+
Assert.IsFalse(featStruct.ContainsBlank());
471+
Assert.AreEqual("Agr", featStruct.TypeRA.Abbreviation.AnalysisDefaultWritingSystem.Text, "Expect type Agr");
472+
Assert.AreEqual(1, featStruct.FeatureSpecsOC.Count, "should have one feature spec");
473+
foreach (IFsClosedValue cv in featStruct.FeatureSpecsOC)
474+
{
475+
Assert.AreEqual("Gen", cv.FeatureRA.Abbreviation.AnalysisDefaultWritingSystem.Text, "Expect to have Gen feature name");
476+
Assert.AreEqual("Neut", cv.ValueRA.Abbreviation.AnalysisDefaultWritingSystem.Text, "Expect to have Neut feature value");
477+
}
462478
}
463479

464480
/// ------------------------------------------------------------------------------------
@@ -667,6 +683,31 @@ public void AddComplexFeaturesToFeatureSystemAndThenToAFeatureStructure()
667683
// Check for correct LongName
668684
Assert.AreEqual("[asp:aor sbj:[gen:n num:sg pers:1]]", featStruct.LongName, "Incorrect LongName for merged feature struture");
669685
Assert.AreEqual("[asp:aor sbj:[gen:n num:sg pers:1]]", featStruct.LongNameSorted, "Incorrect LongNameSorted for merged feature struture");
686+
687+
// Test FillInBlanks.
688+
pos.DefaultFeaturesOA = null;
689+
pos.DefaultFeaturesOA = Cache.ServiceLocator.GetInstance<IFsFeatStrucFactory>().Create();
690+
featStruct = pos.DefaultFeaturesOA;
691+
featStruct.AddFeatureFromXml(itemFem, msfs);
692+
IFsComplexValue complexValue = featStruct.FeatureSpecsOC.First() as IFsComplexValue;
693+
IFsFeatStruc fsValue = complexValue.ValueOA as IFsFeatStruc;
694+
IFsClosedValue closedValue = fsValue.FeatureSpecsOC.First() as IFsClosedValue;
695+
closedValue.ValueRA = null;
696+
Assert.AreEqual("gen:*", featStruct.ShortName);
697+
Assert.IsTrue(featStruct.ContainsBlank());
698+
Assert.IsFalse(featStruct2.ContainsBlank());
699+
featStruct = featStruct.FillInBlanks(featStruct2);
700+
Assert.IsFalse(featStruct2.ContainsBlank());
701+
Assert.AreEqual("[sbj:[gen:n]]", featStruct.LongName, "Incorrect LongName for merged feature struture");
702+
703+
// Test removing FillInBlanks.
704+
closedValue.ValueRA = null;
705+
featStruct2.FeatureSpecsOC.Remove(featStruct2.FeatureSpecsOC.First());
706+
Assert.AreEqual("gen:*", featStruct.ShortName);
707+
Assert.IsTrue(featStruct.ContainsBlank());
708+
Assert.IsFalse(featStruct2.ContainsBlank());
709+
featStruct = featStruct.FillInBlanks(featStruct2);
710+
Assert.AreEqual(null, featStruct, "FillInBlanks didn't remove unfilled blanks");
670711
}
671712

672713

0 commit comments

Comments
 (0)