|
From: <jom...@us...> - 2011-06-09 20:19:29
|
Revision: 1648
http://jason.svn.sourceforge.net/jason/?rev=1648&view=rev
Author: jomifred
Date: 2011-06-09 20:19:22 +0000 (Thu, 09 Jun 2011)
Log Message:
-----------
include PE (pending events) in C
also consider this new structure in the concerned internal actions (suspend, resumo, drop*, ...)
Modified Paths:
--------------
trunk/src/jason/asSemantics/Circumstance.java
trunk/src/jason/stdlib/drop_all_desires.java
trunk/src/jason/stdlib/drop_all_events.java
trunk/src/jason/stdlib/drop_all_intentions.java
trunk/src/jason/stdlib/drop_desire.java
trunk/src/jason/stdlib/drop_intention.java
trunk/src/jason/stdlib/intend.java
trunk/src/jason/stdlib/resume.java
trunk/src/jason/stdlib/succeed_goal.java
trunk/src/jason/stdlib/suspend.java
trunk/src/xml/agInspection.xsl
Modified: trunk/src/jason/asSemantics/Circumstance.java
===================================================================
--- trunk/src/jason/asSemantics/Circumstance.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/asSemantics/Circumstance.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -64,6 +64,7 @@
private List<ActionExec> FA; // Feedback actions, those that are already executed
private Map<String, Intention> PI; // pending intentions, intentions suspended by any other reason
+ private Map<String, Event> PE; // pending events, events suspended by .suspend
private List<CircumstanceListener> listeners = new CopyOnWriteArrayList<CircumstanceListener>();
@@ -80,6 +81,7 @@
MB = new LinkedList<Message>();
PA = new ConcurrentHashMap<Integer, ActionExec>();
PI = new ConcurrentHashMap<String, Intention>();
+ PE = new ConcurrentHashMap<String, Event>();
FA = new ArrayList<ActionExec>();
}
@@ -352,6 +354,41 @@
return false;
}
+ /** pending events */
+
+ public Map<String, Event> getPendingEvents() {
+ return PE;
+ }
+
+ public boolean hasPendingEvent() {
+ return PE != null && PE.size() > 0;
+ }
+
+ public void clearPendingEvents() {
+ // notify listeners
+ if (listeners != null)
+ for (CircumstanceListener el : listeners)
+ for (Event e: PE.values())
+ if (e.getIntention() != null)
+ el.intentionDropped(e.getIntention());
+
+ PE.clear();
+ }
+
+ public void addPendingEvent(String id, Event e) {
+ PE.put(id, e);
+
+ if (listeners != null && e.getIntention() != null)
+ for (CircumstanceListener el : listeners)
+ el.intentionSuspended(e.getIntention(), id);
+ }
+
+ public Event removePendingEvent(String pendingId) {
+ return PE.remove(pendingId);
+ }
+
+ /** actions */
+
public ActionExec getAction() {
return A;
}
@@ -500,6 +537,9 @@
for (String k: this.PI.keySet()) {
c.PI.put(k, (Intention)PI.get(k).clone());
}
+ for (String k: this.PE.keySet()) {
+ c.PE.put(k, (Event)PE.get(k).clone());
+ }
for (ActionExec ae: FA) {
c.FA.add((ActionExec)ae.clone());
}
@@ -508,19 +548,16 @@
/** get the agent circumstance as XML */
- @SuppressWarnings("unchecked")
public Element getAsDOM(Document document) {
Element c = (Element) document.createElement("circumstance");
Element e;
- Iterator i;
// MB
if (getMailBox() != null && !getMailBox().isEmpty()) {
Element ms = (Element) document.createElement("mailbox");
- i = getMailBox().iterator();
- while (i.hasNext()) {
+ for (Message m: getMailBox()) {
e = (Element) document.createElement("message");
- e.appendChild(document.createTextNode(i.next().toString()));
+ e.appendChild(document.createTextNode(m.toString()));
ms.appendChild(e);
}
c.appendChild(ms);
@@ -530,10 +567,8 @@
Element events = (Element) document.createElement("events");
boolean add = false;
if (E != null && !E.isEmpty()) {
- i = E.iterator();
- while (i.hasNext()) {
+ for (Event evt: E) {
add = true;
- Event evt = (Event) i.next();
e = evt.getAsDOM(document);
events.appendChild(e);
}
@@ -544,6 +579,14 @@
e.setAttribute("selected", "true");
events.appendChild(e);
}
+ if (hasPendingEvent()) {
+ for (String k: PE.keySet()) {
+ add = true;
+ e = PE.get(k).getAsDOM(document);
+ e.setAttribute("pending", k);
+ events.appendChild(e);
+ }
+ }
if (add) {
c.appendChild(events);
}
@@ -576,9 +619,7 @@
}
if (getRelevantPlans() != null && !getRelevantPlans().isEmpty()) {
- i = getRelevantPlans().iterator();
- while (i.hasNext()) {
- Option o = (Option) i.next();
+ for (Option o: getRelevantPlans()) {
if (!alreadyIn.contains(o)) {
alreadyIn.add(o);
e = o.getAsDOM(document);
@@ -631,7 +672,7 @@
}
Element acts = (Element) document.createElement("actions");
- alreadyIn = new ArrayList();
+ alreadyIn = new ArrayList<Object>();
// action
if (getAction() != null) {
@@ -664,9 +705,7 @@
// FA
if (hasFeedbackAction()) {
- i = getFeedbackActions().iterator();
- while (i.hasNext()) {
- ActionExec o = (ActionExec) i.next();
+ for (ActionExec o: getFeedbackActions()) {
if (!alreadyIn.contains(o)) {
alreadyIn.add(o);
e = o.getAsDOM(document);
Modified: trunk/src/jason/stdlib/drop_all_desires.java
===================================================================
--- trunk/src/jason/stdlib/drop_all_desires.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/drop_all_desires.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -65,6 +65,7 @@
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
super.execute(ts, un, args);
ts.getC().clearEvents();
+ ts.getC().clearPendingEvents();
return true;
}
}
Modified: trunk/src/jason/stdlib/drop_all_events.java
===================================================================
--- trunk/src/jason/stdlib/drop_all_events.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/drop_all_events.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -72,6 +72,7 @@
public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception {
checkArguments(args);
ts.getC().clearEvents();
+ ts.getC().clearPendingEvents();
return true;
}
}
Modified: trunk/src/jason/stdlib/drop_all_intentions.java
===================================================================
--- trunk/src/jason/stdlib/drop_all_intentions.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/drop_all_intentions.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -27,7 +27,6 @@
import jason.asSemantics.Circumstance;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.Event;
-import jason.asSemantics.Intention;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.Term;
@@ -81,12 +80,19 @@
// drop intentions in E
for (Event e: C.getEvents()) {
- // it is not an external event
- if (e.getIntention() != Intention.EmptyInt) {
+ if (e.isInternal()) {
C.removeEvent(e);
}
}
+ // drop intentions in PE
+ for (String ek: C.getPendingEvents().keySet()) {
+ Event e = C.getPendingEvents().get(ek);
+ if (e.isInternal()) {
+ C.removePendingEvent(ek);
+ }
+ }
+
// cancel future events generated by .at
at atia = (at)ts.getAg().getIA(at.atAtom);
atia.cancelAts();
Modified: trunk/src/jason/stdlib/drop_desire.java
===================================================================
--- trunk/src/jason/stdlib/drop_desire.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/drop_desire.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -83,17 +83,25 @@
public void dropEvt(Circumstance C, Literal l, Unifier un) {
Trigger te = new Trigger(TEOperator.add, TEType.achieve, l);
- Iterator<Event> ie = C.getEvents().iterator();
+
+ // search in E
+ dropEvt(te, un, C.getEvents().iterator());
+
+ // search in PE (only the event need to be checked, the related intention is handeled by dropInt)
+ dropEvt(te, un, C.getPendingEvents().values().iterator());
+ }
+
+ private static void dropEvt(Trigger te, Unifier un, Iterator<Event> ie) {
while (ie.hasNext()) {
Event ei = ie.next();
Trigger t = ei.getTrigger();
if (ei.getIntention() != Intention.EmptyInt) { // since the unifier of the intention will not be used, apply it to the event before comparing to the event to be dropped
- t = (Trigger) t.clone();
+ t = t.clone();
t.apply(ei.getIntention().peek().getUnif());
}
if (un.clone().unifiesNoUndo(te, t)) {
ie.remove();
}
}
- }
+ }
}
Modified: trunk/src/jason/stdlib/drop_intention.java
===================================================================
--- trunk/src/jason/stdlib/drop_intention.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/drop_intention.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -90,7 +90,7 @@
Trigger g = new Trigger(TEOperator.add, TEType.achieve, l);
- // intention may be suspended in E
+ // intention may be suspended in E or PE
for (Event e: C.getEvents()) {
Intention i = e.getIntention();
if (i != null && i.hasTrigger(g, un)) {
@@ -98,6 +98,13 @@
un = bak.clone();
}
}
+ for (String k: C.getPendingEvents().keySet()) {
+ Intention i = C.getPendingEvents().get(k).getIntention();
+ if (i != null && i.hasTrigger(g, un)) {
+ C.removePendingEvent(k);
+ un = bak.clone();
+ }
+ }
// intention may be suspended in PA! (in the new semantics)
for (ActionExec a: C.getPendingActions().values()) {
Modified: trunk/src/jason/stdlib/intend.java
===================================================================
--- trunk/src/jason/stdlib/intend.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/intend.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -104,11 +104,17 @@
return true;
}
- // intention may be suspended in E
+ // intention may be in E
for (Event evt : C.getEvents()) {
if (evt.getIntention() != null && evt.getIntention().hasTrigger(g, un))
return true;
}
+
+ // intention may be suspended in PE
+ for (Event evt : C.getPendingEvents().values()) {
+ if (evt.getIntention() != null && evt.getIntention().hasTrigger(g, un))
+ return true;
+ }
// intention may be suspended in PA! (in the new semantics)
if (C.hasPendingAction()) {
Modified: trunk/src/jason/stdlib/resume.java
===================================================================
--- trunk/src/jason/stdlib/resume.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/resume.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -26,6 +26,7 @@
import jason.JasonException;
import jason.asSemantics.Circumstance;
import jason.asSemantics.DefaultInternalAction;
+import jason.asSemantics.Event;
import jason.asSemantics.Intention;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
@@ -81,6 +82,7 @@
Trigger g = new Trigger(TEOperator.add, TEType.achieve, (Literal)args[0]);
Circumstance C = ts.getC();
+ // Search the goal in PI
Iterator<String> ik = C.getPendingIntentions().keySet().iterator();
while (ik.hasNext()) {
String k = ik.next();
@@ -100,6 +102,22 @@
}
}
}
+
+ // Search the goal in PE
+ ik = C.getPendingEvents().keySet().iterator();
+ while (ik.hasNext()) {
+ String k = ik.next();
+ if (k.startsWith(suspend.SUSPENDED_INT)) {
+ Event e = C.getPendingEvents().get(k);
+ Intention i = e.getIntention();
+ if (un.unifies(g, e.getTrigger()) || (i != null && i.hasTrigger(g, un))) {
+ ik.remove();
+ C.addEvent(e);
+ if (i != null)
+ i.setSuspended(false);
+ }
+ }
+ }
return true;
}
Modified: trunk/src/jason/stdlib/succeed_goal.java
===================================================================
--- trunk/src/jason/stdlib/succeed_goal.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/succeed_goal.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -110,8 +110,12 @@
for (Event e: C.getEvents()) {
// test in the intention
Intention i = e.getIntention();
- if (dropIntention(i, g, ts, un) > 1) {
+ int r = dropIntention(i, g, ts, un);
+ if (r > 0) {
C.removeEvent(e);
+ if (r == 1) {
+ C.resumeIntention(i);
+ }
un = bak.clone();
} else {
// test in the event
@@ -120,13 +124,39 @@
t = t.clone();
t.apply(i.peek().getUnif());
}
- if (un.unifies(t, g)) {
+ if (un.unifies(g, t)) {
dropInEvent(ts,e,i);
un = bak.clone();
}
}
}
+ // dropping G in Pending Events
+ for (String ek: C.getPendingEvents().keySet()) {
+ // test in the intention
+ Event e = C.getPendingEvents().get(ek);
+ Intention i = e.getIntention();
+ int r = dropIntention(i, g, ts, un);
+ if (r > 0) {
+ C.removePendingEvent(ek);
+ if (r == 1) {
+ C.resumeIntention(i);
+ }
+ un = bak.clone();
+ } else {
+ // test in the event
+ Trigger t = e.getTrigger();
+ if (i != Intention.EmptyInt && i.size() > 0) {
+ t = t.clone();
+ t.apply(i.peek().getUnif());
+ }
+ if (un.unifies(g, t)) {
+ dropInEvent(ts,e,i);
+ un = bak.clone();
+ }
+ }
+ }
+
// dropping from Pending Actions
for (ActionExec a: C.getPendingActions().values()) {
Intention i = a.getIntention();
Modified: trunk/src/jason/stdlib/suspend.java
===================================================================
--- trunk/src/jason/stdlib/suspend.java 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/jason/stdlib/suspend.java 2011-06-09 20:19:22 UTC (rev 1648)
@@ -28,15 +28,10 @@
import jason.asSemantics.Circumstance;
import jason.asSemantics.DefaultInternalAction;
import jason.asSemantics.Event;
-import jason.asSemantics.IntendedMeans;
import jason.asSemantics.Intention;
-import jason.asSemantics.Option;
import jason.asSemantics.TransitionSystem;
import jason.asSemantics.Unifier;
import jason.asSyntax.Literal;
-import jason.asSyntax.Plan;
-import jason.asSyntax.PlanBody.BodyType;
-import jason.asSyntax.PlanBodyImpl;
import jason.asSyntax.Term;
import jason.asSyntax.Trigger;
import jason.asSyntax.Trigger.TEOperator;
@@ -147,6 +142,14 @@
// suspending G in Events
for (Event e: C.getEvents()) {
i = e.getIntention();
+ if (un.unifies(g, e.getTrigger()) || (i != null && i.hasTrigger(g, un))) {
+ C.removeEvent(e);
+ C.addPendingEvent(k, e);
+ if (i != null)
+ i.setSuspended(true);
+ }
+
+ /*
if ( i != null &&
(i.hasTrigger(g, un) || // the goal is in the i's stack of IM
un.unifies(g, e.getTrigger()) // the goal is the trigger of the event
@@ -169,7 +172,10 @@
C.removeEvent(e);
C.addPendingIntention(k, i);
}
+ */
}
+
+ // TODO: consider the case of suspending something that is already suspended.
return true;
}
Modified: trunk/src/xml/agInspection.xsl
===================================================================
--- trunk/src/xml/agInspection.xsl 2011-06-09 18:24:37 UTC (rev 1647)
+++ trunk/src/xml/agInspection.xsl 2011-06-09 20:19:22 UTC (rev 1648)
@@ -196,6 +196,7 @@
<xsl:if test="@selected='true'">
<b>X</b>
</xsl:if>
+ <xsl:value-of select="@pending" />
</td>
<td valign="top" style="{$td-style}">
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|