Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //----------------------------------------------------------------------------
  // Copyright (C) 2003  Rafael H. Bordini, Jomi F. Hubner, et al.
  // 
  // This library is free software; you can redistribute it and/or
  // modify it under the terms of the GNU Lesser General Public
  // License as published by the Free Software Foundation; either
  // version 2.1 of the License, or (at your option) any later version.
  // 
  // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 // 
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 // 
 // To contact the authors:
 // http://www.inf.ufrgs.br/~bordini
 // http://www.das.ufsc.br/~jomi
 //
 //----------------------------------------------------------------------------
 
 
 package jason.asSyntax;
 
 
 
 import java.util.List;
 import java.util.Map;
 
Represents a set of plans used by an agent

Has:
- plans 0..* Plan
   
 public class PlanLibrary implements Iterable<Plan> {

    
a MAP from TE to a list of relevant plans
 
     private Map<PredicateIndicator,List<Plan>> relPlans = new HashMap<PredicateIndicator,List<Plan>>();

    
All plans as defined in the AS code (maintains the order of the plans)
 
     private List<Planplans = new ArrayList<Plan>();
    
    
list of plans that have var as TE
 
     private List<PlanvarPlans = new ArrayList<Plan>();
    
    
A map from labels to plans
 
     private Map<String,PlanplanLabels = new HashMap<String,Plan>();
     
     private boolean hasMetaEventPlans = false
     
     private static AtomicInteger lastPlanLabel = new AtomicInteger(0);
 
     private boolean hasUserKqmlReceived = false;
     
     //private Logger logger = Logger.getLogger(PlanLibrary.class.getName());  
     
    
Add a new plan written as a String. The source normally is "self" or the agent that sent this plan. If the PL already has a plan equals to "stPlan", only a new source is added. The plan is added in the end of the PlanLibrary.

Deprecated:
parse the plan before (ASSyntax methods) and call add(Plan, ...) methods
Returns:
the plan just added
 
     public Plan add(StringTerm stPlanTerm tSourcethrows ParseExceptionJasonException {
        return add(stPlantSourcefalse); 
     }
    
    
Add a new plan written as a String. The source normally is "self" or the agent that sent this plan. If the PL already has a plan equals to "stPlan", only a new source is added. If before is true, the plan will be added in the begin of the PlanLibrary; otherwise, it is added in the end.

Deprecated:
parse the plan before (ASSyntax methods) and call add(Plan, ...) methods
Returns:
the plan just added
    public Plan add(StringTerm stPlanTerm tSourceboolean beforethrows ParseExceptionJasonException {
        String sPlan = stPlan.getString();
        // remove quotes \" -> "
        StringBuilder sTemp = new StringBuilder();
        for (int c=0; c <sPlan.length(); c++) {
            if (sPlan.charAt(c) != '\\') {
                sTemp.append(sPlan.charAt(c));
            }
        }
        sPlan  = sTemp.toString();
        Plan p = ASSyntax.parsePlan(sPlan);
        return add(p,tSource,before);
    }


    
Add a new plan in PL. The source normally is "self" or the agent that sent this plan. If the PL already has a plan equals to the parameter p, only a new source is added. If before is true, the plan will be added in the begin of the PlanLibrary; otherwise, it is added in the end.

Returns:
the plan just added
    public Plan add(Plan pTerm sourceboolean beforethrows JasonException {
        int i = .indexOf(p);
        if (i < 0) {
            // add label, if necessary
            if (p.getLabel() == null)
                p.setLabel(getUniqueLabel());
            p.getLabel().addSource(source);
            add(pbefore);
        } else {
            p = .get(i);
            p.getLabel().addSource(source);
        }
        return p;
    }
    public void add(Plan pthrows JasonException {
        add(p,false);
    }
    
    
Adds a plan into the plan library, either before or after all other plans depending on the boolean parameter.

Parameters:
p The plan to be added to the plan library
before Whether or not to place the new plan before others
Throws:
jason.JasonException
    public void add(Plan pboolean beforethrows JasonException {
        // test p.label
        if (p.getLabel() != null && .keySet().contains(p.getLabel().getFunctor())) {
            // test if the new plan is equal, in this case, just add a source
            Plan planInPL = get(p.getLabel().getFunctor());
            if (p.equals(planInPL)) {
                planInPL.getLabel().addSource(p.getLabel().getSources().get(0));
                return;
            } else {
                throw new JasonException("There already is a plan with label " + p.getLabel());
            }
        }
        
        // add label, if necessary
        if (p.getLabel() == null
            p.setLabel(getUniqueLabel());
        // add self source
        if (!p.getLabel().hasSource()) 
            p.getLabel().addAnnot(.);
        
            if (! (p.getSrcInfo() != null && "kqmlPlans.asl".equals(p.getSrcInfo().getSrcFile()))) {
                 = true;
            }
        }
        p.setAsPlanTerm(false); // it is not a term anymore
        .put(p.getLabel().getFunctor(), p);
        Trigger pte = p.getTrigger();
        if (pte.getLiteral().isVar()) {
            if (before
                .add(0,p); 
            else 
                .add(p);
            // add plan p in all entries
            for (List<Planlp.values())
                if (!lp.isEmpty() && lp.get(0).getTrigger().sameType(pte)) // only add if same type
                    if (before)
                        lp.add(0,p);
                    else
                        lp.add(p);
        } else {
            List<PlancodesList = .get(pte.getPredicateIndicator());
            if (codesList == null) {
                codesList = new ArrayList<Plan>();
                // copy plans from var plans
                for (Plan vp)
                    if (vp.getTrigger().sameType(pte))
                        codesList.add(vp);
                .put(pte.getPredicateIndicator(), codesList);
            }
            if (before)
                codesList.add(0,p);
            else
                codesList.add(p);
        }
        if (pte.getOperator() == .)
             = true;
        
        if (before)
            .add(0,p);
        else
            .add(p);
    }
    
    public void addAll(PlanLibrary plthrows JasonException {
        for (Plan ppl) { 
            add(pfalse);
        }
    }
    public void addAll(List<Planplansthrows JasonException {
        for (Plan pplans) { 
            add(pfalse);
        }
    }
    
    public boolean hasMetaEventPlans() {
        return 
    }
    
    public boolean hasUserKqmlReceivedPlans() {
        return ;
    }

    
add a label to the plan
    private Pred getUniqueLabel() {
        String l;
        do {
            l = "l__" + (.incrementAndGet());
        } while (.keySet().contains(l));
        return new Pred(l);
    }
    
    
return a plan for a label
    public Plan get(String label) {
        return .get(label);
    }
    
    public int size() {
        return .size();
    }
    
    public List<PlangetPlans() {
        return ;
    }
    
    public Iterator<Planiterator() {
        return .iterator();
    }

    
remove all plans
    public void clear() {
        .clear();
        .clear();
        .clear();
        .clear();
    }
    
    
Remove a plan represented by the label pLabel. In case the plan has many sources, only the plan's source is removed.
	public boolean remove(Atom pLabelTerm source) {
		// find the plan
		Plan p = get(pLabel.getFunctor());
		if (p != null) {
			boolean hasSource = p.getLabel().delSource(source);
			// if no source anymore, remove the plan
			if (hasSource && !p.getLabel().hasSource()) {
			    remove(pLabel.getFunctor());
			}
			return true;
		}
		return false;
	}

remove the plan with label pLabel
    public Plan remove(String pLabel) {
        Plan p = .remove(pLabel);
        // remove it from plans' list
        .remove(p);
        if (p.getTrigger().getLiteral().isVar()) {
            .remove(p);
            // remove p from all entries and
            // clean empty entries
            Iterator<PredicateIndicatoripi = .keySet().iterator();
            while (ipi.hasNext()) {
                PredicateIndicator pi = ipi.next();
                List<Planlp = .get(pi); 
                lp.remove(p);
                if (lp.isEmpty()) {
                    ipi.remove();
                }
            }
        } else {
            List<PlancodesList = .get(p.getTrigger().getPredicateIndicator());
            codesList.remove(p);
            if (codesList.isEmpty()) {
                // no more plans for this TE
                .remove(p.getTrigger().getPredicateIndicator());
            }
        }
        return p;
    }

    

Deprecated:
use hasCandidatePlan(te) instead
    public boolean isRelevant(Trigger te) {
        return hasCandidatePlan(te);
    }
    public boolean hasCandidatePlan(Trigger te) {
        return getCandidatePlans(te) != null;
    }

    
    

Deprecated:
use getCandidatePlans(te) instead
    public List<PlangetAllRelevant(Trigger te) {
        return getCandidatePlans(te);
    }
    
    public List<PlangetCandidatePlans(Trigger te) {
        List<Planl = .get(te.getPredicateIndicator());
        if ((l == null || l.isEmpty()) && !.isEmpty()) {  // no rel plan, try varPlan
            for (Plan p)
                if (p.getTrigger().sameType(te)) {
                    if (l == null)
                        l = new ArrayList<Plan>();
                    l.add(p);
                }
        }
        return l// if no rel plan, have to return null instead of empty list
    }
    public static final Trigger TE_JAG_SLEEPING  = new Trigger(..new Atom("jag_sleeping"));
    public static final Trigger TE_JAG_AWAKING   = new Trigger(..new Atom("jag_awaking"));
    public PlanLibrary clone() {
        PlanLibrary pl = new PlanLibrary();
        try {
            for (Plan pthis) { 
                pl.add((Plan)p.clone(), false);
            }
        } catch (JasonException e) {
            e.printStackTrace();
        }
        return pl;
    }
    public String toString() {
        return .toString();
    }
    
    
get as XML
    public Element getAsDOM(Document document) {
        Element eplans = (Elementdocument.createElement("plans");
        String lastFunctor = null;
        for (Plan p) {
            String currentFunctor = p.getTrigger().getLiteral().getFunctor();
            if (lastFunctor != null && !currentFunctor.equals(lastFunctor)) {
                eplans.appendChild((Elementdocument.createElement("new-set-of-plans"));
            }
            lastFunctor = currentFunctor;
            eplans.appendChild(p.getAsDOM(document));
        }
        return eplans;
    }
New to GrepCode? Check out our FAQ X