Skip to content

Commit 080e891

Browse files
[WIP] Supports everything except Multiple Alternatives
1 parent bdada01 commit 080e891

File tree

4 files changed

+114
-52
lines changed

4 files changed

+114
-52
lines changed

SysML2.NET.CodeGenerator/HandleBarHelpers/RulesHelper.cs

Lines changed: 84 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ private static void DeclareAllRequiredIterators(EncodedTextWriter writer, IClass
175175
{
176176
switch (ruleElement)
177177
{
178-
case AssignmentElement { Value: NonTerminalElement } assignmentElement:
179-
DeclareIteratorIfRequired(writer, umlClass, assignmentElement, ruleGenerationContext);
180-
break;
181-
case AssignmentElement { Value: GroupElement } assignmentElement:
178+
case AssignmentElement assignmentElement:
182179
DeclareIteratorIfRequired(writer, umlClass, assignmentElement, ruleGenerationContext);
183180
break;
184181
case GroupElement groupElement:
@@ -232,7 +229,7 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
232229

233230
if (groupElement.IsCollection)
234231
{
235-
var assignmentRule = groupElement.Alternatives.SelectMany(x => x.Elements).FirstOrDefault(x => x is AssignmentElement { Value: NonTerminalElement });
232+
var assignmentRule = groupElement.Alternatives.SelectMany(x => x.Elements).FirstOrDefault(x => x is AssignmentElement { Value: NonTerminalElement } || x is AssignmentElement {Value: ValueLiteralElement});
236233

237234
if (assignmentRule is AssignmentElement assignmentElement)
238235
{
@@ -274,6 +271,27 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
274271

275272
ProcessNonTerminalElement(writer, umlClass, nonTerminalElement, $"{iteratorToUse.IteratorVariableName}.Current", ruleGenerationContext);
276273
}
274+
else if(assignmentElement.Value is GroupElement groupElement)
275+
{
276+
var previousCaller = ruleGenerationContext.CallerRule;
277+
ruleGenerationContext.CallerRule = assignmentElement;
278+
ProcessAlternatives(writer, umlClass, groupElement.Alternatives, ruleGenerationContext);
279+
ruleGenerationContext.CallerRule = previousCaller;
280+
}
281+
else if(assignmentElement.Value is ValueLiteralElement valueLiteralElement && valueLiteralElement.QueryIsQualifiedName())
282+
{
283+
var iteratorToUse = ruleGenerationContext.DefinedIterators.Single(x => x.ApplicableRuleElements.Contains(assignmentElement));
284+
285+
if (assignmentElement.Container is not GroupElement { IsCollection: true } && assignmentElement.Container is not GroupElement { IsOptional: true })
286+
{
287+
writer.WriteSafeString($"{iteratorToUse.IteratorVariableName}.MoveNext();{Environment.NewLine}");
288+
}
289+
290+
writer.WriteSafeString($"{Environment.NewLine}if({iteratorToUse.IteratorVariableName}.Current != null){Environment.NewLine}");
291+
writer.WriteSafeString($"{{{Environment.NewLine}");
292+
writer.WriteSafeString($"stringBuilder.Append({iteratorToUse.IteratorVariableName}.Current.qualifiedName);{Environment.NewLine}");
293+
writer.WriteSafeString("}");
294+
}
277295
else
278296
{
279297
writer.WriteSafeString("throw new System.NotSupportedException(\"Assigment of enumerable with non NonTerminalElement not supported yet\");");
@@ -286,7 +304,7 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
286304
writer.WriteSafeString($"{Environment.NewLine}if({targetProperty.QueryIfStatementContentForNonEmpty("poco")}){Environment.NewLine}");
287305
writer.WriteSafeString($"{{{Environment.NewLine}");
288306
writer.WriteSafeString($"stringBuilder.Append(poco.{targetProperty.Name.CapitalizeFirstLetter()});{Environment.NewLine}");
289-
writer.WriteSafeString("}}");
307+
writer.WriteSafeString("}");
290308
}
291309
else
292310
{
@@ -463,55 +481,75 @@ private static void DeclareIteratorIfRequired(EncodedTextWriter writer, IClass u
463481
{
464482
DeclareIteratorIfRequired(writer, umlClass, assignment, ruleGenerationContext);
465483
}
466-
}
467-
468-
if (assignmentElement.Value is not NonTerminalElement nonTerminalElement)
469-
{
484+
470485
return;
471486
}
472487

473-
var referencedRule = ruleGenerationContext.AllRules.SingleOrDefault(x => x.RuleName == nonTerminalElement.Name);
474-
475-
if (ruleGenerationContext.DefinedIterators.SingleOrDefault(x => x.IsIteratorValidForProperty(targetProperty, referencedRule) || x.ApplicableRuleElements.Contains(assignmentElement)) is { } alreadyDefinedIterator)
488+
switch (assignmentElement.Value)
476489
{
477-
alreadyDefinedIterator.ApplicableRuleElements.Add(assignmentElement);
478-
return;
479-
}
490+
case NonTerminalElement nonTerminalElement:
491+
var referencedRule = ruleGenerationContext.AllRules.SingleOrDefault(x => x.RuleName == nonTerminalElement.Name);
492+
493+
if (ruleGenerationContext.DefinedIterators.SingleOrDefault(x => x.IsIteratorValidForProperty(targetProperty, referencedRule) || x.ApplicableRuleElements.Contains(assignmentElement)) is { } alreadyDefinedIterator)
494+
{
495+
alreadyDefinedIterator.ApplicableRuleElements.Add(assignmentElement);
496+
return;
497+
}
480498

481-
var iteratorToUse = new IteratorDefinition
482-
{
483-
DefinedForProperty = targetProperty
484-
};
485-
486-
iteratorToUse.ApplicableRuleElements.Add(assignmentElement);
499+
var iteratorToUse = new IteratorDefinition
500+
{
501+
DefinedForProperty = targetProperty
502+
};
503+
504+
iteratorToUse.ApplicableRuleElements.Add(assignmentElement);
487505

488-
string typeTarget;
489-
490-
if (referencedRule == null)
491-
{
492-
typeTarget = umlClass.Name;
493-
}
494-
else
495-
{
496-
typeTarget = referencedRule.TargetElementName ?? referencedRule.RuleName;
497-
}
506+
string typeTarget;
507+
508+
if (referencedRule == null)
509+
{
510+
typeTarget = umlClass.Name;
511+
}
512+
else
513+
{
514+
typeTarget = referencedRule.TargetElementName ?? referencedRule.RuleName;
515+
}
498516

499-
if (typeTarget != targetProperty.Type.Name)
500-
{
501-
var targetType = umlClass.Cache.Values.OfType<INamedElement>().SingleOrDefault(x => x.Name == typeTarget);
502-
iteratorToUse.ConstrainedType = targetType;
517+
if (typeTarget != targetProperty.Type.Name)
518+
{
519+
var targetType = umlClass.Cache.Values.OfType<INamedElement>().SingleOrDefault(x => x.Name == typeTarget);
520+
iteratorToUse.ConstrainedType = targetType;
503521

504-
writer.WriteSafeString(targetType != null
505-
? $"using var {iteratorToUse.IteratorVariableName} = poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.OfType<{targetType.QueryFullyQualifiedTypeName(targetInterface: false)}>().GetEnumerator();"
506-
: $"using var {iteratorToUse.IteratorVariableName} = poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.GetEnumerator();");
507-
}
508-
else
509-
{
510-
writer.WriteSafeString($"using var {iteratorToUse.IteratorVariableName} = poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.GetEnumerator();");
522+
writer.WriteSafeString(targetType != null
523+
? $"using var {iteratorToUse.IteratorVariableName} = poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.OfType<{targetType.QueryFullyQualifiedTypeName(targetInterface: false)}>().GetEnumerator();"
524+
: $"using var {iteratorToUse.IteratorVariableName} = poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.GetEnumerator();");
525+
}
526+
else
527+
{
528+
writer.WriteSafeString($"using var {iteratorToUse.IteratorVariableName} = poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.GetEnumerator();");
529+
}
530+
531+
writer.WriteSafeString(Environment.NewLine);
532+
ruleGenerationContext.DefinedIterators.Add(iteratorToUse);
533+
break;
534+
case ValueLiteralElement:
535+
if (ruleGenerationContext.DefinedIterators.SingleOrDefault(x => x.IsIteratorValidForProperty(targetProperty, null) || x.ApplicableRuleElements.Contains(assignmentElement)) is { } existingValueLiteralIterator)
536+
{
537+
existingValueLiteralIterator.ApplicableRuleElements.Add(assignmentElement);
538+
return;
539+
}
540+
541+
var valueLiteralIterator = new IteratorDefinition
542+
{
543+
DefinedForProperty = targetProperty
544+
};
545+
546+
valueLiteralIterator.ApplicableRuleElements.Add(assignmentElement);
547+
writer.WriteSafeString($"using var {valueLiteralIterator.IteratorVariableName} = poco.{targetProperty.QueryPropertyNameBasedOnUmlProperties()}.GetEnumerator();");
548+
writer.WriteSafeString(Environment.NewLine);
549+
ruleGenerationContext.DefinedIterators.Add(valueLiteralIterator);
550+
551+
break;
511552
}
512-
513-
writer.WriteSafeString(Environment.NewLine);
514-
ruleGenerationContext.DefinedIterators.Add(iteratorToUse);
515553
}
516554

517555
/// <summary>

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/DependencyTextualNotationBuilder.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public static void BuildDependency(SysML2.NET.Core.POCO.Root.Dependencies.IDepen
5454

5555
}
5656
stringBuilder.Append("dependency ");
57+
using var clientIterator = poco.Client.GetEnumerator();
58+
using var supplierIterator = poco.Supplier.GetEnumerator();
5759

5860
if (BuildGroupConditionForDependencyDeclaration(poco))
5961
{
@@ -62,17 +64,39 @@ public static void BuildDependency(SysML2.NET.Core.POCO.Root.Dependencies.IDepen
6264
stringBuilder.Append(' ');
6365
}
6466

65-
throw new System.NotSupportedException("Assigment of enumerable with non NonTerminalElement not supported yet");
67+
clientIterator.MoveNext();
68+
69+
if (clientIterator.Current != null)
70+
{
71+
stringBuilder.Append(clientIterator.Current.qualifiedName);
72+
}
73+
74+
while (clientIterator.MoveNext())
6675
{
6776
stringBuilder.Append(",");
68-
throw new System.NotSupportedException("Assigment of enumerable with non NonTerminalElement not supported yet");
77+
78+
if (clientIterator.Current != null)
79+
{
80+
stringBuilder.Append(clientIterator.Current.qualifiedName);
81+
}
6982

7083
}
7184
stringBuilder.Append("to ");
72-
throw new System.NotSupportedException("Assigment of enumerable with non NonTerminalElement not supported yet");
85+
supplierIterator.MoveNext();
86+
87+
if (supplierIterator.Current != null)
88+
{
89+
stringBuilder.Append(supplierIterator.Current.qualifiedName);
90+
}
91+
92+
while (supplierIterator.MoveNext())
7393
{
7494
stringBuilder.Append(",");
75-
throw new System.NotSupportedException("Assigment of enumerable with non NonTerminalElement not supported yet");
95+
96+
if (supplierIterator.Current != null)
97+
{
98+
stringBuilder.Append(supplierIterator.Current.qualifiedName);
99+
}
76100

77101
}
78102

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/IfActionUsageTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void BuildIfNode(SysML2.NET.Core.POCO.Systems.Actions.IIfActionUsa
6161
if (BuildGroupConditionForIfNode(poco))
6262
{
6363
stringBuilder.Append("else ");
64-
throw new System.NotSupportedException("Assigment of enumerable with non NonTerminalElement not supported yet");
64+
throw new System.NotSupportedException("Multiple alternatives not implemented yet");
6565
stringBuilder.Append(' ');
6666
}
6767

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/OwningMembershipTextualNotationBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static void BuildOwnedMultiplicity(SysML2.NET.Core.POCO.Root.Namespaces.I
131131
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
132132
public static void BuildMultiplicityExpressionMember(SysML2.NET.Core.POCO.Root.Namespaces.IOwningMembership poco, StringBuilder stringBuilder)
133133
{
134-
throw new System.NotSupportedException("Assigment of enumerable with non NonTerminalElement not supported yet");
134+
throw new System.NotSupportedException("Multiple alternatives not implemented yet");
135135

136136
}
137137

0 commit comments

Comments
 (0)