FlexSim Supplemental Training: Session 4 1 20121029.

29
FlexSim Supplemental Training: Session 4 1 20121029

Transcript of FlexSim Supplemental Training: Session 4 1 20121029.

Page 1: FlexSim Supplemental Training: Session 4 1 20121029.

FlexSim Supplemental Training: Session 4

120121029

Page 2: FlexSim Supplemental Training: Session 4 1 20121029.

2

Session Philosophy • Increase your modelling power through:

– Knowing where and how model data is stored/accessed

– Reusability– Abstraction– Be able to ask yourself: What Event and what

Object will allow me to accomplish what I want?

2

Page 3: FlexSim Supplemental Training: Session 4 1 20121029.

3

The Flexsim Tree and Flexscript

1. Trees and Nodes 2. Functions3. Understanding Modeling events 4. The Modelling Language –

Flexscript5. Model building

3

Page 4: FlexSim Supplemental Training: Session 4 1 20121029.

4

What is a Node?

• Basic data structure of Flexsim is a hierarchal tree– main tree (model and project related objects and data)– view tree (GUI related objects and picklists)– model tree (model related objects and data)

• The node is the basic building block of a tree• Nodes hold all the information behind the scenes

for objects, GUIs and data.

4

Page 5: FlexSim Supplemental Training: Session 4 1 20121029.

5

Node Structure

• Nodes have a name• Nodes can have a data item

– number– string– object

• If nodes have object data, use > to view a separate node list containing the object info (data members and member functions)

• If nodes contain sub nodes, use + to expand and view the child nodes

5

Page 6: FlexSim Supplemental Training: Session 4 1 20121029.

6

Node Symbols

Standard FolderObjectObject dataFunction (C++)Function (FlexScript)

6

Page 7: FlexSim Supplemental Training: Session 4 1 20121029.

7

Sample Model Tree

7

Page 8: FlexSim Supplemental Training: Session 4 1 20121029.

8

What is a function?

functionname(argument1, argument2, etc)

• An argument can be:– Numerical value– String (“text“)– Reference to an object or node

• Function example: colorrandom(item)– (see OnExit trigger of a Source object)

• Many of Flexsim’s functions are used to read data from the tree and save data to the tree.

8

Page 9: FlexSim Supplemental Training: Session 4 1 20121029.

Functions and the ‘return’ statement

• Function ‘calls’ are like asking a question• Input is required of the user and an

answer is given as a ‘return’ value• Picklist properties on Objects are

functions• The return values will mean different

things for each object function, it answers the question asked by the function

9

Page 10: FlexSim Supplemental Training: Session 4 1 20121029.

10

Object Properties

• Understanding the edit fields of an object will help you understand Flexsim.

• You should be able to answer the following four questions about each edit field:– what is its purpose?– when is it evaluated?– what are its access variables?– what does it return?

10

Page 11: FlexSim Supplemental Training: Session 4 1 20121029.

11

Order of Execution(pushed flowitem)

11

OnEntry flowitem enters Setup Time Pick Operator

OnSetupFinishProcess Time

delay

Pick Operator

OnProcessFinish

Request Transport From

OnExit

Send To Port

delay

delaypossible

delay

Page 12: FlexSim Supplemental Training: Session 4 1 20121029.

12

Access Variables & ReturnsEdit Field Access Variables Returns

Setup Time current, item, port delay time

Process Time current, item delay time

Send To Port current, item port number

Pull From Port current port number

Pull Requirement current, item, port true/false (1/0)

Request Transport From current, item, port numeric pointer

Pick Operator current, item numeric pointer

OnReset current N/A

OnMessage current, msgsendingobject, msgparam1, msgparam2, msgparam3

N/A

OnEntry current, item, port N/A

OnExit current, item, port N/A

OnSetupFinish current, item N/A

OnProcessFinish current, item N/A

12

Page 13: FlexSim Supplemental Training: Session 4 1 20121029.

13

General Rules• language is case sensitive (A does not equal a)• no specific format is required (free use of spaces, tabs and line

returns are encouraged for readable code)• text strings are entered between quotes. “mytext”• parenthesis follow a function call and commas separate the

arguments of the function. moveobject(object1, object2);• a function or command will always end with a SEMI-COLON (;)• parenthesis can also be used freely to make associations in

your math and logic statements. Ex: ((x+3)*2)• curly braces { } are used to define a block of statements. • to comment the rest of a line use // and type note here• multi-line comments start with /* and end with */. • don’t use spaces or special characters in name definitions

( _ is ok).• names may include numbers, but may not begin with a number

(machine_9 is acceptable, 9machine is not). 13

Page 14: FlexSim Supplemental Training: Session 4 1 20121029.

14

Variable Types

• int• double• string• treenode

integer (1, 2, 3, 14324)

double precision (2.5, 3.14159)

text string (“Anthony was here.”)

reference to a node in the tree

14

Page 15: FlexSim Supplemental Training: Session 4 1 20121029.

15

Declaring and Setting Variables

• int index = 1;• double weight = 175.8;• string category = “groceries”;• treenode forklift = centerobject(current,1);

15

Page 16: FlexSim Supplemental Training: Session 4 1 20121029.

16

Math Operators

• x + y• x - y• x * y• x / y• sqrt(x)• pow(x,y)• round(x)• frac(x)• fmod(x,y)• min(x,y)• max(x,y)

x plus yx minus yx times yx divided by ysquare root of xx to the power of y (xy)x rounded to the nearest integerreturns the decimal portion of xreturns the remainder of x/yreturns minimum of x and yreturns maximum of x and y

16

Page 17: FlexSim Supplemental Training: Session 4 1 20121029.

17

Comparing

• x > y• x < y• x >= y• x <= y• x == y• x != y• comparetext(string1,string2)

x greater than y

x less than y

x greater than or equal to y

x less than or equal to y

x equal to y

x not equal to y

string1 matches string2

17

• These values return TRUE or FALSE.

Page 18: FlexSim Supplemental Training: Session 4 1 20121029.

18

Updating

• x = y• x += y• x -= y• x *= y• x /= y• x ++• x --

set x to y

set x to x plus y

set x to x minus y

set x to x times y

set x to x divided by yadd 1 to xsubtract 1 from x

18

• Remember: ‘=‘ is not the same as ‘==‘= is used to set a value== is a comparison operator

Page 19: FlexSim Supplemental Training: Session 4 1 20121029.

19

Relating

• &&• ||• !

logical AND logical OR

logical NOT

19

Page 20: FlexSim Supplemental Training: Session 4 1 20121029.

20

Logical ‘if’ Statement

if (test expression)

{

code block

}

else

{

code block

}

20

if (content(item) == 2)

{

colorred(item);

}

else

{

colorblack(item);

}

Page 21: FlexSim Supplemental Training: Session 4 1 20121029.

21

switch ( switchvariable ){

case casenum:{

code blockbreak;

}case casenum2:{

code blockbreak;

}default:{

code blockbreak;

}}

Logical ‘switch’ Statement

21

int type = getitemtype(item);

switch (type){

case 1:{

coloryellow(item);break;

}case 5:{

colorred(item);break;

}default:{

colorgreen(item);break;

}}

Page 22: FlexSim Supplemental Training: Session 4 1 20121029.

22

Basic Object Referencing

• current - the current object, the owner of the code• item - the involved flowitem that triggered the event• model - references the model tree

• rank(node, ranknum) rank(current,3)• first(object) first(current)• last(object) last(current)

• inobject(object, portnum) inobject(current,1)• outobject(object, portnum) outobject(current,1)• centerobject(object, portnum) centerobject(current,1)

• node(“relativepath”, object) node(“/Operator1”,model())22

Page 23: FlexSim Supplemental Training: Session 4 1 20121029.

23

Basic Object Statistics

• content( object )• getinput( object )• getoutput( object )• getstatenum( object ), setstatenum( object, value )

• int inventory = content(current);• int produced = getoutput(current);

23

Page 24: FlexSim Supplemental Training: Session 4 1 20121029.

Accessing Data in the Tree• getvarnum(object, “varname”), setvarnum(object, “varname”, value)• getvarstr(object, “varname”), setvarstr(object, “varname”, string)• getvarnode(object, “varname”)

• getnodenum(node), setnodenum(node, value)• getnodestr(node), setnodestr (node, string)

• labels(object)• itemtype(object)

Many nodes are arranged as tables within objects and can also be accessed with the table commands

• gettablenum(ReferenceToTable, row, col), settablenum() etc.

24

Page 25: FlexSim Supplemental Training: Session 4 1 20121029.

25

Logical ‘while’ Statement

while (test expression)

{

code block

}

25

while (content(current) > 0)

{

destroyobject(last(current));

}

In programming, a while loop is a control structure that allows a code block to be repeated as long as a test expression is true. May also use the break or return statement to force loop execution to halt.

Avoid infinite loops by ensuring that the test expression will eventually fail.

Page 26: FlexSim Supplemental Training: Session 4 1 20121029.

26

Logical ‘for’ Statement

26

for (int index=1; index<=content(current); index++){

colorblue(rank(current,index));}

A for loop allows you to repeat a block of code a set number of times. The head of the for loop defines the repeat conditions:

1. specify a changing variable & give it an initial value2. set a condition to exit the loop3. how to treat your variable at the end of each iteration

Avoid infinite loops by ensuring that the test expression will eventually fail.

for (start expression; test expression; count expression){

//code block}

Page 27: FlexSim Supplemental Training: Session 4 1 20121029.

Model Key Concepts

• Logic Control via Loops and Conditional statements

• Locating, and manipulating data from the Tree, ex. how does the Combiner know how many flowitems to pack?– Combiner component list table shows target quantities from each input port– Additional node in its tree called targetcomponentsum which is the sum of

all target quantities from all input ports. The cumulative number of flowitems to pack across all ports.

– If you need to alter the combiner’s component list dynamically, you need to adjust both values; the value from each port, and the sum of all ports

27

Page 28: FlexSim Supplemental Training: Session 4 1 20121029.

28

Model Description• Purpose

– Practice using FlexScript to accomplish various modeling needs, such as routing, decision making and object manipulation.

• Description– 4 item types, distributed according to the duniform distribution, enter a

conveyor, and are routed to 1 of 2 object groups consisting of a queue, pallet source and a combiner. Write a Send to Port rule that will send 70% of the flowitems to the queue on port 1, and the rest to port 2.

– Combiners will read a Label value off of pallets and pack that many flowitems into the pallet. The label value will vary according to duniform(3, 12). Assume an infinite supply of pallets from the pallet source.

– Loaded pallets are sent to a processor where they will process for a length of time equal to the lognormal2(15, 3.1, 0.5) times the number of flowitems on the pallet.

– Change the color of the boxes inside the pallet according to their itemtype, as they leave the processor. Arrange the colors such that itemtype 1 = green, 2 = red, 3 = orange, and 4 = blue.

– Send pallets to the appropriate sink such that if they have less than or equal to 6 items they go to sink 1 the rest to sink 2.

28

Page 29: FlexSim Supplemental Training: Session 4 1 20121029.

29

Model Layout

29