본문 바로가기
UiPath/UiPath 공부

Control Flow

by kimyosunnyc 2020. 6. 11.
반응형

출처 : UiPath Academy

 

소프트웨어 프로젝트에서 개별 상태, 설계 또는 기능 호출을 실행하거나 평가하는 순서이다.
Control Flow가 규정되는 컨셉에는 두 가지가 있다.
미리 정의된 Work Flow 유형에는 Sequence, Flow Chart, State Machine, Global Exception Handler가 있다.
우선 Sequence와 FlowChart의 차이점에 대해 알아보자.

 

1. Sequence : 프로세스 단계가 간결하다. Decision Trees는 거의 사용되지 않는다. Sequence의 Activity는 읽고 유지하기가 더 쉬우므로 단순하고 선형적인 Work Flow에 매우 권장된다.

2. Flowchart : 개별적인 Activity는 읽고 편집하는 것이 조금 더 어렵지만, 각 Activity들 사이의 흐름은 훨씬 더 명확하다. 복잡한 시나리오와 workarounds 그리고 decision machanisms를 수용하기 위해 decision point와 branching이 필요할 때 Flowchart를 사용한다.

 

Activities와 Methods는 Work Flow 실행하는 동안 수행할 결정을 정의하는데 사용된다.
가장 일반적인 Control Flow 상태는 if/else decision과 loops 그리고 switch가 있다. 하나씩 살펴보자.

 

if 문

UiPath에서 If문은 정확하게 다음과 같이 예상할 수 있다.

  • 확인된 조건 (두 가지 잠재된 결과 - true or false)
  • 조건이 true일 때 실행 (Then Branch)
  • 조건이 false일 때 실행 (the Else Branch)

다른점은 선택한 프로젝트 Type에 따라 If문 역할을 수행하는 Activity에는 2가지 방법이 있다는 것 이다.

  • Sequence 의 If문
  • Flowcharts 의 Flow Decision

또한 If의 결정은 Activities 안에서 Operator로 사용될 수 있다.

 

If 문을 사용 할 비즈니스 시나리오는 무엇인가?

임의적이지 않은 두 가지 action course가 있을 때마다, If문이 가장 많이 사용 될 것이다.

  • 결제 상태 확인(완료/미완료) 및 사례별 작업 수행
  • Sequence 에서 이전 작업의 결과가 성공적인지 확인
  • 송장 금액을 지불하기에 충분한 돈이 있는지 확인하기 위해 계좌 잔액 확인
  • 요소나 이미지가 존재하는지 등 시스템에 어떤 일이 일어났는지 확인하고, 그에 따른 작업을 수행하는 것

 

DEMO - If 문 (3가지 방법)

MISSION : If문을 사용하여 윤년을 확인한다.

 

1. Sequence 에서의 If문

- Sequence Activity 생성

Sequence에서의 If문

1.1 Sequence Leap Year (Sequence)					
	Private = False				
	Variables				
		year(Int32)			
	Activities				
		1.11 Input Dialog (InputDialog)			
			Title = Year Dialog		
			Label = Please enter the year: 		
			IsPassword = False		
			Result = year		
			Private = False		
		1.2 If Leap year (If)			
			Condition = (year mod 4 = 0 and  year mod 100 <> 0) or (year mod 400 = 0)		
			Private = False		
			Then		
				1.9 Message Box (MessageBox)	
					Content = "Leap Year"
					Buttons = Ok
					TopMost = True
					Private = False
			Else		
				1.7 Message Box (MessageBox)	
					Content = "Not a Leap Year"
					Buttons = Ok
					TopMost = True
					Private = False

 

2. Flowchart에서의 If문

- Flowchart를 생성

Flowchart에서의 If문

1.1 LeapYearFlowchart (Flowchart)			
	StartNode = 1.10 Input Dialog		
	ValidateUnconnectedNodes = False		
	Private = False		
	Variables		
		year(Int32)	
	Nodes		
		1.10 Input Dialog (InputDialog)	
			Title = Year Dialog
			Label = Please enter the year: 
			IsPassword = False
			Result = year
			Private = False
		Is Leap Year (FlowDecision)	
			True = 1.2 Message Box
			False = 1.4 Message Box
			Condition = (year mod 4 = 0 and  year mod 100 <> 0) or (year mod 400 = 0)
			TrueLabel = True
			FalseLabel = False
		1.2 Message Box (MessageBox)	
			Content = "Leap Year"
			Buttons = Ok
			TopMost = True
			Private = False
		1.4 Message Box (MessageBox)	
			Content = "Not a Leap Year"
			Buttons = Ok
			TopMost = True
			Private = False

 

3. VB.NET IF Operator

- Sequence를 생성

VB.Net If Operator

1.1 LeapYearSequence (Sequence)			
	Private = False		
	Variables		
		year(Int32)	
		message(String)	
	Activities		
		1.12 Input Dialog (InputDialog)	
			Title = Year Dialog
			Label = Please enter the year: 
			IsPassword = False
			Result = year
			Private = False
		1.5 Assign message (Assign)	
			To = message
			Value = if ((year mod 4 = 0 and  year mod 100 <> 0) or (year mod 400 = 0) , “Leap Year”, “Not a leap Year”)
			Private = False
		1.2 Message Box (MessageBox)	
			Content = message
			Buttons = Ok
			TopMost = True
			Private = False

 

Loops

Loops는 주어진 조건에 따라 일련의 작업을 반복하는 것이다.

UiPath에서 가장 중요한 Loop는 아래와 같다.

  • Do While : 조건이 충족되는 동안 특정 Sequence를 실행한다. 그 조건은 각 Statements의 실행 후에 평가된다. 예를 들어, 로봇이 웹사이트 새로고침 명령을 수행한 다음 관련 요소가 로드 되었는지 확인할 수 있다. 요소가 로드될 때 까지 새로고침-점검 주기를 계속한다.
  • While : 조건이 충족되는 동안 특정 Sequence를 실행한다. 그 조건은 각 Statements의 실행 전에 평가된다. Do While과 교체 가능한 경우가 많은데, 유일한 차이점은 조건 검증을 할 때이다. 그러나 경우에 따라서는 한쪽이 다른쪽보다 낫다. 예를 들어, 로봇이 블랙잭을 할 경우, 다른 카드를 낼지 결정하기 전에 손을(?) 계산해야 한다.
  • For Each : 수집의 각 요소에 대한 Activity 또는 Activities의 시리즈를 수행한다. 이것은 데이터 처리에 매우 유용하다. 정수의 배열을 고려하라. For Each는 로봇이 각 숫자 항목이 특정 조건을 충족하는지 점검할 수 있다.

 

DEMO - Do While

MISSION : 사용자가 윤년을 입력할 때 까지 실행을 반복하기 위해 위에 작성된 워크플로우(If문)에 Loop를 추가한다.

 

1. Sequence에서의 Do While

Sequence에서의 Do While 예제

  • Do While Activity를 가져온다.
  • Do While Activity 안에 위의 If문 예제 중 Sequence에서의 If문과 동일하게 조건을 만든다.
  • Do While의 Condition에 not((year mod 4 = 0 and year mod 100 <> 0) or (year mod 400 = 0))를 적는다.
  • 실행하여 확인한다.
1.1 Sequence (Sequence)									
	Private = False								
	Variables								
		year(Int32)							
	Activities								
		1.2 Do While (DoWhile)							
			Condition = not((year mod 4 = 0 and  year mod 100 <> 0) or (year mod 400 = 0))						
			Private = False						
			Body						
				1.3 Sequence (Sequence)					
					Private = False				
					Activities				
						1.13 User Input (InputDialog)			
							Title = Year Dialog		
							Label = Please enter the year: 		
							IsPassword = False		
							Result = year		
							Private = False		
						1.4 If (If)			
							Condition = (year mod 4 = 0 and  year mod 100 <> 0) or (year mod 400 = 0)		
							Private = False		
							Then		
								1.11 Message Box Leap Year (MessageBox)	
									Content = "Leap Year"
									Buttons = Ok
									TopMost = True
									Private = False
							Else		
								1.9 Message Box Not a Leap Year (MessageBox)	
									Content = "Not a Leap Year"
									Buttons = Ok
									TopMost = True
									Private = False

 

2. FlowChart에서의 Do While

Flowchart에서의 Do While

  • 위의 If문 예시 중 Flowchart에서의 If문에서 False일 경우 나타나는 Message Box Activity에  False일 경우 Input Dialog로 가는 Arrow를 추가한다.
  • 실행하여 확인한다.
1.1 LeapYearLoopFlowchart (Flowchart)			
	StartNode = 1.10 User Input		
	ValidateUnconnectedNodes = False		
	Private = False		
	Variables		
		year(Int32)	
	Nodes		
		1.10 User Input (InputDialog)	
			Title = Year Dialog
			Label = Please enter the year: 
			IsPassword = False
			Result = year
			Private = False
		Flow Decision (FlowDecision)	
			True = 1.2 Message Box Leap Year
			False = 1.4 Message Box Not a Leap Year
			Condition = (year mod 4 = 0 and  year mod 100 <> 0) or (year mod 400 = 0)
			TrueLabel = True
			FalseLabel = False
		1.2 Message Box Leap Year (MessageBox)	
			Content = "Leap Year"
			Buttons = Ok
			TopMost = True
			Private = False
		1.4 Message Box Not a Leap Year (MessageBox)	
			Content = "Not a Leap Year"
			Buttons = Ok
			TopMost = True
			Private = False

 

DEMO - For Each 

MISSION : For Each 루프와 몇 가지 특정 문자열 Method를 사용하여 지정된 폴더에서 모든 PDF파일의 이름을 수정한다.

For Each 예제

1.1 RenameInvoices (Sequence)							
	Private = False						
	Variables						
		folderPath(String)					
		invoicesArray(String[])					
	Activities						
		1.24 Select Folder (SelectFolder)					
			SelectedFolder = folderPath				
			Private = False				
		1.19 Assign invoicesArray (Assign)					
			To = invoicesArray				
			Value = Directory.GetFiles(folderPath)				
			Private = False				
		1.2 For Each (ForEach<String>)					
			Values = invoicesArray				
			Private = False				
			TypeArgument = System.String				
			Body				
				1.3 Body (Sequence)			
					Private = False		
					Variables		
						newFileName(String)	
					Activities		
						1.12 Assign new file name (Assign)	
							To = newFileName
							Value = filename.Replace(".pdf", Now.ToString("_yyyy_MM_dd")+".pdf")
							Private = False
						1.9 Log Message (LogMessage)	
							Message = "New File Name: "+ newFileName
							Level = Info
							Private = False
						1.4 Move File for Rename (MoveFile)	
							Path = fileName
							Destination = newFileName
							Overwrite = False
							Private = False
INPUT FileName
Bill4344341.pdf
Inv868_55.pdf
Invoice432re.pdf
rawmaterials1.pdf
scan_inv_19_issue.pdf


OUTPUT
LoopsExample execution started
New File Name: C:\Users\Downloads\Demo 5 - For Each (Workflow)\Pre-req\new invoices\Bill4344341_2020_07_03.pdf
New File Name: C:\Users\Downloads\Demo 5 - For Each (Workflow)\Pre-req\new invoices\Inv868_55_2020_07_03.pdf
New File Name: C:\Users\Downloads\Demo 5 - For Each (Workflow)\Pre-req\new invoices\Invoice432re_2020_07_03.pdf
New File Name: C:\Users\Downloads\Demo 5 - For Each (Workflow)\Pre-req\new invoices\rawmaterials1_2020_07_03.pdf
New File Name: C:\Users\Downloads\Demo 5 - For Each (Workflow)\Pre-req\new invoices\scan_inv_19_issue_2020_07_03.pdf
LoopsExample execution ended in: 00:00:12

 

 

Switch

특정 표현값에 기초하여 여러가지 문장으로 구성된 집합을 실행하는 Control Flow문이다.

다시 말해서, 우리는 적어도 3가지의 잠재된 Actions 코스가 필요할 때 If문 대신에 Switch를 사용한다.

이것은 If문과 같이 Boolean값이 아니라 다양한 조건을 통해 이루어진다.

 

Switch를 사용할 비즈니스 시나리오는 무엇일까?

  • 세 가지 잠재적인 상태(시작되지 않음, 보류중, 승인됨)와 각 상태에 대해 3가지 작업 세트가 있는 송장
  • 일정 조건에 따라 4가지 공급 업체에 원료를 자동 발주하는 과정

 

DEMO 1 - Sequence 에서의 Switch

MISSION : 파일 이름에 지정된 연도를 기준으로 폴더에서 송장을 분리하고 Switch를 사용하여 송장을 전용 폴더로 이동한다.

Sequence에서의 Switch

[참고]

- new FileInfo : 파일을 만들고, 복사하고, 삭제하고, 이동하고, 열기 위한 속성 및 인스턴스 메서드를 제공하고, FileStream 개체를 만드는 것을 도와준다. 이 클래스는 상속될 수 없다.

FileInfo 클래스는 파일에 대 한 정보를 검색 하는 데 사용할 수 있는 다음 속성을 제공 한다. 각 속성을 사용 하는 방법의 예에 대 한 속성 페이지를 참조.

  • Directory 속성은 파일의 부모 디렉터리를 나타내는 개체를 검색 한다.
  • DirectoryName 속성은 파일의 부모 디렉터리의 전체 경로를 검색 한다.
  • Exists 속성은 파일에 대 한 작업을 수행 하기 전에 파일의 존재 여부를 확인 한다.
  • IsReadOnly 속성은 파일을 수정할 수 있는지 여부를 지정 하는 값을 검색 하거나 설정 한다.
  • Length는 파일의 크기를 검색 한다.
  • Name는 파일의 이름을 검색 한다.

 

1.1 SwitchSequence (Sequence)										
	Private = False									
	Activities									
		1.2 For Each (ForEach<String>)								
			Values = directory.GetFiles("C:\Users\razvan.petria\Desktop\all invoices")							
			Private = False							
			TypeArgument = System.String							
			Body							
				1.3 Body (Sequence)						
					Private = False					
					Variables					
						file(FileInfo)				
						fileName(String)				
					Activities					
						1.31 Assign - get the FileInfo Type (Assign)				
							To = file			
							Value = new fileinfo(item)			
							Private = False			
						1.26 Assign - get the File Name (Assign)				
							To = fileName			
							Value = file.Name.Split("."c)(0)			
							Private = False			
						1.4 Switch (Switch<String>)				
							Expression = fileName.Substring(fileName.Length-4)			
							Private = False			
							TypeArgument = System.String			
							Cases			
								Case 2015		
									1.22 MoveFile (MoveFile)	
										Path = file.FullName
										Destination = C:\Users\razvan.petria\Desktop\all invoices\invoices 2015\
										Overwrite = False
										Private = False
								Case 2016		
									1.18 MoveFile (MoveFile)	
										Path = file.FullName
										Destination = C:\Users\razvan.petria\Desktop\all invoices\invoices 2016\
										Overwrite = False
										Private = False
								Case 2017		
									1.14 MoveFile (MoveFile)	
										Path = file.FullName
										Destination = C:\Users\razvan.petria\Desktop\all invoices\invoices 2017\
										Overwrite = False
										Private = False
								Case 2018		
									1.10 MoveFile (MoveFile)	
										Path = file.FullName
										Destination = C:\Users\razvan.petria\Desktop\all invoices\invoices 2018\
										Overwrite = False
										Private = False
							Default			
								1.8 Message Box (MessageBox)		
									Content = "No file was found"	
									Buttons = Ok	
									TopMost = True	
									Private = False	

 

DEMO 2 - Flowchart에서의 Switch

Flowchart에서의 Switch

1.1 SwitchFlowchart (Flowchart)				
	StartNode = 1.48 Assign - get array of files			
	ValidateUnconnectedNodes = False			
	Private = False			
	Variables			
		fileArray(String[])		
		index(Int32)		
		file(FileInfo)		
		fileName(GenericValue)		
	Nodes			
		1.48 Assign - get array of files (Assign)		
			To = fileArray	
			Value = directory.GetFiles("C:\Users\cosmin.postolica\Desktop\Invoices")	
			Private = False	
		1.38 Assign (Assign)		
			To = file	
			Value = new fileinfo(fileArray(index))	
			Private = False	
		1.44 Assign (Assign)		
			To = index	
			Value = 0	
			Private = False	
		1.33 Assign (Assign)		
			To = fileName	
			Value = file.Name.Split("."c)(0)	
			Private = False	
		Flow Switch (FlowSwitch<String>)		
			Default = 1.28 Message Box - Default case	
			Expression = fileName.Substring(fileName.Length-4)	
			TypeArgument = System.String	
			Cases	
				Case 2015 = 1.2 Move File 2015
				Case 2016 = 1.6 Move File 2016
				Case 2017 = 1.10 Move File 2017
				Case 2018 = 1.14 Move File 2018
		1.28 Message Box - Default case (MessageBox)		
			Content = "No file found"	
			Buttons = Ok	
			TopMost = True	
			Private = False	
		1.2 Move File 2015 (MoveFile)		
			Path = file.FullName	
			Destination = C:\Users\cosmin.postolica\Desktop\Invoices 2015	
			Overwrite = False	
			Private = False	
		1.6 Move File 2016 (MoveFile)		
			Path = file.FullName	
			Destination = C:\Users\cosmin.postolica\Desktop\Invoices 2016	
			Overwrite = False	
			Private = False	
		1.10 Move File 2017 (MoveFile)		
			Path = file.FullName	
			Destination = C:\Users\cosmin.postolica\Desktop\Invoices 2017	
			Overwrite = False	
			Private = False	
		1.14 Move File 2018 (MoveFile)		
			Path = file.FullName	
			Destination = C:\Users\cosmin.postolica\Desktop\Invoices 2018	
			Overwrite = False	
			Private = False	
		Flow Decision (FlowDecision)		
			True = 1.18 Assign	
			False = 1.23 Message Box	
			Condition = index < fileArray.Length - 1	
			TrueLabel = True	
			FalseLabel = False	
		1.18 Assign (Assign)		
			To = index	
			Value = index+1	
			Private = False	
		1.23 Message Box (MessageBox)		
			Content = "all files were moved"	
			Buttons = Ok	
			TopMost = True	
			Private = False	

 

반응형

댓글