FlexSim Supplemental Training: Session 4 20121029 1 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 The Flexsim Tree and Flexscript 1. 2. 3. 4. Trees and Nodes Functions Understanding Modeling events The Modelling Language – Flexscript 5. Model building 3 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 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 Node Symbols Standard Folder Object Object data Function (C++) Function (FlexScript) 6 Sample Model Tree 7 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 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 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 Order of Execution (pushed flowitem) flowitem enters OnEntry Pick Operator Process Time delay OnProcessFinish OnExit delay Setup Time Pick Operator OnSetupFinish Request Transport From delay Send To Port possible delay 11 Access Variables & Returns Edit 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 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 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 Declaring and Setting Variables • • • • int index = 1; double weight = 175.8; string category = “groceries”; treenode forklift = centerobject(current,1); 15 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 y x minus y x times y x divided by y square root of x x to the power of y (xy) x rounded to the nearest integer returns the decimal portion of x returns the remainder of x/y returns minimum of x and y returns maximum of x and y 16 Comparing • • • • • • • x>y x<y x >= y x <= y x == y x != y comparetext(string1,string2) • These values return TRUE or FALSE. 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 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 y add 1 to x subtract 1 from x • Remember: ‘=‘ is not the same as ‘==‘ = is used to set a value == is a comparison operator 18 Relating • && • || • ! logical AND logical OR logical NOT 19 Logical ‘if’ Statement if (test expression) { code block } else { code block } if (content(item) == 2) { colorred(item); } else { colorblack(item); } 20 Logical ‘switch’ Statement int type = getitemtype(item); switch ( switchvariable ) { case casenum: { code block break; } case casenum2: { code block break; } default: { code block break; } } switch (type) { case 1: { coloryellow(item); break; } case 5: { colorred(item); break; } default: { colorgreen(item); break; } } 21 Basic Object Referencing • current • item • model - the current object, the owner of the code the involved flowitem that triggered the event references the model tree • rank(node, ranknum) • first(object) • last(object) rank(current,3) first(current) last(current) • inobject(object, portnum) • outobject(object, portnum) • centerobject(object, portnum) inobject(current,1) outobject(current,1) centerobject(current,1) • node(“relativepath”, object) node(“/Operator1”,model()) 22 Basic Object Statistics • • • • content( object ) getinput( object ) getoutput( object ) getstatenum( object ), setstatenum( object, value ) • int inventory = content(current); • int produced = getoutput(current); 23 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 Logical ‘while’ Statement while (test expression) { code block } 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. 25 Logical ‘for’ Statement for (start expression; test expression; count expression) { //code block } 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 value 2. set a condition to exit the loop 3. how to treat your variable at the end of each iteration Avoid infinite loops by ensuring that the test expression will eventually fail. 26 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 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 Model Layout 29