Commit 7a974453 authored by panos's avatar panos Committed by Georgios Dagkakis

Insert the required scripts for the implementation of the DB approach in CapacityStations model

parent 1eabf447
# ===========================================================================
# Copyright 2013 University of Limerick
#
# This file is part of DREAM.
#
# DREAM is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DREAM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DREAM. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
'''
Created on 20 March 2015
@author: Panos
'''
import Tkinter as tk
from Tkinter import *
import pyodbc
import tkMessageBox
from datetime import datetime
class Demo1(Frame):
def __init__(self):
tk.Frame.__init__(self)
self.pack()
self.master.title("CapacityStations Interface")
if not self.checkInsertedProject():
self.labelText = StringVar()
self.labelText.set('There is no new order inserted in the system, please insert one and try again')
label5 = Label(self, textvariable=self.labelText, height=5)
label5.pack()
else:
self.labelText = StringVar()
self.labelText.set('Please follow the instructions to update the system.')
label1 = Label(self, textvariable=self.labelText, height=2)
label1.pack()
self.labelText = StringVar()
self.labelText.set('1. Please select order')
label2 = Label(self, textvariable=self.labelText, height=2)
label2.pack()
self.OrderOption = StringVar()
self.OrderOption.set(None)
options = self.checkInsertedProject()
self.ProjectDropDown = OptionMenu(self, self.OrderOption, *options).pack()
self.labelText = StringVar()
self.labelText.set('2. Please select the operation')
label4 = Label(self, textvariable=self.labelText, height=2)
label4.pack()
self.operationOption = StringVar()
self.operationOption.set(None)
options = ['SMF', 'WELD', 'CNC', 'MCH', 'EEP', 'PPASB', 'PAINT', 'ASBTST']
self.operationDropDown = OptionMenu(self, self.operationOption, *options).pack()
self.labelText = StringVar()
self.labelText.set('3. Please click below only once, when you insert a WP_id. \n If you have already clicked, please go to next step no 4')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.checkBoxVal1 = IntVar()
checkBox1 = Checkbutton(self, variable=self.checkBoxVal1, text='Click to record the START DATE', height=3, command=self.recordStartDate)
checkBox1.pack()
self.labelText = StringVar()
self.labelText.set('4. Please insert estimated capacity left, to be completed')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.capleft = StringVar(None)
self.capacity = Entry(self, textvariable=self.capleft)
self.capacity.pack()
self.labelText = StringVar()
self.labelText.set('5. Please insert any comments')
label2 = Label(self, textvariable=self.labelText, height=2)
label2.pack()
self.remark = StringVar(None)
self.comments = Entry(self, textvariable=self.remark)
self.comments.pack()
self.labelText = StringVar()
self.labelText.set('6. Please click below only when the operation finished')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.checkBoxVal2 = IntVar()
checkBox = Checkbutton(self, variable=self.checkBoxVal2, text='Click to record the END DATE', height=3, command=self.recordEndDate)
checkBox.pack()
self.labelText = StringVar()
self.labelText.set('7. Please update the system clicking the button below')
label6 = Label(self, textvariable=self.labelText, height=2)
label6.pack()
self.button4 = Button(self, text='Update the system', width=20, command=self.updateDatabase)
self.button4.pack()
def recordStartDate(self):
self.startdate = str(datetime.now())
print self.startdate
return
def recordEndDate(self):
self.endDate = str(datetime.now())
print self.endDate
return
def checkInsertedProject(self):
cnxn =pyodbc.connect("Driver={MySQL ODBC 3.51 Driver};SERVER=localhost; PORT=3306;DATABASE=bal_database;UID=root; PASSWORD=Pitheos10;")
cursor1 = cnxn.cursor()
cursor2 = cnxn.cursor()
a=cursor1.execute("""
select Order_id, ProjectName, Status
from orders
""")
availableProject=[]
for j in range(a.rowcount):
#get the next line
ind1=a.fetchone()
#and create a dictionary order
if ind1.Status == 'in progress' or ind1.Status =='accepted':
availableProject.append(ind1.Order_id)
return availableProject
def alreadyInsertedWP(self):
cnxn =pyodbc.connect("Driver={MySQL ODBC 3.51 Driver};SERVER=localhost; PORT=3306;DATABASE=bal_database;UID=root; PASSWORD=Pitheos10; ")
cursor = cnxn.cursor()
c=cursor.execute("""
select WP_id, END_DATE
from production_status
""")
listb=[]
for i in range(c.rowcount):
ind=c.fetchone()
if ind.WP_id:
listb.append(ind.WP_id)
else:
continue
return
def updateDatabase(self):
cnxn =pyodbc.connect("Driver={MySQL ODBC 3.51 Driver};SERVER=localhost; PORT=3306;DATABASE=bal_database;UID=root; PASSWORD=Pitheos10; ")
cursor = cnxn.cursor()
cursor1 = cnxn.cursor()
if self.checkBoxVal2.get():
update_order= ("INSERT INTO production_status(`status_id`, `WP_id`, `Operation_Name`, `START_DATE`, `Capacity_left`, `Remarks`,`END_DATE`) VALUES ( ?, ?, ?, ?, ?, ?, ?)")
cursor.execute("SELECT @@IDENTITY AS ID")
order = self.OrderOption.get()
a = cursor1.execute("""
select WP_id, Order_id
from sequence where Order_id=?
""", order)
for j in range(a.rowcount):
ind2=a.fetchone()
lastWP =ind2.WP_id
print lastWP
b = cursor1.execute("""
select sequence.WP_id, sequence.Order_id
from sequence where Operation_Name=?
""", self.operationOption.get())
ind4=b.fetchone()
status2 = 'finished'
row = cursor.fetchone()
WP=ind4[0]
order_ref = row.ID
status1 = 'in progress'
cursor.execute(update_order, (order_ref, WP, self.operationOption.get(), str(datetime.now()), self.capacity.get(), self.comments.get(),str(datetime.now()) ))
if WP == lastWP:
cursor.execute("UPDATE orders SET `Status`=? WHERE Order_id=? ", status2, order)
cursor.commit()
self.close_window()
else:
update_order= ("INSERT INTO production_status(`status_id`, `WP_id`, `Operation_Name`, `START_DATE`, `Capacity_left`, `Remarks`) VALUES ( ?, ?, ?, ?, ?, ?)")
cursor.execute("SELECT @@IDENTITY AS ID")
order = self.OrderOption.get()
a = cursor1.execute("""
select sequence.WP_id, sequence.Order_id, sequence.Operation_Name
from sequence where Order_id=? and Operation_Name=?
""", order, self.operationOption.get())
ind3=a.fetchone()
WP=ind3.WP_id
row = cursor.fetchone()
order_ref = row.ID
status1 = 'in progress'
cursor.execute(update_order, (order_ref, WP, self.operationOption.get(), str(datetime.now()), self.capacity.get(), self.comments.get()))
cursor.execute("UPDATE orders SET `Status`=? WHERE Order_id=? ", status1, order)
cursor.commit()
self.close_window()
return
def close_window(self):
self.destroy()
def main():
Demo1().mainloop()
if __name__ == '__main__':
main()
\ No newline at end of file
......@@ -20,14 +20,19 @@
import json
from datetime import datetime
configuration=open("PilotCases\CapacityStations\sampleConfiguration.json", "r")
# configuration=open("sampleConfiguration.json", "r")
# gets the data and the station id and returns the buffer id of this station
def getBuffer(data,stationId):
for (edge_id, edge) in data['graph']['edge'].iteritems():
if edge['destination']==stationId:
return edge['source']
# configuration=open("PilotCases\CapacityStations\sampleConfiguration.json", "r")
configuration=open("sampleConfiguration.json", "r")
configurationData=configuration.read()
configurationJSON=json.loads(configurationData)
db=open("PilotCases\CapacityStations\sampleDBExtraction.json", "r")
# configuration=open("sampleConfiguration.json", "r")
# db=open("PilotCases\CapacityStations\sampleDBExtraction.json", "r")
db=open("sampleDBExtraction.json", "r")
dbData=db.read()
dbJSON=json.loads(dbData)
operations=dbJSON.get('operations',{})
......@@ -75,7 +80,27 @@ for order in orders:
"orderDate":orderDate
})
WIP=dbJSON.get('WIP',{})
if WIP:
node=configurationJSON['graph']['node']
#create an empty wip list in all CapacityStationBuffers
for (node_id,node_data) in node.iteritems():
if node_data['_class']=='dream.simulation.applications.CapacityStations.CapacityStationBuffer.CapacityStationBuffer':
node_data['wip']=[]
for taskid, wip in WIP.iteritems():
oper=wip.get('operation',[])
requiredCapacity=wip.get('Capacity required', 0)
buffered=wip.get('buffered', 0)
orderId=wip.get('order_id',[])
if requiredCapacity or buffered:
capacityBuffer=getBuffer(configurationJSON,oper)
configurationJSON['graph']['node'][capacityBuffer]['wip'].append({
"_class": "dream.simulation.applications.CapacityStations.CapacityEntity.CapacityEntity",
"requiredCapacity": max([requiredCapacity,buffered]),
"capacityProjectId": orderId,
"name": orderId+'_'+oper+'_'+str(requiredCapacity)
})
updatedModelJSONString=json.dumps(configurationJSON, indent=5)
updatedModel=open('UpdatedModel.json', mode='w')
updatedModel.write(updatedModelJSONString)
......
# ===========================================================================
# Copyright 2013 University of Limerick
#
# This file is part of DREAM.
#
# DREAM is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DREAM is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with DREAM. If not, see <http://www.gnu.org/licenses/>.
# ===========================================================================
import xlrd
import pyodbc
import datetime
import os
import ImportDatabase
#Static variables that hold the column's number in Workplan (Excel document - template)
WPColumn=11
OrderColumn=0
ProjectColumn=2
FloorSpaceReq=5
DayColumn=0
#Method created to extract the required data from the spreadsheet
def dataExtraction(File):
# loop through all the sheets
for sheet in xls_workbook.sheets():
if sheet.name=='Workplan':
orderId=sheet.cell(4,0).value
customer=sheet.cell(4,1).value
projectName=sheet.cell(4,2).value
orderDate=datetime.datetime.utcfromtimestamp((sheet.cell(4,3).value - 25569) * 86400.0)
dueDate=datetime.datetime.utcfromtimestamp((sheet.cell(4,4).value - 25569) * 86400.0)
floorSpace=sheet.cell(4,5).value
#create a dictionary called workplan with key the order id
workplan[orderId]={}
workplan[orderId][projectName]=[]
workplan[orderId][projectName].insert(0, customer)
workplan[orderId][projectName].insert(1, orderDate)
workplan[orderId][projectName].insert(2, dueDate)
workplan[orderId][projectName].insert(3, floorSpace)
#loop in the rows of the spreadsheet
for i in range(4,sheet.nrows):
#check and continue the loop if there are WP_ids in column 11.
if sheet.cell(i,11).value:
wpId=sheet.cell(i,11).value
partId=sheet.cell(i,6).value
partType=sheet.cell(i,7).value
operation=sheet.cell(i,8).value
capacityRequired=sheet.cell(i,9).value
earliestStart=datetime.datetime.utcfromtimestamp((sheet.cell(i,10).value - 25569) * 86400.0)
workplan[orderId][wpId]=[]
workplan[orderId][wpId].insert(0, partId)
workplan[orderId][wpId].insert(1, partType)
workplan[orderId][wpId].insert(2, operation)
workplan[orderId][wpId].insert(3, capacityRequired)
workplan[orderId][wpId].insert(4, earliestStart)
#pop-up message that inform the user that he should insert the directory where he stores the workplans
folder_path=raw_input('insert the path to the folder containing the required files:')
print folder_path
os.chdir(folder_path)
#create a list that hold the already inserted orders
alreadyInserted=[]
#use of the KE tool object to connect to database
cnxn=ImportDatabase.ConnectionData(seekName='ServerData', implicitExt='txt', number_of_cursors=6)
cursor=cnxn.getCursors()
#loop that searches the files in the given directory
for fileName in os.listdir("."):
print fileName
#using the xlrd python library, we open the documents
xls_workbook = xlrd.open_workbook(fileName)
#define Main the first sheet - sheet with the name 'Workplan'
Main= xls_workbook.sheet_by_name('Workplan')
workplan={}
#SQL query to extract data from orders table
check_order= cursor[0].execute("""
select Order_id, ProjectName, Customer, Order_date, Due_date,FloorSpaceRequired,Status
from orders
""")
#check if rowcount is 0, if it is move on - otherwise go to the else part
if check_order.rowcount==0:
#call the method giving as argument the document
dataExtraction(fileName)
for i in range(4,Main.nrows):
orderId=Main.cell(4,OrderColumn).value
projectName=Main.cell(4,ProjectColumn).value
#Another SQL query to input data this time in the database in orders table
add_order= ("INSERT INTO orders(`id`, `Order_id`, `ProjectName`, `Customer`, `Order_date`, `Due_date`, `FloorSpaceRequired`, `Status`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
cursor[0].execute("SELECT @@IDENTITY AS ID")
row = cursor[0].fetchone()
order_ref = row.ID
status= 'accepted'
#the following data inserted to the the database based on the structure of the query above
cursor[0].execute(add_order, (order_ref, orderId, projectName, workplan[orderId][projectName][0], workplan[orderId][projectName][1], workplan[orderId][projectName][2], workplan[orderId][projectName][3], status))
cursor[0].commit()
#SQL query to input data in the database in sequence table
add_sequence= ("INSERT INTO sequence(`seq_id`, `WP_id`, `Order_id`, `PartCode`, `PartName`, `Operation_Name`, `CapacityRequirement`, `EarliestStart`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ")
cursor[1].execute("SELECT seq_id AS ID from sequence")
seq_ref = row.ID
#loop used to insert the required data based on the structure of the above query
for i in range(4,Main.nrows):
wpId=Main.cell(i,WPColumn).value
cursor[1].execute(add_sequence, (seq_ref, wpId, orderId, workplan[orderId][wpId][0], workplan[orderId][wpId][1], workplan[orderId][wpId][2], workplan[orderId][wpId][3], workplan[orderId][wpId][4]))
cursor[1].commit()
seq_ref+=1
else:
#follows the logic of the if part
check_order= cursor[3].execute("""
select Order_id, ProjectName, Customer, Order_date, Due_date,FloorSpaceRequired
from orders
""")
for j in range(check_order.rowcount):
#get the next line
ind1=check_order.fetchone()
dataExtraction(fileName)
for i in range(4,Main.nrows):
orderId=Main.cell(4,OrderColumn).value
projectName=Main.cell(4,ProjectColumn).value
#check the existence of orderId
if not orderId in ind1.Order_id:
add_order= ("INSERT INTO orders(`id`, `Order_id`, `ProjectName`, `Customer`, `Order_date`, `Due_date`, `FloorSpaceRequired`, `Status`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)")
cursor[3].execute("SELECT MAX(id)+1 AS ID from orders")
row = cursor[3].fetchone()
order_ref = row.ID
status= 'accepted'
cursor[3].execute(add_order, (order_ref, orderId, projectName, workplan[orderId][projectName][0], workplan[orderId][projectName][1], workplan[orderId][projectName][2], workplan[orderId][projectName][3], status))
cursor[3].commit()
add_sequence= ("INSERT INTO sequence(`seq_id`, `WP_id`, `Order_id`, `PartCode`, `PartName`, `Operation_Name`, `CapacityRequirement`, `EarliestStart`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ")
cursor[4].execute("SELECT MAX(seq_id)+1 AS ID from sequence")
row = cursor[4].fetchone()
seq_ref = row.ID
for i in range(4,Main.nrows):
wpId=Main.cell(i,WPColumn).value
cursor[4].execute(add_sequence, (seq_ref, wpId, orderId, workplan[orderId][wpId][0], workplan[orderId][wpId][1], workplan[orderId][wpId][2], workplan[orderId][wpId][3], workplan[orderId][wpId][4]))
cursor[4].commit()
seq_ref+=1
else:
continue
\ No newline at end of file
......@@ -171,21 +171,129 @@
"dueDate": "2014-08-25",
"orderName": "italy",
"orderDate": "2014-08-19"
},
{
"orderID": "Touto",
"customer": "t2",
"floorSpaceRequired": 300,
"sequence": [
{
"SMF": {
"operation": "SMF",
"earliestStart": "2014-08-23",
"task_id": "ID-009",
"requiredCapacity": 40
}
},
{
"WELD": {
"operation": "WELD",
"earliestStart": "2014-08-24",
"task_id": "ID-010",
"requiredCapacity": 25
}
},
{
"CNC": {
"operation": "CNC",
"earliestStart": "2014-08-25",
"task_id": "ID-011",
"requiredCapacity": 55
}
},
{
"MCH": {
"operation": "MCH",
"earliestStart": "2014-08-26",
"task_id": "ID-012",
"requiredCapacity": 35
}
},
{
"EEP": {
"operation": "EEP",
"earliestStart": "2014-08-27",
"task_id": "ID-013",
"requiredCapacity": 25
}
},
{
"PPASB": {
"operation": "PPASB",
"earliestStart": "2014-08-28",
"task_id": "ID-014",
"requiredCapacity": 35
}
},
{
"PAINT": {
"operation": "PAINT",
"earliestStart": "2014-08-29",
"task_id": "ID-015",
"requiredCapacity": 45
}
},
{
"ASBTST": {
"operation": "ASBTST",
"earliestStart": "2014-08-30",
"task_id": "ID-016",
"requiredCapacity": 55
}
}
],
"dueDate": "2014-08-31",
"orderName": "ecofil",
"orderDate": "2014-08-21"
}
],
"WIP": {
"WELD_idTade": {
"ID-002": {
"End date": "None",
"Total Capacity required": 15,
"Capacity required": 7.5,
"order_id": "Tade",
"buffered": 10.0,
"operation": "WELD"
"operation": "WELD",
"Start date": "2015-03-20"
},
"ID-003": {
"End date": "None",
"Total Capacity required": 45,
"Capacity required": 5,
"order_id": "Tade",
"operation": "CNC",
"Start date": "2015-03-20"
},
"ID-001": {
"End date": "None",
"Total Capacity required": 30,
"Capacity required": 10,
"Capacity required": 5,
"order_id": "Tade",
"operation": "SMF",
"Start date": "2015-03-13"
"Start date": "2015-03-17"
},
"WELD_idTouto": {
"order_id": "Touto",
"buffered": 17.5,
"operation": "WELD"
},
"ID-009": {
"End date": "None",
"Total Capacity required": 40,
"Capacity required": 5,
"order_id": "Touto",
"operation": "SMF",
"Start date": "2015-03-20"
},
"PPASB_idTade": {
"order_id": "Tade",
"buffered": 45,
"operation": "PPASB"
},
"WELD_idTade": {
"order_id": "Tade",
"buffered": 12.5,
"operation": "WELD"
}
}
}
\ No newline at end of file
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