Skip to content

Commit a27b1b4

Browse files
[WIP] Correctly implemented reference assignement
1 parent a312e31 commit a27b1b4

File tree

3 files changed

+54
-77
lines changed

3 files changed

+54
-77
lines changed

SysML2.NET.CodeGenerator.Tests/Generators/UmlHandleBarsGenerators/UmlCoreTextualNotationBuilderGeneratorTestFixture.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ namespace SysML2.NET.CodeGenerator.Tests.Generators.UmlHandleBarsGenerators
3030
using SysML2.NET.CodeGenerator.Grammar;
3131
using SysML2.NET.CodeGenerator.Grammar.Model;
3232

33+
using uml4net.CommonStructure;
34+
3335
[TestFixture]
3436
public class UmlCoreTextualNotationBuilderGeneratorTestFixture
3537
{

SysML2.NET.CodeGenerator/HandleBarHelpers/RulesHelper.cs

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static void RegisterRulesHelper(this IHandlebars handlebars)
6969

7070
if (namedElement is IClass umlClass)
7171
{
72-
var ruleGenerationContext = new RuleGenerationContext();
72+
var ruleGenerationContext = new RuleGenerationContext(namedElement);
7373
ruleGenerationContext.AllRules.AddRange(allRules);
7474
ProcessAlternatives(writer, umlClass, textualRule.Alternatives, ruleGenerationContext);
7575
}
@@ -93,7 +93,7 @@ private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClas
9393
var elements = alternative.Elements;
9494
DeclareAllRequiredIterators(writer, umlClass, alternative, ruleGenerationContext);
9595

96-
if (ruleGenerationContext.CallerContext?.CallerRule is { IsOptional: true, IsCollection: false })
96+
if (ruleGenerationContext.CallerRule is { IsOptional: true, IsCollection: false })
9797
{
9898
var targetPropertiesName = elements.OfType<AssignmentElement>().Select(x => x.Property).Distinct().ToList();
9999
var allProperties = umlClass.QueryAllProperties();
@@ -127,9 +127,9 @@ private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClas
127127

128128
foreach (var textualRuleElement in elements)
129129
{
130-
var previousCaller = ruleGenerationContext.CallerContext;
130+
var previousCaller = ruleGenerationContext.CallerRule;
131131
ProcessRuleElement(writer, umlClass, textualRuleElement, ruleGenerationContext);
132-
ruleGenerationContext.CallerContext = previousCaller;
132+
ruleGenerationContext.CallerRule = previousCaller;
133133
}
134134
}
135135
else
@@ -150,9 +150,9 @@ private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClas
150150
{
151151
foreach (var textualRuleElement in elements)
152152
{
153-
var previousCaller = ruleGenerationContext.CallerContext;
153+
var previousCaller = ruleGenerationContext.CallerRule;
154154
ProcessRuleElement(writer, umlClass, textualRuleElement, ruleGenerationContext);
155-
ruleGenerationContext.CallerContext = previousCaller;
155+
ruleGenerationContext.CallerRule = previousCaller;
156156
}
157157
}
158158
}
@@ -215,18 +215,20 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
215215
writer.WriteSafeString($"stringBuilder.Append(\"{valueToAdd}\");");
216216
break;
217217
case NonTerminalElement nonTerminalElement:
218-
if (ruleGenerationContext.CallerContext?.CallerRule is NonTerminalElement callerNonTerminalElement && callerNonTerminalElement.Container is AssignmentElement)
218+
if (ruleGenerationContext.CallerRule is NonTerminalElement { Container: AssignmentElement assignmentElementContainer })
219219
{
220-
ProcessNonTerminalElement(writer, umlClass, nonTerminalElement, "poco", ruleGenerationContext.CallerContext.CallerClassContext, ruleGenerationContext);
220+
var textualBuilderClass = ruleGenerationContext.NamedElementToGenerate as IClass;
221+
var assignedProperty = textualBuilderClass.QueryAllProperties().SingleOrDefault(x => x.Name == assignmentElementContainer.Property);
222+
ProcessNonTerminalElement(writer, umlClass, nonTerminalElement, assignedProperty == null ? "poco" : $"poco.{assignedProperty.QueryPropertyNameBasedOnUmlProperties()}", ruleGenerationContext);
221223
}
222224
else
223225
{
224-
ProcessNonTerminalElement(writer, umlClass, nonTerminalElement, "poco", umlClass, ruleGenerationContext);
226+
ProcessNonTerminalElement(writer, umlClass, nonTerminalElement, "poco", ruleGenerationContext);
225227
}
226228

227229
break;
228230
case GroupElement groupElement:
229-
ruleGenerationContext.CallerContext = new CallerContext(groupElement);
231+
ruleGenerationContext.CallerRule = groupElement ;
230232

231233
if (groupElement.IsCollection)
232234
{
@@ -270,7 +272,7 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
270272
writer.WriteSafeString($"{iteratorToUse.IteratorVariableName}.MoveNext();{Environment.NewLine}");
271273
}
272274

273-
ProcessNonTerminalElement(writer, umlClass, nonTerminalElement, $"{iteratorToUse.IteratorVariableName}.Current", umlClass, ruleGenerationContext);
275+
ProcessNonTerminalElement(writer, umlClass, nonTerminalElement, $"{iteratorToUse.IteratorVariableName}.Current", ruleGenerationContext);
274276
}
275277
else
276278
{
@@ -311,10 +313,10 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
311313
{
312314
if (assignmentElement.Value is NonTerminalElement nonTerminalElement)
313315
{
314-
var previousCaller = ruleGenerationContext.CallerContext;
315-
ruleGenerationContext.CallerContext = new CallerContext(nonTerminalElement);
316-
ProcessNonTerminalElement(writer, targetProperty.Type as IClass, nonTerminalElement, $"poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}", umlClass, ruleGenerationContext);
317-
ruleGenerationContext.CallerContext = previousCaller;
316+
var previousCaller = ruleGenerationContext.CallerRule;
317+
ruleGenerationContext.CallerRule = nonTerminalElement;
318+
ProcessNonTerminalElement(writer, targetProperty.Type as IClass, nonTerminalElement, $"poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}", ruleGenerationContext);
319+
ruleGenerationContext.CallerRule = previousCaller;
318320
}
319321
else
320322
{
@@ -355,9 +357,8 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
355357
/// <param name="umlClass">The related <see cref="IClass"/></param>
356358
/// <param name="nonTerminalElement">The <see cref="NonTerminalElement"/> to process</param>
357359
/// <param name="variableName">The name of the variable that should be used to call the non-terminal method</param>
358-
/// <param name="callerClass">Gets the <see cref="IClass"/> that initially calls this function</param>
359360
/// <param name="ruleGenerationContext"></param>
360-
private static void ProcessNonTerminalElement(EncodedTextWriter writer, IClass umlClass, NonTerminalElement nonTerminalElement, string variableName, IClass callerClass, RuleGenerationContext ruleGenerationContext)
361+
private static void ProcessNonTerminalElement(EncodedTextWriter writer, IClass umlClass, NonTerminalElement nonTerminalElement, string variableName, RuleGenerationContext ruleGenerationContext)
361362
{
362363
var referencedRule = ruleGenerationContext.AllRules.SingleOrDefault(x => x.RuleName == nonTerminalElement.Name);
363364

@@ -380,7 +381,7 @@ private static void ProcessNonTerminalElement(EncodedTextWriter writer, IClass u
380381
writer.WriteSafeString($"{{{Environment.NewLine}");
381382
}
382383

383-
if (typeTarget != callerClass.Name)
384+
if (typeTarget != ruleGenerationContext.NamedElementToGenerate.Name)
384385
{
385386
var targetType = umlClass.Cache.Values.OfType<INamedElement>().SingleOrDefault(x => x.Name == typeTarget);
386387

@@ -392,18 +393,18 @@ private static void ProcessNonTerminalElement(EncodedTextWriter writer, IClass u
392393
}
393394
else
394395
{
395-
var previousCaller = ruleGenerationContext.CallerContext;
396-
ruleGenerationContext.CallerContext = new CallerContext(nonTerminalElement, targetType as IClass);
397-
ProcessAlternatives(writer, callerClass, referencedRule?.Alternatives, ruleGenerationContext);
398-
ruleGenerationContext.CallerContext = previousCaller;
396+
var previousCaller = ruleGenerationContext.CallerRule;
397+
ruleGenerationContext.CallerRule = nonTerminalElement;
398+
ProcessAlternatives(writer, umlClass, referencedRule?.Alternatives, ruleGenerationContext);
399+
ruleGenerationContext.CallerRule = previousCaller;
399400
}
400401
}
401402
else
402403
{
403-
var previousCaller = ruleGenerationContext.CallerContext;
404-
ruleGenerationContext.CallerContext = new CallerContext(nonTerminalElement);
405-
ProcessAlternatives(writer, callerClass, referencedRule?.Alternatives, ruleGenerationContext);
406-
ruleGenerationContext.CallerContext = previousCaller;
404+
var previousCaller = ruleGenerationContext.CallerRule;
405+
ruleGenerationContext.CallerRule = nonTerminalElement;
406+
ProcessAlternatives(writer, umlClass, referencedRule?.Alternatives, ruleGenerationContext);
407+
ruleGenerationContext.CallerRule = previousCaller;
407408
}
408409
}
409410
else
@@ -559,29 +560,6 @@ public bool IsIteratorValidForProperty(IProperty property, TextualNotationRule t
559560
}
560561
}
561562

562-
/// <summary>
563-
/// Provide context about the caller context
564-
/// </summary>
565-
private class CallerContext
566-
{
567-
/// <summary>Initializes a new instance of the <see cref="CallerContext" /> class.</summary>
568-
public CallerContext(RuleElement callerRule, IClass callerClassContext = null)
569-
{
570-
this.CallerRule = callerRule;
571-
this.CallerClassContext = callerClassContext;
572-
}
573-
574-
/// <summary>
575-
/// Gets or sets the <see cref="RuleElement"/> that called other rule
576-
/// </summary>
577-
public RuleElement CallerRule { get; }
578-
579-
/// <summary>
580-
/// Gets or sets the <see cref="IClass"/> used during the caller rule
581-
/// </summary>
582-
public IClass CallerClassContext { get; }
583-
}
584-
585563
/// <summary>
586564
/// Provides context over the rule generation history
587565
/// </summary>
@@ -598,9 +576,20 @@ private class RuleGenerationContext
598576
public List<TextualNotationRule> AllRules { get; } = [];
599577

600578
/// <summary>
601-
/// Gets or sets the <see cref="CallerContext"/>
579+
/// Gets or sets the <see cref="RuleElement"/> that called other rule
580+
/// </summary>
581+
public RuleElement CallerRule { get; set; }
582+
583+
/// <summary>Initializes a new instance of the <see cref="T:System.Object" /> class.</summary>
584+
public RuleGenerationContext(INamedElement namedElementToGenerate)
585+
{
586+
this.NamedElementToGenerate = namedElementToGenerate;
587+
}
588+
589+
/// <summary>
590+
/// Gets the <see cref="INamedElement"/> that is used in the current generation
602591
/// </summary>
603-
public CallerContext CallerContext { get; set; }
592+
public INamedElement NamedElementToGenerate { get; }
604593
}
605594
}
606595
}

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/FeatureMembershipTextualNotationBuilder.cs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -379,21 +379,21 @@ public static void BuildMetadataBodyUsageMember(SysML2.NET.Core.POCO.Core.Types.
379379
{
380380
RedefinitionTextualNotationBuilder.BuildOwnedRedefinition(ownedRelationshipOfRedefinitionIterator.Current, stringBuilder);
381381
}
382-
throw new System.NotSupportedException("Multiple alternatives not implemented yet");
383-
using var ownedRelationshipOfFeatureValueIterator = poco.OwnedRelationship.OfType<SysML2.NET.Core.POCO.Kernel.FeatureValues.FeatureValue>().GetEnumerator();
384382

385-
if (ownedRelationshipOfFeatureValueIterator.MoveNext())
383+
if (poco.ownedMemberFeature != null)
386384
{
387-
ownedRelationshipOfFeatureValueIterator.MoveNext();
385+
FeatureTextualNotationBuilder.BuildFeatureSpecializationPart(poco.ownedMemberFeature, stringBuilder);
386+
}
388387

389-
if (ownedRelationshipOfFeatureValueIterator.Current != null)
390-
{
391-
FeatureValueTextualNotationBuilder.BuildFeatureValue(ownedRelationshipOfFeatureValueIterator.Current, stringBuilder);
392-
}
393-
stringBuilder.Append(' ');
388+
if (poco.ownedMemberFeature != null)
389+
{
390+
FeatureTextualNotationBuilder.BuildValuePart(poco.ownedMemberFeature, stringBuilder);
394391
}
395392

396-
throw new System.NotSupportedException("Multiple alternatives not implemented yet");
393+
if (poco.ownedMemberFeature != null)
394+
{
395+
TypeTextualNotationBuilder.BuildMetadataBody(poco.ownedMemberFeature, stringBuilder);
396+
}
397397

398398
}
399399

@@ -515,25 +515,11 @@ public static void BuildExpressionBodyMember(SysML2.NET.Core.POCO.Core.Types.IFe
515515
if (poco.ownedMemberFeature != null)
516516
{
517517
stringBuilder.Append("{");
518-
using var ownedRelationshipOfReturnParameterMembershipIterator = poco.OwnedRelationship.OfType<SysML2.NET.Core.POCO.Kernel.Functions.ReturnParameterMembership>().GetEnumerator();
519-
using var ownedRelationshipOfResultExpressionMembershipIterator = poco.OwnedRelationship.OfType<SysML2.NET.Core.POCO.Kernel.Functions.ResultExpressionMembership>().GetEnumerator();
520518

521-
while (ownedRelationshipOfReturnParameterMembershipIterator.MoveNext())
519+
if (poco.ownedMemberFeature != null)
522520
{
523-
throw new System.NotSupportedException("Multiple alternatives not implemented yet");
521+
TypeTextualNotationBuilder.BuildFunctionBodyPart(poco.ownedMemberFeature, stringBuilder);
524522
}
525-
526-
if (ownedRelationshipOfResultExpressionMembershipIterator.MoveNext())
527-
{
528-
529-
if (ownedRelationshipOfResultExpressionMembershipIterator.Current != null)
530-
{
531-
ResultExpressionMembershipTextualNotationBuilder.BuildResultExpressionMember(ownedRelationshipOfResultExpressionMembershipIterator.Current, stringBuilder);
532-
}
533-
stringBuilder.Append(' ');
534-
}
535-
536-
537523
stringBuilder.Append("}");
538524

539525
}

0 commit comments

Comments
 (0)