# JRules Example

Back to PRD Logic Example

## Execution results

When run, this programs displays:

```Uncle or aunt of william is: edwardW
Uncle or aunt of william is: andrew
Uncle or aunt of william is: mark
Uncle or aunt of william is: anne
```

This result is correct.

## JRules rule file, accompanied with a main function

```function void ilrmain(Object ?arg)
{
// JRules trick: do not care.
// Initializations needed to assign distingushed values to each constant.
constants.william = 0;
constants.diana = 1;
constants.charles = 2;
constants.henry = 3;
constants.elizabeth = 4;
constants.philip = 5;
constants.frances = 6;
constants.edward = 7;
constants.anne = 8;
constants.andrew = 9;
constants.mark = 10;
constants.edwardW = 11;

// Parents relationships fact basea
insert parents() { p1=william; p2=diana; p3=charles; }
insert parents() { p1=henry; p2=diana; p3=charles; }
insert parents() { p1=charles; p2=elizabeth; p3=philip; }
insert parents() { p1=diana; p2=frances; p3=edward; }

insert parents() { p1=anne; p2=elizabeth; p3=philip; }
insert parents() { p1=andrew; p2=elizabeth; p3=philip; }
insert parents() { p1=edwardW; p2=elizabeth; p3=philip; }

insert married() { p1=diana; p2=charles; }
insert married() { p1=elizabeth; p2=philip; }
insert married() { p1=frances; p2=edward; }
insert married() { p1=anne; p2=mark; }

execute();
}

/*
* Rule: parent(C,M) <= parents(C,M,D).
*/
rule parent1
{
when
{
parents(?C: p1; ?M: p2; ?D: p3);
}
then
{
insert parent() { p1=?C; p2=?M; }
}
};

/*
* parent(C,D) <= parents(C,M,D).
*/
rule parent2
{
when
{
parents(?C: p1; ?M: p2; ?D: p3);
}
then
{
insert parent() { p1=?C; p2=?D; };
}
};

/*
* sibling(X,Y) <= parents(X,M,D) and parents(Y,M,D).
*/
rule sibling
{
when
{
parents(?X: p1; ?M: p2; ?D: p3);
parents(?Y: p1; p2 == ?M; p3 == ?D; ?X != ?Y);
}
then
{
insert sibling() { p1=?X; p2=?Y; };
}
};

/*
* aORuDirect(C, A) <= parent(C,P) and sibling(P,A).
*/
rule aORuDirect
{
when
{
parent(?C: p1; ?P: p2);
sibling(p1 == ?P; ?A: p2);
}
then
{
insert aORuDirect() { p1=?C; p2=?A; };
}
};

/*
* aORuMarr(C, A) <= aORuDirect(C,X) and married(X,A).
*/
rule aORuMarriage1
{
when
{
aORuDirect(?C: p1; ?X: p2);
married(p1 == ?X; ?A: p2);
}
then
{
insert aORuMarriage() { p1=?C; p2=?A; };
}
};

/*
* aORuMarr(C, A) <= aORuDirect(C,X) and married(A,X).
*/
rule aORuMarriage2
{
when
{
aORuDirect(?C: p1; ?X: p2);
married(?A: p1; p2 == ?X);
}
then
{
insert aORuMarriage() { p1=?C; p2=?A; };
}
};

/*
* aORu(C,A) <= aORuDirect(C,A).
*/
rule aORu1
{
when
{
aORuDirect(?C: p1; ?A: p2);
}
then
{
insert aORu() { p1=?C; p2=?A; };
}
};

/*
* aORu(C,A) <= aORuMarr(C,A).
*/
rule aORu2
{
when
{
aORuMarriage(?C: p1; ?A: p2);
}
then
{
insert aORu() {p1=?C; p2=?A; };
}
};

/*
* ? aORu(william, A).
*/
rule queryRule
{
when
{
aORu(p1==william; ?A: p2);
}
then
{
// ?A here is an int. We need to convert it into a string.
String ?s = "";
if (?A == 0) ?s = "william";
else if (?A == 1) ?s = "diana";
else if (?A == 2) ?s = "charles";
else if (?A == 3) ?s = "henry";
else if (?A == 4) ?s = "elizabeth";
else if (?A == 5) ?s = "philip";
else if (?A == 6) ?s = "frances";
else if (?A == 7) ?s = "edward";
else if (?A == 8) ?s = "anne";
else if (?A == 9) ?s = "andrew";
else if (?A == 10) ?s = "mark";
else if (?A == 11) ?s = "edwardW";

out.println("Uncle or aunt of william is: " + ?s);
}
};

```

## JRules Executable Object Model (XOM) file

JRules supports dynamic object model. This model has been generated on-the-fly to support the execution of these rules. While a developer can do this, it becomes much more challenging for an automatic translator. See my comments on this.

```/*
* ILOG JRules virtual classes used to implement the "parents" object model.
*/
public interface constants extends ilog.rules.factory.IlrHashObject
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public static int william;
public static int diana;
public static int charles;
public static int henry;
public static int elizabeth;
public static int philip;
public static int frances;
public static int edward;
public static int anne;
public static int andrew;
public static int mark;
public static int edwardW;
}

public class parents extends ilog.rules.factory.IlrHashObject, constants
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public parents();
public int p1;
public int p2;
public int p3;
}

public class parent extends ilog.rules.factory.IlrHashObject, constants
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public parent();
public int p1;
public int p2;
}

public class sibling extends ilog.rules.factory.IlrHashObject, constants
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public sibling();
public int p1;
public int p2;
}

public class married extends ilog.rules.factory.IlrHashObject, constants
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public married();
public int p1;
public int p2;
}

public class aORuDirect extends ilog.rules.factory.IlrHashObject, constants
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public aORuDirect();
public int p1;
public int p2;
}

public class aORuMarriage extends ilog.rules.factory.IlrHashObject, constants
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public aORuMarriage();
public int p1;
public int p2;
}

public class aORu extends ilog.rules.factory.IlrHashObject, constants
property "ilog.rules.engine.driver" "ilog.rules.factory.IlrHashDriver"
{
public aORu();
public int p1;
public int p2;
}
```