<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<!-- Generated from latex, don't update this file, update -->
<!-- ptIIdoc/doc/design/src/tex/expression.tex -->
<meta name="LASTUPDATED" content="$Id: expressionsa6_5.htm 57603 2010-04-16 22:15:50Z cxh $">
<link href="default.css" rel="stylesheet" type="text/css">
<title>Casting</title>
</head>
<body link="#3366CC" vlink="#9999CC" text="#000000" alink="#0000CC" bgcolor="#FFFFFF"
<table width="331" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td><a href="expressions.htm"><img src="img/navtoc.gif" width="84" height="23"
border="0" alt="TOC"> </a></td>
<td><a href="expressionsa5.htm"><img src="img/navprev.gif" width="81" height="23"
border="0" alt="PREV"> </a></td>
<td><a href="expressionsa7.htm"><img src="img/navnext.gif" width="81" height="23"
border="0" alt="NEXT"> </a></td>
</tr>
</table>
<blockquote>
<div class="p"><!----></div>
<h2><a name="tth_sEc4">
6</a> Casting</h2>
<div class="p"><!----></div>
The cast function can be used to explicitly cast a value into a type.
<div class="p"><!----></div>
When the cast function is invoked with <tt>cast(type, value)</tt>, where <tt>type</tt>
is the target type and <tt>value</tt> is the value to be cast, a new value is
returned (if a predefined casting is applicable) that is in the specified type.
For example, <tt>cast(long, 1)</tt> yields 1L, which is equal to 1 but is in the
long data type, and <tt>cast(string, 1)</tt> yields "1", which is in the string
data type.
<div class="p"><!----></div>
<h3><a name="tth_sEc6.1">
6.1</a> Object Types</h3>
<div class="p"><!----></div>
An object token encapsulates a Java object. Methods defined in the Java class of
that object can be invoked in an expression. For example, in a model that
contains an actor named <tt>C</tt>, <tt>C</tt> in an expression may refer to that actor
in an object token.
<div class="p"><!----></div>
An object token has a type, which is an object type that is specific for the
class of the encapsulated Java object or any class that is a superclass of that
class. For example, with <tt>C</tt> being a Const actor, expression <tt>C</tt>
is of an object type that is specific to the Java class ptolemy.actor.lib.Const.
<div class="p"><!----></div>
An object type specific to Java class <tt>A</tt> can be specified with
<tt>object(Ä")</tt>, and its value is null. Comparison between object tokens
is by reference with the Java objects that they encapsulate. Therefore,
<tt>object(Ä") == object("B")</tt> is always true, because the values in both
tokens are null.
<div class="p"><!----></div>
<h3><a name="tth_sEc6.2">
6.2</a> Relationship between Object Types</h3>
<div class="p"><!----></div>
An object type A is more specific than object type B if the Java class
represented by A is a subclass of that represented by B.
<div class="p"><!----></div>
For example, <tt>object("ptolemy.actor.TypedIOPort")</tt> is more specific than <tt>
object("ptolemy.actor.IOPort")</tt> and <tt>object("ptolemy.kernel.Port")</tt>.
<div class="p"><!----></div>
The most general object type is <tt>object</tt> (without any argument). Conceptually
it encapsulates a null class (which, of course, does not exist in Java). The most
specific object type is <tt>object("ptolemy.data.type.ObjectType$BottomClass")</tt>
(which is not very useful in practice). The family of object types forms a
lattice.
<div class="p"><!----></div>
<h3><a name="tth_sEc6.3">
6.3</a> Object Tokens</h3>
<div class="p"><!----></div>
Object tokens are tokens of object types. A predefined object token is
<tt>null</tt>, which has null as the value and the null class as the Java
class. Its type is <tt>object</tt>. It is special in that it can be cast
into any object type. For example, you can cast it into a port with
<tt>cast(object("ptolemy.kernel.Port"), null)</tt>. If you enter this in
the expression evaluator, you shall see the stringification of the
token as <tt>öbject(null: ptolemy.kernel.Port)"</tt>.
<div class="p"><!----></div>
(Notice that the string here is not a valid Ptolemy expression. In fact, most
object tokens do not have string representations that are valid expressions, and
therefore, they cannot be stored permanently in a Ptolemy model.)
<div class="p"><!----></div>
Except for <tt>null</tt>, for a Ptolemy expression that evaluates to an object
token, the Java class represented by that token's type is always the most
specific class. For example, if <tt>C</tt> is a Const actor, then <tt>C</tt> in an
expression refers to an object token that has actor <tt>C</tt> as its value, and
<tt>object("ptolemy.actor.lib.Const")</tt> as its type. You can cast this type into
a more general actor type by doing <tt>cast(object("ptolemy.actor.Actor"), C)</tt>.
<div class="p"><!----></div>
<h3><a name="tth_sEc6.4">
6.4</a> Casting between Object Types</h3>
<div class="p"><!----></div>
An object token can be cast into a different object type, as long as the target
object type represents a Java class that the encapsulated object is in. That
class need not always be a superclass of the class that the object type
represents.
<div class="p"><!----></div>
For example, again let <tt>C</tt> be a Const actor. As discussed, the following
expression casts it into a more general actor type:
<pre>
cast(object("ptolemy.actor.Actor"), C)
</pre>
<div class="p"><!----></div>
The result of the cast is another object token with <tt>C</tt> as its value and
<tt>object("ptolemy.actor.Actor")</tt> as its type. That token can be casted
back into one of a more specific object type:
<pre>
cast(object("ptolemy.actor.TypedAtomicActor"),
cast(object("ptolemy.actor.Actor"), C))
</pre>
<div class="p"><!----></div>
This is valid because the value <tt>C</tt> is in any of the mentioned classes.
<div class="p"><!----></div>
As mentioned, <tt>null</tt> is a special object token that can be cast into any
object type. Any object token can also be cast into the most general object type,
which is <tt>object</tt>. The only object that can be cast into the most specific
object type, <tt>object("ptolemy.data.type.ObjectType$BottomClass")</tt>, is <tt>
null</tt>.
<div class="p"><!----></div>
<h3><a name="tth_sEc6.5">
6.5</a> Method Invocation</h3>
<div class="p"><!----></div>
Native Java methods may be invoked on the objects encapsulated in object tokens.
<div class="p"><!----></div>
For example, if <tt>C</tt> is a Const actor, <tt>C.portList()</tt> returns a list of
its ports. The returned list itself is a Java object in the class java.util.List,
so it is encapsulated in an object token. You may further invoke <tt>
C.portList().isEmpty()</tt> to test whether the list is empty. In that case, the <tt>
isEmpty</tt> method is invoked on the returned list. The <tt>isEmpty</tt> method returns
a Java boolean value, and the <em>boolean</em> type corresponds to the Ptolemy boolean
data type, so the value is converted into the latter type.
</blockquote>
<table width="331" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><a href="expressions.htm"><img src="img/navtoc.gif" width="84" height="23" border="0"
alt="TOC"> </a></td>
<td><a href="expressionsa5.htm"><img src="img/navprev.gif" width="81" height="23" border="0"
alt="PREV"> </a></td>
<td><a href="expressionsa7.htm"><img src="img/navnext.gif" width="81" height="23" border="0"
alt="NEXT"> </a></td>
</tr>
</table>
</body>
</html>