#======================================= The CMSD_Output object ============================================#
#This object gives as an output the CMSD document of the example topology with the updated calculated values in Operation time and Scrap quantity in the different stations of the production line
defCMSD_example(list1,list2):
#Use of XML.ETREE Python library to create the CMSD document in XML for the CRE production line
#Processes definition in XML-based CMSD. The production planning package contains classes and relationships to create plans for timing of usage of resources
#and describing the sequence of steps to manufacture products using the available resources.
#It defines information such as orders, resources, and operation time description. Properties like the scrap quantity in our case, used in CMSD information model to define characteristics and capabilities of equipment and employees
#In the following script the eleven stations of the CRE line are defined. In each station there is information about the orders, the required resources, the operation time and the scrap quantity.
#================= Main script of KE tool =====================================#
#Read from the given directory the Excel document with the input data
workbook=xlrd.open_workbook('inputData.xls')
worksheets=workbook.sheet_names()
worksheet_ProcessingTimes=worksheets[1]#Define the worksheet with the Processing times data
worksheet_ScrapQuantity=worksheets[0]#Define the worksheet with the Scrap Quantity data
A=Import_Excel()#Call the Python object Import_Excel
ProcessingTimes=A.Input_data(worksheet_ProcessingTimes,workbook)#Create the Processing Times dictionary with keys the different stations in the line and values the processing times of different batches in these stations
ScrapQuantity=A.Input_data(worksheet_ScrapQuantity,workbook)#Create the Scrap Quantity dictionary with keys the different stations in the line and values the scrap quantity data of different batches in these stations
##Get from the Scrap Quantity dictionary the different keys and define the following lists with the scrap quantity data of the different stations in the topology
P7_Scrap=ScrapQuantity.get('P7',[])
P1_Scrap=ScrapQuantity.get('P1',[])
P2_Scrap=ScrapQuantity.get('P3',[])
P3_Scrap=ScrapQuantity.get('P3',[])
P8_Scrap=ScrapQuantity.get('P8',[])
P9_Scrap=ScrapQuantity.get('P9',[])
##Get from the Processing times dictionary the different keys and define the following lists with the processing times data of the different stations in the topology
P7_Proc=ProcessingTimes.get('P7',[])
P1_Proc=ProcessingTimes.get('P1',[])
P2_Proc=ProcessingTimes.get('P2',[])
P3_Proc=ProcessingTimes.get('P3',[])
P8_Proc=ProcessingTimes.get('P8',[])
P9_Proc=ProcessingTimes.get('P9',[])
#Call the HandleMissingValues object and replace with zero the missing values in the lists with the scrap quantity data
B=HandleMissingValues()
P7_Scrap=B.ReplaceWithZero(P7_Scrap)
P1_Scrap=B.ReplaceWithZero(P1_Scrap)
P2_Scrap=B.ReplaceWithZero(P2_Scrap)
P3_Scrap=B.ReplaceWithZero(P3_Scrap)
P8_Scrap=B.ReplaceWithZero(P8_Scrap)
P9_Scrap=B.ReplaceWithZero(P9_Scrap)
# #Call the BasicSatatisticalMeasures object
C=BasicStatisticalMeasures()
#Create a list with values the calculated mean value of scrap quantity on the different stations in the line
#================================================ This script is a simple example of the Knowledge extraction tool ===============================================================#
#The following is the Main script, that calls two Python objects in order to conduct the three main components of the Knowledge extraction tool
#In the following example the operation times of the topology's two machines are given in an Excel document.
#Import_Excel object imports data from the Excel document to the tool and DistFittest object fits the data to a statistical distribution using Kolmogorov-Smirnov test
workbook=xlrd.open_workbook('inputsKEtool.xls')#Using xlrd library opens the Excel document with the input data
worksheets=workbook.sheet_names()
worksheet_OperationTime=worksheets[0]#It creates a variable that holds the first Excel worksheet
X=Import_Excel()#Call the import_Excel object
OperationTimes=X.Input_data(worksheet_OperationTime,workbook)#It defines a Python dictionary, giving as name OpearationTimes and as value the returned dictionary from the import_Excel object
Machine1_OpearationTimes=OperationTimes.get('Machine1',[])#Two lists are defined (Machine1_OpearationTimes, Machine2_OpearationTimes) with the operation times data of each machine
Dict['M1']=B.ks_test(Machine1_OpearationTimes)#It conducts the Kolmogorov-Smirnov test in the list with the operation times data
Dict['M2']=B.ks_test(Machine2_OpearationTimes)
M1=Dict.get('M1')
M2=Dict.get('M2')
#==================================== Output preparation: output the updated values in the CMSD information model of Topology10 ====================================================#
datafile=('CMSD_Topology10.xml')#It defines the name or the directory of the XML file that is manually written the CMSD information model
tree=et.parse(datafile)#This file will be parsed using the XML.ETREE Python library
M1Parameters=[]
M1ParameterValue=[]
forindexinlist(Dict['M1'].keys()):
ifindexisnot'distributionType':
M1Parameters.append(index)
M1ParameterValue.append(Dict['M1'][index])
ifDict['M1']['distributionType']=='Normal':
delM1['min']
delM1['max']
elifDict['M2']['distributionType']=='Normal':
delM2['min']
delM2['max']
M2Parameters=[]
M2ParameterValue=[]
forindexinlist(Dict['M2'].keys()):
ifindexisnot'distributionType':
M2Parameters.append(index)
M2ParameterValue.append(Dict['M2'][index])
root=tree.getroot()
process=tree.findall('./DataSection/ProcessPlan/Process')#It creates a new variable and using the 'findall' order in XML.ETREE library, this new variable holds all the processes defined in the XML file
forprocessinprocess:
process_identifier=process.find('Identifier').text#It creates a new variable that holds the text of the Identifier element in the XML file
ifprocess_identifier=='A020':#It checks using if...elif syntax if the process identifier is 'A020', so the process that uses the first machine
OperationTime=process.get('OpeationTime')#It gets the element attribute OpearationTime inside the Process node
Distribution=process.get('./OperationTime/Distribution')#It gets the element attribute Distribution inside the OpearationTime node
Name=process.find('./OperationTime/Distribution/Name')#It finds the subelement Name inside the Distribution attribute
Name.text=Dict['M1']['distributionType']#It changes the text between the Name element tags, putting the name of the distribution (e.g. in Normal distribution that will be Normal)
Name.text=str(M1Parameters[0])#It changes the text between the Name element tags, putting the name of the distribution's first parameter (e.g. in Normal that will be the mean)
Value.text=str(M1ParameterValue[0])#It changes the text between the Value element tags, putting the value of the distribution's first parameter (e.g. in Normal so for mean value that will be 5.0)
Name.text=str(M1Parameters[1])#It changes the text between the Name element tags, putting the name of the distribution's second parameter (e.g. in Normal that will be the standarddeviation)
Value.text=str(M1ParameterValue[1])#It changes the text between the Value element tags, putting the value of the distribution's second parameter (e.g. in Normal so for standarddeviation value that will be 1.3)
elifprocess_identifier=='A040':#It checks using if...elif syntax if the process identifier is 'A040', so the process that uses the second machine
tree.write('CMSD_Topology10_Output.xml',encoding="utf8")#It writes the element tree to a specified file, using the 'utf8' output encoding
#================================= Output preparation: output the updated values in the JSON file of Topology10 =========================================================#
jsonFile=open('JSON_Topology10.json','r')#It opens the Topology10 JSON file
data=json.load(jsonFile)#It loads the file
jsonFile.close()
nodes=data.get('coreObject',[])#It creates a variable that holds the 'coreObject' list
forelementinnodes:
name=element.get('name')#It creates a variable that gets the element attribute 'name'
processingTime=element.get('processingTime',{})#It creates a variable that gets the element attribute 'processingTime'
ifname=='Machine1':
element['processingTime']=Dict['M1']#It checks using if...elif syntax if the name is 'Machine1', so the first machine in the Topology10
elifname=='Machine2':
element['processingTime']=Dict['M2']
else:
continue
jsonFile=open('JSON_Topology10_Output.json',"w")#It opens the JSON file
jsonFile.write(json.dumps(data,indent=True))#It writes the updated data to the JSON file
jsonFile.close()#It closes the file
#================================ Calling the ExcelOutput object, outputs the outcomes of the statistical analysis in Excel files =============================================#