-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation): Validate that sector borders are contiguous (#169)
- Loading branch information
Showing
9 changed files
with
936 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/Compiler/Validate/AllSectorsBordersMustBeContiguous.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Collections.Generic; | ||
using Compiler.Event; | ||
using Compiler.Model; | ||
using Compiler.Error; | ||
using Compiler.Argument; | ||
using System.Linq; | ||
|
||
namespace Compiler.Validate | ||
{ | ||
public class AllSectorBordersMustBeContiguous : IValidationRule | ||
{ | ||
public void Validate(SectorElementCollection sectorElements, CompilerArguments args, IEventLogger events) | ||
{ | ||
Dictionary<string, (Coordinate, Coordinate)> sectorlines = sectorElements.SectorLines.ToDictionary( | ||
sectorline => sectorline.Name, | ||
sectorline => (sectorline.Start(), sectorline.End()) | ||
); | ||
|
||
foreach (Sector sector in sectorElements.Sectors) | ||
{ | ||
foreach (SectorBorder border in sector.Borders) | ||
{ | ||
// Get the start and end coordinates of all the SECTORLINE elements used in the border. | ||
List<KeyValuePair<string, (Coordinate, Coordinate)>> borderLines = border.BorderLines | ||
.Where(borderLine => sectorlines.ContainsKey(borderLine)) | ||
.Select( | ||
borderLine => | ||
new KeyValuePair<string, (Coordinate, Coordinate)>(borderLine, sectorlines[borderLine]) | ||
).ToList(); | ||
|
||
if (borderLines.Count == 0) | ||
{ | ||
continue; | ||
} | ||
|
||
/* | ||
* First up, lets work out whether it's the "first" or the "last" coordinate of the first BORDER | ||
* segment that's joining up to the last BORDER segment, so we know how to proceed. | ||
* | ||
* If the END coordinate of the first border line matches up with any of the coordinates | ||
* of the last border line, then it's going to be the START coordinate of that line that | ||
* matches up with something on the second border line as we work our way around. | ||
*/ | ||
bool startOfLine = borderLines.First().Value.Item2.Equals(borderLines.Last().Value.Item1) || | ||
borderLines.First().Value.Item2.Equals(borderLines.Last().Value.Item2); | ||
|
||
// Loop around the lines | ||
for (int i = 0; i < borderLines.Count; i++) | ||
{ | ||
var firstCoordinate = startOfLine | ||
? borderLines[i].Value.Item1 | ||
: borderLines[i].Value.Item2; | ||
|
||
if (firstCoordinate.Equals(borderLines[(i + 1) % borderLines.Count].Value.Item1)) | ||
{ | ||
startOfLine = false; | ||
} | ||
else if (firstCoordinate.Equals(borderLines[(i + 1) % borderLines.Count].Value.Item2)) | ||
{ | ||
startOfLine = true; | ||
} | ||
else | ||
{ | ||
events.AddEvent(new ValidationRuleFailure( | ||
ErrorMessage(sector, borderLines[i].Key, borderLines[(i + 1) % borderLines.Count].Key), | ||
border)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private string ErrorMessage(Sector sector, string firstBorderline, string secondBorderline) | ||
{ | ||
return | ||
$"Non-contiguous BORDER for SECTOR {sector.Name}, line {firstBorderline} does not join with {secondBorderline}"; | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Compiler/Validate/AllSectorsBordersMustBeSingleIfCircle.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Collections.Generic; | ||
using Compiler.Event; | ||
using Compiler.Model; | ||
using Compiler.Error; | ||
using Compiler.Argument; | ||
using System.Linq; | ||
|
||
namespace Compiler.Validate | ||
{ | ||
public class AllSectorsBordersMustBeSingleIfCircle : IValidationRule | ||
{ | ||
public void Validate(SectorElementCollection sectorElements, CompilerArguments args, IEventLogger events) | ||
{ | ||
Dictionary<string, CircleSectorline> sectorlines = sectorElements.CircleSectorLines.ToDictionary( | ||
sectorline => sectorline.Name, | ||
sectorline => sectorline | ||
); | ||
|
||
foreach (Sector sector in sectorElements.Sectors) | ||
{ | ||
foreach (SectorBorder border in sector.Borders) | ||
{ | ||
var circleBorders = border.BorderLines.Where( | ||
borderLine => sectorlines.ContainsKey(borderLine) | ||
).ToList(); | ||
|
||
if (circleBorders.Count > 0 && border.BorderLines.Count > 1) | ||
{ | ||
events.AddEvent(new ValidationRuleFailure(ErrorMessage(sector), border)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
private string ErrorMessage(Sector sector) | ||
{ | ||
return | ||
$"SECTOR {sector.Name} has a BORDER with a CIRCLELINE, but contains more than one element"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.