diff options
Diffstat (limited to 'Map/MapParser')
| -rw-r--r-- | Map/MapParser/MapParser.swift | 142 | ||||
| -rw-r--r-- | Map/MapParser/Strategies/BlockerParserStrategy.swift | 26 | ||||
| -rw-r--r-- | Map/MapParser/Strategies/EdgeParserStrategy.swift | 32 | ||||
| -rw-r--r-- | Map/MapParser/Strategies/OpportunityParserStrategy.swift | 33 | ||||
| -rw-r--r-- | Map/MapParser/Strategies/StageParserStrategy.swift | 24 | ||||
| -rw-r--r-- | Map/MapParser/Strategies/VertexParserStrategy.swift | 36 |
6 files changed, 0 insertions, 293 deletions
diff --git a/Map/MapParser/MapParser.swift b/Map/MapParser/MapParser.swift deleted file mode 100644 index cce6251..0000000 --- a/Map/MapParser/MapParser.swift +++ /dev/null @@ -1,142 +0,0 @@ -import CoreGraphics -import Foundation - -// MARK: - Types - -struct MapParsingPatterns { - static let vertex = try! NSRegularExpression( - pattern: - "([^\\(]+?)[\\s]*\\([\\s]*([0-9]+.?[0-9]*)[\\s]*,[\\s]*([0-9]+.?[0-9]*)[\\s]*\\)[\\s]*(?:\\[(.*?)\\])?", - options: .caseInsensitive) - static let edge = try! NSRegularExpression( - pattern: "(.+?)[\\s]*-([->])[\\s]*(.+)", options: .caseInsensitive) - static let blocker = try! NSRegularExpression( - pattern: "\\[(Blocker)\\][\\s]*(.+)", options: .caseInsensitive) - static let opportunity = try! NSRegularExpression( - pattern: "\\[(Opportunity)\\][\\s]*(.+)[\\s]+([-+])[\\s]*([0-9]+.?[0-9]*)", - options: .caseInsensitive) - static let stage = try! NSRegularExpression( - pattern: "\\[(I{1,3})\\][\\s]*([0-9]+.?[0-9]*)", options: .caseInsensitive) -} - -struct ParsedMap { - let vertices: [Vertex] - let edges: [MapEdge] - let blockers: [Blocker] - let opportunities: [Opportunity] - let stages: [CGFloat] - - static let empty: ParsedMap = ParsedMap( - vertices: [], edges: [], blockers: [], opportunities: [], stages: defaultDimensions) -} - -struct Vertex { - let id: Int - let label: String - let position: CGPoint - var shape: VertexShape = .circle -} - -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 Blocker { - 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 blockers: [Blocker] = [] - private var opportunities: [Opportunity] = [] - private var stages: [CGFloat] = defaultDimensions - - 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 == Blocker.self { - let blocker = object as! Blocker - blockers.append(blocker) - } - - if type == Opportunity.self { - let opportunity = object as! Opportunity - opportunities.append(opportunity) - } - - if type == StageDimensions.self { - let stageDimensions = object as! StageDimensions - stages[stageDimensions.index] = stageDimensions.dimensions - - } - } - - func build() -> ParsedMap { - let mappedVertices = vertices.map { label, vertex in return vertex } - return ParsedMap( - vertices: mappedVertices, edges: edges, blockers: blockers, opportunities: opportunities, - stages: stages) - } -} diff --git a/Map/MapParser/Strategies/BlockerParserStrategy.swift b/Map/MapParser/Strategies/BlockerParserStrategy.swift deleted file mode 100644 index d9b6f22..0000000 --- a/Map/MapParser/Strategies/BlockerParserStrategy.swift +++ /dev/null @@ -1,26 +0,0 @@ -import Foundation - -struct BlockerParserStrategy: MapParserStrategy { - private let regex = MapParsingPatterns.blocker - - 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 blocker = Blocker(id: index, position: vertex.position) - return (Blocker.self, blocker) - } - - return (NSObject.self, NSObject()) // No matching object - } -} diff --git a/Map/MapParser/Strategies/EdgeParserStrategy.swift b/Map/MapParser/Strategies/EdgeParserStrategy.swift deleted file mode 100644 index 0df1ff4..0000000 --- a/Map/MapParser/Strategies/EdgeParserStrategy.swift +++ /dev/null @@ -1,32 +0,0 @@ -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/MapParser/Strategies/OpportunityParserStrategy.swift b/Map/MapParser/Strategies/OpportunityParserStrategy.swift deleted file mode 100644 index 104fd30..0000000 --- a/Map/MapParser/Strategies/OpportunityParserStrategy.swift +++ /dev/null @@ -1,33 +0,0 @@ -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/MapParser/Strategies/StageParserStrategy.swift b/Map/MapParser/Strategies/StageParserStrategy.swift deleted file mode 100644 index 42535fc..0000000 --- a/Map/MapParser/Strategies/StageParserStrategy.swift +++ /dev/null @@ -1,24 +0,0 @@ -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/MapParser/Strategies/VertexParserStrategy.swift b/Map/MapParser/Strategies/VertexParserStrategy.swift deleted file mode 100644 index 10c94a2..0000000 --- a/Map/MapParser/Strategies/VertexParserStrategy.swift +++ /dev/null @@ -1,36 +0,0 @@ -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) - } -} |