Java in SAS Java Obj a DATA Step

Java in SAS® Java. Obj, a DATA Step Component Object Richard A. De. Venezia Independent Consultant SAS is a registered trademark or trademark of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. Other brand product names are registered trademarks or Trademarks of their respective companies. Slide imagery Copyright © 2005, SAS Institute Inc. All rights reserved. Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Overview n Component Object n Component Interface • Syntax • Design Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Component Interface n DECLARE Statement n _NEW_ Operator n Object References n Object Methods Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

DECLARE Statement DECLARE Component. Object variable; n Places variable in PDV n Variable is an object reference n Cannot be output to a data set DECLARE Component. Object variable ( constructor arguments ); n Instantiates an object at declaration Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

_NEW_ Operator DECLARE Component. Object variable; variable = _NEW_ (constructor arguments); n Instantiates an object n Pattern of argument types • signature Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Object References n Object variables refer to an underlying component n Assignable my. Obj 1 == my. Obj 2; • original my. Obj 1 reference lost! n Terminate objects no longer needed my. Obj 1. delete(); n No documented garbage collection • some automatic termination Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Object Methods n Object Dot Syntax my. Obj. method ( arguments ); n Each object has a set of documented methods • Allows DATA Step to interact with component n Pattern of argument types • signature Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Java. Obj Component Object n One of three production objects • Others are Hash, HIter n Constructor signature ( Char, {other args} ); Char - name of Java class { other args } - signature of class constructor Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

JVM - Java Virtual Machine n Execution environment for Java classes n Initialized when first Java. Obj instantiated • Persists until SAS session ends Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Java classes n Loaded from CLASSPATH n Must be compatible with active JVM -JREOPTIONS(-Dsas. jre. home=. . . ) n Class names - hierarchy and nesting Java code SAS Code x. y. z. My. Class 'x/y/z/My. Class' x. y. My. Class. Foo 'x/y/z/My. Class$Foo' Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

CLASSPATH n Environment Variable location-1; location-2; . . . ; location-N Folders or Jar files n Set via • command line • configuration file • invoking process • user profile • system profile Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Java fields n Type • Primitive or Class n Value • Primitive or Object reference n Java. Obj can interact, if • field is primitive type or String Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Java methods n Have arguments • signature - pattern of types n Return nothing, a primitive type, or an object n Java. Obj can interact • signature is void • signature contains doubles, Strings and classes that Java. Obj can instantiate. - Important! • if return is not an object (String excluded) Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Java. Obj accessor methods n Allow interaction between DATA Step and underlying Java object n Fields javaobj. get[Static]Type. Field(field, out-variable) javaobj. set[Static]Type. Field(field, in-expression) n Method javaobj. call[Static]Type. Method (method, in-args, out-variable) • in-args signature must match method signature n j. get. Double. Field (‘score’, score); Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Java. Obj accessor methods Java: public Type Method (args…) {… return * } SAS: javaobj. call. Type. Method ( ‘Method’, args…, return ); SAS: javaobj. get. Type. Field ( ‘Field’, out-variable ); Java: public Type Field = value; SAS: javaobj. set. Type. Field ( ‘Field’, in-expression ); Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

First Program data _null_; declare Java. Obj j ('java/lang/String' , 'My first Java. Obj'); length s $200; j. call. String. Method ('to. String', s); put s=; run; Copyright © 2005 , Richard A. De. Venezia. All rights reserved.
![Second Program - Java public class Greetings { private String[3] phrases = { 'Hello', Second Program - Java public class Greetings { private String[3] phrases = { 'Hello',](http://slidetodoc.com/presentation_image_h2/589687cfbce18aa02adeaddbfdba7862/image-17.jpg)
Second Program - Java public class Greetings { private String[3] phrases = { 'Hello', 'Hi There', '' }; public void set. Greeting (String phrase, double index) { phrases [ (int) index ] = phrase; } public String get. Greeting () { return phrases [ 0 ]; } public String get. Greeting (double index) { return phrases [ (int) index ]; } } Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Second Program - SAS data _null_; declare Java. Obj j ('Greetings'); length s 1 -s 3 $200; j. call. String. Method j. call. Void. Method j. call. String. Method put (s 1 -s 3) (=/); run; ------ log -----s 1=Hello s 2=Hi There s 3=Wassup Copyright © 2005 , Richard A. De. Venezia. All rights reserved. ('get. Greeting', ('set. Greeting', ('get. Greeting', s 1); 1, s 2); 2, 'Wassup'); 2, s 3);

Java. Obj support methods n Interact with Java exceptions n exception. Describe (in-debug-logging) • use 1 to log exception stack traces on SAS session stderr n exception. Check (out-status) • returns 1 if an exception occurred n exception. Clear () • resets status (internal to Java. Obj system) Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Third Program - Uh, oh data _null_; length s $200; declare Java. Obj j ('Greetings'); j. exception. Describe (1); j. call. String. Method ('get. Greeting', 9, s); j. exception. Check (e); put e= s=; if (e) then do; put 'An exception occurred. '; j. exception. Clear (); end; run; ------ log -----e=1 s= An exception occurred. ------ stderr -----java. lang. Array. Index. Out. Of. Bounds. Exception: 9 at Greetings. get. Greeting(Greetings. java: 11) Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Java 2 D n Object oriented approach to Graphics n More colors than SAS/Graph n Anti-aliased drawing n Alpha blending n Affine Transformations n JPG and PNG • Other formats via plug-in classes Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Fourth Program - Display an Image n Runtime library has many useful classes n Program uses a few import java. io. File; import java. awt. image. Buffered. Image; import javax. imageio. Image. IO; import javax. swing. Image. Icon; import javax. swing. JOption. Pane; Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Fourth Program SUGI 30. Graphics package SUGI 30; . . . import statements. . . public Graphics { private String read. Source; read. Source private Buffered. Image read. Buffer; read. Buffer void read. Image (String source) throws Exception { read. Source = null; read. Buffer = Image. IO. read (new File(source)); Image. IO File read. Source = source; } void show. Read. Buffer () { show ( read. Buffer, read. Buffer read. Source ); } void show (Buffered. Image img, String title) { JOption. Pane. show. Message. Dialog(null, title, JOption. Pane. PLAIN_MESSAGE, new Image. Icon(img)); JOption. Pane Image. Icon } } Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Fourth Program - SAS n Short and sweet • Real work occurs ‘over in Java’ data _null_; declare Java. Obj j ('SUGI 30/Graphics'); j. exception. Describe(1); j. call. Void. Method ('read. Image', 'sugi 30_logo. png'); j. call. Void. Method ('show. Read. Buffer'); j. delete(); run; Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Recompiling Java n Occurs often during rapid development • Corrections or changes made to java source n If done while SAS active • New version of an already loaded class will not get loaded ! n Solution - use Batch SAS • Compile Java, then run SAS • Non-trivial Java systems - build tool, Ant Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Development Configuration n batch file - example 1. bat • compiles java • runs SAS program Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Batch for Development clear log set program=Graphics. Tester echo. > %program%. log compile java if not exist class mkdir class javac -d class java*. java if errorlevel 1 goto done sas program set CLASSPATH=class; %CLASSPATH% 2>stderr. log >stdout. log sas -sysin sas%program%. sas review logs notepad %program%. log notepad stderr. log notepad stdout. log : done Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

SUGI 30. Graphics - An Adapter private Buffered. Image buffer; buffer private Graphics 2 D g; private double anti. Alias; anti. Alias // drawing buffer // graphics context // state variable void create. Buffer (double width, double height) { buffer = new Buffered. Image ((int)width, (int)height , Buffered. Image. TYPE_INT_RGB); g = buffer. create. Graphics(); buffer set. Anti. Alias (1 d); // 1 as double } void set. Anti. Alias (double smooth) { if (smooth != 0 d) { g. set. Rendering. Hint ( Rendering. Hints. KEY_ANTIALIASING , Rendering. Hints. KEY_ANTIALIASING_ON ); } else {. . . } } } Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Adapter n Also known as Wrapper n “Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces” [ Design Patterns - Gang of Four ] n Incompability • Java. Obj passes primitives, Strings and Java. Obj - cannot receive object reference in return Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Drawing (Adapters) void set. Color (double r, double g, double b) { this. g. set. Color (new Color ((int)r, (int)g, (int)b)); } void draw. Line (double x 1, double y 1, double x 2, double y 2) { g. draw. Line ( (int) x 1, (int) y 1, (int) x 2, (int) y 2); } void fill. Rect (double x, double y, double width, double height) { g. fill. Rect ( (int) x, (int) y, (int) width, (int) height); } void draw. Oval (double x, double y, double width, double height) { g. draw. Oval ( (int) x, (int) y, (int) width, (int) height); } void fill. Oval (double x, double y, double width, double height) { g. fill. Oval ( (int) x, (int) y, (int) width, (int) height); } Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Saving Graphics String save. As ( String type, String name ) { String saved. As = ""; try { File file = new File(name); saved. As = file. get. Canonical. Path(); Image. IO. write (buffer, type, new File(saved. As)); } catch (Exception e) { saved. As = ""; } return saved. As; } Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Graphics. Tester n Notice the j. * pattern data _null_; length fname $200; declare Java. Obj j ('SUGI 30/Graphics'); j. exception. Describe(1); j. call. Void. Method (’create. Buffer', 200); j. call. Void. Method (‘set. Color’, 255, 255); j. call. Void. Method (‘fill. Rect’, 0, 0, 200); j. call. Void. Method (‘set. Color’, 255, 0, 0); j. call. Void. Method (‘draw. Oval’, 1, 1, 198); j. call. Void. Method (‘fill. OVal’, 80, 40); j. call. Void. Method ('save. As’, ‘png’, ‘sqare. png’, fname); j. call. Void. Method (‘show’, fname, ‘Drawing buffer’); j. delete(); run; Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Emulate DSGI n An adaper surfaces a set of methods n State variables • Maintain properties of context n Extensible • Add new methods and states to Graphics. java n Downside • Not GRSEG compatible n Upside • Other graphics formats via Image. IO plugins Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Patterns in SAS Code ==> Macro %macro create. Buffer (width, height); &jdsgi. . call. Void. Method ('create. Buffer', &width, &height) %mend; %macro set. Color ( r , g, b); &jdsgi. . call. Void. Method ('set. Color', &r, &g, &b) %mend; %macro fill. Rect ( x, y, width, height); &jdsgi. . call. Void. Method ('fill. Rect', &x, &y, &width, &height) %mend; and so on. . . Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Patterns in SAS Code ==> Macro %macro canvas_create (javaobj, width, height, color); %global jdsgi; %let jdsgi = &javaobj; declare javaobj &jdsgi (‘SUGI 30/Graphics’); &jdsgi. . exception. Describe(1); %create. Buffer (&width, &height); %set. Color (&color); %fill. Rect (0, 0, &width, &height); %set. Color (0, 0, 0); %mend; %canvas_delete (); &jdsgi. . delete(); %symdel &jdsgi; %mend; Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Graphics. Tester n Simplifies adapter use data _null_; length fname $200; %canvas_create (_g, 200, 255, 255); %set. Color (255, 0, 0); %draw. Oval (1, 1, 198); %fill. Oval (80, 40, 40); %save. As (‘png’, ‘target 2. png’, fname); %show. TM (saved. As, cat(‘Drawing buffer’, ’ 0 a’x, ’Drawn with Macros. ’); %canvas_delete(); run; Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

Conclusion n Component Object technology introduces the concept of Object to DATA Step n Java. Obj is one doorway into the vast domain of Java based tools and solutions n Extensive j. DSGI samples at www. devenezia. com/downloads/samples/#jdsgi Copyright © 2005 , Richard A. De. Venezia. All rights reserved.

About the Speaker Richard A. De. Venezia Independent Consultant Location 9949 East Steuben Road Remsen, NY 13438 Telephone (315) 831 -8802 E-Mail radevenz@ix. netcom. com Copyright © 2005 , Richard A. De. Venezia. All rights reserved.
- Slides: 38