/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myagent.attentioncodelets;

import edu.memphis.ccrg.lida.attentioncodelets.AttentionCodeletImpl;
import edu.memphis.ccrg.lida.framework.shared.Link;
import edu.memphis.ccrg.lida.framework.shared.Linkable;
import edu.memphis.ccrg.lida.framework.shared.Node;
import edu.memphis.ccrg.lida.framework.shared.NodeStructure;
import edu.memphis.ccrg.lida.framework.shared.NodeStructureImpl;
import edu.memphis.ccrg.lida.workspace.workspacebuffers.WorkspaceBuffer;
import java.util.HashSet;

/**
 *
 * @author Marcel
 */
public class GenericAttentionCodelet extends AttentionCodeletImpl {
    
    @Override
    public void init() {
        super.init();
        HashSet<Node> nodes = (HashSet<Node>) getParam("nodes", null);
        
        if (nodes != null) {
            for(Node node:nodes){
                soughtContent.addDefaultNode(node);
            }
        }
    }

    /**
     * Returns true if specified WorkspaceBuffer contains this codelet's sought
     * content.
     * 
     * @param buffer
     *            the WorkspaceBuffer to be checked for content
     * @return true, if successful
     */
    @Override
    public boolean bufferContainsSoughtContent(WorkspaceBuffer buffer) {
        NodeStructure model = (NodeStructure) buffer.getBufferContent(null);

        for (Linkable ln : soughtContent.getLinkables()) {
            if (!model.containsLinkable(ln)) {
                return false;
            }
        }
        return true;
    }

    /**
     * Returns sought content and related content from specified
     * WorkspaceBuffer.
     * 
     * @param buffer
     *            the buffer
     * @return the workspace content
     */
    @Override
    public NodeStructure retrieveWorkspaceContent(WorkspaceBuffer buffer) {
        NodeStructure ns = ((NodeStructure) buffer.getBufferContent(null));
        NodeStructure result = new NodeStructureImpl();
        for (Node n : soughtContent.getNodes()) {
            if (ns.containsNode(n)) {
                result.addDefaultNode(ns.getNode(n.getId()));
            }
        }
        for (Link l : soughtContent.getLinks()) {
            if (ns.containsLink(l)) {
                result.addDefaultLink(ns.getLink(l.getExtendedId()));
            }
        }
        return result;
    }
    
}
