1 java basics for anylogic 6 dang van son. 2 agenda development environment types & expressions...

12
1 Java Basics for AnyLogic 6 Dang van son

Upload: jodie-skinner

Post on 16-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

1

Java Basics for AnyLogic 6

Dang van son

Page 2: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

2

Agenda

Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects Built-in Functions Main Language Constructs Example

Page 3: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

3

Development Environment

Development environment is Java and Eclipse based application

Simulation applets run in any Java-enabled browser with the following version of JRE

Simulation Application is pure Java application

Page 4: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

4

Types & Expressions

Primitive Types Double –represent real numbers: 1.43,

3.6E18, -14.0 Int –represents integer numbers: 12, 16384, -

5000 Boolean –represents Boolean (true/false)

values Compound Types –Classes

String –represents textual strings ArrayList, LinkedList –collections of objects HyperArray –represents multi-dimensional

array in System Dynamics models …many others.

Page 5: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

5

Types & Expressions

Arithmetic operations Notation: +, –, *, /, %(remainder) In integer divisions, the fraction part is lost, e.g. 3 / 2

equals 1, and 2 / 3 equals 0 Multiplication operators have priority over addition

operators The ‘+‘operator allows operands of type String

Comparison operations Notation: >, >=, <, <=, ==, !=

Boolean operations Notation: &&(AND), ||(OR), !(NOT)

Conditional operator Notation: condition ?value-if-true :value-if-false

Assignments and shortcuts Notation: =, +=, -=, *=, /=, %=, ++, -- Example: a+=b is equivalent to a = a + b

Page 6: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

6

Calling Methods and Accessing Fields

Method call Type its name followed by parenthesis. If

necessary, put parameters separated by commas within the parenthesis. Ex: x = time();

Accessing object fields and methods Use the model element name followed by

dot ‘.’followed by the field/method name. Ex:

statechart.fireEvent( “go”);

Page 7: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

7

Replicated Objects

Replicated objects are stored in a collection

Items are indexed from 0 to N-1 Getting the current size of the collection:

people.size() Obtaining i-thitem of the collection:

people.get( i ) Adding a new object to the collection:

add_people(); Removing an object from the collection:

remove_people( person );

Page 8: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

8

Built-in Functions

System functions time(), getOwner(), pause(), isStateActive(…), etc

Mathematical functions Basic: sqrt, sin, cos, tan, exp, log, round, zidz, xidz,

etc–Array: add, sub, mul, sum, avg, min, max, get, etc

Special functions Random numbers: uniform, exponential, bernoulli,

beta, etc–Time related: delay, etc And more…

See Utilities, Presentable, ActiveObjectand Agentclasses in AnyLogic Class Reference

Page 9: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

9

Main Language Constructs

Assignment / action statement:y = f(x1,x2) + 5*z;event.restart( uniform( 10, 20 ) );

Decision statement:if ( friendsRatio> attackConstant)attack();else {escape();}

Loop statement:double total = 0;for ( Person p : people ) total += p.income;for( inti=0; i<100; i++ ) send( msg, RANDOM );

Page 10: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

10

Example

Page 11: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

11

Page 12: 1 Java Basics for AnyLogic 6 Dang van son. 2 Agenda Development Environment Types & Expressions Calling Methods and Accessing Fields Replicated Objects

12