Package it.unito.di.artifact

Source Code of it.unito.di.artifact.Role

package it.unito.di.artifact;

import java.lang.annotation.Annotation;
import java.util.Observer;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import cartago.Artifact;
import cartago.ArtifactId;
import cartago.CartagoException;
import cartago.IEventFilter;
import cartago.Op;
import cartago.OpFeedbackParam;
import cartago.WorkspaceId;
import cartago.security.AgentCredential;
import cartago.util.agent.ActionFailedException;
import cartago.util.agent.ActionFeedback;
import cartago.util.agent.ArtifactObsProperty;
import cartago.util.agent.CartagoBasicContext;
import cartago.util.agent.Percept;
import it.unito.di.agents.exception.MissingRequirementForRoleException;
import jade.core.AID;
import jade.core.behaviours.Behaviour;

public abstract class Role {

  protected Logger logger = LogManager.getLogger(this.getClass());
  protected static Logger staticLogger = LogManager.getLogger(Role.class);
 
  protected RoleId roleId;
 
  protected Behaviour player;
  protected AID agent;
  protected ArtifactId artId;
 
  private CartagoBasicContext ctx; 
  private static CartagoBasicContext staticCtx = new CartagoBasicContext("agent-Abstract-Role","default");
 
 
//    Annotation annotation = this.getClass().getAnnotation(RoleDefinition.class);
//    if (!(annotation != null && annotation instanceof RoleDefinition)) {
//      throw new MissingRequirementForRoleException(this.getClass());
//    }   
 
  public Role (String roleName, Behaviour player, AID agent, ArtifactId artId) {
    super();
    this.roleId = new RoleId(roleName, this);
    this.player = player;
    this.agent = agent;
    this.artId = artId;
//    ctx = staticCtx; // need to align contexts to correctly use artifacts
    ctx = new CartagoBasicContext(roleName, "default");
  }
 
  public AID getAgent() {
    return this.agent;
  }
 
  public ArtifactId getArtifactId() {
    return this.artId;
  }
 
  public String toString() {
    return this.roleId.toString();
  }

  public RoleId getRoleId() {
    return roleId;
  }
 
  /**
   * Static method for creating artifacts.
   *
   * @param artifactName The name of the artifact to be created.
   * @param artifactClass The class of the artifact.
   * @return
   */
  public static ArtifactId createArtifact(String artifactName, Class<? extends Artifact> artifactClass) {
   
    try {
      staticLogger.debug("Creating artifact "+artifactName+" of class "+artifactClass);
      return staticCtx.makeArtifact(artifactName, artifactClass.getName());
     
    } catch (CartagoException e) {
      // if artifact already exists, try to retrieve it
      staticLogger.debug("Trying to retrieve artifact "+artifactName+" of class "+artifactClass);
      try {
        return staticCtx.lookupArtifact(artifactName);
      } catch (CartagoException e1) {
        staticLogger.debug("Error in retrieving artifact "+artifactName+" of class "+artifactClass);
        e1.printStackTrace();
        return null;
      }     
    }
  }
 
  /**
   * Enacting must be a static method, a Behaviour cannot interact directly with artifacts indeed.
   * To enacting as "User", offeredPlayerBehaviour can be null.
   * @param roleName
   * @param artifact
   * @param offeredPlayerBehaviour
   * @return
   */
  @SuppressWarnings("unchecked")
  public static <T extends Role> T enact(String roleName, ArtifactId artifact, Behaviour offeredPlayerBehaviour, AID agent) {
    OpFeedbackParam<Role> r = new OpFeedbackParam<Role>();
    try {
      staticCtx.doAction(artifact, new Op(CommunicationArtifact.ENACT, roleName, offeredPlayerBehaviour, agent, r));
      staticLogger.debug("Enactment completed succesfully.");
      return (T)r.get();
    } catch (CartagoException e) {
      staticLogger.error("Error in enacting artifact "+artifact);
      e.printStackTrace();
      return null;
    }
   
   
  }
 
 

  // Code from cartago.util.agent.Agent
  protected String getAgentName(){
    return ctx.getName();
  }

  protected ActionFeedback doActionAsync(Op op) throws CartagoException {
    return ctx.doActionAsync(op);
  }


  protected ActionFeedback doActionAsync(ArtifactId aid, Op op, long timeout) throws CartagoException {
    return ctx.doActionAsync(aid, op, timeout);
  }

  protected ActionFeedback doActionAsync(Op op, long timeout) throws CartagoException {
    return ctx.doActionAsync(op, timeout);
  }

  protected void doAction(Op op, long timeout) throws ActionFailedException, CartagoException {
    ctx.doAction(op, timeout);
  }

  protected void doAction(Op op) throws ActionFailedException, CartagoException {
    this.doAction(op, -1);
  }

  protected void doAction(ArtifactId aid, Op op, long timeout) throws ActionFailedException, CartagoException {
    staticCtx.doAction(aid, op, timeout);
  }

  protected void doAction(ArtifactId aid, Op op) throws ActionFailedException, CartagoException {
    this.doAction(aid,op,-1);
  }

  protected Percept fetchPercept() throws InterruptedException {
    return ctx.fetchPercept();
  }

  protected Percept waitPercept() throws InterruptedException {
    return ctx.waitForPercept();
  }

  protected Percept waitForPercept(IEventFilter filter) throws InterruptedException {
    return ctx.waitForPercept(filter);
  }

  protected void log(String msg){
    System.out.println("["+ctx.getName()+"] "+msg);
  }


  //Utility methods

  protected WorkspaceId joinWorkspace(String wspName, AgentCredential cred) throws CartagoException {
    return ctx.joinWorkspace(wspName, cred);
  }

  protected WorkspaceId joinRemoteWorkspace(String wspName, String address, String roleName, AgentCredential credthrows CartagoException {
    return ctx.joinRemoteWorkspace(wspName, address, roleName, cred);
  }

  protected ArtifactId lookupArtifact(String artifactName) throws CartagoException {
    return ctx.lookupArtifact(artifactName);
  }

  protected ArtifactId makeArtifact(String artifactName, String templateName) throws CartagoException {
    return ctx.makeArtifact(artifactName, templateName);
  }

  protected ArtifactId makeArtifact(String artifactName, String templateName, Object[] params) throws CartagoException {
    return ctx.makeArtifact(artifactName, templateName, params);
  }

  protected void disposeArtifact(ArtifactId artifactId) throws CartagoException {
    ctx.disposeArtifact(artifactId);
  }

  protected void focus(ArtifactId artifactId) throws CartagoException {
    ctx.focus(artifactId);
  }

  protected void focus(ArtifactId artifactId, IEventFilter filter) throws CartagoException {
    ctx.focus(artifactId, filter);
  }

  protected void stopFocus(ArtifactId aid) throws CartagoException {
    ctx.stopFocus(aid);
  }
 
  protected ArtifactObsProperty getObsProperty(String name){
    return ctx.getObsProperty(name);
  }

}
TOP

Related Classes of it.unito.di.artifact.Role

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.