package org.studiorailgun.ai.linguistics;

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;
import org.studiorailgun.ai.philosophy.ConceptQuery;

/**
 * Queries related to the linguistic concept of a name
 */
public class NameQuery {
    
    /**
     * Gets the names of a given node
     */
    public static List<Node> getNames(Node node){
        List<Relation> relations = Globals.web.getRelationsOfChildNode(node);
        return relations.stream().filter(relation -> relation.getName().equals(RelationTypes.NAME_OF)).map(relation -> relation.getParent()).collect(Collectors.toList());
    }

    /**
     * Attaches a name to a child
     * @param properNoun The proper noun
     * @param child The child
     */
    public static void attachName(Node properNoun, Node child){
        Globals.web.createRelation(RelationTypes.NAME_OF, properNoun, child);
    }

    /**
     * Checks if this node is a name or not
     * @param node The node
     * @return true if it is a name, false otherwise
     */
    public static boolean isName(Node node){
        Node nameConcept = ConceptQuery.getConcept("name");
        List<Relation> childRelations = Globals.web.getRelationsOfChildNode(node);
        List<Node> nameSearch = childRelations.stream()
            .filter(relation -> relation.getName().equals(RelationTypes.INSTANCE_OF))
            .map(relation -> relation.getParent())
            .filter(instParent -> instParent.equals(nameConcept))
            .collect(Collectors.toList());
        return nameSearch.size() > 0;
    }

}
