Skip to content

Simple Genealogy Rules – part 3

August 27, 2013

In part1 we covered the Problem Domain, in part2 we set up our project to work with Drools 5.4.0.Final and JUnit.  Now in part3 we will create the Classes for our Facts that we are going to pass to the Rule Engine.

Creating the Facts

If we remember from our Problem Domain, the main fact that we will provide to the Rule Engine (for now) is each Person for whom we have found a record.

For this reason we will need a Person class.  Also, since we are going to be doing a lot of Date comparison and manipulation, we will extend the java.util.Date class to give us and ExtendedDate class into which we can add any new methods that we require.

Since the Person class will have a dependency on the ExtendedDate class, let’s start with the ExtendedDate class.

ExtendedDate

in the src/main/java source folder, create the package com.skills421.training.extend.util

Now create the ExtendedDate class in that package as follows:

package com.skills421.training.extend.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ExtendedDate extends Date
{
	public static final String DEFAULTFORMAT = "dd/MM/yyyy";
	private String dateFormat;

	/**
	 *
	 */
	private static final long serialVersionUID = 4921346043888844588L;

	public ExtendedDate(String dateFormat, String dateString) throws ParseException
	{
		this.dateFormat = dateFormat;

		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);

		Date date = formatter.parse(dateString);
		this.setTime(date.getTime());
	}

	public ExtendedDate(String dateFormat, long time)
	{
		this.setTime(time);
		this.dateFormat = dateFormat;
	}

	public ExtendedDate(String dateString) throws ParseException
	{
		this(DEFAULTFORMAT,dateString);
	}

	public ExtendedDate(long time)
	{
		this(DEFAULTFORMAT,time);
	}

	public String toString()
	{
		SimpleDateFormat formatter = new SimpleDateFormat(this.dateFormat);
		return formatter.format(this);
	}

	public ExtendedDate add(int days, int months, int years)
	{
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(this.getTime());

		calendar.add(Calendar.DAY_OF_YEAR, days);
		calendar.add(Calendar.MONTH, months);
		calendar.add(Calendar.YEAR, years);

		ExtendedDate newDate  = new ExtendedDate(calendar.getTimeInMillis());

		return newDate;
	}

}

ExtendedDate.java

This class extends the java.util.Date class and adds the following constructors and methods:

  • public ExtendedDate(String dateFormat, String dateString)
  • public ExtendedDate(String dateString)

These methods enable us to pass the dateFormat – e.g. dd/MM/yyyy (the default) and the dateString to instantiate and ExtendedDate object.  The dateFormat is then used by the overloaded toString() method to return the correctly formatted Date as  a String.

  • public ExtendedDate(String dateFormat, long time)
  • public ExtendedDate(long time)

Likewise, these methods enable us to pass the dateFormat to the class and use this for formatting the output returned by the toString() method.

  • public ExtendedDate add(int days, int months, int years)

This method will be used from within the Rule Engine and allows us to add days, months and years to the existing ExtendedDate and return a new ExtendedDate object.

This is particularly useful for determining if, for example a father is 18 years older than a prospective son , and if the father was married 9 months before the son was born:

sonsDateOfBirth > fathersDateOfBirth.add(0,0,18)

sonsDateOfBirth  > fathersDateOfMarriage.add(0,9,0)

as we will see when we write the Rules.

 

Testing the ExtendedDate

Since we have JUnit installed, let’s create a quick JUnit test to test our ExtendedDate class works as planned.

In the src/test/java source folder we need to create the same package as we created in src/main/Java: com.skills421.training.extend.util

inside that package we can create the JUnit test TestExtendedDate by doing the following:

Right-click on the com.skills421.training.extend.util package and from the pop-up menu select New -> Other -> JUnit

020-JunitTest

Select the JUnit Test Case and click Next

021-JunitTest

set the name to TestExtendedDate and click Finish.

Now we can edit the source as follows:

package com.skills421.training.extend.util;

import static org.junit.Assert.*;

import java.text.ParseException;

import org.junit.Test;

import com.skills421.training.extend.util.ExtendedDate;

public class TestExtendedDate
{

	@Test
	public void test() throws ParseException
	{
		ExtendedDate date = new ExtendedDate("28/05/1907");
		ExtendedDate newDate = date.add(0, 0, 15);

		assertEquals("28/05/1907", date.toString());
		assertEquals("28/05/1922", newDate.toString());
	}

	@Test
	public void test2() throws ParseException
	{
		ExtendedDate date = new ExtendedDate("28/05/1907");
		ExtendedDate newDate = date.add(2, 1, 3);

		assertEquals("28/05/1907", date.toString());
		assertEquals("30/06/1910", newDate.toString());
	}

	@Test
	public void test3() throws ParseException
	{
		ExtendedDate date = new ExtendedDate("28/05/1907");
		ExtendedDate newDate = date.add(5, 7, 20);

		assertEquals("28/05/1907", date.toString());
		assertEquals("02/01/1928", newDate.toString());
	}

}

TestExtendedDate.java

 

Running the Tests

Now to run the tests right click on the class in the Project Explorer and from the pop-up menu select Run As -> Junit Test

You should see the following output:

024-JunitTest

 

Moving Forward

In the next part we will create the Person class which is the type of Fact that we will insert into the Rule Engine, we will create the Rule Runner that will build the Knowledge Base for our Rule Engine and insert our Facts into the Rule Engine, and we will create and run our first rule.

Advertisements

From → Tutorials

2 Comments
  1. Reblogged this on Skills421.

Trackbacks & Pingbacks

  1. Moving Geneology to Drools 6.0 | Drools Rules

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: