5 static func parse(content: String) -> ParsedMap {
8 AnyMapParserStrategy(NoteParserStrategy()),
9 AnyMapParserStrategy(VertexParserStrategy()),
10 AnyMapParserStrategy(EdgeParserStrategy()),
11 AnyMapParserStrategy(BlockerParserStrategy()),
12 AnyMapParserStrategy(OpportunityParserStrategy()),
13 AnyMapParserStrategy(StageParserStrategy()),
14 AnyMapParserStrategy(GroupParserStrategy()),
16 let builder = MapBuilder()
18 let lines = content.split(whereSeparator: \.isNewline)
20 for (index, line) in lines.enumerated() {
21 for parser in parsers {
22 if parser.canHandle(line: String(line)) {
23 let (type, object) = parser.handle(
24 index: index, line: String(line), vertices: builder.vertices)
25 builder.addObjectToMap(type: type, object: object)
31 return builder.build()
37 struct MapParsingPatterns {
38 static let vertex = try! NSRegularExpression(
40 "^([^\\(\\[\\]]*?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(?:\\[(.*?)\\])?[\\s]*$",
41 options: [.caseInsensitive, .anchorsMatchLines])
42 static let edge = try! NSRegularExpression(
43 pattern: "^(.+?)[\\s]*-([->])[\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
44 static let blocker = try! NSRegularExpression(
45 pattern: "^\\[(Blocker)\\][\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
46 static let opportunity = try! NSRegularExpression(
47 pattern: "^\\[(Evolution)\\][\\s]*(.+)[\\s]+([-+])[\\s]*([0-9]+.?[0-9]*)",
48 options: [.caseInsensitive, .anchorsMatchLines])
49 static let note = try! NSRegularExpression(
51 "^\\[(Note)\\][\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(.*)",
52 options: [.caseInsensitive, .anchorsMatchLines])
53 static let stage = try! NSRegularExpression(
54 pattern: "^\\[(I{1,3})\\][\\s]*([0-9]+.?[0-9]*)",
55 options: [.caseInsensitive, .anchorsMatchLines])
56 static let group = try! NSRegularExpression(
57 pattern: "^\\[(Group)\\][\\s]*(.+)", options: [.caseInsensitive, .anchorsMatchLines])
61 let vertices: [Vertex]
63 let blockers: [Blocker]
64 let opportunities: [Opportunity]
67 let groups: [[Vertex]]
69 static let empty: ParsedMap = ParsedMap(
70 vertices: [], edges: [], blockers: [], opportunities: [], notes: [], stages: defaultDimensions,
74 struct Vertex: Identifiable, Hashable {
78 var shape: VertexShape = .circle
87 enum VertexShape: String {
97 let destination: CGPoint
103 let position: CGPoint
109 let destination: CGPoint
112 struct StageDimensions {
114 let dimensions: CGFloat
117 private let defaultDimensions: [CGFloat] = [
123 // MARK: - MapParserStrategy protocol
125 protocol MapParserStrategy {
126 func canHandle(line: String) -> Bool
127 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any)
130 struct AnyMapParserStrategy: MapParserStrategy {
132 private let base: MapParserStrategy
134 init<T: MapParserStrategy>(_ base: T) {
138 func canHandle(line: String) -> Bool {
139 return base.canHandle(line: line)
141 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
142 return base.handle(index: index, line: line, vertices: vertices)
146 // MARK: - Map Builder
149 var vertices: [String: Vertex] = [:]
150 private var edges: [MapEdge] = []
151 private var blockers: [Blocker] = []
152 private var opportunities: [Opportunity] = []
153 private var notes: [Note] = []
154 private var stages: [CGFloat] = defaultDimensions
155 private var groups: [[Vertex]] = []
157 func addObjectToMap(type: Any.Type, object: Any) {
158 if type == Vertex.self {
159 let vertex = object as! Vertex
160 vertices[vertex.label] = vertex
163 if type == MapEdge.self {
164 let edge = object as! MapEdge
168 if type == Blocker.self {
169 let blocker = object as! Blocker
170 blockers.append(blocker)
173 if type == Opportunity.self {
174 let opportunity = object as! Opportunity
175 opportunities.append(opportunity)
178 if type == Note.self {
179 let note = object as! Note
183 if type == StageDimensions.self {
184 let stageDimensions = object as! StageDimensions
185 stages[stageDimensions.index] = stageDimensions.dimensions
188 if type == [Vertex].self {
189 let group = object as! [Vertex]
194 func build() -> ParsedMap {
195 let mappedVertices = vertices.map { label, vertex in return vertex }
197 vertices: mappedVertices, edges: edges, blockers: blockers, opportunities: opportunities,
199 stages: stages, groups: groups)