Skip to content

Commit 2d760ec

Browse files
missing derived properties implementation to go further
1 parent 79015e3 commit 2d760ec

26 files changed

Lines changed: 76 additions & 43 deletions

SysML2.NET.CodeGenerator/HandleBarHelpers/RulesHelper.cs

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,17 @@ private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClas
579579

580580
if (targetProperty != null)
581581
{
582-
writer.WriteSafeString($"if(poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.Count == 0){Environment.NewLine}");
582+
// Use cursor state for ';' vs '{' decision. By the time the body
583+
// is reached, previous methods (prefix, declaration) have already
584+
// consumed typing, multiplicity, etc. from the shared cursor.
585+
// GetOrCreateCursor returns the same cursor at its current position.
586+
// Use cursor state for ';' vs '{' decision. Previous methods
587+
// (prefix, declaration) have consumed typing/multiplicity from
588+
// the shared cursor via GetOrCreateCursor. If cursor.Current is
589+
// null, no body members remain → emit ';'.
590+
var bodyPropertyAccess = targetProperty.QueryPropertyNameBasedOnUmlProperties();
591+
writer.WriteSafeString($"if(cursorCache.GetOrCreateCursor(poco.Id, \"{targetProperty.Name}\", poco.{bodyPropertyAccess}).Current == null){Environment.NewLine}");
592+
583593
writer.WriteSafeString($"{{{Environment.NewLine}");
584594
writer.WriteSafeString($"stringBuilder.AppendLine(\"{terminalValue}\");{Environment.NewLine}");
585595
writer.WriteSafeString($"}}{Environment.NewLine}");
@@ -627,7 +637,8 @@ private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClas
627637
{
628638
var propertyAccessName = targetProperty.QueryPropertyNameBasedOnUmlProperties();
629639

630-
writer.WriteSafeString($"if(poco.{propertyAccessName}.Count == 0){Environment.NewLine}");
640+
writer.WriteSafeString($"if(cursorCache.GetOrCreateCursor(poco.Id, \"{targetProperty.Name}\", poco.{propertyAccessName}).Current == null){Environment.NewLine}");
641+
631642
writer.WriteSafeString($"{{{Environment.NewLine}");
632643
writer.WriteSafeString($"stringBuilder.AppendLine(\"{terminalValue}\");{Environment.NewLine}");
633644
writer.WriteSafeString($"}}{Environment.NewLine}");
@@ -721,7 +732,8 @@ private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClas
721732
{
722733
var propertyAccessName = targetProperty.QueryPropertyNameBasedOnUmlProperties();
723734

724-
writer.WriteSafeString($"if(poco.{propertyAccessName}.Count == 0){Environment.NewLine}");
735+
writer.WriteSafeString($"if(cursorCache.GetOrCreateCursor(poco.Id, \"{targetProperty.Name}\", poco.{propertyAccessName}).Current == null){Environment.NewLine}");
736+
725737
writer.WriteSafeString($"{{{Environment.NewLine}");
726738
writer.WriteSafeString($"stringBuilder.AppendLine(\"{terminalValue}\");{Environment.NewLine}");
727739
writer.WriteSafeString($"}}{Environment.NewLine}");
@@ -2846,6 +2858,27 @@ private static string ResolveContentTypeGuard(string cursorVariableName, Textual
28462858

28472859
if (complementaryProperty == null)
28482860
{
2861+
// No complementary composite property exists on this class (e.g., Usage is not
2862+
// a Relationship). Follow the rule's same-property assignment one level deeper:
2863+
// if the rule has ownedRelationship += PrefixMetadataMember, recurse into
2864+
// PrefixMetadataMember which IS a Relationship and has ownedRelatedElement.
2865+
var samePropertyAssignment = referencedRule.Alternatives
2866+
.SelectMany(alt => alt.Elements)
2867+
.OfType<AssignmentElement>()
2868+
.FirstOrDefault(a => (a.Operator == "+=" || a.Operator == "=")
2869+
&& string.Equals(a.Property, outerPropertyName, StringComparison.OrdinalIgnoreCase)
2870+
&& a.Value is NonTerminalElement);
2871+
2872+
if (samePropertyAssignment?.Value is NonTerminalElement innerNonTerminal)
2873+
{
2874+
var innerRule = ruleGenerationContext.AllRules.SingleOrDefault(x => x.RuleName == innerNonTerminal.Name);
2875+
2876+
if (innerRule != null)
2877+
{
2878+
return ResolveContentTypeGuard(cursorVariableName, innerRule, outerPropertyName, umlClass, ruleGenerationContext);
2879+
}
2880+
}
2881+
28492882
return null;
28502883
}
28512884

@@ -3088,7 +3121,7 @@ private static void DeclareCursorIfRequired(EncodedTextWriter writer, IClass uml
30883121
/// Terminals after which no trailing space should be emitted,
30893122
/// because the next element is directly adjacent (e.g., content inside angle brackets, closing angle bracket, or the <c>~</c> prefix operator).
30903123
/// </summary>
3091-
private static readonly HashSet<string> NoTrailingSpaceTerminals = ["<", ">", "~"];
3124+
private static readonly HashSet<string> NoTrailingSpaceTerminals = ["<", ">", "~", "[", "(", "#", ".", "'", "*"];
30923125

30933126
/// <summary>
30943127
/// Writes the <c>stringBuilder.Append</c> or <c>stringBuilder.AppendLine</c> call for a terminal value,
@@ -3128,7 +3161,7 @@ private static void WriteTerminalAppend(EncodedTextWriter writer, string termina
31283161
return;
31293162
}
31303163

3131-
writer.WriteSafeString($"stringBuilder.Append(\"{terminalValue}\");");
3164+
writer.WriteSafeString($"stringBuilder.Append(\"{terminalValue} \");");
31323165
}
31333166

31343167
/// <summary>

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/BindingConnectorAsUsageTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static void BuildBindingConnectorAsUsage(SysML2.NET.Core.POCO.Systems.Con
6565
}
6666
ownedRelationshipCursor.Move();
6767

68-
stringBuilder.Append("=");
68+
stringBuilder.Append("= ");
6969

7070
if (ownedRelationshipCursor.Current != null)
7171
{

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/ConnectionUsageTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static void BuildNaryConnectorPart(SysML2.NET.Core.POCO.Systems.Connectio
141141
ownedRelationshipCursor.Move();
142142

143143
}
144-
stringBuilder.Append(")");
144+
stringBuilder.Append(") ");
145145

146146
}
147147

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/ConnectorTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public static void BuildNaryConnectorDeclaration(SysML2.NET.Core.POCO.Kernel.Con
147147
ownedRelationshipCursor.Move();
148148

149149
}
150-
stringBuilder.Append(")");
150+
stringBuilder.Append(") ");
151151

152152
}
153153

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/DefinitionTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static void BuildDefinitionPrefix(SysML2.NET.Core.POCO.Systems.Definition
7373
SharedTextualNotationBuilder.BuildBasicDefinitionPrefix(poco, cursorCache, stringBuilder);
7474
}
7575
var ownedRelationshipCursor = cursorCache.GetOrCreateCursor(poco.Id, "ownedRelationship", poco.OwnedRelationship);
76-
while (ownedRelationshipCursor.Current != null && ownedRelationshipCursor.Current is SysML2.NET.Core.POCO.Root.Namespaces.IOwningMembership)
76+
while (ownedRelationshipCursor.Current is SysML2.NET.Core.POCO.Root.Namespaces.IOwningMembership owningMembershipGuard && owningMembershipGuard.OwnedRelatedElement.OfType<SysML2.NET.Core.POCO.Systems.Metadata.IMetadataUsage>().Any())
7777
{
7878
BuildDefinitionExtensionKeyword(poco, cursorCache, stringBuilder);
7979
}

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/ElementFilterMembershipTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static void BuildFilterPackageMember(SysML2.NET.Core.POCO.Kernel.Packages
8383
}
8484
ownedRelatedElementCursor.Move();
8585

86-
stringBuilder.Append("]");
86+
stringBuilder.Append("] ");
8787

8888
}
8989
}

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/EnumerationDefinitionTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static partial class EnumerationDefinitionTextualNotationBuilder
4343
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
4444
public static void BuildEnumerationBody(SysML2.NET.Core.POCO.Systems.Enumerations.IEnumerationDefinition poco, ICursorCache cursorCache, StringBuilder stringBuilder)
4545
{
46-
if (poco.OwnedRelationship.Count == 0)
46+
if (cursorCache.GetOrCreateCursor(poco.Id, "ownedRelationship", poco.OwnedRelationship).Current == null)
4747
{
4848
stringBuilder.AppendLine(";");
4949
}

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/ExpressionTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static void BuildSequenceExpression(SysML2.NET.Core.POCO.Kernel.Functions
141141
{
142142
stringBuilder.Append("(");
143143
BuildSequenceExpressionList(poco, cursorCache, stringBuilder);
144-
stringBuilder.Append(")");
144+
stringBuilder.Append(") ");
145145

146146
}
147147

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/FeatureTextualNotationBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public static void BuildTypings(SysML2.NET.Core.POCO.Core.Features.IFeature poco
140140
public static void BuildTypedBy(SysML2.NET.Core.POCO.Core.Features.IFeature poco, ICursorCache cursorCache, StringBuilder stringBuilder)
141141
{
142142
var ownedRelationshipCursor = cursorCache.GetOrCreateCursor(poco.Id, "ownedRelationship", poco.OwnedRelationship);
143-
stringBuilder.Append(":");
143+
stringBuilder.Append(": ");
144144

145145
if (ownedRelationshipCursor.Current != null)
146146
{
@@ -1033,7 +1033,7 @@ public static void BuildArgumentList(SysML2.NET.Core.POCO.Core.Features.IFeature
10331033
break;
10341034
}
10351035

1036-
stringBuilder.Append(")");
1036+
stringBuilder.Append(") ");
10371037

10381038
}
10391039

@@ -1138,7 +1138,7 @@ public static void BuildNamedArgument(SysML2.NET.Core.POCO.Core.Features.IFeatur
11381138
}
11391139
ownedRelationshipCursor.Move();
11401140

1141-
stringBuilder.Append("=");
1141+
stringBuilder.Append("= ");
11421142

11431143
if (ownedRelationshipCursor.Current != null)
11441144
{

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/IndexExpressionTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static void BuildIndexExpression(SysML2.NET.Core.POCO.Kernel.Expressions.
6868
}
6969
ownedRelationshipCursor.Move();
7070

71-
stringBuilder.Append(")");
71+
stringBuilder.Append(") ");
7272

7373
}
7474
}

0 commit comments

Comments
 (0)