/* Copyright (C) 2009  CSE,IIT Bombay  http://www.cse.iitb.ac.in

This file is part of the ConStore open source storage facility for concept-nets.

ConStore is free software and distributed under the 
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License;
you can copy, distribute and transmit the work
with the work attribution in the manner specified by the author or licensor.
You may not use this work for commercial purposes and may not alter, 
transform, or build upon this work.

Please refer the legal code of the license, available at
http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode

ConStore is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  */

package iitb.con.core;

import iitb.con.util.Result;


/**
 * Relations are associated with the entities and instances.<br>
 * RelationIntance interface provides methods to set and remove the
 * relations among the entity instances.<br><br>
 * The relation is created between two items, which is considered
 * as left-item and right-item.<br>
 * 
 * @author Prathab K
 * @see Instance
 * @see Relation
 */
public class RelationInstance extends Instance{
    
    /** Relation's left instance id */
    public int leftInstanceId;
    
    /** Relation's right instance id */
    public int rightInstanceId;
    
    /**
     * Sets the instance for the left item.
     * @param instance {@link EntityInstance}
     * @return {@link Result}
     */
    public Result setLeftInstance(EntityInstance instance) {
        if(instance == null) {
            this.leftInstanceId = -1;
            return new Result(false,"Entity Instance is NULL");
        } else {
            if(instance.id < 0)
                return new Result(false,"Instance Id is NULL");
            else {
                this.leftInstanceId = instance.id;
                return new Result(true);
            }
        }
    }
    
    /**
     * Sets the instance for the right item.
     * @param instance {@link EntityInstance}
     * @return {@link Result}
     */
    public Result setRightInstance(EntityInstance instance) {
        if(instance == null) {
            this.rightInstanceId = -1;
            return new Result(false,"Entity Instance is NULL");
        } else {
            if(instance.id < 0)
                return new Result(false,"Instance Id is NULL");
            else {
                this.rightInstanceId = instance.id;
                return new Result(true);
            }
        }
    }
    
    /*
     * Returns the left entity instance in the relation.
     * @return {@link EntityInstance}
     */
    /*public EntityInstance getLeftInstance() {
        //TODO: Using entity instance id , fetch the instance
        return null;
    }*/
    
    /*
     * Returns the right entity instance in the relation.
     * @return {@link EntityInstance}
     */
    /*public EntityInstance getRightInstance() {
        //TODO: Using entity instance id , fetch the instance
        return null;
    }*/
    
    /**
     * Returns the left role name of the relation.
     * @return left role name
     */
    public String getLeftRole() {
        if(type != null)
            return ((Relation)type).leftRole;
        return null;
    }
    
    /**
     * Returns the right role name of the relation.
     * @return right role name
     */
    public String getRightRole() {
        if(type != null)
            return ((Relation)type).rightRole;
        return null;
    }
    
    /**
     * Returns the direction kind in the relation.<br>
     * 0 - LEFT_TO_RIGHT<br>
     * 1 - BIDIRECTIONAL<br>
     * @return byte - direction kind
     */
    public byte getDirection() {
        if(type != null)
            return ((Relation)type).getDirection();
        return Relation.BIDIRECTIONAL;
    }
    
    public void setAttributeValue(String name, Object value) {
        if(name.equals("leftInstanceId")) {
            this.leftInstanceId = (Integer) value;
        }else if(name.equals("rightInstanceId")) {
            this.rightInstanceId = (Integer) value;
        }else { 
        }
    }
    
    public Object getAttributeValue(String name) {
        if(name.equals("leftInstanceId")) {
            return this.leftInstanceId;
        }else if(name.equals("rightInstanceId")) {
            return this.rightInstanceId;
        }else { 
            return null;
        }
    }
    
}