package org.studiorailgun.knowledge.query;

import java.util.List;
import java.util.stream.Collectors;

import org.studiorailgun.Globals;
import org.studiorailgun.knowledge.Node;
import org.studiorailgun.knowledge.types.RelationTypes;

/**
 * Queries based around possession
 */
public class PossessionQuery {
    
    /**
     * Gets all the child nodes that this node possesses
     * @param parent The parent node
     * @return The child nodes that are possessed by this node
     */
    public static List<Node> getPossessions(Node parent){
        return Globals.web.getRelationsOfParentNode(parent).stream().filter(relation -> relation.getName().equals(RelationTypes.POSSESSION_OF)).map(relation -> relation.getChild()).collect(Collectors.toList());
    }

    /**
     * Gets the node that possesses the child
     * @param child The child
     * @return The possessor's node
     */
    public static Node getPossessor(Node child){
        List<Node> nodes = Globals.web.getRelationsOfChildNode(child).stream().filter(relation -> relation.getName().equals(RelationTypes.POSSESSION_OF)).map(relation -> relation.getParent()).collect(Collectors.toList());
        if(nodes.size() != 1){
            throw new Error("Not just one possessor! " + nodes.size() + " " + child.getName());
        }
        return nodes.get(0);
    }

}
