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)

5, -11, 127

int / java.lang.Integer

long

An integer number (8 bytes)

55l, -225l, 4000l

long / java.lang.Long

float

A real number (4 bytes)

3.14f, -4.567f

float / java.lang.Float

double

A real number (8 bytes)

4.01, -1234.5678

double / java.lang.Double

char

A character

'a', '3', '@'

char / java.lang.Character

boolean

true or false

true, false

boolean / java.lang.Boolean

string

A string of characters

"hello", "12\t34"

java.lang.String

list

A list of values (replaces Java arrays)

[1, 2, 3], ["the", 4, 'a', true]

astra.term.ListTerm (implements java.util.List)

funct

A logical function

fatherOf("rem")

astra.term.Funct

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

X == "kitchen"

~=

not equal

D ~= R

<, <=

less than, less than or equal

N < 10

>, >=

greater than, greater than or equal

N >= 0

“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
}