quinta-feira, 20 de maio de 2010

segunda-feira, 17 de maio de 2010

Thoughts about Document Oriented Databases - part 3

In the last post I presented the first Use Story and the base classes for the problem, now I will walk into the service layer.

Let's make our example a little bit more complete with others user stories:
NEWTON: As a nerd, I want to be cool having the possibility to have a specific night for a specific day.
I can have a night for a specific day that differs from the normal night for that day of the week. It is a special night occasion.
PASCAL: As a friend of a nerd, I want to know when a night happens.
Based on the name of the night I want to know when it happens.
Based on the day of the week I want to know what is the night of that day <(I don't want to avoid those nerd guys)>.
I want to know which night of a specific day.

We have based your class diagram on the PYTHAGORAS use story, now with the addition of the NEWTON and PASCAL user stories we have enought information to create our first service interface (based on the Night) and your test case for the further service implementations.

So your first service will look like this:

public interface NightService {

/**
* Add a new night to the store. If the night is supplied with an ID it will be replaced.
* After the call to the registerNight method the Night object will have the ID filled.
* @param night night to be added.
*/
public void registerNight(Night night);

/**
* Replace the night stored. The provided night should have the id filled in order
* to have the date replaced correct.
* @param night night to be replaced.
*/
public void changeNight(Night night);

/**
* Returns the night by its unique identifier
* @param nightId identifier of the night.
* @return
* Night object, null if there isn't any night for that id.
*/
public Night findById(String nightId);

/**
* Returns the list of nights of the specified day of week.
*

    *
  1. The order returned by the find week day should be from the first include to the last one.

  2. *

* @param dayOfWeek
* @return null when there isn't any night for the specified day of week, otherwise a list with the nights.
*/
public List<Night> findWeekDayNights(DayOfWeek dayOfWeek);

/**
* Returns the list of nights of the specified name, the name match is not case sensitive.
*

    *
  1. The order returned should be from the first include to the last one.

  2. *

* @param dayOfWeek
* @return null when there isn't any night for the specified name, otherwise a list with the nights.
*/
public List<Night> findNightWithName(String nightName);

/**
* Returns the corresponding night for a specific day.
* @param date date of the night
* @return
* Night corresponding to the date, if there isn't any night for that day then returns null.
*/
public Night nightOf(Date date);
}


Based on this service let's created a test case to test all your service implementations. This test case is to make sure that all the services methods are working properly, after that we will be able to start executing your desired tests.


public class NightServiceTest {

private NightService nightService = null;
private TestUtils testUtils = null;

//... Code removed for simplicity

@Test
public void testRegisterNight() {
Night movieNight = testUtils.createNight(DayOfWeek.MONDAY, "MOVIE_NIGHT");
nightService.registerNight(movieNight);
Night movieNightRead = nightService.findById(movieNight.getId());

assertEquals(movieNight, movieNightRead);
}

@Test
public void testChangeNight() {
Night gameNight = testUtils.createNight(DayOfWeek.TUESDAY, "GAME_NIGHT");
nightService.registerNight(gameNight);

gameNight.setName("Man this is the really the GAME night");
nightService.changeNight(gameNight);

Night gameNightRead = nightService.findById(gameNight.getId());

assertEquals(gameNight, gameNightRead);
}

@Test
public void testFindNightById() {
Night wOwNight = testUtils.createNight(DayOfWeek.WEDNESDAY, "WOW_NIGHT");
nightService.registerNight(wOwNight);

String validId = wOwNight.getId();
String invalidId = "invalidId";

Night validNight = nightService.findById(validId);
assertNotNull(validNight);
assertEquals(validId, validNight.getId());

Night invalidNight = nightService.findById(invalidId);
assertNull(invalidNight);
}

@Test
public void testFindWeekDayNights() {
//Create the nights
Night bownlingNight = testUtils.createNight(DayOfWeek.FRIDAY, "BOWNLING NIGHT");
Night arcadeNight = testUtils.createNight(DayOfWeek.FRIDAY, "ARCADE NIGHT");
Night kartNight = testUtils.createNight(DayOfWeek.FRIDAY, "KART NIGHT");
Night pizzaNight = testUtils.createNight(DayOfWeek.THURSDAY, "PIZZA NIGHT");

nightService.registerNight(bownlingNight);
nightService.registerNight(arcadeNight);
nightService.registerNight(kartNight);
nightService.registerNight(pizzaNight);


List<Night> mondayNights = nightService.findWeekDayNights(DayOfWeek.MONDAY);
List<Night> tuesdayNights = nightService.findWeekDayNights(DayOfWeek.TUESDAY);
List<Night> wednesdayNights = nightService.findWeekDayNights(DayOfWeek.WEDNESDAY);
List<Night> thursdayNights = nightService.findWeekDayNights(DayOfWeek.THURSDAY);
List<Night> fridayNights = nightService.findWeekDayNights(DayOfWeek.FRIDAY);
List<Night> saturdayNights = nightService.findWeekDayNights(DayOfWeek.SATURDAY);
List<Night> sundayNights = nightService.findWeekDayNights(DayOfWeek.SUNDAY);

assertNull(mondayNights);
assertNull(tuesdayNights);
assertNull(wednesdayNights);
assertNotNull(thursdayNights);
assertNotNull(fridayNights);
assertNull(saturdayNights);
assertNull(sundayNights);

assertEquals(thursdayNights.size(), 1);
assertEquals(fridayNights.size(), 2);

assertEquals(pizzaNight, thursdayNights.get(0));

//The order returned by the find week day should be from the first include to the last one.
assertEquals(bownlingNight, fridayNights.get(0));
assertEquals(arcadeNight, fridayNights.get(1));
assertEquals(kartNight, fridayNights.get(2));
}

@Test
public void testFindNightWithName() {
Night surprise_Surprise_Night = testUtils.createNight(DayOfWeek.MONDAY, "SURPRISE SURPRISE NIGHT");
Night big_Surprise_Night = testUtils.createNight(DayOfWeek.MONDAY, "BIG SURPRISE NIGHT");
Night surprise_surprise_Night = testUtils.createNight(DayOfWeek.MONDAY, "suprise surprise NIGHT");
Night double_Night_one = testUtils.createNight(DayOfWeek.TUESDAY, "DOUBLE NIGHT");
Night double_Night_two = testUtils.createNight(DayOfWeek.TUESDAY, "DOUBLE NIGHT");

nightService.registerNight(surprise_Surprise_Night);
nightService.registerNight(big_Surprise_Night);
nightService.registerNight(surprise_surprise_Night);
nightService.registerNight(double_Night_one);
nightService.registerNight(double_Night_two);

List<Night> nightsRead = nightService.findNightWithName("SURPRISE");
assertNull(nightsRead);

nightsRead = nightService.findNightWithName("SURPRISE NIGHT");
assertNull(nightsRead);

nightsRead = nightService.findNightWithName("SURPRISE SURPRISE");
assertNull(nightsRead);

nightsRead = nightService.findNightWithName("SURPRISE SURPRISE NIGHT");
assertEquals(nightsRead.get(0), surprise_Surprise_Night);
assertFalse(nightsRead.get(0).equals(big_Surprise_Night));
assertFalse(nightsRead.get(0).equals(surprise_surprise_Night));

nightsRead = nightService.findNightWithName("BIG SURPRISE NIGHT");
assertFalse(nightsRead.get(0).equals(surprise_Surprise_Night));
assertEquals(nightsRead.get(0), big_Surprise_Night);
assertFalse(nightsRead.get(0).equals(surprise_surprise_Night));

nightsRead = nightService.findNightWithName("suprise surprise NIGHT");
assertFalse(nightsRead.get(0).equals(surprise_Surprise_Night));
assertFalse(nightsRead.get(0).equals(big_Surprise_Night));
assertEquals(nightsRead.get(0),surprise_surprise_Night);

nightsRead = nightService.findNightWithName("DOUBLE NIGHT");
assertEquals(nightsRead.size(), 2);
assertEquals(nightsRead.get(0), double_Night_one);
assertEquals(nightsRead.get(1), double_Night_two);
}

@Test
public void testNightOf() {
//24.12.2010 is a friday
//29.02.2012 is a wednesday
//15.05.2010 is saturday
//22.05.2010 is saturday
Night gameNight = testUtils.createNight(DayOfWeek.FRIDAY, "GAME NIGHT");
Night thaiFoodNight = testUtils.createNight(DayOfWeek.WEDNESDAY, "THAI FOOD NIGHT");
Night feverNight = testUtils.createNight(DayOfWeek.SATURDAY, "FEVER NIGHT");

nightService.registerNight(gameNight);
nightService.registerNight(thaiFoodNight);
nightService.registerNight(feverNight);

Calendar _15_05_2010 = Calendar.getInstance();
_15_05_2010.set(Calendar.DAY_OF_MONTH, 15);
_15_05_2010.set(Calendar.MONTH, Calendar.MAY);
_15_05_2010.set(Calendar.YEAR, 2010);

Calendar _22_05_2010 = Calendar.getInstance();
_22_05_2010.set(Calendar.DAY_OF_MONTH, 22);
_22_05_2010.set(Calendar.MONTH, Calendar.MAY);
_22_05_2010.set(Calendar.YEAR, 2010);

Calendar _24_12_2010 = Calendar.getInstance();
_24_12_2010.set(Calendar.DAY_OF_MONTH, 24);
_24_12_2010.set(Calendar.MONTH, Calendar.DECEMBER);
_24_12_2010.set(Calendar.YEAR, 2010);

Calendar _29_02_2012 = Calendar.getInstance();
_29_02_2012.set(Calendar.DAY_OF_MONTH, 29);
_29_02_2012.set(Calendar.MONTH, Calendar.FEBRUARY);
_29_02_2012.set(Calendar.YEAR, 2012);

Night night_24_12_2010 = nightService.nightOf(_24_12_2010.getTime());
Night night_29_02_2012 = nightService.nightOf(_29_02_2012.getTime());
Night night_15_05_2010 = nightService.nightOf(_15_05_2010.getTime());
Night night_22_05_2010 = nightService.nightOf(_22_05_2010.getTime());

assertEquals(gameNight, night_24_12_2010);
assertEquals(thaiFoodNight, night_29_02_2012);
assertEquals(feverNight, night_15_05_2010);
assertEquals(feverNight, night_22_05_2010);
}
}


Next we will make our first implementation of this interface, using a relational database.

See you soon!

terça-feira, 11 de maio de 2010

High availability: defining the terms

It is quite common to see a lot of places talking about availability, how to make your website, program, web service, etc, 24x7.

Even here I talked few times about this subject, but what I miss is some papers discussing the meanings of the terms in this area. A kind of a common language that we can talk about, I know that it depends a lot of your environment, company, software and so on. So in this post I would like to put some light in this subject.

Last month IBM release one of their famous Redbooks (I remember a time that an IBM Redbook was a kind of: "OHHHH really do you have one!, but those times are over with the floppy disks): High Availability in WebSphere Messaging Solutions.

No, I am not doing advertisement about the IBM products, it has an excellent part about concepts, so it really worths reading the Chapter1. Introduction to Availability and Chapter2. Application Design for Availability.

There is a lot of concepts and therms definition that will help you a lot, doesn't matter if you are implementing something in your own software, designing an application, for general knowledge. For me it is a must read for developers, architects, software development support team and so on.

Hope you enjoy the tip!

sexta-feira, 30 de abril de 2010

Thoughts about Document Oriented Databases - part 2

In the first post I presented some issues that I would like to investigate concerning the Document-Oriented Databases. Now I am going to introduce you with the User Story that I will use as basis for that, so "Sit! Here comes the story!".

Let's imagine a group of geek-nerd-friends, every week day they do something during the night. They have the Halo night, the comic store night, the movie night and so on, a full life. Because of that one of those guys wants to manage the nights, they want to know for each night: the participants, the activities of each one, time of those activites, how much people are involved in each activity and which nice widgets they have to use for those activities.


So let's write our first use story (I will give to the user stories some short name to simplify further references):

PYTHAGORAS: As a nerd, I want to organize the night and distribute the activities of that night across all my nerd friends.
  • All the nights have a name, like Halo night or Comic store night.
  • It always happens in a specific day of the week, like Halo night is always on wednesday.
  • To have a nice night we should execute a group of activities (buy food, bring the game, etc).
  • Each activity has one responsible.
  • Each activity could need the help of 1 or more people.
  • Each activity could require one or more widget to be executed (using the iPhone to find the pizza store, the teleport machine to arrive in time, etc).
  • Each activity should be done to have a nice night.
  • A night is ready to start when all the activities are done.

Based on this user story we can create this base class diagram:


These classes will be improved over the time, but with the current information is what we can create.

To understand the class model, here is the test case of the Night::isNightReady() method, it will help to understand what a Night and an Activity is:


@Test
public void testIsNightReady() {
Night saturdayNightFever = new Night();
saturdayNightFever.setDayOfWeek(DayOfWeek.SATURDAY);
saturdayNightFever.setName("Saturday Night Fever revival night!");

//No activities means that the night is read to start
assertTrue(saturdayNightFever.isNightReady());

//Let's add a list of activities in the night
List<Activity> activitiesOfTheNight = new ArrayList<Activity>();
saturdayNightFever.setActivityList(activitiesOfTheNight);
assertTrue(saturdayNightFever.isNightReady());

//For a saturday night fever we need to practive dance listening the BeeGees K7
Activity practiceDance = new Activity();
practiceDance.setWhatShouldBeDone("Dance listening BeeGees K7");
practiceDance.setResponsible("me");
saturdayNightFever.getActivityList().add(practiceDance);

//And also get the nice white suite
Activity takeTheSuite = new Activity();
takeTheSuite.setWhatShouldBeDone("Take the white suite at mother's house");
takeTheSuite.setResponsible("me");
saturdayNightFever.getActivityList().add(takeTheSuite);

//And finally meet the girl
Activity meetTheGirl = new Activity();
meetTheGirl.setWhatShouldBeDone("Meet the girl at his house");
meetTheGirl.setResponsible("me");
saturdayNightFever.getActivityList().add(meetTheGirl);

//I have done none activity, so the night is not ready to start
assertFalse(saturdayNightFever.isNightReady());

//I've found the K7 and danced a lot, but still not ready.
practiceDance.setDone(true);
assertFalse(saturdayNightFever.isNightReady());

//Got the old father's white suite at moms house
takeTheSuite.setDone(true);
assertFalse(saturdayNightFever.isNightReady());

//Meet the girl
meetTheGirl.setDone(true);

//Now it is time to ROCK
assertTrue(saturdayNightFever.isNightReady());
}


And finally I have set a project at GitHub: TADOD, so I will keep publishing the code there.
It is a maven project, so it should be easy for everyone to download it and run. All the entries in the blog will be present in the project site (just need to execute mvn site).

In the next post I will define the interfaces of the services, so we will be able to create the test cases and later start your analysis.

See you soon, and have a nice 1st of May (beware of the Punks around).

terça-feira, 27 de abril de 2010

New pattern - The Match Marker Design Pattern

Yes that is true, the GOF Design Pattern book is not the bible of the OO design patterns; it is just The Book of Genesis, the beginning but it is far away of the end. In fact new problems, causes new common ways to solve them and at a final stage new patterns.

In a recent article Michael Bar-Sinai presents a new pattern The Match Marker Design Pattern, which has the intention to solve similar problems to the Visitor pattern. The main idea of this pattern is to solve the situation where you want to extend the action applied to your business objects in a better and clear way than just extending objects.

Let's suppose this problem (well it is the same one presented by Michael, I guess it is easier to show the same): you have an Employee and needs to calculate the salary of it, as type of Employee we have Manager and Engineer. How to solve this problem? Create a salary calculator that has a method that checks the instance of the class to calculate? Does allow the Employee to return the salary and overwrite it for each class?

Well take a look at this pattern: it is a interesting way to solve the problem: The Match Marker Design Pattern.
For those that want to remember the Visitor pattern, here it goes an article explaining it: Design Patterns Uncovered: The Visitor Pattern.

Enjoy it!

terça-feira, 20 de abril de 2010

Java in the factory field

Back to the middle of 2003 I have spent sometime reading and investigating how to use Java in the factory field, based on some standards like Fieldbus, Profibus, Supervisory Systems and so on. It was an early time, a time that I was walking in green fields but without money or contacts to transform the research in something more concrete.

Not that nowadays I have contacts or money, in fact I am not even in the green fields anymore, but looking at my old papers and pdfs I have found those documents. And it brought me some curiosity where was the state of things in this area; back to that time we had a lot of papers, researches but no real implementation in the factory fields itself; it was still the realm of C programmers.

For my disappointment a brief search and inspection proved that Java hadn't stepped forward in most of these areas, but a work demonstration at JavaOne 2007 looked really promising. It is an industry robot automation using merely JAVA, for sure it is using the Real-Time Specification for Java 2.0, as we need a more strict control over what is happening in the machine.
It is used on top of a bus standard called EtherCAT, which is a kind of bus that communicates over Ethernet and not some serial protocol.

The main point here is to see that it is possible to have Java running in a critical environment that needs time based response in a preemptive mode. By the way if you want to see the robot in action just go for this video World's First Java-Controlled Industrial Robot



For those that want to remember about industrial bus here is collection of wiki pages that could helpful:
- Fieldbus (http://en.wikipedia.org/wiki/Fieldbus)
- EtherCat (http://en.wikipedia.org/wiki/EtherCAT)
- Profibus (http://en.wikipedia.org/wiki/PROFIBUS)

And finally the paper Using Real-time Java for Industrial Robot Control that has an overview about the robot implementation with java.

I hope you enjoy it as a new curiosity area or make you remember old hard working days!

quarta-feira, 14 de abril de 2010

Where is your coin?

Some time ago people used to believe that you should bury a person with a coin in his mouth in order to pay the ship (or at least to guarantee a good place in the ship) to the land of the death.
If even in the land of the death, in the other world, there isn't free entrance, imagine here, imagine in the software's land. In a nutshell: there isn't free lunch. But why all this death, lunch talks; are you hungry? Are you afraid of dying?

No, absolutely not! The question is that people still think as new technologies as a free lunch, something like, an all around solution. If you need speedy them you should let it go the versatility, if you want all around solution them forget the speedy and welcome to the complexity nightmare; the blanket is not big enough for everyone.

I guess that all of you already heard about those Agile things like XP, Scrum and others, they are not really the same thing but all of them propagate the idea of: postpone as much as you can the decisions, most of the time you will realize that they are not needed or you will have the enough knowledge to do it right; I completely agree with that (by the way it doesn't means that the decision should not be made or it should be a no meaning decision, like a lot that we see around).

And them you come to NoSQL alternatives, quite flexible and it seems to fit so smoothly that it is like a temptation. No schema, you are so free to change it that you can even have different versions of data in a productive environment, really nice. Do you remember that we need to give the coin to the punter? Well, the NoSQL is a really light because of the simplicity of its schema, no rules, but it has a cost, the queries.
Once you go for this kind of solutions you need to pay in the queries, you don't have the power of the relational databases here. If in one end you don't need to think about the schema in the early beginning at the other end you DO need to think about how you are going to look for that information in the future.

Most of those solutions requires you to duplicate data if you want two different queries at the same time, or you need to create a view that will be your query, or simply no query at all, just a sequential access to the data (in a predefined order). The benefits? Fast, light, easy to distribute, but you need to pay a price for that.

So are they not really going to work in the real world? Absolutely not! Do not misunderstand me, they have a place, and I guess a big one. But we need really to understand what kind of application we are going develop, in the early beginning to know if it fits or not, and to address the needs of the technology.

The point here is: you need to know with whom you are dealing; do not take only the nice words of Agile and NoSQL; otherwise you will fall in the sing of the sirens and your ship will crash the rocks of reality.

As usual I will let a link here, I guess it will help you all to clarify what I am talking about the drawn backs of the NoSQL technology (in this case Cassandra): WTF is a Super Column