User Tools

Site Tools


at:tutorial:symbiosis

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
at:tutorial:symbiosis [2008/10/22 18:32]
tvcutsem *
at:tutorial:symbiosis [2013/05/17 20:25] (current)
tvcutsem updated
Line 11: Line 11:
 When AmbientTalk values and Java values cross language boundaries (e.g. when they are passed as arguments of or return values from a method that is invoked from the other language), these values are converted. AmbientTalk features a number of built-in conversions that maps AmbientTalk's native language values onto JVM native data types and vice versa. The following tables show the conversion rules for going from Java to AmbientTalk and from AmbientTalk to Java. When AmbientTalk values and Java values cross language boundaries (e.g. when they are passed as arguments of or return values from a method that is invoked from the other language), these values are converted. AmbientTalk features a number of built-in conversions that maps AmbientTalk's native language values onto JVM native data types and vice versa. The following tables show the conversion rules for going from Java to AmbientTalk and from AmbientTalk to Java.
  
-^ Java type and value ^ AmbientTalk value ^+^ Java value : type ^ AmbientTalk value ^
 | null: Object | nil | | null: Object | nil |
 | n: int | a number n | | n: int | a number n |
Line 33: Line 33:
 | a class wrapper( c ) | c : Class | | a class wrapper( c ) | c : Class |
 | a java wrapper(obj : Type) | obj : Type | | a java wrapper(obj : Type) | obj : Type |
-| any object o | AT wrapper (o) : Interface |+| any object o | AT wrapper (o) : |
  
 Note that non-native Java or AmbientTalk objects are represented in the other language by means of "wrappers". The task of these "wrappers" is to represent an object written in one language to act as if it were an object written directly in the other language. Note that non-native Java or AmbientTalk objects are represented in the other language by means of "wrappers". The task of these "wrappers" is to represent an object written in one language to act as if it were an object written directly in the other language.
 +
 +In the last conversion rule of the second table, ''I'' represents a Java interface type. ''I'' is determined by a static type declaration in Java.
  
 ===== Accessing Java from within AmbientTalk ===== ===== Accessing Java from within AmbientTalk =====
Line 49: Line 51:
  
 A class is represented as a normal AmbientTalk object. All ''public static'' methods defined on the class can be invoked like normal methods on its "wrapped" object representation. A class is represented as a normal AmbientTalk object. All ''public static'' methods defined on the class can be invoked like normal methods on its "wrapped" object representation.
 +
 +<note>
 +If you want to load your own Java class make sure that the .class file is in the classpath of the JVM running AmbientTalk. By default, the iat script adds the standard $CLASSPATH classpath. 
 +</note>
 +
 +<note>
 +Please do not name your own Java packages starting with a capital letter. Otherwise, AmbientTalk will confuse the package with a Java class. 
 +</note>
  
 ==== Creating Java objects ==== ==== Creating Java objects ====
Line 75: Line 85:
 >>nil >>nil
 >aVector >aVector
->><java:[1, 2, 3, 4, 5, 6, 7, 8, 9]>+>><java:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]>
 </code> </code>
  
Line 169: Line 179:
 >>42 >>42
 </code> </code>
 +
  
 ==== Starting an AmbientTalk interpreter from Java ==== ==== Starting an AmbientTalk interpreter from Java ====
Line 174: Line 185:
 So far, the examples have illustrated how to reuse Java code from within AmbientTalk. They have shown how to access Java classes, instantiate them and invoke methods on the resulting objects. Moreover,  AmbientTalk objects can be passed as parameters to such Java methods, provided that the method expects an interface type. Whereas the ability to reuse Java code from within AmbientTalk provides  means to e.g. build applications which offer a graphical user interface or which talk to a database, it is also often useful to embed AmbientTalk in an existing Java application. So far, the examples have illustrated how to reuse Java code from within AmbientTalk. They have shown how to access Java classes, instantiate them and invoke methods on the resulting objects. Moreover,  AmbientTalk objects can be passed as parameters to such Java methods, provided that the method expects an interface type. Whereas the ability to reuse Java code from within AmbientTalk provides  means to e.g. build applications which offer a graphical user interface or which talk to a database, it is also often useful to embed AmbientTalk in an existing Java application.
  
-Embedding AmbientTalk in an application, requires one to start an AmbientTalk virtual machine, a task performed by the EmbeddableAmbientTalk class. Java programs start a virtual machine by sending an instance of this class the ''initialize'' message. The corresponding method expects the following arguments in order to be able to correctly initialize the resulting virtual machine and evaluation actor: a parsed version of the init file, a set of fields which should be visible in every actor created on the virtual machine and the network on which the virtual machine will broadcast its presence. The example below shows the default settings: +Embedding AmbientTalk in an application, requires one to start an AmbientTalk virtual machine, a task performed by the EmbeddableAmbientTalk class. This is an abstract class. To create new embeddable AmbientTalk virtual machine, you need to create a subclass that implements its abstract methods. These methods, such as ''handleParseError'' and ''handleATException'', should define appropriate error-handling code to be executed when an AmbientTalk script crashes. 
 + 
 +Once an instance of an EmbeddableAmbientTalk subclass is created, the AmbientTalk VM can be started by sending it the ''initialize'' message. The corresponding method expects the following arguments in order to be able to correctly initialize the resulting virtual machine and evaluation actor: a parsed version of the init file, a set of fields which should be visible in every actor created on the virtual machine and the network on which the virtual machine will broadcast its presence. The example below shows the default settings: 
  
 <code> <code>
-EmbeddedAmbientTalk vm = new EmbeddedAmbientTalk();+EmbeddableAmbientTalk vm = new MyEmbeddableAmbientTalk();
 vm.initialize( vm.initialize(
  NATParser.parse(  NATParser.parse(
Line 190: Line 203:
 The code excerpt also illustrates that the EmbeddableAmbientTalk class provides methods to create definitions for fields such as ''system'' which by default offers I/O through the console and provides access to the program arguments, ''~'' which allows addressing source files located in the working directory (i.e. the directory from which the Java application was started) and the object path (''lobby'' or ''/'') which allows loading files situated in a library path. The code excerpt also illustrates that the EmbeddableAmbientTalk class provides methods to create definitions for fields such as ''system'' which by default offers I/O through the console and provides access to the program arguments, ''~'' which allows addressing source files located in the working directory (i.e. the directory from which the Java application was started) and the object path (''lobby'' or ''/'') which allows loading files situated in a library path.
  
-Once the virtual machine is properly initialized, the embedding program can start to evaluate AmbientTalk code. The EmbeddedAmbientTalk class provides two methods to do this, namely ''evalAndPrint'' and ''evalAndWrap''. The former method can be used to write the result of evaluating the code to a PrintStream, which is used for instance to build the Interactive AmbientTalk (iat) shell. The latter can be used to return the resulting object, albeit wrapped as an object adhering to a particular interface. This wrapped object can then be used further by the Java application. In the example below we create a controller instance in a model-view-controller application by evaluating an AmbientTalk source file. This controller will take care of the distribution aspects and will be sent messages by the views when they request changes: +Once the virtual machine is properly initialized, the embedding program can start to evaluate AmbientTalk code. The EmbeddableAmbientTalk class provides two methods to do this, namely ''evalAndPrint'' and ''evalAndWrap''. The former method can be used to write the result of evaluating the code to a PrintStream, which is used for instance to build the Interactive AmbientTalk (iat) shell. The latter can be used to return the resulting object, albeit wrapped as an object adhering to a particular interface. This wrapped object can then be used further by the Java application. In the example below we create a controller instance in a model-view-controller application by evaluating an AmbientTalk source file. This controller will take care of the distribution aspects and will be sent messages by the views when they request changes: 
  
 <code> <code>
Line 208: Line 221:
  
 <note> <note>
-When starting an AmbientTalk virtual machine from a Java application, the resulting system is inherently multithreaded. The wrappers created by the ''evalAndWrap'' method will ensure that the Java code cannot break the concurrency properties of AmbientTalk. Moreover, by default this wrapper will ensure that the Java thread waits for the result of evaluating the AmbientTalk code which prevents concurrent access on possible Java objects used by the evaluated code. For more detailed information on this topic we refer to our [[ftp://prog.vub.ac.be/tech_report/2007/vub-prog-tr-07-15.pdf|ICDL2007 paper]].+When starting an AmbientTalk virtual machine from a Java application, the resulting system is inherently multithreaded. The wrappers created by the ''evalAndWrap'' method will ensure that the Java code cannot break the concurrency properties of AmbientTalk. Moreover, by default this wrapper will ensure that the Java thread waits for the result of evaluating the AmbientTalk code which prevents concurrent access on possible Java objects used by the evaluated code. For more detailed information on this topic we refer to our [[http://soft.vub.ac.be/Publications/2007/vub-prog-tr-07-15.pdf|ICDL2007 paper]].
 </note> </note>
at/tutorial/symbiosis.1224693126.txt.gz · Last modified: 2008/10/22 18:35 (external edit)