    001    /*
    002    @license.text@
    003     */
    004    package com.hammurapi.reasoning.tutorial.rules;
    005
    006    import com.hammurapi.reasoning.spi.Condition;
    007    import com.hammurapi.reasoning.spi.Infer;
    008    import com.hammurapi.reasoning.spi.InferenceContext;
    009    import com.hammurapi.reasoning.tutorial.objectmodel.Brother;
    010    import com.hammurapi.reasoning.tutorial.objectmodel.Child;
    011    import com.hammurapi.reasoning.tutorial.objectmodel.Sibling;
    012    import com.hammurapi.reasoning.tutorial.objectmodel.Sister;
    013
    014    public class SiblingRules {
    015
    016            /**
    017             * Male sibling is brother, female sibling is sister.
    018             * If A is a sibling of B then B is a sibling of A.
    019             * @param sibling
    020             * @return Brother or Sister.
    021             */
    022            @Infer({Sibling.class, Brother.class, Sister.class})
    023            public Sibling infer(Sibling sibling) {
    024                    InferenceContext.INSTANCE.post(new Sibling(sibling.getObject(), sibling.getSubject()));
    025
    026                    if (sibling.getSubject().isMale()) {
    027                            if (!(sibling instanceof Brother)) {
    028                                    return new  Brother(sibling.getSubject(), sibling.getObject());
    029                            }
    030                    } else {
    031                            if (!(sibling instanceof Sister)) {
    032                                    return new Sister(sibling.getSubject(), sibling.getObject());
    033                            }
    034                    }
    035                    return null;
    036            }
    037
    038            /**
    039             * If two children have common parent then they are siblings.
    040             * @param child
    041             * @param anotherChild
    042             */
    043            @Condition("!arg0.getSubject().equals(arg1.getSubject()) && arg0.getObject().equals(arg1.getObject())")
    044            @Infer(Sibling.class)
    045            public void infer(Child child, Child anotherChild) {
    046                    InferenceContext.INSTANCE.post(new Sibling(child.getSubject(), anotherChild.getSubject()));
    047                    InferenceContext.INSTANCE.post(new Sibling(anotherChild.getSubject(), child.getSubject()));
    048            }
    049    }