Gridflip

I received and invitation facebook.com site, so i registed. I have to
say that it’s a great site, you can share photos and a lot of other
stuff with your friends. It’s very cool.

But … the history begins when i clicked in the “jobs” bottom link,
then puzzles and … my obsession begins :D

The grid flip puzzle disturbs me for several days.

Then i began to think in the algorithm. First i wanted to isolate the
row and columns operations in others, something like flip a single cell.
Later i can transform the complete mask, previously calculated with
flip_cell(x,y), into row and columns operations.
At this moments i’m not absolutly sure if this is possible at all,
so … i thougt another way.

With an small array i thought in the all possible combinations with
columns and rows flips.

This is the formula:

2^(colums+rows) = number of mask combinatios

so in a 5 x 5 array, the program have to test 2^10 = 1024

The next step is to make all the masks combinations. I was a very easy
task. I only had to iterate from 0 to 2^columns and in this loop iterate
from 0 to 2^rows.

Maybe i can explain with an example. Let’s take a look to a 3×3 array,
and start with columns:
loop from 0 to 2^3 = 8

0 -> 000
1 -> 001
2 -> 010
3 -> 011
4 -> 100
5 -> 101
6 -> 110
7 -> 111

In binary notation i have all combination for the columns like:

000
000
000

001
001
001

010
010
010

then the rows time:

000
000
000

100
100
100

010
010
010

and the combination of each other with

for i in 0 .. 2^columns
for j in 0 .. 2^rows
mask(i,j)

Then i can test the mask with the data. This process is very slow and
tooks me several hours to get the fist success combination, but the
solution is simple and the number of column and rows flips is very low.

I used java to implement the program, but probably in c or c++ it would
be faster.

I’ve enjoyed a lot with this puzzle, thanks to the facebook group for
this personal challenge.

My next step is trying to optimize the proccess to get a solution in
less time. Maybe i can infer row and column flips from cell operations
or with minimax, or perhaps … with something different, in don’t know.

A simple debug XSLT

For debug purposes i want to output the input xml document inside a textarea. This is a easy way to select and copy the xml and use later in other application.

So, here it is:

<xsl:stylesheet 
 version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 
 method="html" 
 encoding="ISO-8859-1" 
 indent="yes"
 omit-xml-declaration="yes"  
 media-type="text/html"/>
 
<xsl:template match="node()|@*">
 <xsl:copy>
  <xsl:apply-templates select="node()|@*"/>
 </xsl:copy> 
</xsl:template>
				
<xsl:template match="/">
 <textarea cols="80" rows="20">
  <xsl:apply-templates select="node()|@*"/>
 </textarea>
</xsl:template>
	
</xsl:stylesheet>

XSLT and PHP little trick

Hi all,

several days ago i’ve to use XSLT transformation in PHP code. One of the requirements is to use external .xml files, so in the .xsl file i have to use references like http://example.com/xml/myfile.xml, but sablotron doesn’t seems to implement this kind of schema. Only works with file: schema.

<xsl:value-of
select="document('http://example.com/xml/myfile.xml')
/items/group/item[./@code='ES']/@value"/>

this is a piece of XSLT that i want to use

I thought an update system that download the external resources, but, i can’t believe that didn’t exist any better solution (of course simpler).

My fist attemp was using xslt_base_dir with and url like:

$xh = xslt_create();
$fileBase = 'http://example.com/xml/';
xslt_set_base ( $xh, $fileBase );

… but didn’t work.

Checking all the XSLT methods in the PHP reference i’ve found one that seems fit: xslt_set_scheme_handlers. Despite it isn’t already documented one comment is enough explanatory.

function mySchemeHandler($processor, $scheme, $rest) {
 if($scheme == 'http') {
  return file_get_contents($scheme.":".$rest);
 }
}

$SchemeHandlerArray = array();
$SchemeHandlerArray["get_all"] = "mySchemeHandler";
			
$xh = xslt_create();
xslt_set_scheme_handlers($xh,$SchemeHandlerArray);
			
echo xslt_process($xh, 'arg:/_xml', $config['xslt'], NULL,
 array ( '/_xml' => utf8_encode($result) ), 
 array (	
  'link' => $link , 
  'locale' => $config['locale'] ,
  'extra' => $config['extra']
) );

xslt_free($xh);

and .. works!!

Java, PHP, XML and AJAX (how to mix all together)

Introduction

Why does anyone want to mix all this stuff together? are you crazy? maybe i am :)
The main purpouse of this experiment is to test the real availability of this heterogeneus environments working together.

First of all, i want to explain in a nutshell what i’m going to to and how.

I’ll use the powerfull platform of Java to implemente all the application logic. In this test, the application logic will be very simple, but in real scenarios maybe we want to use advanced features like replication, clustering, relational databases, work with XMLs documents, operations like XSL-T, XPath, XQuery, and so on. And there isn’t a better solution than Java.

With PHP you can code big projects, but it doen’t have all the Java third party APIs and the big comunity of the open source. But always is interesting to use the strengths of each other, and the web services is a way to connect both.

In Java i’m going to use Apache Axis to expose a web service and in PHP i’m going to use NuSOAP API to connect to this web service.

And the last step is to use AJAX to improve the use experience. In the Java side, the choice for me is very clear, i would use DWR (of course), but in PHP side i decided to use an agnostic Javascript library, i’m going to use Dojo Toolkit.

Java and PHP
This is a flow of whole process, how the several libraries and APIs interconnect.

Read more »

JMeter & DWR 2.0RC2 Addendum

The previous exposed tests have done with a milestone version of DWR 2. So with the new release candidate, there are several changes that affects the way we can use JMeter with DWR. Those changes forces us to:

  • change the regular expresion to obtain the script session id value
    dwr.engine._origScriptSessionId = '(.*)';
  • make a jsp file to get the jsessionid value (now there isn’t a javascript variable with this value in engine.js)
  • attach an XML XPath extractor or a regular expression extractor to the previous .jsp

The rest of procedures works perfect with this version.

See here the original article

XML Schema Explorer

Introduction

Now with the XML momentum, is very important to generate a valid XMLs against a XML Schema. The siblings order or the required elements can produce us a big headache. The first step is to review the schema definition, and there are a lot of tools that can help, but when we need to generate a XML file programatically, we need and API to solve this kind of problems.

My first try was generate a XML from the Schema definition, but with a Schema you can generate a lot of differents XMLs. The choices, sequences and the recursive parts make imposible to get a universal file. You can generate one of the posibilities, but not all.

Other way is to parse the .xsd file with xpath, but this would be a very complex task.

The natural way is to use the Schema definition to browse it and one APIs that can do it is Eclipse XSD.

Other API is XSOM XML Schema Object Model. I’ve not tested it, but if any of you have done, please send me your experiences.

If any of you know other APIs, please report me. I didn’t find other APIs.

First, i recoment to read the XML Schema specs at http://www.w3.org/TR/xmlschema-0/, it seems to be a hard task, but despite you can edit a .xsd file with high level tools like Eclipse WTP, always is a good idea make your first by hand. Reading the specs you have a complete review of all posibilities.

Read more »

JMeter and DWR

Testing a web application with JMeter is easy, but when your application uses AJAX (with DWR, for instance) and sessions, it can be a complex task.

I’ve developed an application with DWR and i want to test the performance. DWR is one of the coolest project to make an AJAX applications, IMHO.
The first aproach with JMeter is to save a set of requests to reproduce later, but those requests are asociated with a session. So, if i run concurrently those requests in several threads:

  • i don’t test what i want. I want one session per thread
  • maybe i obtain inconsistent data

To record a set of request, we have to:

  • In Test Plan add a Config Element/HTTP Request Defaults
  • In HTTP Request Defaults set a value to Server Name or IP, Port Number and Protocol
  • This is necesary to chage easily the host and port of all request. If you don’t use a HTTP Request Defaults, every request recorded will have a host and port value, and if you want to test another host you have to chane a lot of values.

  • In Test Plan add a Thread Group
  • In this Thread Group add a Logic Controler/Recording Controller
  • It is a simple container for the requests. ;)

  • In WorkBench add a Non-Test Elements/HTTP Proxy Server
  • In HTTP Proxy Server set Port, Target Controller and Pattern to Include
  • In HTTP Proxy Server add a Timer/Gaussian Random Timer
  • In the Gaussian Timer, set the Constant Delay Offset to ${T}
  • This record the time between every request with a random deviation. It’s very important to try to reproduce the user experience.

view JMeter screencast

In this screencast i introduced HTTP Cookie Manager and a listener item that didn’t be included in the above explanation. It will be used in stress time to let JMeter manage cookies to store JSESSIONID value of every thread (synthetic user) and view the results.

Now, it’s time to deploy your application and configure your browser to use the JMeter proxy.

view proxy screencast

Easy, isn’t it?

Try 1

Then, my next step was recording several users and grow up the JMeter config file. I’ve created several thread groups, one per concurrent user with distinct session IDs (remember to close the browser between every recording to clean cookies). But .., with six or seven users the files was almost 6 MB, it may vary in your project and the complexity of the user requests.

JMeter is a really cool application, but with this kind of file sizes, it needs a lot of memory. I’ve got a lot of OutOfMemoryExceptions and decided to record every new user from scratch to later merge in only one project file.

This was not a success experience, i only could get less than ten users and, the JMeter can’t use those big files, and i wasted a lot of time recording every new user, so …

Try 2

Regenerate a new session ID per thread. This seems to be easy, i have a lot of experience with xpath, and i can extract a portion of a html with it. First i created a .jsp to print the session ID:

<html>
<body>
<sessionid><%=session.getId()%></sessionid>
</body>
</html>

At the beginning of the user recording, i request this .jsp, and the i attach a Post Processor/XPath Extractor in this HTTP Request, with this values:

  • Reference Name: USERSESSIONID
  • XPath query: /html/body/sessionid/text()

Now i only have to replace the session IDs ocurrences in the JMeter config file (.jmx) with:

${USERSESSIONID}

I used jEdit to replace, only have to find the ID like E4B1B20074F2106ED330B98387EAF8E5 and replace all with the above text.

But … DWR needs another sessionid (script session id) and the JMeter failed :(

Try 3

I thought i could get DWR script session id like the session id, but minutes ago of reading the DWR code, i discover that engine.js have this values in JavaScript variables. Instead using XPath extractor y used a Regular Expresion extractor.

In the engine.js HTTP Request, attach two Regular Expresion Post Processor with:

  • Reference Name: HTTPSESSIONID
  • Regular Expression: DWREngine._httpSessionId = “(.*)”;
  • Template: $1$
  • Reference Name: SCRIPTSESSIONID
  • Regular Expression: DWREngine._scriptSessionId = “(.*)”;
  • Template: $1$

regexp

Then you have to replace the values like Try 2 in the DWR requests:

[...]
<stringProp name="Argument.value">1
httpSessionId=${HTTPSESSIONID}
scriptSessionId=${SCRIPTSESSIONID}
page=/smartcv2/Edit.do
c0-scriptName=MainForm
c0-methodName=setValue
c0-id=5943_1159885700394
c0-param0=string:biologicalDescriptors.dateOfBirth
c0-param1=string:October%2015%2C%201979</stringProp>
[...]

Check the .jmx file twice before loading in JMeter and if you have problems, use a TCP Monitor to see the HTTP traffic. Axis and Eclipse + WTP come with one (also netbeans, but i’ve not used yet).

See here the addendum for DWR 2.0RC2

Install bugzilla in Solaris 10

First of all i’ve to say that i love Solaris and the other UNIX variants like GNU/Linux, but sometimes i love the easy way of Mac OSX or Windows installations too.

The first thing i did is install Solaris 10 in a x86 machine, the version i’ve used is the last one (at this time 10 6/6) and have a lot of open source software out-of-the-box like MySQL, Apache, Perl …

MySQL

I think it was one of the easiest steps of whole process. There are a file in /etc/sfw/mysql called README.solaris.mysql that explaint the entire installation.

/usr/sfw/bin/mysql_install_db
groupadd mysql
useradd -g mysql mysql
chgrp -R mysql /var/mysql
chmod -R 770 /var/mysql
installf SUNWmysqlr /var/mysql d 770 root mysql
cp /usr/sfw/share/mysql/my-medium.cnf /var/mysql/my.cnf
/etc/sfw/mysql/mysql.server start

Check the file, maybe in you version there are more steps.

Apache 2

Just copy /etc/apache2/httpd.conf-example into /etc/apache2/httpd.conf and try:

svcadm enable apache2

Now check in your browser thar Apache is serving properly.

Maybe someday i continue this history … sorry

Perl and Modules

sunfreeware

GCC and Sun compiler tools

Bugzilla

Sendmail

Conclusion

Commons Validator outside Struts framework

Introduction

Commons Validator API is a piece of software that let us to decouple the validation routines and the main code of the project. With Validator, we can save a lot of time using predefined rules to accomplish the repetive tasks. The constraints are defined in a xml document and can be reusable in several projects.

Maybe, you have already used Validator in Struts framework, but with minimal effort we can integrate in your application.

This is your actual application, you have validations written in java code in many parts, and the code is redundant, and not reusable. When you begin another project, you have to code again this validations or copy & paste. This way is a error prone aproach, and only recomended in very simple applications.
myApp3

On the other side, we have Struts. This framework is modular, and one specific part has the only purpose of validation, commons validator. Despite validator was originally coded for struts is also valid for any other kind of applications.
struts
Of course, this is not a real struts diagram, is only for this explanation purpose.

We want to plug validator inside the application and move the java code to xml document. In the future, adding more validation constraints will be quick and easy.
myApp2

Read more »