package org.studiorailgun.ai.knowledge.query;

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

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

/**
 * Queries for dealing with instances of nodes
 */
public class InstanceQuery {
    
    /**
     * Gets the instances of a node
     * @param web The web
     * @param parent The parent node
     * @return The list of instances of that node
     */
    public static List<Node> getInstances(Node parent){
        List<Relation> relations = Globals.web.getRelationsOfParentNode(parent);
        return relations.stream().filter(relation -> relation.getName().equals(RelationTypes.INSTANCE_OF)).map(relation -> relation.getChild()).collect(Collectors.toList());
    }

    /**
     * Creates an instance relationship
     * @param concept The concept
     * @param instance The instance
     */
    public static void setInstance(Node concept, Node instance){
        Globals.web.createRelation(RelationTypes.INSTANCE_OF, concept, instance);
    }

}
