From: <jom...@us...> - 2015-12-11 11:01:10
|
Revision: 1866 http://sourceforge.net/p/jason/svn/1866 Author: jomifred Date: 2015-12-11 11:01:07 +0000 (Fri, 11 Dec 2015) Log Message: ----------- new buf Modified Paths: -------------- trunk/src/jason/asSemantics/Agent.java Modified: trunk/src/jason/asSemantics/Agent.java =================================================================== --- trunk/src/jason/asSemantics/Agent.java 2015-12-09 14:20:46 UTC (rev 1865) +++ trunk/src/jason/asSemantics/Agent.java 2015-12-11 11:01:07 UTC (rev 1866) @@ -45,6 +45,7 @@ import jason.asSyntax.parser.as2j; import jason.bb.BeliefBase; import jason.bb.DefaultBeliefBase; +import jason.bb.StructureWrapperForLiteral; import jason.functions.Count; import jason.functions.RuleToFunction; import jason.jeditplugin.Config; @@ -64,10 +65,12 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.logging.Level; @@ -714,6 +717,7 @@ * @return the number of changes (add + dels) */ public int buf(List<Literal> percepts) { + // complexity 3n if (percepts == null) { return 0; } @@ -723,11 +727,33 @@ int dels = 0; //long startTime = qProfiling == null ? 0 : System.nanoTime(); + // to copy percepts allows the use of contains below + Set<StructureWrapperForLiteral> perW = new HashSet<StructureWrapperForLiteral>(); + Iterator<Literal> iper = percepts.iterator(); + while (iper.hasNext()) + perW.add(new StructureWrapperForLiteral(iper.next())); + + // deleting percepts in the BB that is not perceived anymore Iterator<Literal> perceptsInBB = getBB().getPercepts(); while (perceptsInBB.hasNext()) { Literal l = perceptsInBB.next(); - + if (! perW.remove(new StructureWrapperForLiteral(l))) { // l is not perceived anymore + dels++; + perceptsInBB.remove(); // remove l as perception from BB + + // new version (it is sure that l is in BB, only clone l when the event is relevant) + Trigger te = new Trigger(TEOperator.del, TEType.belief, l); + if (ts.getC().hasListener() || pl.hasCandidatePlan(te)) { + l = ASSyntax.createLiteral(l.getFunctor(), l.getTermsArray()); + l.addAnnot(BeliefBase.TPercept); + te.setLiteral(l); + ts.getC().addEvent(new Event(te, Intention.EmptyInt)); + } + } + } + /* + ////// previous version, without perW hashset // could not use percepts.contains(l), since equalsAsTerm must be // used (to ignore annotations) boolean wasPerceived = false; @@ -759,7 +785,7 @@ te.setLiteral(l); ts.getC().addEvent(new Event(te, Intention.EmptyInt)); } - + */ /* // old version // can not delete l, but l[source(percept)] @@ -770,22 +796,19 @@ ts.updateEvents(new Event(new Trigger(TEOperator.del, TEType.belief, l), Intention.EmptyInt)); } */ - } - } + //} + //} - // BUF only adds a belief when appropriate - // checking all percepts for new beliefs - for (Literal lp : percepts) { + for (StructureWrapperForLiteral lw: perW) { try { - lp = lp.copy().forceFullLiteralImpl(); + Literal lp = lw.getLiteral().copy().forceFullLiteralImpl(); lp.addAnnot(BeliefBase.TPercept); if (getBB().add(lp)) { adds++; - Trigger te = new Trigger(TEOperator.add, TEType.belief, lp); - ts.updateEvents(new Event(te, Intention.EmptyInt)); + ts.updateEvents(new Event(new Trigger(TEOperator.add, TEType.belief, lp), Intention.EmptyInt)); } } catch (Exception e) { - logger.log(Level.SEVERE, "Error adding percetion " + lp, e); + logger.log(Level.SEVERE, "Error adding percetion " + lw.getLiteral(), e); } } @@ -796,6 +819,61 @@ return adds + dels; } + public int bufAlternative(List<Literal> percepts) { + // complexity 4n + + if (percepts == null) { + return 0; + } + + // stat + int adds = 0; + int dels = 0; + + //Set<Literal> pinBB = ((DefaultBeliefBase)getBB()).getPerceptsSet(); + Set<StructureWrapperForLiteral> pinBBW = new HashSet<StructureWrapperForLiteral>(); + Iterator<Literal> ibb = getBB().getPercepts(); + while (ibb.hasNext()) + pinBBW.add(new StructureWrapperForLiteral( ibb.next())); + + Iterator<Literal> ip = percepts.iterator(); + while (ip.hasNext()) { + Literal t = ip.next(); + StructureWrapperForLiteral lrt = new StructureWrapperForLiteral(t); + if ( pinBBW.remove(lrt) ) { + // since this percept in in BB, do not need to be added neither removed + ip.remove(); + } + } + + // percepts that are not in BB + for (Literal lp : percepts) { + try { + lp = lp.copy().forceFullLiteralImpl(); + lp.addAnnot(BeliefBase.TPercept); + if (getBB().add(lp)) { + adds++; + ts.updateEvents(new Event(new Trigger(TEOperator.add, TEType.belief, lp), Intention.EmptyInt)); + } + } catch (Exception e) { + logger.log(Level.SEVERE, "Error adding percetion " + lp, e); + } + } + + // remove what was not perceived + for (StructureWrapperForLiteral lw: pinBBW) { + Literal l = lw.getLiteral(); + l = ASSyntax.createLiteral(l.getFunctor(), l.getTermsArray()); + l.addAnnot(BeliefBase.TPercept); + if (bb.remove(l)) { + dels++; + ts.updateEvents(new Event(new Trigger(TEOperator.del, TEType.belief, l), Intention.EmptyInt)); + } + } + return adds + dels; + } + + /* public QueryCacheSimple getQueryCache() { return qCache; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |