Skip to content

Commit aee8c1a

Browse files
authored
LT-21205: Features should obey the order set in Feature Types (#319)
* LT-21205: Sort features in LongName and make LongName and LongNameSorted use the same code
1 parent 4625513 commit aee8c1a

File tree

2 files changed

+23
-62
lines changed

2 files changed

+23
-62
lines changed

src/SIL.LCModel/DomainImpl/OverridesCellar.cs

Lines changed: 19 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,7 +2460,7 @@ public ITsString LongNameSortedTSS
24602460
{
24612461
get
24622462
{
2463-
return GetFeatureValueStringSorted();
2463+
return GetFeatureValueString(true);
24642464
}
24652465
}
24662466
/// <summary>
@@ -2494,17 +2494,6 @@ private ITsString GetFeatureValueString(bool fLongForm)
24942494
return tisb.GetString();
24952495
}
24962496

2497-
private ITsString GetFeatureValueStringSorted()
2498-
{
2499-
var tisb = TsStringUtils.MakeIncStrBldr();
2500-
tisb.SetIntPropValues((int)FwTextPropType.ktptWs,
2501-
0, Cache.DefaultAnalWs);
2502-
var sFeature = GetFeatureString(true);
2503-
tisb.Append(sFeature);
2504-
tisb.AppendTsString((ValueOA as FsFeatStruc).GetFeatureValueStringSorted());
2505-
return tisb.GetString();
2506-
}
2507-
25082497
private string GetValueString(bool fLongForm)
25092498
{
25102499
var sValue = "";
@@ -3794,7 +3783,7 @@ public ITsString LongNameSortedTSS
37943783
{
37953784
get
37963785
{
3797-
return GetFeatureValueStringSorted();
3786+
return GetFeatureValueString(true);
37983787
}
37993788
}
38003789
/// <summary>
@@ -3947,7 +3936,10 @@ private ITsString GetFeatureValueString(bool fLongForm)
39473936
tisb.Append("[");
39483937
}
39493938
var count = 0;
3950-
foreach (var spec in FeatureSpecsOC)
3939+
var sortedSpecs = from s in FeatureSpecsOC
3940+
orderby FeatureSpecKey(s)
3941+
select s;
3942+
foreach (var spec in sortedSpecs)
39513943
{
39523944
if (count++ > 0)
39533945
{
@@ -3994,56 +3986,25 @@ private ITsString GetFeatureValueString(bool fLongForm)
39943986
return tisb.GetString();
39953987
}
39963988

3997-
internal ITsString GetFeatureValueStringSorted()
3989+
private string FeatureSpecKey(IFsFeatureSpecification spec)
39983990
{
3999-
var tisb = TsStringUtils.MakeIncStrBldr();
4000-
var iCount = FeatureSpecsOC.Count;
4001-
if (iCount > 0)
3991+
// specs with features in the type go first (because they begin with 0),
3992+
// then specs with features (because they begin with 1),
3993+
// then specs without features (because they begin with 2).
3994+
if (spec.FeatureRA == null)
3995+
return "2" + spec.Guid.ToString();
3996+
if (TypeRA != null)
40023997
{
4003-
tisb.SetIntPropValues((int)FwTextPropType.ktptWs, (int)FwTextPropVar.ktpvDefault,
4004-
m_cache.DefaultAnalWs);
4005-
tisb.Append("[");
4006-
}
4007-
var count = 0;
4008-
var sortedSpecs = from s in FeatureSpecsOC
4009-
orderby s.FeatureRA.Name.BestAnalysisAlternative.Text
4010-
select s;
4011-
foreach (var spec in sortedSpecs)
4012-
{
4013-
if (count++ > 0)
4014-
{
4015-
tisb.SetIntPropValues((int)FwTextPropType.ktptWs, (int)FwTextPropVar.ktpvDefault,
4016-
m_cache.DefaultAnalWs);
4017-
tisb.Append(" "); // insert space except for first item
4018-
}
4019-
var cv = spec as IFsClosedValue;
4020-
if (cv != null)
4021-
{
4022-
tisb.AppendTsString((cv as FsClosedValue).LongNameTSS);
4023-
}
4024-
else
3998+
int index = TypeRA.FeaturesRS.IndexOf(spec.FeatureRA);
3999+
if (index >= 0)
40254000
{
4026-
var complex = spec as IFsComplexValue;
4027-
if (complex != null)
4028-
{
4029-
tisb.AppendTsString((complex as FsComplexValue).LongNameSortedTSS);
4030-
}
4001+
// This won't work properly if there are more than 999 features in TypeRA.
4002+
return index.ToString("0000");
40314003
}
40324004
}
4033-
if (iCount > 0)
4034-
{
4035-
tisb.SetIntPropValues((int)FwTextPropType.ktptWs, (int)FwTextPropVar.ktpvDefault,
4036-
m_cache.DefaultAnalWs);
4037-
tisb.Append("]");
4038-
}
4039-
if (tisb.Text == null)
4040-
{
4041-
// Ensure that we have a ws for the empty string! See FWR-1122.
4042-
tisb.SetIntPropValues((int)FwTextPropType.ktptWs, (int)FwTextPropVar.ktpvDefault,
4043-
m_cache.DefaultAnalWs);
4044-
}
4045-
return tisb.GetString();
4005+
return "1" + spec.FeatureRA.Name.BestAnalysisAlternative.Text;
40464006
}
4007+
40474008
/// <summary>
40484009
/// Provide a "Name" for this (is a long name)
40494010
/// </summary>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ public void AddComplexFeaturesToFeatureSystemAndThenToAFeatureStructure()
640640
pos.AddInflectableFeatsFromXml(itemAorist);
641641
featStruct.AddFeatureFromXml(itemAorist, msfs);
642642
// Check for correct LongName
643-
Assert.AreEqual("[sbj:[gen:f pers:1] asp:aor]", featStruct.LongName, "Incorrect LongName for complex and closed");
643+
Assert.AreEqual("[asp:aor sbj:[gen:f pers:1]]", featStruct.LongName, "Incorrect LongName for complex and closed");
644644
// Now add the features in the featurs struct in a different order
645645
pos.DefaultFeaturesOA = null;
646646
pos.DefaultFeaturesOA = Cache.ServiceLocator.GetInstance<IFsFeatStrucFactory>().Create();
@@ -649,9 +649,9 @@ public void AddComplexFeaturesToFeatureSystemAndThenToAFeatureStructure()
649649
featStruct.AddFeatureFromXml(item1st, msfs);
650650
featStruct.AddFeatureFromXml(itemFem, msfs);
651651
// check for correct short name
652-
Assert.AreEqual("aor 1 f", featStruct.ShortName, "Incorrect ShortName for complex");
652+
Assert.AreEqual("aor f 1", featStruct.ShortName, "Incorrect ShortName for complex");
653653
// Check for correct LongName
654-
Assert.AreEqual("[asp:aor sbj:[pers:1 gen:f]]", featStruct.LongName, "Incorrect LongName for complex and closed");
654+
Assert.AreEqual("[asp:aor sbj:[gen:f pers:1]]", featStruct.LongName, "Incorrect LongName for complex and closed");
655655
// Now create another feature structure with different values and merge it into the first feature structure
656656
pos.InherFeatValOA = Cache.ServiceLocator.GetInstance<IFsFeatStrucFactory>().Create();
657657
IFsFeatStruc featStruct2 = pos.InherFeatValOA;
@@ -665,7 +665,7 @@ public void AddComplexFeaturesToFeatureSystemAndThenToAFeatureStructure()
665665
featStruct2.AddFeatureFromXml(itemSg, msfs);
666666
featStruct.PriorityUnion(featStruct2);
667667
// Check for correct LongName
668-
Assert.AreEqual("[asp:aor sbj:[pers:1 gen:n num:sg]]", featStruct.LongName, "Incorrect LongName for merged feature struture");
668+
Assert.AreEqual("[asp:aor sbj:[gen:n num:sg pers:1]]", featStruct.LongName, "Incorrect LongName for merged feature struture");
669669
Assert.AreEqual("[asp:aor sbj:[gen:n num:sg pers:1]]", featStruct.LongNameSorted, "Incorrect LongNameSorted for merged feature struture");
670670
}
671671

0 commit comments

Comments
 (0)