Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions Semantics.Test/Quantities/UnkeepableRelationshipTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace ktsu.Semantics.Test.Quantities;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
Expand Down Expand Up @@ -108,8 +107,8 @@ public void TheSensitivityBugIsCaughtWithBothDimensionsWrittenOut()
refused.Subject == "Sensitivity * Pressure -> ElectricPotential");

Assert.AreEqual(VocabularyIssueKind.NotDimensionallyTrue, issue.Kind);
StringAssert.Contains(issue.Reason, "L⁻² I", StringComparison.Ordinal);
StringAssert.Contains(issue.Reason, "L² M T⁻³ I⁻¹", StringComparison.Ordinal);
Assert.Contains("L⁻² I", issue.Reason);
Assert.Contains("L² M T⁻³ I⁻¹", issue.Reason);
}

/// <summary>
Expand All @@ -119,14 +118,16 @@ public void TheSensitivityBugIsCaughtWithBothDimensionsWrittenOut()
[TestMethod]
public void TheKindsWithTheirOwnDiagnosticsAreKeptSeparate()
{
IReadOnlyList<VocabularyIssue> refused = Vocabulary().Refused;
VocabularyIssueKind[] kinds = [.. Vocabulary().Refused.Select(issue => issue.Kind)];

Assert.IsFalse(
refused.Any(issue => issue.Kind is VocabularyIssueKind.UnknownDimension),
Assert.DoesNotContain(
VocabularyIssueKind.UnknownDimension,
kinds,
"an unknown dimension is SEM001's to report, and the metadata should have none.");

Assert.IsFalse(
refused.Any(issue => issue.Kind is VocabularyIssueKind.NoMagnitudeForm),
Assert.DoesNotContain(
VocabularyIssueKind.NoMagnitudeForm,
kinds,
"every dimension should declare a magnitude form for the other forms to measure against.");
}
}
49 changes: 44 additions & 5 deletions Semantics.Vocabulary/QuantityVocabulary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,33 @@
overload.Description,
exponents,
Refines: declared.Base,
form != 0 ? Magnitude.Signed
: overload.IsStrictlyPositive ? Magnitude.Positive : Magnitude.NonNegative,
BoundOf(form, overload),
form,
magnitudeType);
}
}

/// <summary>
/// How far down an overload is bounded.
/// </summary>
/// <param name="form">How many components the form has.</param>
/// <param name="overload">The overload being declared.</param>
/// <returns>Its lower bound.</returns>
/// <remarks>
/// Only the magnitude form is bounded below at all, which is the whole reason the forms are
/// separate types: a component of a vector is signed whatever the overload asked for, so a
/// stricter floor declared on one applies to the magnitude and to nothing else.
/// </remarks>
private static Magnitude BoundOf(int form, OverloadDeclaration overload)
{
if (form != 0)
{
return Magnitude.Signed;
}

return overload.IsStrictlyPositive ? Magnitude.Positive : Magnitude.NonNegative;
}

private static string Describe(string dimension, int form) => form switch
{
0 => $"The magnitude of {Article(dimension)} {Spaced(dimension)}.",
Expand Down Expand Up @@ -333,7 +353,7 @@
{
string subject = Subject(dimension.Name, relationship, kind);

foreach (string named in (string[])[relationship.Other, relationship.Result])

Check warning on line 356 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 356 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 356 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method

Check warning on line 356 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Loops should be simplified using the "Where" LINQ method
{
if (!byDimensionName.ContainsKey(named))
{
Expand Down Expand Up @@ -401,9 +421,7 @@
List<VocabularyIssue> refused)
{
bool crossed = kind == RelationshipKind.Cross;
IReadOnlyList<int> wanted = relationship.Forms.Count > 0
? [.. relationship.Forms]
: crossed ? [3] : [.. Enumerable.Range(0, DimensionDeclaration.FormCount)];
IReadOnlyList<int> wanted = Wanted(relationship, crossed);

List<QuantityRelationship> emitted = [];

Expand Down Expand Up @@ -436,6 +454,27 @@
return emitted;
}

/// <summary>
/// The forms a relationship asks to be emitted at.
/// </summary>
/// <param name="relationship">The declared relationship.</param>
/// <param name="crossed">Whether it is a cross product.</param>
/// <returns>The forms, in the order they should be walked.</returns>
/// <remarks>
/// An explicit list is taken as it stands. With none, a cross product defaults to three
/// components and nothing else, because that is where a cross product exists; everything else
/// defaults to every form and lets the participants decide which of them it reaches.
/// </remarks>
private static IReadOnlyList<int> Wanted(RelationshipDeclaration relationship, bool crossed)
{
if (relationship.Forms.Count > 0)
{
return [.. relationship.Forms];
}

return crossed ? [3] : [.. Enumerable.Range(0, DimensionDeclaration.FormCount)];
}

private static string Missing(
IReadOnlyDictionary<string, DimensionDeclaration> declarations,
int form,
Expand Down Expand Up @@ -474,7 +513,7 @@
string.Concat(name.Select((character, index) =>
index > 0 && char.IsUpper(character) && !char.IsUpper(name[index - 1])
? $" {char.ToLowerInvariant(character)}"
: $"{(index == 0 ? char.ToLowerInvariant(character) : character)}"));

Check warning on line 516 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 516 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 516 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 516 in Semantics.Vocabulary/QuantityVocabulary.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

/// <summary>
/// Whether a dimension's name takes "an" rather than "a".
Expand Down
Loading