def test003(self):

        concStore = ConcStore()
        agentFactory = ConcAgentFactory()
                
        N = 2**3
        for _ in range(N):
                
            trainId = TrainId.generateTrainId()
            buildOrder = ConcBuildOrder(100, 1, 2, 10, 32, 128, "test", 10, 2, 0.01, 2**3)
            
            agent = agentFactory.create(buildOrder)
                
            timeSimulation  = 123
            agentMemento = agent.createMemento()        
            timeStamp = "2020-05-05 16:23:59"
            
            storeField = StoreField(timeSimulation, agentMemento, buildOrder, timeStamp);
            
            concStore.save(trainId, storeField)
            
            storeField2 = concStore.load(trainId)
            
            agent2 = agentFactory.create(storeField2.getBuildOrder())
            agent2.loadFromMemento(storeField2.getAgentMemento())
    def test001(self):

        concStore = ConcStore()

        isinstance(concStore, ConcStore)

        trainId = TrainId.generateTrainId()

        timeSimulation = 123
        agentMemento = ConcAgentMemento(saveFilePath="saveFilePathTEST")
        buildOrder = ConcBuildOrder(100, 1, 2, 10, 32, 128, "test", 2)
        timeStamp = "2020-05-05 16:23:59"

        storeField = StoreField(timeSimulation, agentMemento, buildOrder,
                                timeStamp)

        concStore.save(trainId, storeField)

        storeField2 = concStore.load(trainId)

        assert storeField.timeSimulation == storeField2.timeSimulation
        assert storeField.timeStamp == storeField2.timeStamp
        assert storeField.agentMemento.toDict(
        ) == storeField2.agentMemento.toDict()
        assert storeField.buildOrder.toDict() == storeField2.buildOrder.toDict(
        )
Example #3
0
def loadTrainLog(trainLogFolderPath, dbName="trainLog.sqlite"):

    dbAlreadyExists = os.path.exists(dbName)

    conn = sqlite3.connect(dbName)
    cur = conn.cursor()

    if not dbAlreadyExists:
        cur.executescript("""

Create Table SubTableTrainLog
(
    id Integer Primary Key,
    buildOrderId Integer, /* foregin id to the subtable build order */
    trainId   Text Unique, /* accord to a trained agent along with a simulation time*/
    timeSimulation Integer,
    timestamp timestamp
);

Create Table SubTableBuildOrder
(
    id Integer Primary Key,
    buildOrderId Text Unique
);

Create View TrainLog As
    Select 
        t.trainId
        , b.buildOrderId
        , t.timeSimulation
        , t.timestamp
        from SubTableTrainLog t
            inner join SubTableBuildOrder b
                on b.id == t.buildOrderId;
    """)

    concStore = ConcStore()
    for filePath in glob(os.path.join(trainLogFolderPath, "*")):
        trainIdStr = os.path.basename(filePath)

        cur.execute(
            '''
Select Exists (
    Select 1 
        From SubTableTrainLog
        Where trainId = ?
        )''', (trainIdStr, ))

        if cur.fetchone(
        )[0] == 1:  # the given train id has already existed in the DB
            continue

        trainId = TrainId(trainIdStr)

        assert isinstance(trainId, TrainId)
        storeField = concStore.load(
            trainId)  # storeField contains: agentMement, buildOrder
        assert isinstance(storeField, StoreField)

        buildOrderIdStr = storeField.getBuildOrder(
        ).description  # like "abcdefg"

        cur.execute(
            '''
            Insert Or Ignore Into SubTableBuildOrder (
                buildOrderId) values (?)''', (buildOrderIdStr, ))
        cur.execute(
            '''
            Select id
                From SubTableBuildOrder
                Where buildOrderId = ?''', (buildOrderIdStr, ))
        buildOrderIdInt = cur.fetchone()[0]

        cur.execute(
            '''
            Insert Into SubTableTrainLog (
            buildOrderId
            , trainId
            , timeSimulation
            , timestamp)
            values (?, ?, ?, ?)''',
            (buildOrderIdInt, trainIdStr, storeField.getTimeSimulation(),
             datetime.strptime(storeField.getTimeStamp(),
                               "%Y-%m-%d %H:%M:%S")))
    conn.commit()

    return cur
 def test002(self): 
     
     N = 2**10
     X = set([TrainId.generateTrainId().__str__()  for _ in range(N)])
     assert len(X) == N