Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "VIATRA2/Examples/VTCL/Terms"

< VIATRA2‎ | Examples‎ | VTCL
(Arithmetic Terms)
(Arithmetic Terms)
Line 87: Line 87:
  
 
Unary operators:
 
Unary operators:
* Minus: '''---'''
+
* Minus: '''-'''
  
 +
For numerical values, they are treated as in Java, for instance.
 
<source lang="text">
 
<source lang="text">
machine arithmetic_terms {
+
machine arithmetic_terms_1 {
 
  rule main() = seq {
 
  rule main() = seq {
print(3 + 4.0);
+
// Add
print(4.0 + 3);
+
print(3 + 4);
 
+
print(4.0 + 3); // also 3+4.0 is correct
 +
print(5.9 + 4.5);
 +
// Subtract
 
print(3 - 4.0);
 
print(3 - 4.0);
print(4.0 - 3);
+
print(4 - 3);
+
print(4.5 - 5.9);
print(3 * 4.0);
+
// Multiply
 +
print(3 * 4);
 
print(4.0 * 3);
 
print(4.0 * 3);
+
print(5.9 * 4.5);
 +
// Divide
 
print(10 / 4.0);
 
print(10 / 4.0);
 
print(6.0 / 3);
 
print(6.0 / 3);
 +
print(6 / 3);
 +
print(9.0 / 4.5);
 +
// Remainder: for integers
 +
print(14 % 4);
 +
}
 +
}
 +
</source>
 +
 +
Strings can be added with any other kind of terms, but no other operations are allowed.
 +
 +
<source lang="text">
 +
machine arithmetic_terms_2 {
 +
rule main() = seq {
 +
print(3 + "hello");
 +
print(3.9 + "hello");
 +
 +
print("hello" + 4);
 +
print("hello" + 3.4);
 +
print("hello" + "test");
 +
print("hello" + true);
 +
print(false + "hello");
  
 +
print("hello" + one_to_many);
 +
print(many_to_one + "hello");
 
  }
 
  }
 
}
 
}
 
</source>
 
</source>

Revision as of 10:55, 17 February 2009

VTCL Terms and Expressions

Constants

The followind types of constants are available in VTCL:

  • Undefined value constant: undef
  • Boolean constants: true, false
  • Integer constants: 1, -1,
  • Double constants: 2.32, -1.9, (as in Java)
  • String constants: "A string." (as in Java)
  • Model element constants: fully qualified names, e.g. uml.metamodel.Classifier, local names: ejbEntity, 'Class' (local names starting with a capital letter should be included within quotes.
  • Multiplicity constants: one_to_one, one_to_many, many_to_one, many_to_many,

In case of model element constants, a corresponding element should be present in the model space (at compile time), otherwise an error message is issued.

machine vtcl_constants {
 rule main() = seq {
	// Undefined constant
	println(undef);
	// Boolean constants
	println(true);
	println(false);
	// String constants
	print("Hello VIATRA2 world!");
	// Integer constants
	println(4);
	println(-4);
	// Double constants
	print(4.6);
	print(-4.7);
	print(6.6e2);
	print(10e-2);
	// Model elemenent constants 
	// (provided that entity testing.models.manager exists in model space)
	print(fqn(testing.models.manager));
	print(fqn('testing'));
	// Multiplicity constant
	print(one_to_one);
	print(one_to_many);
	print(many_to_one);
	print(many_to_many);
 }
}


Logical Terms

Logical terms consist of usual binary Boolean operators such as "&&" (and), "||" (or), "xor" (exclusive or), and unary operator "!" (not).

The precedence of Boolean operators are as follows: "!" (highest), "&&", { "||"; "xor" } (lowest). Operator precedence can be overridden by parenthesis. All previous operators tie as usual (to the left)

Equality (and inequality) operators, namely, "==" (equal), and "!=" (not equal), are also allowed for Boolean values.

There are two logical constants: "true" and "false".

machine logical_operators{
 rule main() = seq {
	print(!true);
	print(!false);
	print(true && false);
	print(true || false);
	print(true xor false);
	print(true == true);
	print(false != true);
	// The internal pairs of parenthesis are not needed
	print((true && false) || (true  && true));
	print((true == true) != false));
	// The internal pairs of parenthesis are needed
	print((true || false) && (true  ||  false));
	print(true == (true != false));
 }
}

Arithmetic Terms

Arithmetic terms can be constructed by binary and unary operators in the following way:

Binary operators

  • Additive: + (add), - (subtract)
  • Multiplicative: * (multiply), / (divide), % (remainder, only for integers)

Unary operators:

  • Minus: -

For numerical values, they are treated as in Java, for instance.

machine arithmetic_terms_1 {
 rule main() = seq {
	// Add
	print(3 + 4);
	print(4.0 + 3); // also 3+4.0 is correct
	print(5.9 + 4.5);
	// Subtract
	print(3 - 4.0);
	print(4 - 3);
	print(4.5 - 5.9);
	// Multiply	
	print(3 * 4);
	print(4.0 * 3);
	print(5.9 * 4.5);
	// Divide	
	print(10 / 4.0);		
	print(6.0 / 3);
	print(6 / 3);
	print(9.0 / 4.5);
	// Remainder: for integers
	print(14 % 4);
 }
}

Strings can be added with any other kind of terms, but no other operations are allowed.

machine arithmetic_terms_2 {
 rule main() = seq {
	print(3 + "hello");
	print(3.9 + "hello");
 
	print("hello" + 4);
	print("hello" + 3.4);
	print("hello" + "test");
	print("hello" + true);
	print(false + "hello");
 
	print("hello" + one_to_many);
	print(many_to_one + "hello");
 }
}

Back to the top