Lua for TerraME: A Short Introduction Pedro Ribeiro de Andrade Tiago G. S. Carneiro Gilberto Câmara Münster, 2014.

Slides:



Advertisements
Similar presentations
Introducing JavaScript
Advertisements

Programming Languages and Paradigms The C Programming Language.
Introduction to C Programming
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Program Design and Development
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
XP 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial 10.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Javascript II Expressions and Data Types. 2 JavaScript Review programs executed by the web browser programs embedded in a web page using the script element.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
JavaScript, Fourth Edition
Chapter 2: Introduction to C++.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
JavaScript, Third Edition
Introduction to C Programming
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
VB .NET Programming Fundamentals
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
Spatial Dynamical Modeling with TerraME Tiago Carneiro Gilberto Câmara Pedro Andrade Licence: Creative Commons ̶̶̶̶ By Attribution ̶̶̶̶ Non Commercial.
Python Programming Fundamentals
JavaScript, Fifth Edition Chapter 1 Introduction to JavaScript.
1. Reference  2  Algorithm :- Outline the essence of a computational procedure, step by step instructions.  Program :- an.
Introduction to Python Basics of the Language. Install Python Find the most recent distribution for your computer at:
Munster Programming Training
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
1 JavaScript in Context. Server-Side Programming.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
CPS120: Introduction to Computer Science Decision Making in Programs.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
XP Tutorial 10New Perspectives on HTML and XHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties Tutorial.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Python uses boolean variables to evaluate conditions. The boolean values True and False are returned when an expression is compared or evaluated.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
More Lua. Expressions Arithmetic operators */-+ “real” numbers only ^ exponentiation a%b == a - floor(a/b)*b x%1 gives fractional part, x-x%1 integer.
4. Javascript M. Udin Harun Al Rasyid, S.Kom, Ph.D Lab Jaringan Komputer (C-307) Desain.
1 JavaScript in Context. Server-Side Programming.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
12. MODULES Rocky K. C. Chang November 6, 2015 (Based on from Charles Dierbach. Introduction to Computer Science Using Python and William F. Punch and.
Expressions and Data Types Professor Robin Burke.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
JavaScript Tutorial. What is JavaScript JavaScript is the programming language of HTML and the Web Programming makes computers do what you want them to.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Lua for TerraME: A Short Introduction Pedro Ribeiro de Andrade Münster, 2013.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Introdução a Linguagem de Modelagem TerraML Dr. Tiago Garcia de Senna Carneiro TerraLAB - Laboratório INPE/UFOP para Simulação e Modelagem dos Sistemas.
Lua for TerraME: A Short Introduction Pedro Ribeiro de Andrade São José dos Campos, 2011.
PHP using MySQL Database for Web Development (part II)
Definition of the Programming Language CPRL
>> Introduction to JavaScript
Data types and variables
JavaScript Syntax and Semantics
Lua for TerraME: A Short Introduction
Variables ICS2O.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Tutorial 10: Programming with javascript
In this class, we will cover:
PHP an introduction.
CIS 136 Building Mobile Apps
Presentation transcript:

Lua for TerraME: A Short Introduction Pedro Ribeiro de Andrade Tiago G. S. Carneiro Gilberto Câmara Münster, 2014

TerraME uses Lua programming language 

What is Lua?  Yet another scripting language  Simple and flexible  “Simple things simple, complex things possible”  Small, Efficient, Portable  Whole library written in ANSI C, compiles the same source code in all platforms  The tarball for Lua takes 860K uncompressed. The source contains around lines of C. Under Linux, the interpreter takes 153K and the library, 203K.  Several benchmarks show Lua as the fastest language in the realm of interpreted scripting languages.  Free and distributed under MIT license

Few keywords & Clean syntax  The following keywords are reserved and cannot be used as names  The following strings denote other tokens:

Lua and the Web Where is Lua?  Inside Brazil  Petrobras, the Brazilian Oil Company  Embratel (the main telecommunication company in Brazil)  many other companies  Outside Brazil  Lua is used in hundreds of projects, both commercial and academic  CGILua still in restricted use  until recently all documentation was in Portuguese Lua is the language of choice for computer games [Ierusalimschy et al, 1996] source: the Lua team

Variables and Values  Case sensitive  semicolon may optionally follow any statement a = 1 B = a*2 print(a) print(b) print(A) print(B)

Comments  double hyphen (--) until the end of the line.  block comments start with --[[ and run until --]] print("hello") -- my comment -- print("hello") --[[ print(10) -- no action (comment) print(20) -- no action (comment) --]]

Types  nil  boolean  number  string  table  function print(type(3))

nil  Everything that does not exist print(c) c = 10 print(type(c)) c = nil print(type(c)) print(type("a string!!")) print(type(print))

 false/true  nil and false are evaluated as false, everything else is true  zero and the empty string are true  operators and, or, and not (and) (or) print(true) print(true and false) print(true and (false or true)) print(false or (true and false) or (true and true)) boolean

number  the only type for numeric values  double-precision floating-point number  arithmetic operators: +, –, *, /  exponent (^) and modulus (%)  boolean operators (, =, ~=, and ==) A = B = 4e+3 a = A ^ 2 b = A % 7.2 print(A) print(B) print(a) print(b) print(a > b) print(b ~= 2)

Parentheses  Always optional (except in the case of function call)  When in doubt, use parentheses a+-i (a+(-i)) < ((b/2)+1) 5+x^2*8 5+((x^2)*8) a (a < y) and (y <= z) –x^y^z –(x^(y^z))

string  single or double quotes  operator.. print("hello") print('hello') x = 2 print ("x = "..x) print ("x = “,x)

for statement  A statement to repeat other statements, with the following syntax: for var = exp1, exp2, exp3 do something end  That loop will execute something for each value of var from exp1 to exp2, using exp3 as the step to increment var. This third expression is optional (default is 1). for i = 1, 10 do print(i) end for i = 1, 10, 2 do print(i) end

if statement  An if statement tests condition and executes its then-part or its else-part (optional) accordingly a = 6; b = 5 if a < 0 then print("a < 0") end if a = b") end -- if a < b then print("a < b") elseif a < b + 5 then print("b <= a < b+5") else print("a > b+5") end

table  The only structured data type  Can implement arrays -- Defining a table x = {7, 3, 2, 6, 4, 3, 9} -- Referencing elements of a table print(x[1]) x[1] = x[2] + x[3] print(x[1]) -- Length of a table print(#x) -- Traversing a table – FIRST WAY for i = 1, #x do print(i.." "..x[i]) end -- Traversing a table – SECOND WAY for k, v in pairs( x ) do print ("key:"..k,"Value: "..v) end

table  Tables can be indexed not only with numbers, but also with strings or any other value of the language, except nil -- Defining a table loc = { cover = "forest", distRoad = 0.3, distUrban = 2 } -- Referencing elements of a table print(loc["cover"]) print(loc.cover) loc.distRoad = loc.distRoad^2 loc.distTotal = loc.distRoad + loc.distUrban print(loc.distTotal) loc.deforestationPot = 1/loc.distTotal print(loc.deforestationPot)

Tables within tables -- Defining a table loc = { cover = "forest", dist = {road = 0.3, urban = 2} } -- Referencing elements of a table print(loc.dist.road) loc.dist.total = loc.dist.road + loc.dist.urban print(loc.dist.total) -- Traversing a table – SECOND WAY for k, v in pairs( x ) do print ("key: "..k,"Value: "..v) end

function  A function is a named codes snippet that can carry out a specific task (commonly called procedure) or compute and return values.  A function is a first-class value in Lua.  Can be assigned to a variable (even a table)  Can be passed as parameter to other function  Can be the returning value of other function myprint = print print = nil myprint(2) print = myprint

Standard libraries within TerraME  Basic (print, type, tostring, tonumber, …)  String (string.find, string.gsub, string.format, …)  Table (table.insert, table.remove, table.sort, …)  Math (math.random, math.sin, math.exp, …)  IO (io.open, io.read, io.close, …)  OS (os.clock, os.execute, …)  Debug (debug.getinfo, debug.getlocal, …) print(8*9, 9/8) print(math.pi) a = math.sin(math.pi/6) + math.cos(math.pi/3) -- angle in radians print(a) -- 1 print(os.time()) print(string.find("pattern", "tt")) – 3 4

Lua math library  Most important math functions:

Definition of a function -- Syntax 1 function add(a, b) return a + b end print(add(2, 3)) -- Syntax 2 add = function(a, b) return a + b end print(add(2, 3))

Parameters of a function function f(a, b) print(a, b) return a and b -- “and” returns the second argument when the first not false end CALLPARAMETERSRESULT f(false,3)a = false, b = 3false f(3)a = 3, b = nilnil f(3, 4)a = 3, b = 44 f(3, 0, 5)a = 3, b = 0 (5 is discarded) 0

High-order Functions  Functions can also be parameters to other functions. This kind of function is what we call a higher-order function. function forEach(tab, func) for position, value in pairs(tab) do func(value, position) end x = {7, 3, 2, 6, 4} forEach(x, function(element) print(element) end) forEach(x, function(value, position) print(position, value) end)

Tables with functions  Table may have their own functions. loc = { cover = "forest", distRoad = 0.3, distUrban = 0.2, deforestPot = function(myloc) return 1/(myloc.distRoad + myloc.distUrban) end } print(loc.deforestPot(loc)) -- 2 print(loc:deforestPot()) -- 2

Tables with functions  We can declare a “class” in Lua by creating a function that takes a table constructor as argument. function MyLocation(locdata) locdata.covertype = "forest" locdata.deforPot = function(self) return 1/(self.distRoad + self.distUrban) end return locdata end loc = MyLocation({distRoad = 0.3, distUrban = 0.2}) loc = MyLocation{distRoad = 0.3, distUrban = 0.2} print(loc.covertype) print(loc:deforPot())

Some tips to organize your code – Make it look professional !  Indent your code for readability  Comment your code! You may need understand/explain it 1 year later.  Give neaningfull names to variables: YES: soilWater, landCover NO: x1, x2, r3, ss  Avoid magic numbers! Use UPPERCASE constant names in head of your code: STEP = 1, SIMULATION_STEP = 1, EMPTY_SEED_BANK = -11  Follow TerraME naming convention

TerraME naming convention  TerraME adopts American English  neighbor instead of neighbour  Names of types have the upper CamelCase style, starting with a capital letter, followed by other words starting with capitalized letters  Agent, Trajectory, CellularSpace, SocialNetwork.  Functions and variable names have the lower CamelCase style, with names starting with lowercase letters followed by other words starting with capitalized letters  load, database, forEachCell, dbType