From 70f80ff46bbcb58b78e1bec6ff243e57b1221ed6 Mon Sep 17 00:00:00 2001 From: Mattias Holm Date: Tue, 14 Nov 2023 21:41:53 +0100 Subject: [PATCH] Refactor --- Package.swift | 25 +++++------- Sources/{Sim => Simulator}/Component.swift | 5 +-- Sources/{Sim => Simulator}/Connectors.swift | 0 Sources/{Sim => Simulator}/Errors.swift | 0 Sources/{Sim => Simulator}/EventManager.swift | 0 Sources/{Sim => Simulator}/Logger.swift | 0 Sources/{Sim => Simulator}/Model.swift | 38 +++++++++++++++++++ Sources/{Sim => Simulator}/Resolver.swift | 0 Sources/{Sim => Simulator}/Scheduler.swift | 0 Sources/{Sim => Simulator}/Service.swift | 0 .../{SimMacro => Simulator}/SimMacro.swift | 8 ++-- Sources/{Sim => Simulator}/Simulator.swift | 0 Sources/{Sim => Simulator}/TimeKeeper.swift | 0 .../SimMacroMacros.swift | 0 .../SimMacroTest.swift | 12 +++--- .../ClockedModelTest.swift | 2 +- .../ConnectorTest.swift | 2 +- .../EventManagerTest.swift | 2 +- .../LoggerTest.swift | 2 +- .../ResolverTest.swift | 2 +- .../SimulatorTest.swift | 2 +- .../TimeKeeperTest.swift | 2 +- 22 files changed, 66 insertions(+), 36 deletions(-) rename Sources/{Sim => Simulator}/Component.swift (91%) rename Sources/{Sim => Simulator}/Connectors.swift (100%) rename Sources/{Sim => Simulator}/Errors.swift (100%) rename Sources/{Sim => Simulator}/EventManager.swift (100%) rename Sources/{Sim => Simulator}/Logger.swift (100%) rename Sources/{Sim => Simulator}/Model.swift (68%) rename Sources/{Sim => Simulator}/Resolver.swift (100%) rename Sources/{Sim => Simulator}/Scheduler.swift (100%) rename Sources/{Sim => Simulator}/Service.swift (100%) rename Sources/{SimMacro => Simulator}/SimMacro.swift (74%) rename Sources/{Sim => Simulator}/Simulator.swift (100%) rename Sources/{Sim => Simulator}/TimeKeeper.swift (100%) rename Sources/{SimMacroLibrary => SimulatorMacroLibrary}/SimMacroMacros.swift (100%) rename Tests/{SimMacroTests => SimulatorMacroTests}/SimMacroTest.swift (88%) rename Tests/{SimTests => SimulatorTests}/ClockedModelTest.swift (98%) rename Tests/{SimTests => SimulatorTests}/ConnectorTest.swift (97%) rename Tests/{SimTests => SimulatorTests}/EventManagerTest.swift (98%) rename Tests/{SimTests => SimulatorTests}/LoggerTest.swift (99%) rename Tests/{SimTests => SimulatorTests}/ResolverTest.swift (98%) rename Tests/{SimTests => SimulatorTests}/SimulatorTest.swift (98%) rename Tests/{SimTests => SimulatorTests}/TimeKeeperTest.swift (99%) diff --git a/Package.swift b/Package.swift index 77e8c93..3a2d2bd 100644 --- a/Package.swift +++ b/Package.swift @@ -5,17 +5,13 @@ import PackageDescription import CompilerPluginSupport let package = Package( - name: "Sim", + name: "swift-sim", platforms: [.macOS(.v10_15), .iOS(.v13), .tvOS(.v13), .watchOS(.v6), .macCatalyst(.v13)], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( - name: "Sim", - targets: ["Sim"]), - .library( - name: "SimMacro", - targets: ["SimMacro"] - ), + name: "Simulator", + targets: ["Simulator"]), ], dependencies: [ // Dependencies declare other packages that this package depends on. @@ -28,31 +24,30 @@ let package = Package( // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages this package depends on. .macro( - name: "SimMacroLibrary", + name: "SimulatorMacroLibrary", dependencies: [ .product(name: "SwiftSyntaxMacros", package: "swift-syntax"), .product(name: "SwiftCompilerPlugin", package: "swift-syntax") ] ), - .target(name: "SimMacro", dependencies: ["SimMacroLibrary"]), .target( - name: "Sim", + name: "Simulator", dependencies: [ .product(name: "Collections", package: "swift-collections"), .product(name: "OrderedCollections", package: "swift-collections"), - "SimMacro" + "SimulatorMacroLibrary" ]), .testTarget( - name: "SimTests", - dependencies: ["Sim"]), + name: "SimulatorTests", + dependencies: ["Simulator"]), .testTarget( - name: "SimMacroTests", + name: "SimulatorMacroTests", dependencies: [ - "SimMacroLibrary", + "SimulatorMacroLibrary", .product(name: "SwiftSyntaxMacrosTestSupport", package: "swift-syntax"), ] ), diff --git a/Sources/Sim/Component.swift b/Sources/Simulator/Component.swift similarity index 91% rename from Sources/Sim/Component.swift rename to Sources/Simulator/Component.swift index a6c675f..df4a6b6 100644 --- a/Sources/Sim/Component.swift +++ b/Sources/Simulator/Component.swift @@ -18,14 +18,11 @@ @dynamicMemberLookup open class Component : Model { - var models: [String: Model] - public override init(name: String) { - self.models = [:] super.init(name: name) } public subscript(dynamicMember member: String) -> Model? { - return models[member] + return children[member] } } diff --git a/Sources/Sim/Connectors.swift b/Sources/Simulator/Connectors.swift similarity index 100% rename from Sources/Sim/Connectors.swift rename to Sources/Simulator/Connectors.swift diff --git a/Sources/Sim/Errors.swift b/Sources/Simulator/Errors.swift similarity index 100% rename from Sources/Sim/Errors.swift rename to Sources/Simulator/Errors.swift diff --git a/Sources/Sim/EventManager.swift b/Sources/Simulator/EventManager.swift similarity index 100% rename from Sources/Sim/EventManager.swift rename to Sources/Simulator/EventManager.swift diff --git a/Sources/Sim/Logger.swift b/Sources/Simulator/Logger.swift similarity index 100% rename from Sources/Sim/Logger.swift rename to Sources/Simulator/Logger.swift diff --git a/Sources/Sim/Model.swift b/Sources/Simulator/Model.swift similarity index 68% rename from Sources/Sim/Model.swift rename to Sources/Simulator/Model.swift index 7bbab7c..7ba91fd 100644 --- a/Sources/Sim/Model.swift +++ b/Sources/Simulator/Model.swift @@ -18,12 +18,20 @@ import Foundation +public protocol Publisher : Service { + func publishField(name: String, reader: ()->(), writer: ()->()) +} + open class Model { weak var _sim: Simulator! public weak var sim: Simulator! { get { _sim } } weak var parent: Model? var children: [String : Model] var entrypoints: [String : ()->()] = [:] + + var stateWriters: [String : (Any)->()] = [:] + var stateReaders: [String : ()->Any] = [:] + public let name: String public init(name: String) { @@ -31,6 +39,36 @@ open class Model { self.children = [:] } + public func publishFieldReader(name: String, reader: @escaping ()->(Any)) throws { + guard !stateReaders.keys.contains(name) else { + throw SimError.DuplicateName + } + + stateReaders[name] = reader + } + public func publishFieldWriter(name: String, writer: @escaping (Any)->()) throws { + guard !stateWriters.keys.contains(name) else { + throw SimError.DuplicateName + } + + stateWriters[name] = writer + } + public func getField(name: String) throws -> Any { + guard let reader = stateReaders[name] else { + throw SimError.InvalidFieldName + } + + return reader() + } + public func setField(name: String, value: Any) throws + { + guard let writer = stateWriters[name] else { + throw SimError.InvalidFieldName + } + + writer(value) + } + public func add(child: Model) throws { try add(child: child, withName: child.name) } diff --git a/Sources/Sim/Resolver.swift b/Sources/Simulator/Resolver.swift similarity index 100% rename from Sources/Sim/Resolver.swift rename to Sources/Simulator/Resolver.swift diff --git a/Sources/Sim/Scheduler.swift b/Sources/Simulator/Scheduler.swift similarity index 100% rename from Sources/Sim/Scheduler.swift rename to Sources/Simulator/Scheduler.swift diff --git a/Sources/Sim/Service.swift b/Sources/Simulator/Service.swift similarity index 100% rename from Sources/Sim/Service.swift rename to Sources/Simulator/Service.swift diff --git a/Sources/SimMacro/SimMacro.swift b/Sources/Simulator/SimMacro.swift similarity index 74% rename from Sources/SimMacro/SimMacro.swift rename to Sources/Simulator/SimMacro.swift index 1ea5c90..e4c5f01 100644 --- a/Sources/SimMacro/SimMacro.swift +++ b/Sources/Simulator/SimMacro.swift @@ -18,16 +18,16 @@ @attached(member) public macro model(_ value: T) - = #externalMacro(module: "SimMacroLibrary", type: "ModelMacro") + = #externalMacro(module: "SimulatorMacroLibrary", type: "ModelMacro") @attached(member) public macro input(_ value: T) - = #externalMacro(module: "SimMacroLibrary", type: "InputMacro") + = #externalMacro(module: "SimulatorMacroLibrary", type: "InputMacro") @attached(member) public macro output(_ value: T) - = #externalMacro(module: "SimMacroLibrary", type: "OutputMacro") + = #externalMacro(module: "SimulatorMacroLibrary", type: "OutputMacro") @attached(member) public macro field(_ value: T) - = #externalMacro(module: "SimMacroLibrary", type: "FieldMacro") + = #externalMacro(module: "SimulatorMacroLibrary", type: "FieldMacro") diff --git a/Sources/Sim/Simulator.swift b/Sources/Simulator/Simulator.swift similarity index 100% rename from Sources/Sim/Simulator.swift rename to Sources/Simulator/Simulator.swift diff --git a/Sources/Sim/TimeKeeper.swift b/Sources/Simulator/TimeKeeper.swift similarity index 100% rename from Sources/Sim/TimeKeeper.swift rename to Sources/Simulator/TimeKeeper.swift diff --git a/Sources/SimMacroLibrary/SimMacroMacros.swift b/Sources/SimulatorMacroLibrary/SimMacroMacros.swift similarity index 100% rename from Sources/SimMacroLibrary/SimMacroMacros.swift rename to Sources/SimulatorMacroLibrary/SimMacroMacros.swift diff --git a/Tests/SimMacroTests/SimMacroTest.swift b/Tests/SimulatorMacroTests/SimMacroTest.swift similarity index 88% rename from Tests/SimMacroTests/SimMacroTest.swift rename to Tests/SimulatorMacroTests/SimMacroTest.swift index 455bb63..9c761af 100644 --- a/Tests/SimMacroTests/SimMacroTest.swift +++ b/Tests/SimulatorMacroTests/SimMacroTest.swift @@ -9,8 +9,8 @@ import SwiftSyntaxMacros import SwiftSyntaxMacrosTestSupport import XCTest -#if canImport(SimMacroLibrary) -import SimMacroLibrary +#if canImport(SimulatorMacroLibrary) +import SimulatorMacroLibrary let testMacros: [String: Macro.Type] = [ "model": ModelMacro.self, @@ -22,7 +22,7 @@ let testMacros: [String: Macro.Type] = [ final class SimMacroTest: XCTestCase { func testModelMacro() throws { - #if canImport(SimMacroLibrary) + #if canImport(SimulatorMacroLibrary) assertMacroExpansion( """ @model @@ -45,7 +45,7 @@ final class SimMacroTest: XCTestCase { } func testInputMacro() throws { - #if canImport(SimMacroLibrary) + #if canImport(SimulatorMacroLibrary) assertMacroExpansion( """ @input @@ -64,7 +64,7 @@ final class SimMacroTest: XCTestCase { } func testOutputMacro() throws { - #if canImport(SimMacroLibrary) + #if canImport(SimulatorMacroLibrary) assertMacroExpansion( """ @output @@ -83,7 +83,7 @@ final class SimMacroTest: XCTestCase { } func testFieldMacro() throws { - #if canImport(SimMacroLibrary) + #if canImport(SimulatorMacroLibrary) assertMacroExpansion( """ @field diff --git a/Tests/SimTests/ClockedModelTest.swift b/Tests/SimulatorTests/ClockedModelTest.swift similarity index 98% rename from Tests/SimTests/ClockedModelTest.swift rename to Tests/SimulatorTests/ClockedModelTest.swift index 39ffa56..2ff1b73 100644 --- a/Tests/SimTests/ClockedModelTest.swift +++ b/Tests/SimulatorTests/ClockedModelTest.swift @@ -17,7 +17,7 @@ // import XCTest -@testable import Sim +@testable import Simulator class ClockedFoo : Model, ClockedModel { var period: Double = 0.1 diff --git a/Tests/SimTests/ConnectorTest.swift b/Tests/SimulatorTests/ConnectorTest.swift similarity index 97% rename from Tests/SimTests/ConnectorTest.swift rename to Tests/SimulatorTests/ConnectorTest.swift index 0111ea5..5d6bb83 100644 --- a/Tests/SimTests/ConnectorTest.swift +++ b/Tests/SimulatorTests/ConnectorTest.swift @@ -17,7 +17,7 @@ // import XCTest -@testable import Sim +@testable import Simulator class MyModel : Model { @Input public var xInput: Int diff --git a/Tests/SimTests/EventManagerTest.swift b/Tests/SimulatorTests/EventManagerTest.swift similarity index 98% rename from Tests/SimTests/EventManagerTest.swift rename to Tests/SimulatorTests/EventManagerTest.swift index 5150149..e0556a0 100644 --- a/Tests/SimTests/EventManagerTest.swift +++ b/Tests/SimulatorTests/EventManagerTest.swift @@ -17,7 +17,7 @@ // import XCTest -@testable import Sim +@testable import Simulator final class EventManagerTest: XCTestCase { func testPublication() throws { diff --git a/Tests/SimTests/LoggerTest.swift b/Tests/SimulatorTests/LoggerTest.swift similarity index 99% rename from Tests/SimTests/LoggerTest.swift rename to Tests/SimulatorTests/LoggerTest.swift index aa8f635..a51086e 100644 --- a/Tests/SimTests/LoggerTest.swift +++ b/Tests/SimulatorTests/LoggerTest.swift @@ -17,7 +17,7 @@ // import XCTest -@testable import Sim +@testable import Simulator fileprivate class NamedModel : Model { override init(name: String) { diff --git a/Tests/SimTests/ResolverTest.swift b/Tests/SimulatorTests/ResolverTest.swift similarity index 98% rename from Tests/SimTests/ResolverTest.swift rename to Tests/SimulatorTests/ResolverTest.swift index 7f0d068..27e61e3 100644 --- a/Tests/SimTests/ResolverTest.swift +++ b/Tests/SimulatorTests/ResolverTest.swift @@ -17,7 +17,7 @@ // import XCTest -@testable import Sim +@testable import Simulator diff --git a/Tests/SimTests/SimulatorTest.swift b/Tests/SimulatorTests/SimulatorTest.swift similarity index 98% rename from Tests/SimTests/SimulatorTest.swift rename to Tests/SimulatorTests/SimulatorTest.swift index de765a3..235842f 100644 --- a/Tests/SimTests/SimulatorTest.swift +++ b/Tests/SimulatorTests/SimulatorTest.swift @@ -17,7 +17,7 @@ // import XCTest -@testable import Sim +@testable import Simulator class Foo : Model { override init(name: String) { diff --git a/Tests/SimTests/TimeKeeperTest.swift b/Tests/SimulatorTests/TimeKeeperTest.swift similarity index 99% rename from Tests/SimTests/TimeKeeperTest.swift rename to Tests/SimulatorTests/TimeKeeperTest.swift index a07f211..e9928e9 100644 --- a/Tests/SimTests/TimeKeeperTest.swift +++ b/Tests/SimulatorTests/TimeKeeperTest.swift @@ -17,7 +17,7 @@ // import XCTest -@testable import Sim +@testable import Simulator final class TimeKeeperTest: XCTestCase { func testEpochToMissionTime() throws {