Skip to content

Commit ae1ce23

Browse files
authored
P8: enforce exact MMS leaf kinds for structured FCD Report projection
P8 reporting: reject type-conflicting structured MMS FCD leaves before scalar projection
2 parents 5923029 + 72e66c2 commit ae1ce23

2 files changed

Lines changed: 140 additions & 3 deletions

File tree

‎src/AR.Iec61850/Mms/MmsSemanticReportValueProjector.cs‎

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public static MmsReportSemanticProjectionContext Create(LiveIedModelDiscoveryDoc
6363
accumulated = $"{accumulated}.{part}";
6464
current = current.GetOrAdd(part, accumulated);
6565
}
66+
// Only a type backed by exact model evidence can veto a
67+
// wire leaf. Partial discovery must remain compatible.
68+
current.ObserveExactType(ExactMmsKind(attribute));
6669
}
6770

6871
if (root.Children.Count == 0)
@@ -194,6 +197,16 @@ private static bool TryFlatten(
194197
return false;
195198
}
196199

200+
if (schema.HasTypeConflict)
201+
{
202+
reason = $"typed schema conflict for {schema.Reference}: exact model sources disagree";
203+
return false;
204+
}
205+
if (schema.ExactKind is MmsDataKind expected && expected != value.Kind)
206+
{
207+
reason = $"typed MMS leaf mismatch at {schema.Reference}: expected {expected}, received {value.Kind}";
208+
return false;
209+
}
197210
leaves.Add(new ExpandedLeaf(schema.Reference, value));
198211
reason = string.Empty;
199212
return true;
@@ -253,6 +266,30 @@ private static string NormalizeDataSetReference(string value)
253266
return normalized[..(slash + 1)] + normalized[(slash + 1)..].Replace('$', '.');
254267
}
255268

269+
// Strictly known, model-qualified MMS kinds only. SCL bType alone is not
270+
// live MMS type authority; unknown/partial types retain existing fallback.
271+
private static MmsDataKind? ExactMmsKind(LiveIedDataAttributeModel attribute)
272+
{
273+
if (attribute.TypeConfidence != LiveIedDiscoveryConfidenceLevel.Exact ||
274+
string.IsNullOrWhiteSpace(attribute.MmsType))
275+
return null;
276+
277+
return attribute.MmsType.Trim().ToLowerInvariant().Replace('_', '-') switch
278+
{
279+
"floating-point" or "floatingpoint" => MmsDataKind.FloatingPoint,
280+
"bit-string" or "bitstring" => MmsDataKind.BitString,
281+
"utc-time" or "utctime" => MmsDataKind.UtcTime,
282+
"boolean" => MmsDataKind.Boolean,
283+
"integer" or "signed-integer" => MmsDataKind.Integer,
284+
"unsigned" or "unsigned-integer" => MmsDataKind.Unsigned,
285+
"octet-string" or "octetstring" => MmsDataKind.OctetString,
286+
"visible-string" or "visiblestring" => MmsDataKind.VisibleString,
287+
"mms-string" or "mmsstring" => MmsDataKind.MmsString,
288+
"binary-time" or "binarytime" => MmsDataKind.BinaryTime,
289+
_ => null
290+
};
291+
}
292+
256293
private sealed class SchemaNode
257294
{
258295
private readonly Dictionary<string, SchemaNode> _byName = new(StringComparer.OrdinalIgnoreCase);
@@ -263,6 +300,18 @@ public SchemaNode(string reference)
263300

264301
public string Reference { get; }
265302
public IReadOnlyList<SchemaNode> Children => _children;
303+
public MmsDataKind? ExactKind { get; private set; }
304+
public bool HasTypeConflict { get; private set; }
305+
306+
public void ObserveExactType(MmsDataKind? kind)
307+
{
308+
if (!kind.HasValue)
309+
return;
310+
if (ExactKind.HasValue && ExactKind != kind)
311+
HasTypeConflict = true;
312+
else
313+
ExactKind = kind;
314+
}
266315

267316
public SchemaNode GetOrAdd(string name, string reference)
268317
{
@@ -302,6 +351,7 @@ public static MmsReportValueProjection Project(
302351

303352
var baseline = MmsReportValueProjector.Project(frame);
304353
var semanticReplacementPositions = new HashSet<int>();
354+
var rejectedTypedPositions = new HashSet<int>();
305355
var semanticUpdates = new List<MmsReportSignalUpdate>();
306356
var semanticWarnings = new List<string>();
307357

@@ -323,7 +373,17 @@ public static MmsReportValueProjection Project(
323373
out var resolvedMemberReference,
324374
out var expansionReason))
325375
{
326-
if (baselineWasRaw)
376+
// A positively contradictory exact MMS type is not permission
377+
// for the generic positional fallback to invent process values.
378+
// Preserve the original frame and explicit rejection evidence,
379+
// but exclude this member's candidate value/quality/timestamp.
380+
if (expansionReason.StartsWith("typed MMS leaf mismatch", StringComparison.Ordinal) ||
381+
expansionReason.StartsWith("typed schema conflict", StringComparison.Ordinal))
382+
{
383+
rejectedTypedPositions.Add(valuePosition);
384+
semanticWarnings.Add($"REPORT_SEMANTIC_REJECTED: {reportedMemberReference}; {expansionReason}; no scalar projection from this member.");
385+
}
386+
else if (baselineWasRaw)
327387
{
328388
semanticWarnings.Add($"REPORT_SEMANTIC_FALLBACK: {reportedMemberReference} remained raw; {expansionReason}. Exact scalar MMS fallback remains eligible.");
329389
}
@@ -369,7 +429,7 @@ public static MmsReportValueProjection Project(
369429
semanticWarnings.Add($"REPORT_SEMANTIC_STRUCT: {resolvedMemberReference} {expansionReason}; exact static DataSet schema overrode generic structured-value heuristics.");
370430
}
371431

372-
if (semanticReplacementPositions.Count == 0)
432+
if (semanticReplacementPositions.Count == 0 && rejectedTypedPositions.Count == 0)
373433
{
374434
return new MmsReportValueProjection
375435
{
@@ -383,7 +443,8 @@ public static MmsReportValueProjection Project(
383443
ReceivedAt = frame.ReceivedAt,
384444
Header = frame.Header,
385445
Values = frame.Values
386-
.Where((_, index) => !semanticReplacementPositions.Contains(index))
446+
.Where((_, index) => !semanticReplacementPositions.Contains(index) &&
447+
!rejectedTypedPositions.Contains(index))
387448
.ToArray(),
388449
DecoderMode = frame.DecoderMode,
389450
Message = frame.Message

‎tests/AR.Iec61850.Tests/Mms/MmsSemanticReportValueProjectorTests.cs‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,82 @@ public void C264_Incomplete_Live_Schema_Still_Projects_Strictly_Typed_Mod_FCD()
292292
x => x.Value.StartsWith("Structure(", StringComparison.OrdinalIgnoreCase));
293293
}
294294

295+
[Fact]
296+
public void ExactTypedFcd_SwappedQualityAndTimeIsRejectedWithoutFalseValuePublication()
297+
{
298+
const string root = "C264MEAS/MMXU1.A";
299+
const string dataSet = "C264MASTER/LLN0.Analog";
300+
var model = BuildMeasurementPairModel(root, dataSet);
301+
var when = new DateTimeOffset(2026, 10, 20, 10, 0, 0, TimeSpan.Zero);
302+
var invalid = BuildFrame(root, dataSet,
303+
MmsDataValue.Structure(new[] { MmsDataValue.FloatingPoint(12f) }),
304+
MmsDataValue.Structure(new[] { MmsDataValue.FloatingPoint(13f) }),
305+
MmsDataValue.UtcTime(new Iec61850UtcTime(when, 0)),
306+
MmsDataValue.BitString(3, new byte[] { 0 }));
307+
var result = MmsSemanticReportValueProjector.Project(
308+
invalid, MmsReportSemanticProjectionContext.Create(model));
309+
310+
Assert.Empty(result.Updates);
311+
Assert.Contains(result.Warnings,
312+
warning => warning.StartsWith("REPORT_SEMANTIC_REJECTED:", StringComparison.Ordinal) &&
313+
warning.Contains("expected BitString, received UtcTime", StringComparison.Ordinal));
314+
// The original received frame remains available to diagnosis.
315+
Assert.Equal(4, invalid.Values[0].Value!.Children.Count);
316+
}
317+
318+
[Fact]
319+
public void ExactTypedFcd_ValidOrderedQualityAndTimestampRetainProcessProjection()
320+
{
321+
const string root = "C264MEAS/MMXU1.A";
322+
const string dataSet = "C264MASTER/LLN0.Analog";
323+
var when = new DateTimeOffset(2026, 10, 20, 10, 0, 0, TimeSpan.Zero);
324+
var valid = BuildFrame(root, dataSet,
325+
MmsDataValue.Structure(new[] { MmsDataValue.FloatingPoint(12f) }),
326+
MmsDataValue.Structure(new[] { MmsDataValue.FloatingPoint(13f) }),
327+
MmsDataValue.BitString(3, new byte[] { 0 }),
328+
MmsDataValue.UtcTime(new Iec61850UtcTime(when, 0)));
329+
var result = MmsSemanticReportValueProjector.Project(
330+
valid, MmsReportSemanticProjectionContext.Create(BuildMeasurementPairModel(root, dataSet)));
331+
332+
Assert.Contains(result.Updates, x => x.Reference == root + ".instMag.f" && x.HasValue);
333+
Assert.Contains(result.Updates, x => x.Reference == root + ".mag.f" && x.HasValue);
334+
Assert.DoesNotContain(result.Warnings, x => x.StartsWith("REPORT_SEMANTIC_REJECTED:", StringComparison.Ordinal));
335+
}
336+
337+
[Fact]
338+
public void ConflictingExactSchemaAndPartialUnknownTypesStayDeterministic()
339+
{
340+
const string root = "C264MEAS/MMXU1.A";
341+
const string dataSet = "C264MASTER/LLN0.Analog";
342+
var contradiction = BuildModel(root, dataSet, "MV", new[]
343+
{
344+
Attribute(root + ".instMag.f", "instMag.f", "FLOAT32", "floating-point"),
345+
Attribute(root + ".instMag.f", "instMag.f", "BOOLEAN", "boolean")
346+
});
347+
var frame = BuildFrame(root, dataSet,
348+
MmsDataValue.Structure(new[] { MmsDataValue.FloatingPoint(12f) }));
349+
var rejected = MmsSemanticReportValueProjector.Project(
350+
frame, MmsReportSemanticProjectionContext.Create(contradiction));
351+
Assert.Empty(rejected.Updates);
352+
Assert.Contains(rejected.Warnings, x => x.StartsWith("REPORT_SEMANTIC_REJECTED:", StringComparison.Ordinal));
353+
354+
var unknown = BuildModel(root, dataSet, "MV", new[]
355+
{
356+
new LiveIedDataAttributeModel
357+
{
358+
ObjectReference = root + ".instMag.f",
359+
AttributePath = "instMag.f",
360+
FunctionalConstraint = "MX",
361+
MmsType = "",
362+
TypeConfidence = LiveIedDiscoveryConfidenceLevel.Unknown
363+
}
364+
});
365+
var fallback = MmsSemanticReportValueProjector.Project(
366+
frame, MmsReportSemanticProjectionContext.Create(unknown));
367+
Assert.Contains(fallback.Updates, x => x.Reference == root + ".instMag.f");
368+
Assert.DoesNotContain(fallback.Warnings, x => x.StartsWith("REPORT_SEMANTIC_REJECTED:", StringComparison.Ordinal));
369+
}
370+
295371
private static LiveIedModelDiscoveryDocument BuildThreePhaseModel(
296372
string objectReference,
297373
string dataSetReference)

0 commit comments

Comments
 (0)