Sunday 21 December 2008

Hints for a complete STRUTS Application (4)

Hints for a complete STRUTS Application (4)
Source code is available from arsaral(at)yahoo.com

4th Hint: selectRow and Master-Detail connection with a record structure

This program uses the Struts - iterator – selectRow facility structure in the 3rd example. The 3rd example passed only a string, this example uses a class record structure to view ina seperate screen. The next example will add new, update and delete facilities. Logging, use of message bundle and messages, imported menus etc. are excluded to make things very simple. Future examples will also handle these.

index.jsp
<BODY>
<logic:forward name="setList"></logic:forward>
</BODY>

Struts-config.xml
<struts-config>
<!-- ========== Global Forward Definitions ============================== -->
<global-forwards>
<forward name="setList" path="/setList.do"/>
</global-forwards>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<action name="setList" path="/setList"
type="com.ars.actions.SetListAction">
<forward name="success" path="/listProcess.jsp"></forward>
</action>
<action name="viewDetail" path="/viewDetail"
type="com.ars.actions.ViewDetailAction">
<forward name="success" path="/viewDetailProcess.jsp"></forward>
</action>
</action-mappings>
<message-resources parameter="arsstruts.ApplicationResources"/>
</struts-config>

As seen, first you will run index.jsp than index.jsp will run setList.do and that will run SetListAction.java.

SetListAction.java
package com.ars.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.ars.beans.Person;

public final class SetListAction extends Action {
// The constructor method for this class
public SetListAction() {
}
// This sets the list as a session bean

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {

Person person = null;
HttpSession session = request.getSession();

java.util.ArrayList list = new java.util.ArrayList();

for (int i = 0; i < 13; i++) {
person = new Person();
person.setFirstName("firstName" + i);
person.setLastName("lastName" + i);
person.setCity("city" + i);
person.setCountry("country" + i);
list.add(person);
}
session.setAttribute("baseList", list);

ActionForward forward = mapping.findForward("success");
return forward;
}
}

As seen above, SetListAction.java prepares a list named baseList and then forwards the action with “success” to struts-config.xml. struts-config.xml than runs listProcess.jsp which is given below.

listProcess.jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<header>
<title>Iterator</title>
<html:base/>
</header>
<body>
<logic:present name="baseList">
<table border="0" cellspacing="0" cellpadding="0" align="center" width="70%" style="border-collapse:collapse;">
<tr bgcolor="#98AFCC">
<th>First Name</th>
<th>Last Name</th>
<th>City</th>
<th>Country</th>
</tr>
<logic:iterate id="person" name="baseList">
<tr>
<td>
<bean:write name="person" property="firstName" />
</td>
<td>
<bean:write name="person" property="lastName" />
</td>
<td>
<bean:write name="person" property="city" />
</td>
<td>
<bean:write name="person" property="country" />
</td>
<td>
<html:link page="/viewDetail.do" paramName="person" paramProperty="firstName"
paramId="firstID">View</html:link>
</td>
</tr>
</logic:iterate>
</table>
</logic:present>
</body>
</html:html>

listProcess.jsp uses an html:link with pameters to run /viewDetail.do of struts-config.xml.
It passes the firstName of the selected row to ViewDetailAction.
package com.ars.actions;


import com.ars.beans.Person;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class ViewDetailAction extends Action {

// The constructor method for this class
public ViewDetailAction() {
}

// This sets the list as a session bean
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

HttpSession session = request.getSession();

String firstNameID= request.getParameter("firstID");
java.util.ArrayList list = (java.util.ArrayList) session.getAttribute("baseList");
Person personDetail = new Person();
Person personCheck = new Person();

for (Object personCheckObject : list){
personCheck = (Person) personCheckObject;

if (personCheck.getFirstName().compareTo(firstNameID) > -1 ){
personDetail.setFirstName(personCheck.getFirstName());
personDetail.setLastName(personCheck.getLastName());
personDetail.setCity(personCheck.getCity());
personDetail.setCountry(personCheck.getCountry());
break;
}
}
session.setAttribute("baseDetail",personDetail);

ActionForward forward = mapping.findForward("success");
return forward;
}
}

viewDetailAction uses the list session object “baseList” that had been created by SetListAction to get the detail
information related to the firstID that has been passed as request param. It prepares the “baseDetail” and passes it as a session attribute to viewDetailProcess.jsp.

viewDetailProcess.jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<logic:present name="baseDetail">
<html:html>
<header>
<title>view detail page</title>
<html:base/>
</header>
<body>
<table>
<tr>
<td colspan="2">Page for Viewing the Personal Info</td>
</tr>
<tr>
<td>First Name :</td>
<td>
<bean:write name="baseDetail" property="firstName"/>
</td>
</tr>
<tr>
<td>Last Name :</td>
<td>
<bean:write name="baseDetail" property="lastName"/>
</td>
</tr>
<tr>
<td>City :</td>
<td>
<bean:write name="baseDetail" property="city"/>
</td>
</tr>
<tr>
<td>Country :</td>
<td>
<bean:write name="baseDetail" property="country"/>
</td>
</tr>
</table>
</body>
</html:html>
</logic:present>

The next hint will implement new, update and delete functions. Source and executable is available on request from arsaral( at )yahoo.com

Friday 19 December 2008

Hints for a complete STRUTS Application (3)

Hints for a complete STRUTS Application (3)
Source code is available from arsaral(at)yahoo.com

3rd Hint: selectRow and Master-Detail connection in a simple Struts example

This program uses the Struts - iterator structure in the 2nd example and builds on it the selectRow facility. It adds a hyperlink named view on each line. When you click on this hyperlink it brings the related record on a detail screen. To make it extremely simple, only a string is used in this example. Nex example will use a complete record.

selectItemList.jsp
<BODY>
<logic:forward name="selectItemList"></logic:forward>
</BODY>
</HTML>

Struts-config.xml
<global-forwards>
<forward name="selectItemList" path="/selectItemList.do"/>
</global-forwards>
<action-mappings>
<action name="selectItemList" path="/selectItemList"
type="com.masslight.actions.SetListAction">
<forward name="success" path="/selectItemProcess.jsp"></forward>
</action>
<action name="setDetail" path="/setDetail"
type="com.masslight.actions.SetDetailAction">
<forward name="success" path="/detailProcess.jsp"></forward>
</action>
</action-mappings>

As seen, first you will run selectItemList.jsp than selectItemList.jsp will run selectItemList.do and that will run SetListAction.java.

package com.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class SetListAction extends Action {

// The constructor method for this class
public SetListAction() {
}

// This sets the list as a session bean
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

HttpSession session = request.getSession();

java.util.ArrayList list = new java.util.ArrayList();
list.add("item 1");
list.add("item 2");
list.add("item 3");

session.setAttribute("baseList",list);

ActionForward forward = mapping.findForward("success");
return forward;
}
}
As seen above, SetListAction.java prepares a list named baseList and then forwards the action with “success” to struts-config.xml. struts-config.xml than runs selectItemProcess.jsp which is given below.

selectItemProcess.jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<header>
<title>Iterator</title>
<html:base/>
</header>
<body>
<logic:present name="baseList">
<table border="2px" bgcolor="#CCCC99" cellspacing="1">
<logic:iterate id="iteratorItem" name="baseList">
<tr>
<td>Item Value:</td>
<td>
<bean:write name="iteratorItem"/>
</td>
<td>
<html:link page="/setDetail.do" paramName="iteratorItem"
paramId="iteratorItem">View</html:link>
</td>
</tr>
</logic:iterate>
</table>
</logic:present>

<html:submit value="Continue"/>
</body>
</html:html>

selectItemProcess.jsp uses an html:link with pameters to run /setDetail.do of struts-config.xml.
This runs SetDetailAction.

com.masslight.actions.SetDetailAction
package com.masslight.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class SetDetailAction extends Action {

// The constructor method for this class
public SetDetailAction() {
}

// This sets the list as a session bean
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

HttpSession session = request.getSession();

String detailInfo= request.getParameter("iteratorItem");

session.setAttribute("baseDetail",detailInfo);

ActionForward forward = mapping.findForward("success");
return forward;
}
}

The ActionForward success runs the detailProcess.jsp which displays the detail, which happens to be the same word alone, for the sake of simplicity.

detailProcess.jsp
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<header>
<title>Iterator</title>
<html:base/>
</header>
<body>
<bean:write name="baseDetail"/>
<br/>
<html:submit value="Continue"/>
</body>
</html:html>

Hints for a complete STRUTS Application (2)

Hints for a STRUTS Application
Source code is available from arsaral(at)yahoo.com

2nd Hint: A very simple Struts Iterator Tag example

This program uses the navigation trick of the 1st HINT to prepare the simple list that will be iterated to be printed on the screen.

Index.jsp
<HTML>

<BODY>
<logic:forward name="setList"></logic:forward>
</BODY>
</HTML>

Struts-config.xml
<global-forwards>
<forward name="setList" path="/setList.do" />
</global-forwards>

<action-mappings>
<action name="setList" path="/setList" type="com.actions.SetListAction">
<forward name="success" path="/iterateList.jsp"></forward>
</action>
</action-mappings>

As seen, first you will run index.jsp than index.jsp will run setList.do and that will run SetListAction.java.

package com.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public final class SetListAction extends Action {

// The constructor method for this class
public SetListAction() {
}

// This sets the list as a session bean
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

HttpSession session = request.getSession();

java.util.ArrayList list = new java.util.ArrayList();
list.add("item 1");
list.add("item 2");
list.add("item 3");

session.setAttribute("baseList",list);

ActionForward forward = mapping.findForward("success");
return forward;
}
}
As seen above, SetListAction.java prepares a list named baseList and then forwards the action with “success” to struts-config.xml. struts-config.xml than runs iterateList.jsp which is given below.

iterateList.jsp
<html:html>
<header>
<title>Iterator</title>
<html:base/>
</header>
<body>
<logic:present name="baseList">
<logic:iterate id="iteratorItem" name="baseList">
Item Val: <bean:write name="iteratorItem" /><br />
</logic:iterate>
</logic:present>
<html:submit value="Continue"/>
</body>
</html:html>

iterateList.jsp very simply writes the items of the list to the screen.

Hints for a complete STRUTS Application (1)

Source code is available from arsaral(at)yahoo.com

1.st Hint: Running a program at the very beginning of a STRUTS application.

It is necessary to prepare the working environment before the standard STRUTS application begins to function through its menus and screens. Opening databases, some logging etc. may be done using this approach. Please note, I do not refer to authentication at this point. Some of these tasks may be done before the authentication or after it, depending on the implementation.

Struts-config.xml

<global-forwards>
<forward path="/init.do" name="init">
</global-forwards>

<action-mappings>
<action path="/init" name="init" type="com.actions.InitAction">
<forward path="/mainmenu.jsp" name="start">
</action>
</action-mappings>


Here, mainmenu.jsp is a jsp file residing in the WEB-INF directory. This can be any jsp which may be used
for the main menu navigation of the STRUTS application.

In the src folder of the application, there is a com and in that there is an actions folder where
InitAction.java resides:

InitAction source follows:

package com.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class InitAction extends Action {

// The constructor method for this class
public InitAction() {
}

// This sets the list as a session bean
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// Any code to prepare the environment for the new born application goes here…

ActionForward forward = mapping.findForward("start");
return forward;
}
}

As seen above, the “start” ActionForward keyword trigs the execution of the mainmenu.jsp in the

struts-config.xml.

Saturday 6 December 2008

KENDİNİ TEKRARLAYAN OLAYLARIN DOĞASI ÜZERİNE

A Mathematical Model of Chronic Events: This article discusses a mathematical model which can be used to study chronic events in large systems, specifically health and air traffic control areas.

Bu makale kronik olayları incelemek için kullanılabilecek bir matematiksel modeli tartışmaktadır. Bu tür bir model büyük sistemler ve özel olarak sağlık, hava trafik kontrolü konularında kullanılabilir. Makalenin daha temiz bir kopyasını arsaral(at)yahoo.com 'dan isteyebilirsiniz.



1. Giriş

Kendini tekrarlayan olaylar, kronik olaylar matematikte periyodik fonksiyonlar[1] şeklinde soyutlanabilirler:

f( t ) ={ f( t + k T ) k e N; t ,T e R ve T = sabit } ( Formul 1 )

Burada t zamanı, Tperiyodu yani kronik olayın iki tekrarı arasında geçen süreyi belirtir. Görüldüğü gibi fonksiyonumuz her T periyodu kadar süre geçtiğinde hep aynı değerleri almaktadır. Bu şekilde tanımlayabileceğimiz bir periyodik fonksiyona[2] ilişkin örnek bir grafik aşağıdadır.


Şekil 1

Bu şekilde t belirli bir zaman birimi cinsinden f(t) ise olayın ölçülen belirli bir niteliğinin birimi cinsinden olmalıdır. Görüldüğü gibi fonksiyon her 10 zaman birimi arayla aynı f(t) değerini almaktadır.

Kronik bir olayı modellemek için ilk olarak olaya ilişkin f(t) fonksiyonunu belirlemek gerekir.


2. Bir Olayın Matematiksel Modeli
Şekil 2

Bir olayı modellediğimiz fonksiyon olayı, olayın başlaması, olması ve bitmesini dikkate alarak soyutlamaktadır.
Buna göre olay tbaş anına kadar yoktur. Bu nedenle f( t ) fonksiyonunun değeri 0’dır. tbaş anından sonra olay vardır. Bu nedenle f( t ) 1 değerini alır, ve benzeri… Buna göre olaya ilişkin f( t ) fonksiyonu şu şekilde tanımlanabilir:




Formul 2

Öte yandan birim basamak fonksiyon U( t ) şu özelliklere sahiptir[3].

Şekil 3 Formul 3

Eğer birim basamak fonksiyonu U( t )’yi zaman içinde tbaş kadar ötelersek Şekil 4

Eğer tbaş kadar ötelenmiş birim basamak fonksiyonundan tson kadar ötelenmiş birim basamak fonksiyonunu çıkarırsak
Şekil 5

İşte bu durumda bir olayı modelleyebileceğimiz matematiksel fonsiyonu elde etmiş oluruz.

f( t ) = U( t – tbaş ) – U( t – tson) ( Formul 4)


Şekil 6

Bu fonksiyon matematiksel olarak şu şekilde ifade edilebilir:
Formul 5

3. Modelin Gerçek Hayatla Karşılaştırış ve Tartılışı

U(t)’ye eşdeğer matematiksel fonksiyonlar eşik(threshold) parça parça doğrusal (piecewise linear), sigmoid ya da gaussian olabilir[4]. Eğer yakından bakarsak U( t ) şu şekli alabilir:

Şekil 7

Yani, olayın her aşamasında artma, yoğunlaşma ya da zıt yönde dalgalanmalar olabilir. Eğer bu dalgalanmalar, incelediğimiz olayın boyutu ile karşılaştırılabilir genlikte iseler, olayı tek bir olay gibi incelemek yerine birden çok olayın etkileşmesi şeklinde incelemek ya da gözlem süresini daha geniş tutmak gerekebilir.

U( t – tbaş) fonksiyonu üç fonksiyon kümeleri kümesinin bir fonksiyonu olabilir:

1- olayın olmasına ortam hazırlayan etken fonksiyonlar kümesi
2- olaya doğrudan neden olan neden fonksiyonlar kümesi
3- olayı harekete geçiren tetikleyici fonksiyonlar kümesi.

U( t – tson) fonksiyonu ise bu üç fonksiyon kümeleri kümesinin tersi işlevleri olabilir. Bu fonksiyonların matematiksel ve genel özelliklerini “Kendini Tekrarlayan Olayların Doğası Üzerine - II” adlı makalemde inceleyeceğim.


4. Kronik Bir Olayın Matematiksel Modeli

Formul 1 ve Formul 4 ‘ten periyodik bir olayı şu şekilde modelleyebiliriz:

Şekil 8

f( t ) = { U( t – tbaş + k T ) – U( t – tson + k T) k e N; t ,T e R ve T = sabit } ( Formul 6)

Bu ifade periyodik olaylar için geçerlidir. Kendini tekrar eden kronik olaylar için ise

Şekil 9

f( t ) = { U( t – tbaş + kbaş(t) Tbaş(t) ) – U( t – tson + kson(t) Tson(t))
k(t) = { kn kn e R , n e N ve n < Ksabit } ve
Tbaş(t)= { T n T n e R , n e N ve n < Ksabit } ve
Tson(t)= { T n T n e R , n e N ve n < Ksabit }
}
( Formul 7)

Tekrarlamayan olay süresi değişiklikleri tbaş tson’u zamana bağlı bir fonksiyon olarak değerlendirerek açıklamak uygun olabilir. Örneğin hava trafiğinin yoğun olduğu bir durumda kullanılan sistemlerde olabilecek bir hata yoğunluğa ilişkin olağanüstü durumun daha uzun sürmesine neden olabilir. Bu durumu tbaş ve t son’un zamana bağlı geçici bir fonksiyon değeri olarak almak gerekir.

Öte yandan, kriz durumlarının giderek ağırlaşması Tbaş (t) ve Tson (t) fonksiyonları arasındaki ilişkiyle simule edilebilir. kbaş ve kson kriz sürelerinin periyodik olarak salınımlarını incelemekte kullanılabilir.

5. Modelin Uygulamaları

5.1 Kronik olayın giderek yok oluşu

Eğer olayın tekrarlayış periyodu T çok uzarsa, yani olayın tekrarları giderek çok seyrekleşirse, Formul 6’da tbaş ve tson ‘u ihmal edip yok sayabiliriz. O zaman Formul 6 şu hali alır:

f( t ) = { U( t – k T ) – U( t – k T) k e N; t ,T e R ve T = sabit }

f( t ) = 0 ( Formul 8)

Kısacası tekrarları giderek seyrekleşen bir kronik olay giderek yok olur.

5.2 Kronik olayın giderek sıklaşması ve sürekli bir nitelik kazanışı

Eğer olayın tekrarlayış periyodu T giderek kısalıyorsa, yani olay giderek daha sık gerçekleşiyorsa, Formul 6’da k T ifadesini ihmal edip yok sayabiliriz. O zaman Formul 6 şu hali alır:

f( t ) = { U( t – tbaş ) – U( t – tson ) k e N; t ,T e R ve T = sabit } ( Formul 9)

Kısacası, formul 9 tek bir olayı modelleyen formul 4 ile aynıdır.

6. Son Değerlendiriş ve Bundan Sonrası Üzerine

Gerçek hayata bakarsak, yaptığımız modelin 5.2’de gösterdiği davranışın her zaman geçerli olmadığını görürüz. Örneğin, bazı problemler iyileşme yoluna girdiklerinde sorun belirtileri hafifleşmekle birlikte sıklaşabilir. Yani, sorun tekrarlamakla birlikte giderek daha hafif şekiller alabilir, örneğin bazı hastalıklarda. Bu durum yalnızca olayın zaman boyutunda başlangıç ve bitişlerini dikkate alan modelimizin daha da genişletilmesi gerektiğini göstermektedir. Örneğin, Formul 2’de vermiş olduğumuz fonksiyonun alanı, ( tbaş – tson ) * f( t ) yani önerdiğimiz fonksiyonun integrali gerçekleşmiş olay kapasitesi’ni ifade eder. Olay çıkma kapasitesi ise yukarıda belirttiğimiz etken, neden ve tetikleyicilere bağlıdır. Bu konuyu “Kendini Tekrarlayan Olayların Doğası Üzerine - II” adlı makalemde inceleyeceğim. Konunun havacılık ve hava trafik kontrolü açısından önemi gerçekleşmiş olay kapasitesi’nin olası kazalara ilişkin riskin hesaplanışında kullanılabilirliği olabilir.

Konunun bir diğer yönü de, kronik bir olayın nasıl algılandığıdır. Periyodik bir algılama fonksiyonunun f( t ) ile çarpımının integralinin vereceği değerler bir olayın kronikleştiğini tespit etmekte kullanılabilir. Bu algılama için geçen kontrol süresi olayı algılayışın gerektirdiği zihinsel yükün bir kısmı ile orantılıdır. Bu yöntemle aniden olay oluş durumu ya da giderek yoğun bir yükün ortaya çıkışı durumlarını gelecek makalelerimde inceleyeceğim.

REFERENCES:







.