From: <jom...@us...> - 2015-09-22 18:47:53
|
Revision: 1844 http://sourceforge.net/p/jason/svn/1844 Author: jomifred Date: 2015-09-22 18:47:50 +0000 (Tue, 22 Sep 2015) Log Message: ----------- the first parameter of .nth can be a variable (to discover the index of same element of a list) Modified Paths: -------------- trunk/src/jason/runtime/MASConsoleColorGUI.java trunk/src/jason/runtime/MASConsoleGUI.java trunk/src/jason/stdlib/nth.java trunk/src/test/RuleTest.java Modified: trunk/src/jason/runtime/MASConsoleColorGUI.java =================================================================== --- trunk/src/jason/runtime/MASConsoleColorGUI.java 2015-08-04 12:47:36 UTC (rev 1843) +++ trunk/src/jason/runtime/MASConsoleColorGUI.java 2015-09-22 18:47:50 UTC (rev 1844) @@ -10,6 +10,7 @@ import javax.swing.JScrollPane; import javax.swing.JTextPane; +import javax.swing.SwingUtilities; import javax.swing.text.AttributeSet; import javax.swing.text.BadLocationException; import javax.swing.text.DefaultCaret; @@ -57,7 +58,7 @@ } @Override - public void append(String agName, String s) { + public void append(final String agName, String s) { try { Color c = null; if (agName != null) { @@ -77,19 +78,22 @@ MASColorTextPane ta = agsTextArea.get(agName); if (ta == null) { // add new tab for the agent - synchronized (this) { - ta = new MASColorTextPane(Color.black); - ta.setEditable(false); - ((DefaultCaret)ta.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); - agsTextArea.put(agName, ta); - int i = 0; - for (; i<tabPane.getTabCount(); i++) { - if (agName.toUpperCase().compareTo( tabPane.getTitleAt(i).toUpperCase()) < 0) - break; + ta = new MASColorTextPane(Color.black); + ta.setEditable(false); + ((DefaultCaret)ta.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); + final MASColorTextPane cta = ta; + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + agsTextArea.put(agName, cta); + int i = 0; + for (; i<tabPane.getTabCount(); i++) { + if (agName.toUpperCase().compareTo( tabPane.getTitleAt(i).toUpperCase()) < 0) + break; + } + tabPane.add(new JScrollPane(cta),i); + tabPane.setTitleAt(i, agName); } - tabPane.add(new JScrollPane(ta),i); - tabPane.setTitleAt(i, agName); - } + }); } if (ta != null) { // print out Modified: trunk/src/jason/runtime/MASConsoleGUI.java =================================================================== --- trunk/src/jason/runtime/MASConsoleGUI.java 2015-08-04 12:47:36 UTC (rev 1843) +++ trunk/src/jason/runtime/MASConsoleGUI.java 2015-09-22 18:47:50 UTC (rev 1844) @@ -45,6 +45,7 @@ import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.JTextArea; +import javax.swing.SwingUtilities; import javax.swing.text.DefaultCaret; /** the GUI console to output log messages */ @@ -178,7 +179,7 @@ append(null, s); } - public void append(String agName, String s) { + public void append(final String agName, String s) { try { if (!frame.isVisible()) { frame.setVisible(true); @@ -192,8 +193,13 @@ ta = new JTextArea(); ta.setEditable(false); ((DefaultCaret)ta.getCaret()).setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE); - agsTextArea.put(agName, ta); - tabPane.add(agName, new JScrollPane(ta)); + final JTextArea cta = ta; + SwingUtilities.invokeAndWait(new Runnable() { + public void run() { + agsTextArea.put(agName, cta); + tabPane.add(agName, new JScrollPane(cta)); + } + }); } if (ta != null) { if (ta.getDocument().getLength() > 100000) { Modified: trunk/src/jason/stdlib/nth.java =================================================================== --- trunk/src/jason/stdlib/nth.java 2015-08-04 12:47:36 UTC (rev 1843) +++ trunk/src/jason/stdlib/nth.java 2015-09-22 18:47:50 UTC (rev 1844) @@ -1,16 +1,24 @@ package jason.stdlib; -import jason.*; -import jason.asSemantics.*; -import jason.asSyntax.*; +import jason.JasonException; +import jason.asSemantics.DefaultInternalAction; +import jason.asSemantics.InternalAction; +import jason.asSemantics.TransitionSystem; +import jason.asSemantics.Unifier; +import jason.asSyntax.ASSyntax; +import jason.asSyntax.ListTerm; +import jason.asSyntax.NumberTerm; +import jason.asSyntax.Term; +import java.util.Iterator; + /** <p>Internal action: <b><code>.nth</code></b>. <p>Description: gets the nth term of a list. <p>Parameters:<ul> -<li>+ index (integer): the position of the term, the first term is at position 0.<br/> +<li>-/+ index (integer): the position of the term (the first term is at position 0)<br/> <li>+ list (list): the list where to get the term from.<br/> <li>-/+ term (term): the term at position <i>index</i> in the <i>list</i>.<br/> </ul> @@ -21,6 +29,7 @@ <li> <code>.nth(0,[a,b,c],d)</code>: false. <li> <code>.nth(0,[a,b,c],a)</code>: true. <li> <code>.nth(5,[a,b,c],X)</code>: error. +<li> <code>.nth(X,[a,b,c,a,e],a)</code>: unifies <code>X</code> with <code>0</code> (and <code>3</code> if it backtracks). </ul> @see jason.stdlib.concat @@ -52,26 +61,69 @@ @Override protected void checkArguments(Term[] args) throws JasonException { super.checkArguments(args); // check number of arguments - if (!args[0].isNumeric()) { - throw JasonException.createWrongArgument(this,"first argument should be numeric and not '"+args[0]+"'."); + if (!args[0].isNumeric() && !args[0].isVar()) { + throw JasonException.createWrongArgument(this,"the first argument should be numeric or a variable -- not '"+args[0]+"'."); } if (!args[1].isList()) { - throw JasonException.createWrongArgument(this,"second argument should be a list and not '"+args[1]+"'."); + throw JasonException.createWrongArgument(this,"the second argument should be a list and not '"+args[1]+"'."); } } @Override - public Object execute(TransitionSystem ts, Unifier un, Term[] args) throws Exception { + public Object execute(TransitionSystem ts, final Unifier un, final Term[] args) throws Exception { checkArguments(args); - int index = (int)((NumberTerm)args[0]).solve(); ListTerm list = (ListTerm)args[1]; - if (index < 0 || index >= list.size()) { - throw new JasonException("nth: index "+index+" is out of bounds ("+list.size()+")"); + if (args[0].isNumeric()) { + int index = (int)((NumberTerm)args[0]).solve(); + + if (index < 0 || index >= list.size()) { + throw new JasonException("nth: index "+index+" is out of bounds ("+list.size()+")"); + } + + return un.unifies(args[2], list.get(index)); } - Term element = list.get(index); + + if (args[0].isVar()) { + + final Iterator<Term> ilist = list.iterator(); + + // return all indexes for thirds arg + return new Iterator<Unifier>() { + int index = -1; + Unifier c = null; // the current response (which is an unifier) + + public boolean hasNext() { + if (c == null) // the first call of hasNext should find the first response + find(); + return c != null; + } - return un.unifies(args[2], element); + public Unifier next() { + if (c == null) find(); + Unifier b = c; + find(); // find next response + return b; + } + + void find() { + while (ilist.hasNext()) { + index++; + Term candidate = ilist.next(); + c = un.clone(); + if (c.unifiesNoUndo( args[2], candidate)) { + c.unifies(args[0], ASSyntax.createNumber(index)); + return; // found another response + } + } + c = null; // no more sublists found + } + + public void remove() {} + }; + + } + return false; } } Modified: trunk/src/test/RuleTest.java =================================================================== --- trunk/src/test/RuleTest.java 2015-08-04 12:47:36 UTC (rev 1843) +++ trunk/src/test/RuleTest.java 2015-09-22 18:47:50 UTC (rev 1844) @@ -1,5 +1,6 @@ package test; +import jason.RevisionFailedException; import jason.asSemantics.Agent; import jason.asSemantics.Unifier; import jason.asSyntax.ASSyntax; @@ -10,6 +11,9 @@ import jason.asSyntax.Term; import jason.asSyntax.VarTerm; import jason.asSyntax.parser.ParseException; +import static jason.asSyntax.ASSyntax.parseLiteral; +import static jason.asSyntax.ASSyntax.parseFormula; +import static jason.asSyntax.ASSyntax.createRule; import java.util.HashMap; import java.util.Iterator; @@ -163,6 +167,57 @@ assertEquals("+!test4 <- .add_plan({ +!g : v <- .print(ok) },self,end); !test4a.", t.toString()); } + public void testMaiquel() throws ParseException, RevisionFailedException { + Agent ag = new Agent(); + ag.initAg(); + //first rule: rule1(X):-teste(X)¬(prop(X))&X + ag.addBel(createRule(parseLiteral("rule1(X)"), parseFormula("teste(X)¬(prop(X))&X"))); + + //second rule: rule2(X):-teste(X)&X¬(prop(X)) + ag.addBel(createRule(parseLiteral("rule2(X)"), parseFormula("teste(X)&X¬(prop(X))"))); + + //asserting some facts + //ag.addBel(parseLiteral("teste(p(K))")); // original proposal and the cause of the problem => test does not produce a ground literal + ag.addBel(parseLiteral("teste(p(tom))")); // correction + ag.addBel(parseLiteral("teste(p(bob))")); // correction + + + ag.addBel(parseLiteral("p(tom)")); + ag.addBel(parseLiteral("p(bob)")); + /* + Iterator<Unifier> it1 = parseFormula("rule1(A)").logicalConsequence(ag, new Unifier()); + System.out.print("Rule 1: "); + while(it1.hasNext()){ + System.out.print(it1.next()); + } + System.out.print("\n"); + Iterator<Unifier> it2 = parseFormula("rule2(A)").logicalConsequence(ag, new Unifier()); + System.out.print("Rule 2: "); + while(it2.hasNext()){ + System.out.print(it2.next()); + } + */ + + //asserting a new fact + ag.addBel(parseLiteral("prop(p(bob))")); + assertEquals(1, iteratorSize(parseFormula("rule1(A)").logicalConsequence(ag, new Unifier()))); + assertEquals(1, iteratorSize(parseFormula("rule2(A)").logicalConsequence(ag, new Unifier()))); + /* + Iterator<Unifier> it12 = parseFormula("rule1(A)").logicalConsequence(ag, new Unifier()); + System.out.print("Rule 1: "); + while(it12.hasNext()){ + System.out.print(it12.next()); + } + System.out.print("\n"); + Iterator<Unifier> it22 = parseFormula("rule2(A)").logicalConsequence(ag, new Unifier()); + System.out.print("Rule 2: "); + while(it22.hasNext()){ + System.out.print(it22.next()); + } + */ + } + + @SuppressWarnings("unchecked") private int iteratorSize(Iterator i) { int c = 0; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |