Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task

Task is an execution body in the task system. Think of it like a line of code that is executed when you use any programming language.

For example the following is a task that simply performs an airthmatic operation:

1 + 2 * 10

Results:

21

Similarly following two tasks call a function, assign a value, and read a value:

sum([1,2,3])
env.x = true;
env.x

Results:

6


true

There are different types of tasks, specially environment, network and node type tasks, and there can be conditional tasks that only execute based on a condition or loops.

So types of task can be summarized as:

  1. Help Task
  2. Exit Task
  3. Attribute Task
  4. Evaluation Task
  5. Function Definition
  6. Conditional
  7. Loops

Attribute and Evaluation Tasks are further divided into env, node and network tasks.

Some examples of different tasks are given below to show a general overview, but the concepts inside the tasks system will be introduced as we progress through the chapters,

Environment tasks that can evaluate expressions, assign variables, or call functions:

env 1 + 2 * 8
env render("my name is {name}", name="John")
env.x = 12 > 2;
env.x

Results:

17

"my name is John"


true

Environment task that just evaluate expressions do not need env keyword. While assignment tasks still need the env keyword.

1 + 2 * 8
render("my name is {name}", name="John")
env.x = 12 > 2;
x

Results:

17

"my name is John"


true

network task loading a network, and node task getting node attributes:

network load_str("a->b\nb->c")
node.NAME

Results:

{
  c = "c",
  b = "b",
  a = "a"
}

Conditional and Loop task

if ( !val? | (val > 5) ) {
	# if val is not defined or greater than 5, set it to 0
    env.val = 0
}
while (val < 5) {
	env.val = env.val + 1;
}
env.val

Results:

5

Tasks system acts like a scripting language for nadi system. A Task consists of getting/evaluating/setting attributes in environment, network or nodes. The value that can be evaluated are expressions that consists of literal values, variables, or function calls that can either be a environment, node or a network function. Functions are unique based on their names, and can have default values if users do not pass all arguments.

The code examples throughout this book, that are being used to generate network diagrams, tables, etc are run using the task system.

Here is an example contents of a more complex task file, do not concern with what each task does, we will go through them in other chapters.

# sample .tasks file which is like a script with functions
node<inputsfirst> print_attrs("uniqueID")
node show_node()
network save_graphviz("/tmp/test.gv")
node<inputsfirst>.cum_val = node.val + sum(inputs.cum_val);

node[WV04113,WV04112,WV04112] print_attr_toml("testattr2")
node render("{NAME} {uniqueID} {Dam_Height_(Ft)}")
node list_attr("; ")
# some functions can take variable number of inputs
network calc_attr_errors(
    "Dam_Height_(Ft)",
    "Hydraulic_Height_(Ft)",
    "rmse", "nse", "abserr"
)
node sum_safe("Latitude")
node<inputsfirst> render("Hi {SUM_ATTR}")
# multiple line for function arguments
network save_table(
	"test.table",
	"/tmp/test.tex",
	true,
	radius=0.2,
	start = 2012-19-20,
	end = 2012-19-23 12:04
	)
node.testattr = 2
node set_attrs_render(testattr2 = "{testattr:2.2}")
node[WV04112] render("{testattr} {testattr2}")


# selecting a list of nodes to run a function
node[
	# comment here?
    WV04113,
    WV04112
] print_attr_toml("testattr2")
# selecting a path
node[WV04112 -> WV04113] render("{NAME}")