/*
@license.text@
 */
package com.hammurapi.reasoning.tutorial.rules;

import com.hammurapi.reasoning.spi.Condition;
import com.hammurapi.reasoning.spi.Infer;
import com.hammurapi.reasoning.spi.InferenceContext;
import com.hammurapi.reasoning.tutorial.objectmodel.Brother;
import com.hammurapi.reasoning.tutorial.objectmodel.Child;
import com.hammurapi.reasoning.tutorial.objectmodel.Sibling;
import com.hammurapi.reasoning.tutorial.objectmodel.Sister;

public class SiblingRules {	
	
	/**
	 * Male sibling is brother, female sibling is sister.
	 * If A is a sibling of B then B is a sibling of A.
	 * @param sibling
	 * @return Brother or Sister.
	 */
	@Infer({Sibling.class, Brother.class, Sister.class})
	public Sibling infer(Sibling sibling) {	
		InferenceContext.INSTANCE.post(new Sibling(sibling.getObject(), sibling.getSubject()));
		
		if (sibling.getSubject().isMale()) {
			if (!(sibling instanceof Brother)) {
				return new  Brother(sibling.getSubject(), sibling.getObject());
			}			
		} else {
			if (!(sibling instanceof Sister)) {
				return new Sister(sibling.getSubject(), sibling.getObject());
			}			
		}
		return null;
	}

	/**
	 * If two children have common parent then they are siblings.
	 * @param child
	 * @param anotherChild
	 */
	@Condition("!arg0.getSubject().equals(arg1.getSubject()) && arg0.getObject().equals(arg1.getObject())")
	@Infer(Sibling.class)
	public void infer(Child child, Child anotherChild) {
		InferenceContext.INSTANCE.post(new Sibling(child.getSubject(), anotherChild.getSubject()));
		InferenceContext.INSTANCE.post(new Sibling(anotherChild.getSubject(), child.getSubject()));
	}
}
