星期日, 2月 27, 2005

Pugs and Perl 6

Pugs 是 Perl 6 interpreter (直譯器) 計畫, 位於 http://pugscode.org 現由 Autrijus (唐宗漢) 主持. 也由於他的熱心, 台灣已有不少人加入了此一計畫. 如他在 irc.tw.freebsd.org, #elixus channel 上所言, "台灣已經變成 Perl 6 重鎮了" (2005/2/26).

Pugs 首頁簡單明瞭, 其中參與方式在最下方, 可以訂閱 mailing list (寄信至perl6-compiler-subscribe@perl.org) 也可以直接看 NNTP archive. 另外歡迎加入 freenode (irc.freenode.org) 上的 #perl6 channel.

程式碼是以 GPL2 公開的, 主要是以 Haskell 撰寫, 但是正如 overview 裡所言, 就算不會 Haskell 一樣有許多工作可以參與, 事實上許多 committer 本身並不會 Haskell.
autrijus> 可以寫範例程式, 寫 unit test, 寫 library
autrijus> 就像你 hack perl / cpan 也不一定要會 C 是一樣的道理
整個計畫是在 2005 年 2 月開始的, 程式碼以 subversion 管理, 至今 (2005/2/27 凌晨) 只有 316 個 revision, 但是已經有基本功能以及許多 unit test. Channel 上人也相當多, autrijus 之外, clkao, gugod 甚至 Ingy 都在上面!

希望這個計畫順利運行, 同時也希望大家有機會多多參與!

星期三, 2月 23, 2005

Xj3D's SAI support

I. Forward
I had spent some time dealing with x3d and SAI these days. As web3d web site said:
X3D is an Open Standards XML-enabled 3D file format to enable real-time communication of 3D data across all applications and network applications. It has a rich set of features for use in engineering and scientific visualization, CAD and Architecture, Medical visualization, Training and simulation, multimedia, entertainment, educational, and more.

3D modeling is not my major, and what I dealt with was not about 3D modeling, either. I mainly focus on the scene access interface (SAI) and trying to make it work on x3d browsers.

In x3d specification, we can write a special kind of node named "script." Usually these scrpts are written in javascript or so called ECMAscript. But as specification said, java byte code is also a valid choose. After several trials, I found that one of the commercial product -- maybe the most widely used one -- BS Contact does not fully support those feature. In fact, in version 6.1, the "url" field is not implemented so that it's impossible to write external script. In version 6.2, external script is supported but java byte code is still absent.

Another x3d browser implementation, Xj3D, is an open source software written in Java. As itself is written is Java, support of java byte code is great and complete. A shortage is that it hasn't support some complex node and can't process some x3d files. Another shortage is lake of documentation. Even though there are some implementation details and architecture documents, comments in source code is also well written, I found only few documents on using and hardly found any document on written SAI code. So this article is about these issues.

II. Xj3D usage
Installation of Xj3D version M10 is easy and smooth. After
download the installing jar file, simply run
  java -jar Xj3D-M10-RC2-platform.jar

and every will be fine. Not quite sure if it's necessary (since Xj3D can run with OpenGL scene), I installed Java3D before installing Xj3D.

To run the browser itself, using browser.bat on Windows or browser.sh on Unix. Don't be surprise that it spends all your CPU time and make your fan running faster -- it always does on my computer, either Windows or Linux. On Windows, it makes system slow down and makes me unable to do any thing else. It seems that it can have some improvement in this issue since on BS Contact, the same script needs little CPU time.

III. SAI and java byte code script for script node
The way to write java scripts is fairly simple -- compare to the effort I took on discovering it. The javadoc of the library Xj3D provided is available
online. According to x3d specification, all the compononts about SAI are defined in package org.web3d.x3d.sai.

First of all, in order to make Xj3D accept the byte code, the class should implement interface org.web3d.x3d.sai.X3dSriptImplementation, and, quite obvious but I forgot at first, should be public. As the javadoc said, there are several motheds should be implemented but only one of them is important
  public void setField(X3DScriptNode externalView, java.util.Map fields)

Where 'fields' will be a map from string to object, with the name of each field mapped to it's corresponding value class -- classes who implemented X3DField.

To use the fields we get from the parameter of setField, Xj3D provided a EventListener architecture. Interface X3DField defined a method
  public void addX3DEventListener(X3DFieldEventListener l)

So we can make our class implement X3DFieldEventListener as well as X3dScriptImplementation to handle the value changing.

Below is a sample code:
(X3d file)

<script DEF="MoverUsingExternalScriptFile" url="Script.class">
<field name="set_fraction" type="SFFloat" accessType='inputOnly'/>
<field name='value_changed' type='SFVec3f' accessType='outputOnly'/>
</script>


(Script.java)

public class Script implements X3DScriptImplementation, X3DFieldEventListener{
public void readableFieldChanged(X3DFieldEvent evt){
value_changed.setValue(new float[]{
set_fraction.getValue(), 0.0f, 0.0f
});
}
public void setFields(X3DScriptNode externalView, Map map){
set_fraction = (SFFloat) map.get("set_fraction");
value_changed = (SFVec3f) map.get("value_changed");
set_fraction.setX3DEventListener(this);
}
public void eventProcessed(){}
public void initializet(){}
public void setBrowser(Browser browser){}
public void shutdown(){}

private SFFloat set_fraction;
private SFVec3f value_changed;
}

星期日, 2月 06, 2005

Apache, Tomcat & Axis (on windows)

為了幫朋友研究些關於 Java web service 的東西要把 server 架起來. 又因為 client 端只有 windows 版的, 在只有一台機器的情況下, 只好在 windows 上把 server 架起來. 事實上步驟不難, 找對東西下載然後點幾下就好了.

要裝的東西包括:
1. Apache for windows, 我用的是 2.0.52 (最新的 stable).
    http://httpd.apache.org/download.cgi
2. Apache Jakarta Tomcat 5.5.7.
    http://jakarta.apache.org/site/binindex.cgi 搜尋 tomcat
3. Apache Webservises project Axis 1.2RC2
    (這是用 java 的, 如果要用 C++ 則是 Axis C++).
    http://ws.apache.org/axis/
4. Sun JDK 5.0 (1.5) update 1.
    http://java.sun.com/j2se/1.5.0/download.jsp
5. JavaBeans Activation Framework, JAK 1.0.2.
    http://java.sun.com/products/javabeans/glasgow/jaf.html

其中 dependency 是:
1. independent.
2. depend on 1. and 4.
3. depend on 2. and 5. (但安裝時只要 2.)
4. independent.
5. depend on 4.

所以以下是建議安裝順序:
1) 下載 1. 和 4.
2) 因為 jdk 很大, 所以 Apache 一定先抓完, 就順手裝起來.
3) 然後可以來抓 2. 和 3. 等到 jdk 裝起來就把 Tomcat 裝起來, 接著裝 Axis.
4) 最後抓 5. 在 JAF 裝起來之前 Tomcat 就可以動了, 可以稍微玩一下.
    用 browser 連到 localhost:8080
    (Tomcat 預設 port 為 8080, 有自行更改請改到相應的地方).
5) JAF 抓下來是 zip 檔. 把其中的 activation.jar 移到 Axis 所在目錄的 WEB-INF\lib 下就好了.
6) 開 browser 連到 localhost:8080/Axis 應該就會看到 Axis 的起始畫面. 點 Validate (http://localhost:8080/Axis/happyaxis.jsp), 核心元件 (core component) 應該都裝起來了, 要裝兩個選擇性元件的話再去頁面上给的 link 抓.
8) 回到 Axis 起始畫面, 點 Call a local endpoint, 可能會出現一個 xml, 其中有一行
java.lang.RuntimeException: No compiler found in your classpath! (you may need to add 'tools.jar')
那就代表碰上了和我同樣的問題. 解決的方法是在右下角圖示中找出 Apache Tomcat, 按右鍵選 configure, 在跳出的視窗中選 Java 頁面, 在 Java Classpath 的後面加上到 tools.jar 的路境. 以下是我的設定:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\bin\bootstrap.jar;C:\Program Files\Java\jdk1.5.0_01\lib\tools.jar
回到這個設定視窗的 General 頁面, 按 Stop 之後按 Start 重新啟動 Tomcat 就可以正常運作了.

要注意的幾點:
1. jdk 安裝檔和裝起來都很佔空間, 所以要留夠多硬碟.
2. Axis 抓下來也是一個 zip, 只要把 webapps\axis 整個目錄移到 Tomcat 的 webapps 下就可以了. 其他 document 和 sample 可以留在別的地方.
3. 如果 用 Tomcat 4.x 和 jdk 1.4 那 activation.jar 要放在 Tomcat 目錄的 common/lib 下 (happyaxis.jsp 上特別註明).

因 為我還必須同時讓 IIS 執行 (做別的事), 所以 Apache 不能聽 port 80 而改到 8080. 可是這是 Tomcat 的預設 port 造成 Tomcat 執行失敗. 在找不到設定工具的情況下直接去改設定檔. 幸好設定檔都是 xml, 格式不難, 只要把 Tomcat 目錄下 conf\server.xml 其中一個 tag <Connector port="8080" ... > 改成要的 port 就好了.