Commit 138c6b26 authored by Jérome Perrin's avatar Jérome Perrin Committed by Sebastien Robin

loop in a pythonic fashion

parent 2ca4abda
...@@ -40,8 +40,8 @@ def readGeneralInput(): ...@@ -40,8 +40,8 @@ def readGeneralInput():
#creates the simulation objects #creates the simulation objects
def createObjects(): def createObjects():
#Read the json data #Read the json data
coreObject=G.JSONData['coreObject'] coreObjectList = G.JSONData['coreObject']
modelResource=G.JSONData['modelResource'] modelResourceList = G.JSONData['modelResource']
#define the lists #define the lists
G.SourceList=[] G.SourceList=[]
...@@ -55,42 +55,42 @@ def createObjects(): ...@@ -55,42 +55,42 @@ def createObjects():
#loop through all the model resources #loop through all the model resources
#read the data and create them #read the data and create them
for i in range(len(modelResource)): for model_resource in modelResourceList:
resourceClass=modelResource[i].get('_class', 'not found') resourceClass = model_resource.get('_class', 'not found')
if resourceClass=='Dream.Repairman': if resourceClass=='Dream.Repairman':
id=modelResource[i].get('id', 'not found') id = model_resource.get('id', 'not found')
name=modelResource[i].get('name', 'not found') name = model_resource.get('name', 'not found')
capacity=int(modelResource[i].get('capacity', '1')) capacity = int(model_resource.get('capacity', '1'))
R=Repairman(id, name, capacity) R = Repairman(id, name, capacity)
G.RepairmanList.append(R) G.RepairmanList.append(R)
#loop through all the core objects #loop through all the core objects
#read the data and create them #read the data and create them
for i in range(len(coreObject)): for core_object in coreObjectList:
objClass=coreObject[i].get('_class', 'not found') objClass=core_object.get('_class', 'not found')
if objClass=='Dream.Source': if objClass=='Dream.Source':
id=coreObject[i].get('id', 'not found') id=core_object.get('id', 'not found')
name=coreObject[i].get('name', 'not found') name=core_object.get('name', 'not found')
interarrivalTime=coreObject[i].get('interarrivalTime', 'not found') interarrivalTime=core_object.get('interarrivalTime', 'not found')
distributionType=interarrivalTime.get('distributionType', 'not found') distributionType=interarrivalTime.get('distributionType', 'not found')
mean=float(interarrivalTime.get('mean', '0')) mean=float(interarrivalTime.get('mean', '0'))
entity=str_to_class(coreObject[i].get('entity', 'not found')) entity=str_to_class(core_object.get('entity', 'not found'))
successorList=coreObject[i].get('successorList', 'not found') successorList=core_object.get('successorList', 'not found')
S=Source(id, name, distributionType, mean, entity) S=Source(id, name, distributionType, mean, entity)
S.nextIds=successorList S.nextIds=successorList
G.SourceList.append(S) G.SourceList.append(S)
G.ObjList.append(S) G.ObjList.append(S)
elif objClass=='Dream.Machine': elif objClass=='Dream.Machine':
id=coreObject[i].get('id', 'not found') id=core_object.get('id', 'not found')
name=coreObject[i].get('name', 'not found') name=core_object.get('name', 'not found')
processingTime=coreObject[i].get('processingTime', 'not found') processingTime=core_object.get('processingTime', 'not found')
distributionType=processingTime.get('distributionType', 'not found') distributionType=processingTime.get('distributionType', 'not found')
mean=float(processingTime.get('mean', '0')) mean=float(processingTime.get('mean', '0'))
stdev=float(processingTime.get('stdev', '0')) stdev=float(processingTime.get('stdev', '0'))
min=float(processingTime.get('min', '0')) min=float(processingTime.get('min', '0'))
max=float(processingTime.get('stdev', '0')) max=float(processingTime.get('stdev', '0'))
failures=coreObject[i].get('failures', 'not found') failures=core_object.get('failures', 'not found')
failureDistribution=failures.get('failureDistribution', 'not found') failureDistribution=failures.get('failureDistribution', 'not found')
MTTF=float(failures.get('MTTF', '0')) MTTF=float(failures.get('MTTF', '0'))
MTTR=float(failures.get('MTTR', '0')) MTTR=float(failures.get('MTTR', '0'))
...@@ -102,8 +102,8 @@ def createObjects(): ...@@ -102,8 +102,8 @@ def createObjects():
for j in range(len(G.RepairmanList)): for j in range(len(G.RepairmanList)):
if(G.RepairmanList[j].id==needRepairman): if(G.RepairmanList[j].id==needRepairman):
repairman=G.RepairmanList[j] repairman=G.RepairmanList[j]
predecessorList=coreObject[i].get('predecessorList', 'not found') predecessorList=core_object.get('predecessorList', 'not found')
successorList=coreObject[i].get('successorList', 'not found') successorList=core_object.get('successorList', 'not found')
M=Machine(id, name, 1, distributionType, [mean,stdev,min,max], failureDistribution, M=Machine(id, name, 1, distributionType, [mean,stdev,min,max], failureDistribution,
MTTF, MTTR, availability, repairman) MTTF, MTTR, availability, repairman)
M.previousIds=predecessorList M.previousIds=predecessorList
...@@ -112,21 +112,21 @@ def createObjects(): ...@@ -112,21 +112,21 @@ def createObjects():
G.ObjList.append(M) G.ObjList.append(M)
elif objClass=='Dream.Exit': elif objClass=='Dream.Exit':
id=coreObject[i].get('id', 'not found') id=core_object.get('id', 'not found')
name=coreObject[i].get('name', 'not found') name=core_object.get('name', 'not found')
predecessorList=coreObject[i].get('predecessorList', 'not found') predecessorList=core_object.get('predecessorList', 'not found')
E=Exit(id, name) E=Exit(id, name)
E.previousIds=predecessorList E.previousIds=predecessorList
G.ExitList.append(E) G.ExitList.append(E)
G.ObjList.append(E) G.ObjList.append(E)
elif objClass=='Dream.Queue': elif objClass=='Dream.Queue':
id=coreObject[i].get('id', 'not found') id=core_object.get('id', 'not found')
name=coreObject[i].get('name', 'not found') name=core_object.get('name', 'not found')
successorList=coreObject[i].get('successorList', 'not found') successorList=core_object.get('successorList', 'not found')
predecessorList=coreObject[i].get('predecessorList', 'not found') predecessorList=core_object.get('predecessorList', 'not found')
capacity=int(coreObject[i].get('capacity', '1')) capacity=int(core_object.get('capacity', '1'))
isDummy=bool(int(coreObject[i].get('isDummy', '0'))) isDummy=bool(int(core_object.get('isDummy', '0')))
Q=Queue(id, name, capacity, isDummy) Q=Queue(id, name, capacity, isDummy)
Q.previousIds=predecessorList Q.previousIds=predecessorList
Q.nextIds=successorList Q.nextIds=successorList
...@@ -134,17 +134,17 @@ def createObjects(): ...@@ -134,17 +134,17 @@ def createObjects():
G.ObjList.append(Q) G.ObjList.append(Q)
elif objClass=='Dream.Assembly': elif objClass=='Dream.Assembly':
id=coreObject[i].get('id', 'not found') id=core_object.get('id', 'not found')
name=coreObject[i].get('name', 'not found') name=core_object.get('name', 'not found')
processingTime=coreObject[i].get('processingTime', 'not found') processingTime=core_object.get('processingTime', 'not found')
distributionType=processingTime.get('distributionType', 'not found') distributionType=processingTime.get('distributionType', 'not found')
mean=float(processingTime.get('mean', '0')) mean=float(processingTime.get('mean', '0'))
stdev=float(processingTime.get('stdev', '0')) stdev=float(processingTime.get('stdev', '0'))
min=float(processingTime.get('min', '0')) min=float(processingTime.get('min', '0'))
max=float(processingTime.get('stdev', '0')) max=float(processingTime.get('stdev', '0'))
predecessorPartList=coreObject[i].get('predecessorPartList', 'not found') predecessorPartList=core_object.get('predecessorPartList', 'not found')
predecessorFrameList=coreObject[i].get('predecessorFrameList', 'not found') predecessorFrameList=core_object.get('predecessorFrameList', 'not found')
successorList=coreObject[i].get('successorList', 'not found') successorList=core_object.get('successorList', 'not found')
A=Assembly(id, name, distributionType, [mean,stdev,min,max]) A=Assembly(id, name, distributionType, [mean,stdev,min,max])
A.previousPartIds=predecessorPartList A.previousPartIds=predecessorPartList
A.previousFrameIds=predecessorFrameList A.previousFrameIds=predecessorFrameList
...@@ -153,17 +153,17 @@ def createObjects(): ...@@ -153,17 +153,17 @@ def createObjects():
G.ObjList.append(A) G.ObjList.append(A)
elif objClass=='Dream.Dismantle': elif objClass=='Dream.Dismantle':
id=coreObject[i].get('id', 'not found') id=core_object.get('id', 'not found')
name=coreObject[i].get('name', 'not found') name=core_object.get('name', 'not found')
processingTime=coreObject[i].get('processingTime', 'not found') processingTime=core_object.get('processingTime', 'not found')
distributionType=processingTime.get('distributionType', 'not found') distributionType=processingTime.get('distributionType', 'not found')
mean=float(processingTime.get('mean', '0')) mean=float(processingTime.get('mean', '0'))
stdev=float(processingTime.get('stdev', '0')) stdev=float(processingTime.get('stdev', '0'))
min=float(processingTime.get('min', '0')) min=float(processingTime.get('min', '0'))
max=float(processingTime.get('stdev', '0')) max=float(processingTime.get('stdev', '0'))
successorPartList=coreObject[i].get('successorPartList', 'not found') successorPartList=core_object.get('successorPartList', 'not found')
successorFrameList=coreObject[i].get('successorFrameList', 'not found') successorFrameList=core_object.get('successorFrameList', 'not found')
predecessorList=coreObject[i].get('predecessorList', 'not found') predecessorList=core_object.get('predecessorList', 'not found')
D=Dismantle(id, name, distributionType, [mean,stdev,min,max]) D=Dismantle(id, name, distributionType, [mean,stdev,min,max])
D.nextPartIds=successorPartList D.nextPartIds=successorPartList
D.nextFrameIds=successorFrameList D.nextFrameIds=successorFrameList
...@@ -172,12 +172,12 @@ def createObjects(): ...@@ -172,12 +172,12 @@ def createObjects():
G.ObjList.append(D) G.ObjList.append(D)
elif objClass=='Dream.Conveyer': elif objClass=='Dream.Conveyer':
id=coreObject[i].get('id', 'not found') id=core_object.get('id', 'not found')
name=coreObject[i].get('name', 'not found') name=core_object.get('name', 'not found')
length=float(coreObject[i].get('length', '10')) length=float(core_object.get('length', '10'))
speed=float(coreObject[i].get('speed', '1')) speed=float(core_object.get('speed', '1'))
successorList=coreObject[i].get('successorList', 'not found') successorList=core_object.get('successorList', 'not found')
predecessorList=coreObject[i].get('predecessorList', 'not found') predecessorList=core_object.get('predecessorList', 'not found')
C=Conveyer(id, name, length, speed) C=Conveyer(id, name, length, speed)
C.previousIds=predecessorList C.previousIds=predecessorList
C.nextIds=successorList C.nextIds=successorList
...@@ -188,55 +188,55 @@ def createObjects(): ...@@ -188,55 +188,55 @@ def createObjects():
def setTopology(): def setTopology():
#loop through all the objects #loop through all the objects
for i in range(len(G.ObjList)): for core_object in G.ObjList:
next=[] next=[]
previous=[] previous=[]
for j in range(len(G.ObjList[i].previousIds)): for j in range(len(core_object.previousIds)):
for q in range(len(G.ObjList)): for q in range(len(G.ObjList)):
if G.ObjList[q].id==G.ObjList[i].previousIds[j]: if G.ObjList[q].id==core_object.previousIds[j]:
previous.append(G.ObjList[q]) previous.append(G.ObjList[q])
for j in range(len(G.ObjList[i].nextIds)): for j in range(len(core_object.nextIds)):
for q in range(len(G.ObjList)): for q in range(len(G.ObjList)):
if G.ObjList[q].id==G.ObjList[i].nextIds[j]: if G.ObjList[q].id==core_object.nextIds[j]:
next.append(G.ObjList[q]) next.append(G.ObjList[q])
if G.ObjList[i].type=="Source": if core_object.type=="Source":
G.ObjList[i].defineRouting(next) core_object.defineRouting(next)
elif G.ObjList[i].type=="Exit": elif core_object.type=="Exit":
G.ObjList[i].defineRouting(previous) core_object.defineRouting(previous)
#Assembly should be changed to identify what the entity that it receives is. #Assembly should be changed to identify what the entity that it receives is.
#previousPart and previousFrame will become problematic #previousPart and previousFrame will become problematic
elif G.ObjList[i].type=="Assembly": elif core_object.type=="Assembly":
previousPart=[] previousPart=[]
previousFrame=[] previousFrame=[]
for j in range(len(G.ObjList[i].previousPartIds)): for j in range(len(core_object.previousPartIds)):
for q in range(len(G.ObjList)): for q in range(len(G.ObjList)):
if G.ObjList[q].id==G.ObjList[i].previousPartIds[j]: if G.ObjList[q].id==core_object.previousPartIds[j]:
previousPart.append(G.ObjList[q]) previousPart.append(G.ObjList[q])
for j in range(len(G.ObjList[i].previousFrameIds)): for j in range(len(core_object.previousFrameIds)):
for q in range(len(G.ObjList)): for q in range(len(G.ObjList)):
if G.ObjList[q].id==G.ObjList[i].previousFrameIds[j]: if G.ObjList[q].id==core_object.previousFrameIds[j]:
previousFrame.append(G.ObjList[q]) previousFrame.append(G.ObjList[q])
G.ObjList[i].defineRouting(previousPart, previousFrame, next) core_object.defineRouting(previousPart, previousFrame, next)
#Dispatch should be changed to identify what the the successor is. #Dispatch should be changed to identify what the the successor is.
#nextPart and nextFrame will become problematic #nextPart and nextFrame will become problematic
elif G.ObjList[i].type=="Dismantle": elif core_object.type=="Dismantle":
nextPart=[] nextPart=[]
nextFrame=[] nextFrame=[]
for j in range(len(G.ObjList[i].nextPartIds)): for j in range(len(core_object.nextPartIds)):
for q in range(len(G.ObjList)): for q in range(len(G.ObjList)):
if G.ObjList[q].id==G.ObjList[i].nextPartIds[j]: if G.ObjList[q].id==core_object.nextPartIds[j]:
nextPart.append(G.ObjList[q]) nextPart.append(G.ObjList[q])
for j in range(len(G.ObjList[i].nextFrameIds)): for j in range(len(core_object.nextFrameIds)):
for q in range(len(G.ObjList)): for q in range(len(G.ObjList)):
if G.ObjList[q].id==G.ObjList[i].nextFrameIds[j]: if G.ObjList[q].id==core_object.nextFrameIds[j]:
nextFrame.append(G.ObjList[q]) nextFrame.append(G.ObjList[q])
G.ObjList[i].defineRouting(previous, nextPart, nextFrame) core_object.defineRouting(previous, nextPart, nextFrame)
else: else:
G.ObjList[i].defineRouting(previous, next) core_object.defineRouting(previous, next)
#used to convert a string read from the input to object type #used to convert a string read from the input to object type
def str_to_class(str): def str_to_class(str):
...@@ -244,16 +244,16 @@ def str_to_class(str): ...@@ -244,16 +244,16 @@ def str_to_class(str):
#initializes all the objects that are in the topology #initializes all the objects that are in the topology
def initializeObjects(): def initializeObjects():
for j in range(len(G.ObjList)): for core_object in G.ObjList:
G.ObjList[j].initialize() core_object.initialize()
for j in range(len(G.RepairmanList)): for repairman in G.RepairmanList:
G.RepairmanList[j].initialize() repairman.initialize()
#activates all the objects #activates all the objects
def activateObjects(): def activateObjects():
for j in range(len(G.ObjList)): for core_object in G.ObjList:
try: try:
activate(G.ObjList[j],G.ObjList[j].run()) activate(core_object, core_object.run())
except AttributeError: except AttributeError:
pass pass
...@@ -294,8 +294,8 @@ def main(argv=[]): ...@@ -294,8 +294,8 @@ def main(argv=[]):
simulate(until=G.maxSimTime) #start the simulation simulate(until=G.maxSimTime) #start the simulation
#carry on the post processing operations for every object in the topology #carry on the post processing operations for every object in the topology
for j in range(len(G.ObjList)): for core_object in G.ObjList:
G.ObjList[j].postProcessing(G.maxSimTime) core_object.postProcessing(G.maxSimTime)
#output trace to excel #output trace to excel
if(G.trace=="Yes"): if(G.trace=="Yes"):
...@@ -310,8 +310,8 @@ def main(argv=[]): ...@@ -310,8 +310,8 @@ def main(argv=[]):
G.outputIndex+=2 G.outputIndex+=2
#output data to excel for every object in the topology #output data to excel for every object in the topology
for j in range(len(G.ObjList)): for core_object in G.ObjList:
G.ObjList[j].outputResultsXL(G.maxSimTime) core_object.outputResultsXL(G.maxSimTime)
G.outputFile.save("output.xls") G.outputFile.save("output.xls")
print "execution time="+str(time.time()-start) print "execution time="+str(time.time()-start)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment