6 struct MapParsingPatterns {
7 static let vertex = try! NSRegularExpression(
9 "([^\\(\\[\\]]*?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(?:\\[(.*?)\\])?",
10 options: .caseInsensitive)
11 static let edge = try! NSRegularExpression(
12 pattern: "(.+?)[\\s]*-([->])[\\s]*(.+)", options: .caseInsensitive)
13 static let blocker = try! NSRegularExpression(
14 pattern: "\\[(Blocker)\\][\\s]*(.+)", options: .caseInsensitive)
15 static let opportunity = try! NSRegularExpression(
16 pattern: "\\[(Evolution)\\][\\s]*(.+)[\\s]+([-+])[\\s]*([0-9]+.?[0-9]*)",
17 options: .caseInsensitive)
18 static let note = try! NSRegularExpression(
19 pattern: "\\[(Note)\\][\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(.*)",
20 options: .caseInsensitive)
21 static let stage = try! NSRegularExpression(
22 pattern: "\\[(I{1,3})\\][\\s]*([0-9]+.?[0-9]*)", options: .caseInsensitive)
26 let vertices: [Vertex]
28 let blockers: [Blocker]
29 let opportunities: [Opportunity]
33 static let empty: ParsedMap = ParsedMap(
34 vertices: [], edges: [], blockers: [], opportunities: [], notes: [], stages: defaultDimensions)
41 var shape: VertexShape = .circle
50 enum VertexShape: String {
60 let destination: CGPoint
72 let destination: CGPoint
75 struct StageDimensions {
77 let dimensions: CGFloat
80 private let defaultDimensions: [CGFloat] = [
86 // MARK: - MapParserStrategy protocol
88 protocol MapParserStrategy {
89 func canHandle(line: String) -> Bool
90 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any)
93 struct AnyMapParserStrategy: MapParserStrategy {
95 private let base: MapParserStrategy
97 init<T: MapParserStrategy>(_ base: T) {
101 func canHandle(line: String) -> Bool {
102 return base.canHandle(line: line)
104 func handle(index: Int, line: String, vertices: [String: Vertex]) -> (Any.Type, Any) {
105 return base.handle(index: index, line: line, vertices: vertices)
109 // MARK: - Map Builder
112 var vertices: [String: Vertex] = [:]
113 private var edges: [MapEdge] = []
114 private var blockers: [Blocker] = []
115 private var opportunities: [Opportunity] = []
116 private var notes: [Note] = []
117 private var stages: [CGFloat] = defaultDimensions
119 func addObjectToMap(type: Any.Type, object: Any) {
120 if type == Vertex.self {
121 let vertex = object as! Vertex
122 vertices[vertex.label] = vertex
125 if type == MapEdge.self {
126 let edge = object as! MapEdge
130 if type == Blocker.self {
131 let blocker = object as! Blocker
132 blockers.append(blocker)
135 if type == Opportunity.self {
136 let opportunity = object as! Opportunity
137 opportunities.append(opportunity)
140 if type == Note.self {
141 let note = object as! Note
145 if type == StageDimensions.self {
146 let stageDimensions = object as! StageDimensions
147 stages[stageDimensions.index] = stageDimensions.dimensions
151 func build() -> ParsedMap {
152 let mappedVertices = vertices.map { label, vertex in return vertex }
154 vertices: mappedVertices, edges: edges, blockers: blockers, opportunities: opportunities, notes: notes,