Skip to content

Commit 5c80125

Browse files
[WIP] in the middle of refactor on correct handling of collection. The CollectionCursor class has to be used all over to not loose collection context position
1 parent 0c9fa8f commit 5c80125

259 files changed

Lines changed: 1943 additions & 1038 deletions

File tree

Some content is hidden

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

SysML2.NET.CodeGenerator/Grammar/Model/Alternatives.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace SysML2.NET.CodeGenerator.Grammar.Model
2222
{
2323
using System.Collections.Generic;
24+
using System.Linq;
2425

2526
/// <summary>
2627
/// Provides mapping data class for the alternative grammar part
@@ -36,5 +37,23 @@ public class Alternatives: IPartOfTextualRule
3637
/// Gets the <see cref="IPartOfTextualRule.TextualNotationRule" />
3738
/// </summary>
3839
public TextualNotationRule TextualNotationRule { get; init; }
40+
41+
/// <summary>
42+
/// Asserts that the current <see cref="Alternatives"/> contains <see cref="AssignmentElement"/> that are part of a collection iteration that could be called from
43+
/// another rule
44+
/// </summary>
45+
/// <returns>The computation of the assertion</returns>
46+
internal bool ContainsAssignmentRequiringDispatch()
47+
{
48+
var assignments = this.Elements.OfType<AssignmentElement>().Where(x => x.Operator == "+=").ToList();
49+
var groupElements = this.Elements.OfType<GroupElement>().Where(x => x.IsCollection).ToList();
50+
51+
if (assignments.Count == 0)
52+
{
53+
return false;
54+
}
55+
56+
return groupElements.Count == 0;
57+
}
3958
}
4059
}

SysML2.NET.CodeGenerator/Grammar/Model/AssignmentElement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ public class AssignmentElement: RuleElement
4343
/// <summary>
4444
/// Gets or sets an optional prefix
4545
/// </summary>
46-
public string Prefix { get; set; }
46+
public string Prefix { get; set; }
4747
}
4848
}

SysML2.NET.CodeGenerator/Grammar/Model/TextualNotationRule.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
namespace SysML2.NET.CodeGenerator.Grammar.Model
2222
{
2323
using System.Collections.Generic;
24+
using System.Linq;
2425

2526
/// <summary>
2627
/// Describe the content of a rule
@@ -51,5 +52,74 @@ public class TextualNotationRule
5152
/// Gets or the collection of <see cref="Alternatives"/> defined by the rule
5253
/// </summary>
5354
public List<Alternatives> Alternatives { get; } = [];
55+
56+
/// <summary>
57+
/// Asserts that the current Rule acts has a dispatcher or not. A dispatcher needs to have index access to access element into a collection.
58+
/// </summary>
59+
public bool IsDispatcherRule => this.ComputeIsDispatcherRule();
60+
61+
/// <summary>
62+
/// Asserts that the rule described assignment of multiple collection
63+
/// </summary>
64+
public bool IsMultiCollectionAssignment => this.ComputeIsMultiCollectionAssigment();
65+
66+
/// <summary>
67+
/// Gets names of property that a multicollection assignment defines
68+
/// </summary>
69+
/// <returns>The collection of property names</returns>
70+
public IReadOnlyCollection<string> QueryMultiCollectionPropertiesName()
71+
{
72+
if (!this.IsMultiCollectionAssignment)
73+
{
74+
return Enumerable.Empty<string>().ToList();
75+
}
76+
77+
var assignments = this.Alternatives.SelectMany(x => x.Elements).OfType<AssignmentElement>();
78+
return assignments.Where(x => x.Operator == "+=").DistinctBy(x => x.Property).Select(x => x.Property).ToList();
79+
}
80+
81+
/// <summary>
82+
/// Computes the value of the <see cref="IsMultiCollectionAssignment"/>
83+
/// </summary>
84+
/// <returns>The result of the assertion computation</returns>
85+
private bool ComputeIsMultiCollectionAssigment()
86+
{
87+
if (this.Alternatives.Count == 1)
88+
{
89+
return false;
90+
}
91+
92+
var assignments = this.Alternatives.SelectMany(x => x.Elements).OfType<AssignmentElement>();
93+
return assignments.Where(x => x.Operator == "+=").DistinctBy(x => x.Property).Count() > 1;
94+
}
95+
96+
/// <summary>
97+
/// Computes the assertion that the <see cref="TextualNotationRule"/> is a dispatcher or not
98+
/// </summary>
99+
/// <returns>The result of the assertion computation</returns>
100+
private bool ComputeIsDispatcherRule()
101+
{
102+
return this.Alternatives.Count > 1 && this.Alternatives.Any(x => x.Elements.OfType<AssignmentElement>().Any(a => a.Operator == "+="))
103+
|| this.Alternatives.Count == 1 && this.Alternatives[0].Elements.Count == 1 && this.Alternatives[0].Elements[0] is AssignmentElement { Operator: "+="};
104+
}
105+
106+
/// <summary>
107+
/// Gets the <see cref="AssignmentElement"/> that requires a dispatcher
108+
/// </summary>
109+
/// <returns>The <see cref="AssignmentElement"/></returns>
110+
public AssignmentElement GetAssignmentElementNeedingDispatcher()
111+
{
112+
if (!this.IsDispatcherRule)
113+
{
114+
return null;
115+
}
116+
117+
if (this.Alternatives.Count == 1)
118+
{
119+
return this.Alternatives[0].Elements[0] as AssignmentElement;
120+
}
121+
122+
return this.Alternatives.SelectMany(x => x.Elements).OfType<AssignmentElement>().FirstOrDefault(x => x.Operator == "+=");
123+
}
54124
}
55125
}

SysML2.NET.CodeGenerator/HandleBarHelpers/PropertyHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public static void RegisterPropertyHelper(this IHandlebars handlebars)
300300
{
301301
if (property.QueryPropertyIsPartOfNonDerivedCompositeAggregation())
302302
{
303-
sb.Append($"IReadOnlyCollection<{typeName}> ");
303+
sb.Append($"IReadOnlyList<{typeName}> ");
304304
}
305305
else
306306
{
@@ -378,7 +378,7 @@ public static void RegisterPropertyHelper(this IHandlebars handlebars)
378378
{
379379
if (property.QueryPropertyIsPartOfNonDerivedCompositeAggregation())
380380
{
381-
sb.Append($"IReadOnlyCollection<{typeName}> ");
381+
sb.Append($"IReadOnlyList<{typeName}> ");
382382
}
383383
else
384384
{

0 commit comments

Comments
 (0)