Demo entry 6762345

智能体

   

Submitted by anonymous on Oct 12, 2018 at 14:15
Language: Java. Code size: 2.6 kB.

package com.translation;


import jadex.bdiv3.annotation.*;
import jadex.bdiv3.features.IBDIAgentFeature;
import jadex.bdiv3.runtime.impl.PlanFailureException;
import java.util.Map;

import javax.swing.SwingUtilities;

@Capability
public class TranslationCapability 
{

    @Belief
    public native void setDirectory(Map<String, String> directory);

    @Belief
    public native Map<String, String> getDirectory();
/*
    public TranslationCapability(final IBDIAgentFeature bdiAgentFeature)
    {
    	SwingUtilities.invokeLater(new Runnable()
    	{
    		public void run()
    		{
    			new TranslationFrame(bdiAgentFeature);
    		}
    	});
    }*/
    
    @Goal
    public static class TranslationGoal
    {
        private String fromWord;
        private String targetWord;
        
        public TranslationGoal(String fromWord) 
        { 
        	this.fromWord = fromWord; 
        	//System.out.println("new TranslationGoal");
        }
        public String getFromWord() 
        {
        	return fromWord; 
        }
        public void setFromWord(String fromWord) 
        { 
        	this.fromWord = fromWord; 
        }
        public String getTargetWord() 
        { 
        	return targetWord; 
        }
        public void setTargetWord(String targetWord)
        { 
        	this.targetWord = targetWord;
        }
    }

    @Plan(trigger = @Trigger(goals = TranslationGoal.class))
    public void executeE2C(TranslationGoal goal) 
    {
    	System.out.println("in executeE2C");
        //String fromWord = goal.fromWord;
    	String fromWord = goal.fromWord.toLowerCase();
        String targetWord = this.getDirectory().get(fromWord);
        if (targetWord == null) 
        { 
        	throw new PlanFailureException();
        } 
        else 
        { 
        	goal.setTargetWord(targetWord);
        }
    }

    @Plan(trigger = @Trigger(goals = TranslationGoal.class))
    public void executeC2E(TranslationGoal goal) 
    {
    	System.out.println("in executeC2E");
        String fromWord = goal.fromWord/*.toLowerCase()*/, 
        		targetWord = null;
        for (Map.Entry<String, String> entry : this.getDirectory().entrySet()) 
        {
            if (entry.getValue().toLowerCase().equals(fromWord)) 
            {
                targetWord = entry.getKey();
                break;
            }
        }
        if (targetWord == null)
        {
        	throw new PlanFailureException();
      	}
        else 
        {
        	goal.targetWord = targetWord;
        }
    }
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).