Skip to content

Commit 1d61fbf

Browse files
Supports assignment for bool and enum
1 parent 68c92f5 commit 1d61fbf

File tree

160 files changed

+1009
-1576
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

160 files changed

+1009
-1576
lines changed

SysML2.NET.CodeGenerator/Extensions/PropertyExtension.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,49 @@ public static bool QueryPropertyIsPartOfNonDerivedCompositeAggregation(this IPro
9292

9393
return property.Opposite is { IsComposite: true, IsDerived: false };
9494
}
95+
96+
/// <summary>
97+
/// Queries the content of a IF statement for non-empty values
98+
/// </summary>
99+
/// <param name="property">The property that have to be used to produce the content</param>
100+
/// <param name="variableName">The name of the name</param>
101+
/// <returns>The If Statement content</returns>
102+
public static string QueryIfStatementContentForNonEmpty(this IProperty property, string variableName)
103+
{
104+
var propertyName = property.QueryPropertyNameBasedOnUmlProperties();
105+
106+
if (property.QueryIsEnumerable())
107+
{
108+
return $"{variableName}.{propertyName}.Count != 0";
109+
}
110+
111+
if (property.QueryIsReferenceProperty())
112+
{
113+
return $"{variableName}.{propertyName} != null";
114+
}
115+
116+
if (property.QueryIsNullableAndNotString())
117+
{
118+
return $"{variableName}.{propertyName}.HasValue";
119+
}
120+
121+
if (property.QueryIsString())
122+
{
123+
return $"!string.IsNullOrWhiteSpace({variableName}.{propertyName})";
124+
}
125+
126+
if (property.QueryIsBool())
127+
{
128+
return $"{variableName}.{propertyName}";
129+
}
130+
131+
if (property.QueryIsEnum())
132+
{
133+
var defaultValue = property.QueryIsEnumPropertyWithDefaultValue() ? $"{property.Type.QueryFullyQualifiedTypeName()}.{property.QueryDefaultValueAsString().CapitalizeFirstLetter()}" : ((IEnumeration)property.Type).OwnedLiteral[0].Name;
134+
return $"{variableName}.{propertyName} != {defaultValue}";
135+
}
136+
137+
return "THIS WILL PRODUCE COMPILE ERROR";
138+
}
95139
}
96140
}

SysML2.NET.CodeGenerator/HandleBarHelpers/RulesHelper.cs

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,45 @@ public static void RegisterRulesHelper(this IHandlebars handlebars)
8181
/// <param name="umlClass">The related <see cref="IClass"/></param>
8282
/// <param name="alternatives">The collection of alternatives to process</param>
8383
/// <param name="rules">A collection of all existing rules</param>
84-
private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClass, IReadOnlyCollection<Alternatives> alternatives, IReadOnlyCollection<TextualNotationRule> rules)
84+
/// <param name="callerElementIsOptional"></param>
85+
private static void ProcessAlternatives(EncodedTextWriter writer, IClass umlClass, IReadOnlyCollection<Alternatives> alternatives, IReadOnlyCollection<TextualNotationRule> rules, bool callerElementIsOptional = false)
8586
{
8687
if (alternatives.Count == 1)
8788
{
88-
foreach (var textualRuleElement in alternatives.ElementAt(0).Elements)
89+
var elements = alternatives.ElementAt(0).Elements;
90+
91+
if (callerElementIsOptional)
8992
{
90-
ProcessRuleElement(writer, umlClass, rules, textualRuleElement);
93+
var targetPropertiesName = elements.OfType<AssignmentElement>().Select(x => x.Property).Distinct().ToList();
94+
95+
if (targetPropertiesName.Count > 0)
96+
{
97+
var allProperties = umlClass.QueryAllProperties();
98+
writer.WriteSafeString("if(");
99+
100+
var ifStatementContent = targetPropertiesName
101+
.Select(propertyName => allProperties.SingleOrDefault(x => string.Equals(x.Name, propertyName, StringComparison.OrdinalIgnoreCase)))
102+
.Select(matchedProperty => matchedProperty.QueryIfStatementContentForNonEmpty("poco")).ToList();
103+
104+
writer.WriteSafeString(string.Join(" && ", ifStatementContent));
105+
writer.WriteSafeString($"){Environment.NewLine}");
106+
writer.WriteSafeString($"{{{Environment.NewLine}");
107+
108+
foreach (var textualRuleElement in elements)
109+
{
110+
ProcessRuleElement(writer, umlClass, rules, textualRuleElement);
111+
}
112+
113+
writer.WriteSafeString($"stringBuilder.Append(' ');{Environment.NewLine}");
114+
writer.WriteSafeString($"}}{Environment.NewLine}");
115+
}
116+
}
117+
else
118+
{
119+
foreach (var textualRuleElement in elements)
120+
{
121+
ProcessRuleElement(writer, umlClass, rules, textualRuleElement);
122+
}
91123
}
92124
}
93125
else
@@ -109,12 +141,18 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
109141
switch (textualRuleElement)
110142
{
111143
case TerminalElement terminalElement:
112-
writer.WriteSafeString($"stringBuilder.Append(\"{terminalElement.Value} \");");
144+
var valueToAdd = terminalElement.Value;
145+
146+
if (valueToAdd.Length > 1)
147+
{
148+
valueToAdd += ' ';
149+
}
150+
151+
writer.WriteSafeString($"stringBuilder.Append(\"{valueToAdd}\");");
113152
break;
114153
case NonTerminalElement nonTerminalElement:
115154
var referencedRule = rules.Single(x => x.RuleName == nonTerminalElement.Name);
116155
var typeTarget = referencedRule.TargetElementName ?? referencedRule.RuleName;
117-
writer.WriteSafeString($"// non Terminal : {nonTerminalElement.Name}; Found rule {referencedRule.RawRule} {Environment.NewLine}");
118156

119157
if (typeTarget != umlClass.Name)
120158
{
@@ -143,17 +181,61 @@ private static void ProcessRuleElement(EncodedTextWriter writer, IClass umlClass
143181

144182
break;
145183
case GroupElement groupElement:
146-
writer.WriteSafeString($"// Group Element{Environment.NewLine}");
147-
ProcessAlternatives(writer, umlClass, groupElement.Alternatives, rules);
184+
ProcessAlternatives(writer, umlClass, groupElement.Alternatives, rules, groupElement.IsOptional);
185+
186+
if (!groupElement.IsOptional)
187+
{
188+
writer.WriteSafeString($"{Environment.NewLine}stringBuilder.Append(' ');");
189+
}
190+
148191
break;
149192
case AssignmentElement assignmentElement:
150-
writer.WriteSafeString($"// Assignment Element : {assignmentElement.Property} {assignmentElement.Operator} {assignmentElement.Value}{Environment.NewLine}");
151193
var properties = umlClass.QueryAllProperties();
152194
var targetProperty = properties.SingleOrDefault(x => string.Equals(x.Name, assignmentElement.Property, StringComparison.OrdinalIgnoreCase));
153195

154196
if (targetProperty != null)
155197
{
156-
writer.WriteSafeString($"// If property {targetProperty.Name} value is set, print {assignmentElement.Value}");
198+
if (targetProperty.QueryIsEnumerable())
199+
{
200+
writer.WriteSafeString("throw new System.NotSupportedException(\"Assigment of enumerable not supported yet\");");
201+
}
202+
else
203+
{
204+
if (assignmentElement.IsOptional)
205+
{
206+
writer.WriteSafeString($"{Environment.NewLine}if({targetProperty.QueryIfStatementContentForNonEmpty("poco")}){Environment.NewLine}");
207+
writer.WriteSafeString($"{{{Environment.NewLine}");
208+
writer.WriteSafeString($"{Environment.NewLine}");
209+
writer.WriteSafeString($"stringBuilder.Append(poco.{targetProperty.Name.CapitalizeFirstLetter()});{Environment.NewLine}");
210+
writer.WriteSafeString("}}");
211+
}
212+
else
213+
{
214+
if (targetProperty.QueryIsString())
215+
{
216+
writer.WriteSafeString($"stringBuilder.Append(poco.{targetProperty.Name.CapitalizeFirstLetter()});");
217+
}
218+
else if(targetProperty.QueryIsBool())
219+
{
220+
if (assignmentElement.Value is TerminalElement terminalElement)
221+
{
222+
writer.WriteSafeString($"stringBuilder.Append(\"{terminalElement.Value}\");");
223+
}
224+
else
225+
{
226+
writer.WriteSafeString("throw new System.NotSupportedException(\"Assigment of bool with rule element value different than TerminalElement not supported\");");
227+
}
228+
}
229+
else if (targetProperty.QueryIsEnum())
230+
{
231+
writer.WriteSafeString($"stringBuilder.Append(poco.{targetProperty.Name.CapitalizeFirstLetter()}.ToString().ToLower());");
232+
}
233+
else
234+
{
235+
writer.WriteSafeString("throw new System.NotSupportedException(\"Assigment of non-string value not yet supported\");");
236+
}
237+
}
238+
}
157239
}
158240
else
159241
{

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/AcceptActionUsageTextualNotationBuilder.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@ public static partial class AcceptActionUsageTextualNotationBuilder
4141
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
4242
public static void BuildAcceptNode(SysML2.NET.Core.POCO.Systems.Actions.IAcceptActionUsage poco, StringBuilder stringBuilder)
4343
{
44-
// non Terminal : OccurrenceUsagePrefix; Found rule OccurrenceUsagePrefix:OccurrenceUsage=BasicUsagePrefix(isIndividual?='individual')?(portionKind=PortionKind{isPortion=true})?UsageExtensionKeyword*
4544
OccurrenceUsageTextualNotationBuilder.BuildOccurrenceUsagePrefix(poco, stringBuilder);
46-
// non Terminal : AcceptNodeDeclaration; Found rule AcceptNodeDeclaration:AcceptActionUsage=ActionNodeUsageDeclaration?'accept'AcceptParameterPart
4745
BuildAcceptNodeDeclaration(poco, stringBuilder);
48-
// non Terminal : ActionBody; Found rule ActionBody:Type=';'|'{'ActionBodyItem*'}'
4946
TypeTextualNotationBuilder.BuildActionBody(poco, stringBuilder);
5047

5148
}
@@ -58,10 +55,8 @@ public static void BuildAcceptNode(SysML2.NET.Core.POCO.Systems.Actions.IAcceptA
5855
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
5956
public static void BuildAcceptNodeDeclaration(SysML2.NET.Core.POCO.Systems.Actions.IAcceptActionUsage poco, StringBuilder stringBuilder)
6057
{
61-
// non Terminal : ActionNodeUsageDeclaration; Found rule ActionNodeUsageDeclaration:ActionUsage='action'UsageDeclaration?
6258
ActionUsageTextualNotationBuilder.BuildActionNodeUsageDeclaration(poco, stringBuilder);
6359
stringBuilder.Append("accept ");
64-
// non Terminal : AcceptParameterPart; Found rule AcceptParameterPart:AcceptActionUsage=ownedRelationship+=PayloadParameterMember('via'ownedRelationship+=NodeParameterMember)?
6560
BuildAcceptParameterPart(poco, stringBuilder);
6661

6762
}
@@ -74,12 +69,13 @@ public static void BuildAcceptNodeDeclaration(SysML2.NET.Core.POCO.Systems.Actio
7469
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
7570
public static void BuildAcceptParameterPart(SysML2.NET.Core.POCO.Systems.Actions.IAcceptActionUsage poco, StringBuilder stringBuilder)
7671
{
77-
// Assignment Element : ownedRelationship += SysML2.NET.CodeGenerator.Grammar.Model.NonTerminalElement
78-
// If property ownedRelationship value is set, print SysML2.NET.CodeGenerator.Grammar.Model.NonTerminalElement
79-
// Group Element
80-
stringBuilder.Append("via ");
81-
// Assignment Element : ownedRelationship += SysML2.NET.CodeGenerator.Grammar.Model.NonTerminalElement
82-
// If property ownedRelationship value is set, print SysML2.NET.CodeGenerator.Grammar.Model.NonTerminalElement
72+
throw new System.NotSupportedException("Assigment of enumerable not supported yet");
73+
if (poco.OwnedRelationship.Count != 0)
74+
{
75+
stringBuilder.Append("via ");
76+
throw new System.NotSupportedException("Assigment of enumerable not supported yet");
77+
stringBuilder.Append(' ');
78+
}
8379

8480

8581
}
@@ -92,9 +88,7 @@ public static void BuildAcceptParameterPart(SysML2.NET.Core.POCO.Systems.Actions
9288
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
9389
public static void BuildStateAcceptActionUsage(SysML2.NET.Core.POCO.Systems.Actions.IAcceptActionUsage poco, StringBuilder stringBuilder)
9490
{
95-
// non Terminal : AcceptNodeDeclaration; Found rule AcceptNodeDeclaration:AcceptActionUsage=ActionNodeUsageDeclaration?'accept'AcceptParameterPart
9691
BuildAcceptNodeDeclaration(poco, stringBuilder);
97-
// non Terminal : ActionBody; Found rule ActionBody:Type=';'|'{'ActionBodyItem*'}'
9892
TypeTextualNotationBuilder.BuildActionBody(poco, stringBuilder);
9993

10094
}
@@ -107,7 +101,6 @@ public static void BuildStateAcceptActionUsage(SysML2.NET.Core.POCO.Systems.Acti
107101
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
108102
public static void BuildTriggerAction(SysML2.NET.Core.POCO.Systems.Actions.IAcceptActionUsage poco, StringBuilder stringBuilder)
109103
{
110-
// non Terminal : AcceptParameterPart; Found rule AcceptParameterPart:AcceptActionUsage=ownedRelationship+=PayloadParameterMember('via'ownedRelationship+=NodeParameterMember)?
111104
BuildAcceptParameterPart(poco, stringBuilder);
112105

113106
}
@@ -120,13 +113,7 @@ public static void BuildTriggerAction(SysML2.NET.Core.POCO.Systems.Actions.IAcce
120113
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
121114
public static void BuildTransitionAcceptActionUsage(SysML2.NET.Core.POCO.Systems.Actions.IAcceptActionUsage poco, StringBuilder stringBuilder)
122115
{
123-
// non Terminal : AcceptNodeDeclaration; Found rule AcceptNodeDeclaration:AcceptActionUsage=ActionNodeUsageDeclaration?'accept'AcceptParameterPart
124116
BuildAcceptNodeDeclaration(poco, stringBuilder);
125-
// Group Element
126-
stringBuilder.Append("{ ");
127-
// non Terminal : ActionBodyItem; Found rule ActionBodyItem:Type=NonBehaviorBodyItem|ownedRelationship+=InitialNodeMember(ownedRelationship+=ActionTargetSuccessionMember)*|(ownedRelationship+=SourceSuccessionMember)?ownedRelationship+=ActionBehaviorMember(ownedRelationship+=ActionTargetSuccessionMember)*|ownedRelationship+=GuardedSuccessionMember
128-
TypeTextualNotationBuilder.BuildActionBodyItem(poco, stringBuilder);
129-
stringBuilder.Append("} ");
130117

131118

132119
}

SysML2.NET/TextualNotation/AutoGenTextualNotationBuilder/ActionDefinitionTextualNotationBuilder.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,10 @@ public static partial class ActionDefinitionTextualNotationBuilder
4141
/// <param name="stringBuilder">The <see cref="StringBuilder" /> that contains the entire textual notation</param>
4242
public static void BuildActionDefinition(SysML2.NET.Core.POCO.Systems.Actions.IActionDefinition poco, StringBuilder stringBuilder)
4343
{
44-
// non Terminal : OccurrenceDefinitionPrefix; Found rule OccurrenceDefinitionPrefix:OccurrenceDefinition=BasicDefinitionPrefix?(isIndividual?='individual'ownedRelationship+=EmptyMultiplicityMember)?DefinitionExtensionKeyword*
4544
OccurrenceDefinitionTextualNotationBuilder.BuildOccurrenceDefinitionPrefix(poco, stringBuilder);
4645
stringBuilder.Append("action ");
4746
stringBuilder.Append("def ");
48-
// non Terminal : DefinitionDeclaration; Found rule DefinitionDeclaration:Definition=IdentificationSubclassificationPart?
4947
DefinitionTextualNotationBuilder.BuildDefinitionDeclaration(poco, stringBuilder);
50-
// non Terminal : ActionBody; Found rule ActionBody:Type=';'|'{'ActionBodyItem*'}'
5148
TypeTextualNotationBuilder.BuildActionBody(poco, stringBuilder);
5249

5350
}

0 commit comments

Comments
 (0)