package examples;


import agentcomponent.behaviour.PCObserver;
import agentcomponent.COElementAddedEvent;
import agentcomponent.metadata.OntoMetaData;
import java.util.Observable;
import java.util.Vector;
import examples.ontology.company.CompanyOwns;


/**
 * Example behaviour that sends over an constructed slot "CompanyDetails"
 * a message with a CompanyOwns CommunicationObject.
 */
public class SendCompanyBehaviour extends PCObserver{
  private OntoMetaData onto = new OntoMetaData("examples.ontology.company.CompanyOntology","company-ontology");

  public void startPC(){
  	//start the behaviour as an observer to the communication base 
  	//and add an ontology to work with
    ac.addRuntimeOnto(onto);
    super.startPC();
		System.out.println(this.getClass().getName() + " started in " + ac.getLocalName());
  }
  protected void action(){
  	try{
  		//when notified by the communication base
  		//get an CompanyOwns CO.
      Vector result = ac.retrieveCO(new CompanyOwns());
      if(result.size()>0){
      	//if available send it to the the slot CompanyDetails
      	//and stop the behaviour
      	CompanyOwns owns = (CompanyOwns) result.get(0);
        ac.sendMessage("CompanyDetails", owns);
		    stopPC();
      }
  	}catch(Exception ex){
  		ex.printStackTrace();
  	}
  }
  public void update(Observable observable, Object object){
  	//called whenever a to the CommunicationBase is changed
  	//here we are only interested in COAddedEvents
  	//if so start the action method 
    if(object instanceof COElementAddedEvent){
      this.action();
    }
  }
}

