aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRuben Beltran del Rio <git@r.bdr.sh>2025-07-09 08:43:04 +0200
committerRuben Beltran del Rio <git@r.bdr.sh>2025-07-09 08:43:04 +0200
commit1e89ea22d551cebfd6c0c0451000c3283ddb2d2e (patch)
tree77afac88484877fbea5fecd1ddda85741584c6c5
parent1ec7f2c6deb64f5b13b32fae2bf74ae2d8aaaabb (diff)
Get rid of old map parser
-rw-r--r--Map/Business/MapParser/MapParser.swift215
-rw-r--r--Map/Business/MapParser/Strategies/EdgeParserStrategy.swift46
-rw-r--r--Map/Business/MapParser/Strategies/GroupParserStrategy.swift51
-rw-r--r--Map/Business/MapParser/Strategies/InertiaParserStrategy.swift40
-rw-r--r--Map/Business/MapParser/Strategies/NoteParserStrategy.swift45
-rw-r--r--Map/Business/MapParser/Strategies/OpportunityParserStrategy.swift47
-rw-r--r--Map/Business/MapParser/Strategies/StageParserStrategy.swift38
-rw-r--r--Map/Business/MapParser/Strategies/VertexParserStrategy.swift50
-rw-r--r--Map/Business/RenderableMap.swift210
-rw-r--r--Map/Presentation/Complex Components/MapRender/MapRenderView.swift136
-rw-r--r--MapTests/MapParserStrategyTests.swift163
-rw-r--r--MapTests/MapTests.swift169
12 files changed, 212 insertions, 998 deletions
diff --git a/Map/Business/MapParser/MapParser.swift b/Map/Business/MapParser/MapParser.swift
deleted file mode 100644
index fc11415..0000000
--- a/Map/Business/MapParser/MapParser.swift
+++ /dev/null
@@ -1,215 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import CoreGraphics
-import Foundation
-
-struct MapParser {
- static func parse(content: String) -> ParsedMap {
-
- let parsers = [
- AnyMapParserStrategy(NoteParserStrategy()),
- AnyMapParserStrategy(VertexParserStrategy()),
- AnyMapParserStrategy(EdgeParserStrategy()),
- AnyMapParserStrategy(InertiaParserStrategy()),
- AnyMapParserStrategy(OpportunityParserStrategy()),
- AnyMapParserStrategy(StageParserStrategy()),
- AnyMapParserStrategy(GroupParserStrategy()),
- ]
- let builder = MapBuilder()
-
- let lines = content.split(whereSeparator: \.isNewline)
-
- for (index, line) in lines.enumerated() {
- for parser in parsers {
- if parser.canHandle(line: String(line)) {
- let (type, object) = parser.handle(
- index: index, line: String(line), vertices: builder.vertices)
- builder.addObjectToMap(type: type, object: object)
- break
- }
- }
- }
-
- return builder.build()
- }
-}
-
-// MARK: - Types
-
-struct MapParsingPatterns {
- static let vertex = try! NSRegularExpression(
- pattern:
- "^([^\\(\\[\\]]*?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(?:\\[(.*?)\\])?[\\s]*$",
- options: [.caseInsensitive, .anchorsMatchLines])
- static let edge = try! NSRegularExpression(
- pattern: "^(.+?)[\\s]*-([->])[\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
- static let inertia = try! NSRegularExpression(
- pattern: "^\\[(Inertia)\\][\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
- static let opportunity = try! NSRegularExpression(
- pattern: "^\\[(Evolution)\\][\\s]*(.+)[\\s]+([-+])[\\s]*([0-9]+.?[0-9]*)",
- options: [.caseInsensitive, .anchorsMatchLines])
- static let note = try! NSRegularExpression(
- pattern:
- "^\\[(Note)\\][\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(.*)",
- options: [.caseInsensitive, .anchorsMatchLines])
- static let stage = try! NSRegularExpression(
- pattern: "^\\[(I{1,3})\\][\\s]*([0-9]+.?[0-9]*)",
- options: [.caseInsensitive, .anchorsMatchLines])
- static let group = try! NSRegularExpression(
- pattern: "^\\[(Group)\\][\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
-}
-
-struct ParsedMap {
- let vertices: [Vertex]
- let edges: [MapEdge]
- let inertias: [Inertia]
- let opportunities: [Opportunity]
- let notes: [Note]
- let stages: [CGFloat]
- let groups: [[Vertex]]
-
- static let empty: ParsedMap = ParsedMap(
- vertices: [], edges: [], inertias: [], opportunities: [], notes: [], stages: defaultDimensions,
- groups: [])
-}
-
-struct Vertex: Identifiable, Hashable {
- let id: Int
- let label: String
- let position: CGPoint
- var shape: VertexShape = .circle
-}
-
-struct Note {
- let id: Int
- let position: CGPoint
- let text: String
-}
-
-enum VertexShape: String {
- case circle
- case square
- case triangle
- case x
-}
-
-struct MapEdge {
- let id: Int
- let origin: CGPoint
- let destination: CGPoint
- let arrowhead: Bool
-}
-
-struct Inertia {
- let id: Int
- let position: CGPoint
-}
-
-struct Opportunity {
- let id: Int
- let origin: CGPoint
- let destination: CGPoint
-}
-
-struct StageDimensions {
- let index: Int
- let dimensions: CGFloat
-}
-
-private let defaultDimensions: [CGFloat] = [
- 25.0,
- 50.0,
- 75.0,
-]
-
-// MARK: - MapParserStrategy protocol
-
-protocol MapParserStrategy {
- func canHandle(line: String) -> Bool
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any)
-}
-
-struct AnyMapParserStrategy: MapParserStrategy {
-
- private let base: MapParserStrategy
-
- init<T: MapParserStrategy>(_ base: T) {
- self.base = base
- }
-
- func canHandle(line: String) -> Bool {
- return base.canHandle(line: line)
- }
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- return base.handle(index: index, line: line, vertices: vertices)
- }
-}
-
-// MARK: - Map Builder
-
-class MapBuilder {
- var vertices: [String: Vertex] = [:]
- private var edges: [MapEdge] = []
- private var inertias: [Inertia] = []
- private var opportunities: [Opportunity] = []
- private var notes: [Note] = []
- private var stages: [CGFloat] = defaultDimensions
- private var groups: [[Vertex]] = []
-
- func addObjectToMap(type: Any.Type, object: Any) {
- if type == Vertex.self {
- let vertex = object as! Vertex
- vertices[vertex.label] = vertex
- }
-
- if type == MapEdge.self {
- let edge = object as! MapEdge
- edges.append(edge)
- }
-
- if type == Inertia.self {
- let inertia = object as! Inertia
- inertias.append(inertia)
- }
-
- if type == Opportunity.self {
- let opportunity = object as! Opportunity
- opportunities.append(opportunity)
- }
-
- if type == Note.self {
- let note = object as! Note
- notes.append(note)
- }
-
- if type == StageDimensions.self {
- let stageDimensions = object as! StageDimensions
- stages[stageDimensions.index] = stageDimensions.dimensions
- }
-
- if type == [Vertex].self {
- let group = object as! [Vertex]
- groups.append(group)
- }
- }
-
- func build() -> ParsedMap {
- let mappedVertices = vertices.map { label, vertex in return vertex }
- return ParsedMap(
- vertices: mappedVertices, edges: edges, inertias: inertias, opportunities: opportunities,
- notes: notes,
- stages: stages, groups: groups)
- }
-}
diff --git a/Map/Business/MapParser/Strategies/EdgeParserStrategy.swift b/Map/Business/MapParser/Strategies/EdgeParserStrategy.swift
deleted file mode 100644
index cc29b49..0000000
--- a/Map/Business/MapParser/Strategies/EdgeParserStrategy.swift
+++ /dev/null
@@ -1,46 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Foundation
-
-struct EdgeParserStrategy: MapParserStrategy {
- private let regex = MapParsingPatterns.edge
-
- func canHandle(line: String) -> Bool {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
- return matches.count > 0 && matches[0].numberOfRanges == 4
- }
-
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
-
- let match = matches[0]
- let arrowhead = String(line[Range(match.range(at: 2), in: line)!]) == ">"
- let vertexA = String(line[Range(match.range(at: 1), in: line)!])
- let vertexB = String(line[Range(match.range(at: 3), in: line)!])
-
- if let origin = vertices[vertexA] {
- if let destination = vertices[vertexB] {
- let edge = MapEdge(
- id: index, origin: origin.position, destination: destination.position,
- arrowhead: arrowhead)
- return (MapEdge.self, edge)
- }
- }
-
- return (NSObject.self, NSObject()) // No matching object
- }
-}
diff --git a/Map/Business/MapParser/Strategies/GroupParserStrategy.swift b/Map/Business/MapParser/Strategies/GroupParserStrategy.swift
deleted file mode 100644
index 96de365..0000000
--- a/Map/Business/MapParser/Strategies/GroupParserStrategy.swift
+++ /dev/null
@@ -1,51 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Foundation
-
-struct GroupParserStrategy: MapParserStrategy {
- private let regex = MapParsingPatterns.group
-
- func canHandle(line: String) -> Bool {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
- return matches.count > 0 && matches[0].numberOfRanges == 3
- }
-
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
-
- let match = matches[0]
- var groupVertices: [Vertex] = []
- let vertexIdString = String(line[Range(match.range(at: 2), in: line)!])
- let vertexIds = vertexIdString.split(separator: ",", omittingEmptySubsequences: true).map(
- String.init
- ).map({ vertexId in
- vertexId.trimmingCharacters(in: .whitespacesAndNewlines)
- })
-
- for vertexId in vertexIds {
- if let vertex = vertices[vertexId] {
- groupVertices.append(vertex)
- }
- }
-
- if groupVertices.count > 0 {
- return ([Vertex].self, groupVertices)
- }
-
- return (NSObject.self, NSObject())
- }
-}
diff --git a/Map/Business/MapParser/Strategies/InertiaParserStrategy.swift b/Map/Business/MapParser/Strategies/InertiaParserStrategy.swift
deleted file mode 100644
index d5d515e..0000000
--- a/Map/Business/MapParser/Strategies/InertiaParserStrategy.swift
+++ /dev/null
@@ -1,40 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Foundation
-
-struct InertiaParserStrategy: MapParserStrategy {
- private let regex = MapParsingPatterns.inertia
-
- func canHandle(line: String) -> Bool {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
- return matches.count > 0 && matches[0].numberOfRanges == 3
- }
-
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
-
- let match = matches[0]
- let vertexA = String(line[Range(match.range(at: 2), in: line)!])
-
- if let vertex = vertices[vertexA] {
- let inertia = Inertia(id: index, position: vertex.position)
- return (Inertia.self, inertia)
- }
-
- return (NSObject.self, NSObject()) // No matching object
- }
-}
diff --git a/Map/Business/MapParser/Strategies/NoteParserStrategy.swift b/Map/Business/MapParser/Strategies/NoteParserStrategy.swift
deleted file mode 100644
index d06c768..0000000
--- a/Map/Business/MapParser/Strategies/NoteParserStrategy.swift
+++ /dev/null
@@ -1,45 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Foundation
-
-struct NoteParserStrategy: MapParserStrategy {
- private let regex = MapParsingPatterns.note
-
- func canHandle(line: String) -> Bool {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
- return matches.count > 0 && matches[0].numberOfRanges == 5
- }
-
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
-
- let match = matches[0]
- let text = String(line[Range(match.range(at: 4), in: line)!])
- let xString = String(line[Range(match.range(at: 2), in: line)!])
- let yString = String(line[Range(match.range(at: 3), in: line)!])
- let x = CGFloat(truncating: NumberFormatter().number(from: xString) ?? 0.0)
- let y = CGFloat(truncating: NumberFormatter().number(from: yString) ?? 0.0)
-
- let note = Note(
- id: index,
- position: CGPoint(x: x, y: y),
- text: text
- )
-
- return (Note.self, note)
- }
-}
diff --git a/Map/Business/MapParser/Strategies/OpportunityParserStrategy.swift b/Map/Business/MapParser/Strategies/OpportunityParserStrategy.swift
deleted file mode 100644
index 25e6cc4..0000000
--- a/Map/Business/MapParser/Strategies/OpportunityParserStrategy.swift
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Foundation
-
-struct OpportunityParserStrategy: MapParserStrategy {
- private let regex = MapParsingPatterns.opportunity
-
- func canHandle(line: String) -> Bool {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
- return matches.count > 0 && matches[0].numberOfRanges == 5
- }
-
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
-
- let match = matches[0]
- let multiplier = CGFloat(
- String(line[Range(match.range(at: 3), in: line)!]) == "-" ? -1.0 : 1.0)
- let vertex = String(line[Range(match.range(at: 2), in: line)!])
- let opportunityString = String(line[Range(match.range(at: 4), in: line)!])
- let opportunity = CGFloat(
- truncating: NumberFormatter().number(from: opportunityString) ?? 0.0)
-
- if let origin = vertices[vertex] {
- let destination = CGPoint(
- x: origin.position.x + opportunity * multiplier, y: origin.position.y)
- let opportunity = Opportunity(id: index, origin: origin.position, destination: destination)
- return (Opportunity.self, opportunity)
- }
-
- return (NSObject.self, NSObject()) // No matching object
- }
-}
diff --git a/Map/Business/MapParser/Strategies/StageParserStrategy.swift b/Map/Business/MapParser/Strategies/StageParserStrategy.swift
deleted file mode 100644
index 6e4a341..0000000
--- a/Map/Business/MapParser/Strategies/StageParserStrategy.swift
+++ /dev/null
@@ -1,38 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Foundation
-
-struct StageParserStrategy: MapParserStrategy {
- private let regex = MapParsingPatterns.stage
-
- func canHandle(line: String) -> Bool {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
- return matches.count > 0 && matches[0].numberOfRanges == 3
- }
-
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
-
- let match = matches[0]
- let stage = String(line[Range(match.range(at: 1), in: line)!])
- let dimensionsString = String(line[Range(match.range(at: 2), in: line)!])
- let dimensions = CGFloat(truncating: NumberFormatter().number(from: dimensionsString) ?? 0.0)
-
- let stageDimensions = StageDimensions(index: stage.count - 1, dimensions: dimensions)
- return (StageDimensions.self, stageDimensions)
- }
-}
diff --git a/Map/Business/MapParser/Strategies/VertexParserStrategy.swift b/Map/Business/MapParser/Strategies/VertexParserStrategy.swift
deleted file mode 100644
index 0ca6fc1..0000000
--- a/Map/Business/MapParser/Strategies/VertexParserStrategy.swift
+++ /dev/null
@@ -1,50 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Foundation
-
-struct VertexParserStrategy: MapParserStrategy {
- private let regex = MapParsingPatterns.vertex
-
- func canHandle(line: String) -> Bool {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
- return matches.count > 0 && matches[0].numberOfRanges >= 4
- }
-
- func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
- let range = NSRange(location: 0, length: line.utf16.count)
- let matches = regex.matches(in: String(line), options: [], range: range)
-
- let match = matches[0]
- let key = String(line[Range(match.range(at: 1), in: line)!])
- let xString = String(line[Range(match.range(at: 2), in: line)!])
- let yString = String(line[Range(match.range(at: 3), in: line)!])
- let x = CGFloat(truncating: NumberFormatter().number(from: xString) ?? 0.0)
- let y = CGFloat(truncating: NumberFormatter().number(from: yString) ?? 0.0)
-
- var vertex = Vertex(
- id: index,
- label: key,
- position: CGPoint(x: x, y: y)
- )
-
- if let range = Range(match.range(at: 4), in: line) {
- let shapeString = String(line[range])
- vertex.shape = VertexShape(rawValue: shapeString.lowercased()) ?? .circle
- }
-
- return (Vertex.self, vertex)
- }
-}
diff --git a/Map/Business/RenderableMap.swift b/Map/Business/RenderableMap.swift
new file mode 100644
index 0000000..c8ab776
--- /dev/null
+++ b/Map/Business/RenderableMap.swift
@@ -0,0 +1,210 @@
+// Copyright (C) 2024 Rubén Beltrán del Río
+
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see https://map.tranquil.systems.
+import Foundation
+
+struct RenderableMap {
+ let vertices: [Vertex]
+ let edges: [MapEdge]
+ let inertias: [Inertia]
+ let opportunities: [Opportunity]
+ let notes: [Note]
+ let stages: [CGFloat]
+ let groups: [[Vertex]]
+
+ static let empty: RenderableMap = RenderableMap(
+ vertices: [], edges: [], inertias: [], opportunities: [], notes: [], stages: defaultDimensions,
+ groups: [])
+
+ static func from(_ entities: [Wmap.Entity]) -> RenderableMap {
+ var vertices: [Vertex] = []
+ var edges: [MapEdge] = []
+ var inertias: [Inertia] = []
+ var opportunities: [Opportunity] = []
+ var notes: [Note] = []
+ var stages: [CGFloat] = [25.0, 50.0, 75.0] // default dimensions
+ var groups: [[Vertex]] = []
+
+ // Build a vertex lookup for edges, groups, etc.
+ var vertexMap: [String: Vertex] = [:]
+ var vertexIdCounter = 0
+
+ for entity in entities {
+ switch entity {
+ case .vertex(let vertex):
+ let newVertex = Vertex(
+ id: vertexIdCounter,
+ label: vertex.label,
+ position: CGPoint(x: vertex.coordinates.0, y: vertex.coordinates.1),
+ shape: convertShape(vertex.shape)
+ )
+ vertices.append(newVertex)
+ vertexMap[vertex.label] = newVertex
+ vertexIdCounter += 1
+
+ case .note(let note):
+ notes.append(
+ Note(
+ id: notes.count,
+ position: CGPoint(x: note.coordinates.0, y: note.coordinates.1),
+ text: note.text
+ ))
+
+ case .stage(let stage):
+ let stageIndex = convertStageNumber(stage.number) - 1
+ if stageIndex >= 0 && stageIndex < 3 {
+ stages[stageIndex] = stage.value
+ }
+
+ default:
+ continue
+ }
+ }
+
+ // Second pass for entities that depend on vertices
+ for entity in entities {
+ switch entity {
+ case .edge(let edge):
+ if let fromVertex = vertexMap[edge.from],
+ let toVertex = vertexMap[edge.to]
+ {
+ edges.append(
+ MapEdge(
+ id: edges.count,
+ origin: fromVertex.position,
+ destination: toVertex.position,
+ arrowhead: edge.isDirected
+ ))
+ }
+
+ case .inertia(let inertia):
+ if let vertex = vertexMap[inertia.vertex] {
+ inertias.append(
+ Inertia(
+ id: inertias.count,
+ position: vertex.position
+ ))
+ }
+
+ case .evolution(let evolution):
+ if let vertex = vertexMap[evolution.vertex] {
+ let direction: CGFloat = evolution.isPositive ? 1.0 : -1.0
+ let destination = CGPoint(
+ x: vertex.position.x + (direction * evolution.value),
+ y: vertex.position.y
+ )
+ opportunities.append(
+ Opportunity(
+ id: opportunities.count,
+ origin: vertex.position,
+ destination: destination
+ ))
+ }
+
+ case .group(let group):
+ var groupVertices: [Vertex] = []
+ for vertexName in group.vertices {
+ if let vertex = vertexMap[vertexName] {
+ groupVertices.append(vertex)
+ }
+ }
+ if !groupVertices.isEmpty {
+ groups.append(groupVertices)
+ }
+
+ default:
+ continue
+ }
+ }
+
+ return RenderableMap(
+ vertices: vertices,
+ edges: edges,
+ inertias: inertias,
+ opportunities: opportunities,
+ notes: notes,
+ stages: stages,
+ groups: groups
+ )
+ }
+
+ private static func convertShape(_ shape: String?) -> VertexShape {
+ guard let shape = shape?.lowercased() else { return .circle }
+ switch shape {
+ case "square": return .square
+ case "triangle": return .triangle
+ case "x": return .x
+ default: return .circle
+ }
+ }
+
+ private static func convertStageNumber(_ stageNumber: String) -> Int {
+ switch stageNumber.lowercased() {
+ case "i": return 1
+ case "ii": return 2
+ case "iii": return 3
+ case "iv": return 4
+ default: return 1
+ }
+ }
+}
+
+struct Vertex: Identifiable, Hashable {
+ let id: Int
+ let label: String
+ let position: CGPoint
+ var shape: VertexShape = .circle
+}
+
+struct Note {
+ let id: Int
+ let position: CGPoint
+ let text: String
+}
+
+enum VertexShape: String {
+ case circle
+ case square
+ case triangle
+ case x
+}
+
+struct MapEdge {
+ let id: Int
+ let origin: CGPoint
+ let destination: CGPoint
+ let arrowhead: Bool
+}
+
+struct Inertia {
+ let id: Int
+ let position: CGPoint
+}
+
+struct Opportunity {
+ let id: Int
+ let origin: CGPoint
+ let destination: CGPoint
+}
+
+struct StageDimensions {
+ let index: Int
+ let dimensions: CGFloat
+}
+
+private let defaultDimensions: [CGFloat] = [
+ 25.0,
+ 50.0,
+ 75.0,
+]
diff --git a/Map/Presentation/Complex Components/MapRender/MapRenderView.swift b/Map/Presentation/Complex Components/MapRender/MapRenderView.swift
index 08d1d61..fe98ace 100644
--- a/Map/Presentation/Complex Components/MapRender/MapRenderView.swift
+++ b/Map/Presentation/Complex Components/MapRender/MapRenderView.swift
@@ -34,7 +34,7 @@ struct MapRenderView: View {
Stage.stages(evolution)
}
- @State private var parsedMap: ParsedMap = ParsedMap.empty
+ @State private var parsedMap: RenderableMap = RenderableMap.empty
@State private var smartLabelPositions: [Int: CGPoint] = [:]
@State private var parseTask: Task<Void, Never>?
@@ -53,7 +53,7 @@ struct MapRenderView: View {
parseTask = Task {
let parsed = await Task.detached(priority: .userInitiated) {
- convertEntitiesToParsedMap(currentEntities)
+ RenderableMap.from(currentEntities)
}.value
let positions = await Task.detached(priority: .userInitiated) {
@@ -94,138 +94,6 @@ struct MapRenderView: View {
}
}
- private nonisolated func convertEntitiesToParsedMap(_ entities: [Wmap.Entity]) -> ParsedMap {
- var vertices: [Vertex] = []
- var edges: [MapEdge] = []
- var inertias: [Inertia] = []
- var opportunities: [Opportunity] = []
- var notes: [Note] = []
- var stages: [CGFloat] = [25.0, 50.0, 75.0] // default dimensions
- var groups: [[Vertex]] = []
-
- // Build a vertex lookup for edges, groups, etc.
- var vertexMap: [String: Vertex] = [:]
- var vertexIdCounter = 0
-
- for entity in entities {
- switch entity {
- case .vertex(let vertex):
- let newVertex = Vertex(
- id: vertexIdCounter,
- label: vertex.label,
- position: CGPoint(x: vertex.coordinates.0, y: vertex.coordinates.1),
- shape: convertShape(vertex.shape)
- )
- vertices.append(newVertex)
- vertexMap[vertex.label] = newVertex
- vertexIdCounter += 1
-
- case .note(let note):
- notes.append(
- Note(
- id: notes.count,
- position: CGPoint(x: note.coordinates.0, y: note.coordinates.1),
- text: note.text
- ))
-
- case .stage(let stage):
- let stageIndex = convertStageNumber(stage.number) - 1
- if stageIndex >= 0 && stageIndex < 3 {
- stages[stageIndex] = stage.value
- }
-
- default:
- continue
- }
- }
-
- // Second pass for entities that depend on vertices
- for entity in entities {
- switch entity {
- case .edge(let edge):
- if let fromVertex = vertexMap[edge.from],
- let toVertex = vertexMap[edge.to]
- {
- edges.append(
- MapEdge(
- id: edges.count,
- origin: fromVertex.position,
- destination: toVertex.position,
- arrowhead: edge.isDirected
- ))
- }
-
- case .inertia(let inertia):
- if let vertex = vertexMap[inertia.vertex] {
- inertias.append(
- Inertia(
- id: inertias.count,
- position: vertex.position
- ))
- }
-
- case .evolution(let evolution):
- if let vertex = vertexMap[evolution.vertex] {
- let direction: CGFloat = evolution.isPositive ? 1.0 : -1.0
- let destination = CGPoint(
- x: vertex.position.x + (direction * evolution.value),
- y: vertex.position.y
- )
- opportunities.append(
- Opportunity(
- id: opportunities.count,
- origin: vertex.position,
- destination: destination
- ))
- }
-
- case .group(let group):
- var groupVertices: [Vertex] = []
- for vertexName in group.vertices {
- if let vertex = vertexMap[vertexName] {
- groupVertices.append(vertex)
- }
- }
- if !groupVertices.isEmpty {
- groups.append(groupVertices)
- }
-
- default:
- continue
- }
- }
-
- return ParsedMap(
- vertices: vertices,
- edges: edges,
- inertias: inertias,
- opportunities: opportunities,
- notes: notes,
- stages: stages,
- groups: groups
- )
- }
-
- private nonisolated func convertShape(_ shape: String?) -> VertexShape {
- guard let shape = shape?.lowercased() else { return .circle }
- switch shape {
- case "square": return .square
- case "triangle": return .triangle
- case "x": return .x
- default: return .circle
- }
- }
-
- private nonisolated func convertStageNumber(_ stageNumber: String) -> Int {
- switch stageNumber.lowercased() {
- case "i": return 1
- case "ii": return 2
- case "iii": return 3
- case "iv": return 4
- default: return 1
- }
- }
-
var body: some View {
ZStack(alignment: .topLeading) {
diff --git a/MapTests/MapParserStrategyTests.swift b/MapTests/MapParserStrategyTests.swift
deleted file mode 100644
index 7061d4d..0000000
--- a/MapTests/MapParserStrategyTests.swift
+++ /dev/null
@@ -1,163 +0,0 @@
-// Copyright (C) 2024 Rubén Beltrán del Río
-
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-
-import CoreGraphics
-// You should have received a copy of the GNU General Public License
-// along with this program. If not, see https://map.tranquil.systems.
-import Testing
-
-@testable import Map
-
-struct MapParserStrategyTests {
-
- @Test func testVertexParserStrategyCanHandle() async throws {
- let strategy = VertexParserStrategy()
-
- #expect(strategy.canHandle(line: "Node (10, 20)"))
- #expect(strategy.canHandle(line: "NodeWithDecimals (30.5, 40.2)"))
- #expect(strategy.canHandle(line: "Multi Word Node (0, 100)"))
- #expect(strategy.canHandle(line: "Node C (50, 60) [square]"))
-
- #expect(!strategy.canHandle(line: "Node A -> Node B"))
- #expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
- #expect(!strategy.canHandle(line: "Invalid line"))
- }
-
- @Test func testVertexParserStrategyHandle() async throws {
- let strategy = VertexParserStrategy()
- let vertices: [String: Vertex] = [:]
-
- let (type, object) = strategy.handle(index: 0, line: "Test Node (25, 75)", vertices: vertices)
-
- #expect(String(describing: type) == String(describing: Vertex.self))
- let vertex = object as! Vertex
- #expect(vertex.id == 0)
- #expect(vertex.label == "Test Node")
- #expect(vertex.position.x == 25.0)
- #expect(vertex.position.y == 75.0)
- #expect(vertex.shape == .circle)
- }
-
- @Test func testVertexParserStrategyHandleWithShape() async throws {
- let strategy = VertexParserStrategy()
- let vertices: [String: Vertex] = [:]
-
- let (type, object) = strategy.handle(
- index: 1, line: "Shaped Node (10, 20) [triangle]", vertices: vertices)
-
- #expect(String(describing: type) == String(describing: Vertex.self))
- let vertex = object as! Vertex
- #expect(vertex.shape == .triangle)
- }
-
- @Test func testEdgeParserStrategyCanHandle() async throws {
- let strategy = EdgeParserStrategy()
-
- #expect(strategy.canHandle(line: "Node A -- Node B"))
- #expect(strategy.canHandle(line: "Node A -> Node B"))
- #expect(strategy.canHandle(line: "Multi Word A -> Multi Word B"))
-
- #expect(!strategy.canHandle(line: "Node A (10, 20)"))
- #expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
- #expect(!strategy.canHandle(line: "Invalid line"))
- }
-
- @Test func testEdgeParserStrategyHandle() async throws {
- let strategy = EdgeParserStrategy()
- let vertices: [String: Vertex] = [
- "Node A": Vertex(id: 0, label: "Node A", position: CGPoint(x: 10, y: 20)),
- "Node B": Vertex(id: 1, label: "Node B", position: CGPoint(x: 30, y: 40)),
- ]
-
- let (type, object) = strategy.handle(index: 2, line: "Node A -> Node B", vertices: vertices)
-
- #expect(String(describing: type) == String(describing: MapEdge.self))
- let edge = object as! MapEdge
- #expect(edge.id == 2)
- #expect(edge.origin.x == 10.0)
- #expect(edge.origin.y == 20.0)
- #expect(edge.destination.x == 30.0)
- #expect(edge.destination.y == 40.0)
- #expect(edge.arrowhead == true)
- }
-
- @Test func testEdgeParserStrategyHandleWithMissingVertex() async throws {
- let strategy = EdgeParserStrategy()
- let vertices: [String: Vertex] = [
- "Node A": Vertex(id: 0, label: "Node A", position: CGPoint(x: 10, y: 20))
- ]
-
- let (type, object) = strategy.handle(index: 2, line: "Node A -> Node B", vertices: vertices)
-
- #expect(String(describing: type) == String(describing: NSObject.self))
- #expect(object is NSObject)
- }
-
- @Test func testNoteParserStrategyCanHandle() async throws {
- let strategy = NoteParserStrategy()
-
- #expect(strategy.canHandle(line: "[Note] (10, 20) Test note"))
- #expect(strategy.canHandle(line: "[Note] (50.5, 60.2) Multi word note"))
-
- #expect(!strategy.canHandle(line: "Node A (10, 20)"))
- #expect(!strategy.canHandle(line: "Node A -> Node B"))
- #expect(!strategy.canHandle(line: "Invalid line"))
- }
-
- @Test func testInertiaParserStrategyCanHandle() async throws {
- let strategy = InertiaParserStrategy()
-
- #expect(strategy.canHandle(line: "[Indertia] Node A"))
- #expect(strategy.canHandle(line: "[Inertia] Multi Word Node"))
-
- #expect(!strategy.canHandle(line: "Node A (10, 20)"))
- #expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
- #expect(!strategy.canHandle(line: "Invalid line"))
- }
-
- @Test func testOpportunityParserStrategyCanHandle() async throws {
- let strategy = OpportunityParserStrategy()
-
- #expect(strategy.canHandle(line: "[Evolution] Node A +15"))
- #expect(strategy.canHandle(line: "[Evolution] Node B -10"))
- #expect(strategy.canHandle(line: "[Evolution] Multi Word Node +5.5"))
-
- #expect(!strategy.canHandle(line: "Node A (10, 20)"))
- #expect(!strategy.canHandle(line: "[Inertia] Node A"))
- #expect(!strategy.canHandle(line: "Invalid line"))
- }
-
- @Test func testStageParserStrategyCanHandle() async throws {
- let strategy = StageParserStrategy()
-
- #expect(strategy.canHandle(line: "[I] 25"))
- #expect(strategy.canHandle(line: "[II] 50.5"))
- #expect(strategy.canHandle(line: "[III] 75"))
-
- #expect(!strategy.canHandle(line: "[IV] 100"))
- #expect(!strategy.canHandle(line: "Node A (10, 20)"))
- #expect(!strategy.canHandle(line: "Invalid line"))
- }
-
- @Test func testGroupParserStrategyCanHandle() async throws {
- let strategy = GroupParserStrategy()
-
- #expect(strategy.canHandle(line: "[Group] Node A, Node B"))
- #expect(strategy.canHandle(line: "[Group] Node A, Node B, Node C"))
- #expect(strategy.canHandle(line: "[Group] Multi Word A, Multi Word B"))
-
- #expect(!strategy.canHandle(line: "Node A (10, 20)"))
- #expect(!strategy.canHandle(line: "[Note] (10, 20) Test"))
- #expect(!strategy.canHandle(line: "Invalid line"))
- }
-
-}
diff --git a/MapTests/MapTests.swift b/MapTests/MapTests.swift
deleted file mode 100644
index fd092d0..0000000
--- a/MapTests/MapTests.swift
+++ /dev/null
@@ -1,169 +0,0 @@
-import CoreGraphics
-import Testing
-
-@testable import Map
-
-struct MapTests {
-
- @Test func testMapParserWithEmptyInput() async throws {
- let result = MapParser.parse(content: "")
- #expect(result.vertices.isEmpty)
- #expect(result.edges.isEmpty)
- #expect(result.notes.isEmpty)
- #expect(result.inertias.isEmpty)
- #expect(result.opportunities.isEmpty)
- #expect(result.groups.isEmpty)
- }
-
- @Test func testMapParserWithSingleVertex() async throws {
- let content = "Node A (10, 20)"
- let result = MapParser.parse(content: content)
-
- #expect(result.vertices.count == 1)
- let vertex = result.vertices.first!
- #expect(vertex.label == "Node A")
- #expect(vertex.position.x == 10.0)
- #expect(vertex.position.y == 20.0)
- #expect(vertex.shape == .circle)
- }
-
- @Test func testMapParserWithVertexWithShape() async throws {
- let content = "Node B (30, 40) [square]"
- let result = MapParser.parse(content: content)
-
- #expect(result.vertices.count == 1)
- let vertex = result.vertices.first!
- #expect(vertex.label == "Node B")
- #expect(vertex.position.x == 30.0)
- #expect(vertex.position.y == 40.0)
- #expect(vertex.shape == .square)
- }
-
- @Test func testMapParserWithEdge() async throws {
- let content = """
- Node A (10, 20)
- Node B (30, 40)
- Node A -- Node B
- """
- let result = MapParser.parse(content: content)
-
- #expect(result.vertices.count == 2)
- #expect(result.edges.count == 1)
- let edge = result.edges.first!
- #expect(edge.origin.x == 10.0)
- #expect(edge.origin.y == 20.0)
- #expect(edge.destination.x == 30.0)
- #expect(edge.destination.y == 40.0)
- #expect(edge.arrowhead == false)
- }
-
- @Test func testMapParserWithArrowEdge() async throws {
- let content = """
- Node A (10, 20)
- Node B (30, 40)
- Node A -> Node B
- """
- let result = MapParser.parse(content: content)
-
- #expect(result.edges.count == 1)
- let edge = result.edges.first!
- #expect(edge.arrowhead == true)
- }
-
- @Test func testMapParserWithNote() async throws {
- let content = "[Note] (50, 60) This is a test note"
- let result = MapParser.parse(content: content)
-
- #expect(result.notes.count == 1)
- let note = result.notes.first!
- #expect(note.position.x == 50.0)
- #expect(note.position.y == 60.0)
- #expect(note.text == "This is a test note")
- }
-
- @Test func testMapParserWithInertia() async throws {
- let content = """
- Node A (10, 20)
- [Inertia] Node A
- """
- let result = MapParser.parse(content: content)
-
- #expect(result.inertias.count == 1)
- let inertia = result.inertias.first!
- #expect(inertia.position.x == 10.0)
- #expect(inertia.position.y == 20.0)
- }
-
- @Test func testMapParserWithOpportunity() async throws {
- let content = """
- Node A (10, 20)
- [Evolution] Node A +15
- """
- let result = MapParser.parse(content: content)
-
- #expect(result.opportunities.count == 1)
- let opportunity = result.opportunities.first!
- #expect(opportunity.origin.x == 10.0)
- #expect(opportunity.origin.y == 20.0)
- #expect(opportunity.destination.x == 25.0)
- #expect(opportunity.destination.y == 20.0)
- }
-
- @Test func testMapParserWithGroup() async throws {
- let content = """
- Node A (10, 20)
- Node B (30, 40)
- Node C (50, 60)
- [Group] Node A, Node B, Node C
- """
- let result = MapParser.parse(content: content)
-
- #expect(result.groups.count == 1)
- let group = result.groups.first!
- #expect(group.count == 3)
- #expect(group.map { $0.label }.contains("Node A"))
- #expect(group.map { $0.label }.contains("Node B"))
- #expect(group.map { $0.label }.contains("Node C"))
- }
-
- @Test func testMapParserWithStages() async throws {
- let content = """
- [I] 15
- [II] 35
- [III] 80
- """
- let result = MapParser.parse(content: content)
-
- #expect(result.stages.count == 3)
- #expect(result.stages[0] == 15.0)
- #expect(result.stages[1] == 35.0)
- #expect(result.stages[2] == 80.0)
- }
-
- @Test func testMapParserWithComplexMap() async throws {
- let content = """
- Node A (10, 20)
- Node B (30, 40) [square]
- Node C (50, 60)
- Node A -> Node B
- Node B -- Node C
- [Note] (70, 80) Complex map example
- [Inertia] Node A
- [Evolution] Node B +10
- [Group] Node A, Node C
- [I] 25
- [II] 75
- """
- let result = MapParser.parse(content: content)
-
- #expect(result.vertices.count == 3)
- #expect(result.edges.count == 2)
- #expect(result.notes.count == 1)
- #expect(result.inertias.count == 1)
- #expect(result.opportunities.count == 1)
- #expect(result.groups.count == 1)
- #expect(result.stages[0] == 25.0)
- #expect(result.stages[1] == 75.0)
- }
-
-}