Types and Operators
Types
ASTRA is a typed language whose type system is based on the Java type system. This makes it easy to understand how values in ASTRA are passed to methods in Java. How variables are declared is explained in Variables, Choice and Repetition.
Type |
Description |
Example Values |
Java Mapping |
|---|---|---|---|
int |
An integer number (4 bytes) |
|
|
long |
An integer number (8 bytes) |
|
|
float |
A real number (4 bytes) |
|
|
double |
A real number (8 bytes) |
|
|
char |
A character |
|
|
boolean |
true or false |
|
|
string |
A string of characters |
|
|
list |
A list of values (replaces Java arrays) |
|
|
funct |
A logical function |
|
|
Java class |
A reference to a Java object, where the type is the class name |
no literal form |
Any Java class |
Operators
Values can be combined with the following operators:
Operator |
Meaning |
Applies to |
|---|---|---|
|
multiplication, division, remainder |
numbers |
|
addition, subtraction |
numbers |
|
concatenation |
strings (and lists) |
*, / and % are applied before + and -, and operators of the same level are applied from left to right, as in Java:
int a = 10 - 3 - 2; // 5, i.e. (10 - 3) - 2
int b = 24 / 4 / 2; // 3
int c = 2 + 3 * 4; // 14
int d = (2 + 3) * 4; // 20
int e = 2 * -3; // -6
When one side of a + is a string, the other side is converted to a string, so + can be used to build messages from any kind of value, including funct, list and speechact values. Because evaluation is from left to right, "total: " + 1 + 2 is "total: 12", while 1 + 2 + " in total" is "3 in total"; use brackets to add the numbers first: "total: " + (1 + 2). Using + on two lists joins them: [1, 2] + [3] is [1, 2, 3].
An expression can only be evaluated once all of its variables have values. If a variable has not been bound (for example, because a sub-goal that was meant to set it did not), the statement that uses the expression fails with a message such as Cannot evaluate: "total: "+N - unbound variable(s): N. Like other failures, this can be handled with try/recover or a failure plan (see Failure Handling).
Comparisons
Two values are compared with the following operators, which give a formula that is true or false. They can be used wherever a formula can: in the context of a rule, in an if or while condition, and in a query.
Operator |
Meaning |
Example |
|---|---|---|
|
equal |
|
|
not equal |
|
|
less than, less than or equal |
|
|
greater than, greater than or equal |
|
“Not equal” is written ~=, as ~ is ASTRA’s “not”. ASTRA has no !=: from ASTRA 2.8.3, the compiler reports D != R as ASTRA has no != operator: write "not equal" as ~=.
rule +!go(string Room) : at(string Here) & Here ~= Room {
// move towards Room
}