LISTSERV mailing list manager LISTSERV 16.0

Help for MASON-INTEREST-L Archives


MASON-INTEREST-L Archives

MASON-INTEREST-L Archives


MASON-INTEREST-L@LISTSERV.GMU.EDU


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

MASON-INTEREST-L Home

MASON-INTEREST-L Home

MASON-INTEREST-L  January 2014

MASON-INTEREST-L January 2014

Subject:

Re: Endless Loop in SimpleInspector

From:

Roman Seidl <[log in to unmask]>

Reply-To:

MASON Multiagent Simulation Toolkit <[log in to unmask]>

Date:

Thu, 30 Jan 2014 17:26:42 +0100

Content-Type:

text/plain

Parts/Attachments:

Parts/Attachments

text/plain (539 lines)

I have now figured out what is really going on - at least I think so.

The problem only occurs when I use a volatile poropety object. If a 
voilatile propery object gets updated by the SimpleInspector the 
following happens:

     public void updateInspector()
         {
         if (properties.isVolatile())  // need to rebuild each time, YUCK
             {
             remove(propertyList);
             generateProperties(start);
             doEnsuredRepaint(this);
             }
         else for( int i = start ; i < start+count ; i++ )
                  if (members[i] != null)
members[i].setValue(properties.betterToString(properties.getValue(i)));
         }
     }

And its really somehow "YUCK". Rebuilding the properties calls on the 
following method on each newly constructed PropertyField:

     public String newValue(final String newValue)
         {
synchronized(SimpleInspector.this.state.state.schedule)
             {
             // try to set the value
             if (props.setValue(index, newValue) == null)
java.awt.Toolkit.getDefaultToolkit().beep();
             // refresh the controller -- if it exists yet
             if (SimpleInspector.this.state.controller != null)
SimpleInspector.this.state.controller.refresh();
             // set text to the new value
             return props.betterToString(props.getValue(index));
             }
         }


This is where I thought the start of the loop to be. And it indeed gets 
endless as this SimpleInspector.this.state.controller.refresh() again 
leads to a call on updateInspector() resulting in newly constructed 
PropertyFields and so on.

There could be a way out: one could introduce an instance variable to 
the SimpleInspector and set it accordingly to avoid loops:

     boolean updating = false;

     PropertyField makePropertyField(final int index)
     {
     [...]
         public String newValue(final String newValue)
         {
         [...]
             if (!updating && SimpleInspector.this.state.controller != null)
             {
                 updating = true;
                 SimpleInspector.this.state.controller.refresh();
                 updating = false;
             }
[...]
         }
     [...]
     }


This stops the endless loop. But if you have more than one 
SimpleInspector in the controller that use dynamic properties there is 
still a limited loop. Let's suppose we have two SimpleInspectors A and B.

The sequence would then be as follows:

A.updateProperties() -> A.makePropertyField() -> controller.refresh()

A.updateProperties() -> A.makePropertyField()  | stopped by updating check
B.updateProperties() -> B.makePropertyField()  -> controller.refresh()

A.updateProperties() -> A.makePropertyField()  | stopped by updating check
B.updateProperties() -> B.makePropertyField()  | stopped by updating check

One could introduce another check in updateInspector() e.g like this:

     public void updateInspector()
         {
         if (properties.isVolatile())  // need to rebuild each time, YUCK
             {
             remove(propertyList);
             generateProperties(start);
             doEnsuredRepaint(this);
             }
         else for( int i = start ; i < start+count ; i++ )
                  if (members[i] != null)
members[i].setValue(properties.betterToString(properties.getValue(i)));
         }
     }

Resulting in the following sequence:

A.updateProperties() -> A.makePropertyField() -> controller.refresh()

A.updateProperties() | stopped by updating check
B.updateProperties() -> B.makePropertyField()  -> controller.refresh()

A.updateProperties() | stopped by updating check
B.updateProperties() | stopped by updating check

This should make the whole process a bit faster and thus more reponsive.

Still when using this solution with two SimpleInspectors, one beeing 
volatile and the others not, both with domain sliders the following 
happens:

When the model is stopped I can move the silders for the volatile 
Properties pretty smoothly and those for the non-volatile lag a bit.

When the model runs I can't use the sliders for the volatile Propeties 
anymore and those for the non-volatile still lag.

I still don't get everything straight. I am not too concerned anymore as 
I found out I might just drop the use of volatile properties. But still 
SimpleInspector does not seem to like them - even with my modifications 
that at least prevent it from crashing.

cheers
roman

--Diff for SimpleInspector--

<+>UTF-8
===================================================================
--- src/main/java/sim/portrayal/SimpleInspector.java (revision )
+++ src/main/java/sim/portrayal/SimpleInspector.java (revision )
@@ -40,7 +40,8 @@
      int start = 0;
      /** The number of items presently in the propertyList */
      int count = 0;
-    /** If this SimpleInspector is updating its PropertyFields - avoids 
update loops for voilatile */
+    /** If this SimpleInspector is updating its PropertyFields - avoids 
update loops for volatile Properties */
+    boolean updating = false;

      JPanel header = new JPanel()
      {
@@ -167,8 +168,12 @@
                      if (props.setValue(index, newValue) == null)
java.awt.Toolkit.getDefaultToolkit().beep();
                      // refresh the controller -- if it exists yet
-                    if (SimpleInspector.this.state.controller != null)
+                    if (!updating && 
SimpleInspector.this.state.controller != null)
+                    {
+                        updating = true;
SimpleInspector.this.state.controller.refresh();
+                        updating = false;
+                    }
                      // set text to the new value
                      return props.betterToString(props.getValue(index));
                  }
@@ -303,7 +308,7 @@

      public void updateInspector()
      {
-        if (properties.isVolatile())  // need to rebuild each time, YUCK
+        if (!updating && properties.isVolatile()) // need to rebuild 
each time, YUCK
          {
              remove(propertyList);
              generateProperties(start);

--SimpleInspector---

/*
   Copyright 2006 by Sean Luke and George Mason University
   Licensed under the Academic Free License version 3.0
   See the file "LICENSE" for more information
*/

package sim.portrayal;
import sim.portrayal.inspector.*;
import java.awt.*;
import sim.engine.*;
import sim.util.gui.*;
import sim.util.*;
import sim.display.*;
import javax.swing.*;
import java.awt.event.*;

/**
  A simple inspector class that looks at the "getX" and "setX" method of 
the object to be investigates
  and creates a user-friendly graphical interface with read only and 
read/write components of the object.

  <p>SimpleInspector automatically creates an UpdateButton and adds it 
to itself at position BorderLayout.NORTH
  whenever you set it to be non-volatile, and when you set it to be 
volatile, it removes the UpdateButton.

  <p>SimpleInspector automatically sets the title of the inspetor to the 
object name.
  */

public class SimpleInspector extends Inspector
{
     public static final int DEFAULT_MAX_PROPERTIES = 25;
     int maxProperties = DEFAULT_MAX_PROPERTIES;
     /** The GUIState  of the simulation */
     GUIState state;
     /** The property list displayed -- this may change at any time */
     LabelledList propertyList;
     /** The generated object properties -- this may change at any time */
     Properties properties;
     /** Each of the property fields in the property list, not all of 
which may exist at any time. */
     PropertyField[] members = new PropertyField[0];
     /** The current index of the topmost element */
     int start = 0;
     /** The number of items presently in the propertyList */
     int count = 0;
     /** If this SimpleInspector is updating its PropertyFields - avoids 
update loops for volatile Properties */
     boolean updating = false;

     JPanel header = new JPanel()
     {
         public Insets getInsets () { return new Insets(2,2,2,2); }
     };
     String listName;  // used internally

     JLabel numElements = new JLabel();
     Box startField = null;

     public GUIState getGUIState() { return state; }
     public int getMaxProperties() { return maxProperties; }

     /** Creates a new SimpleInspector with the given properties, state, 
maximum number of properties, and
      "name".  The name is what's shown in the labelled list of the 
SimpleInspector.  It is not the
      title of the SimpleInspector (what appears in a window).  For 
that, use setTitle. */
     public SimpleInspector(Properties properties, GUIState state, 
String name, int maxProperties)
     {
         this.maxProperties = maxProperties;
         setLayout(new BorderLayout());
         this.state = state;
         listName = name;
         header.setLayout(new BorderLayout());
         add(header,BorderLayout.NORTH);
         this.properties = properties;
         generateProperties(0);
         setTitle("" + properties.getObject());
     }

     /** Creates a new SimpleInspector with the given properties, state, and
      "name".  The name is what's shown in the labelled list of the 
SimpleInspector.  It is not the
      title of the SimpleInspector (what appears in a window).  For 
that, use setTitle. */
     public SimpleInspector(Properties properties, GUIState state, 
String name)
     {
         this(properties, state, name, DEFAULT_MAX_PROPERTIES);
     }

     /** Creates a new SimpleInspector with the given properties and 
state. */
     public SimpleInspector(Object object, GUIState state)
     {
         this(object,state,null);
     }

     /** Creates a new SimpleInspector with the given object, state, and
      "name".  The name is what's shown in the labelled list of the 
SimpleInspector.  It is not the
      title of the SimpleInspector (what appears in a window).  For 
that, use setTitle. */
     public SimpleInspector(Object object, GUIState state, String name)
     {
         this(object, state, name, DEFAULT_MAX_PROPERTIES);
     }

     /** Creates a new SimpleInspector with the given object, state, 
maximum number of properties, and
      "name".  The name is what's shown in the labelled list of the 
SimpleInspector.  It is not the
      title of the SimpleInspector (what appears in a window).  For 
that, use setTitle. */
     public SimpleInspector(Object object, GUIState state, String name, 
int maxProperties)
     {
         this(Properties.getProperties(object), state, name, maxProperties);
     }

     /* Creates a JPopupMenu that possibly includes "View" to
        view the object instead of using the ViewButton.  If not, 
returns null. */
     JPopupMenu makePreliminaryPopup(final int index)
     {
         Class type = properties.getType(index);
         if (properties.isComposite(index))
         {
             JPopupMenu popup = new JPopupMenu();
             JMenuItem menu = new JMenuItem("View");
             menu.setEnabled(true);
             menu.addActionListener(new ActionListener()
             {
                 public void actionPerformed(ActionEvent e)
                 {
                     Properties props = properties;
                     final SimpleInspector simpleInspector = new 
SimpleInspector(props.getValue(index), SimpleInspector.this.state, null, 
maxProperties);
                     final Stoppable stopper = 
simpleInspector.reviseStopper(
SimpleInspector.this.state.scheduleRepeatingImmediatelyAfter(simpleInspector.getUpdateSteppable()));
SimpleInspector.this.state.controller.registerInspector(simpleInspector,stopper);
                     JFrame frame = simpleInspector.createFrame(stopper);
                     frame.setVisible(true);
                 }
             });
             popup.add(menu);
             return popup;
         }
         else return null;
     }

     PropertyField makePropertyField(final int index)
     {
         Class type = properties.getType(index);
         final Properties props = properties;            // see UNUSUAL 
BUG note below
         return new PropertyField(
                 null,
properties.betterToString(properties.getValue(index)),
                 properties.isReadWrite(index),
                 properties.getDomain(index),
                 (properties.isComposite(index) ?
                         //PropertyField.SHOW_VIEWBUTTON :
                         PropertyField.SHOW_TEXTFIELD :
                         (type == Boolean.TYPE || type == Boolean.class ?
                                 PropertyField.SHOW_CHECKBOX :
(properties.getDomain(index) == null ? PropertyField.SHOW_TEXTFIELD :
(properties.getDomain(index) instanceof Interval) ?
PropertyField.SHOW_SLIDER : PropertyField.SHOW_LIST ))))
         {
             // The return value should be the value you want the 
display to show instead.
             public String newValue(final String newValue)
             {
                 // UNUSUAL BUG: if I say this:
                 // Properties props = properties;
                 // ...or...
                 // Properties props = SimpleInspector.this.properties
                 // ... then sometimes props is set to null even though 
clearly
                 // properties is non-null above, since it'd be 
impossible to return a
                 // PropertyField otherwise.  So instead of declaring it 
as an instance
                 // variable here, we declare it as a final closure 
variable above.

                 // the underlying model could still be running, so we need
                 // to do this safely
synchronized(SimpleInspector.this.state.state.schedule)
                 {
                     // try to set the value
                     if (props.setValue(index, newValue) == null)
java.awt.Toolkit.getDefaultToolkit().beep();
                     // refresh the controller -- if it exists yet
                     if (!updating && 
SimpleInspector.this.state.controller != null)
                     {
                         updating = true;
SimpleInspector.this.state.controller.refresh();
                         updating = false;
                     }
                     // set text to the new value
                     return props.betterToString(props.getValue(index));
                 }
             }
         };
     }

     /** Private method.  Does a repaint that is guaranteed to work (on 
some systems, plain repaint())
      fails if there's lots of updates going on as is the case in our 
simulator thread.  */
     void doEnsuredRepaint(final Component component)
     {
         SwingUtilities.invokeLater(new Runnable()
         {
             public void run()
             {
                 if (component!=null) component.repaint();
             }
         });
     }

     void generateProperties(int start)
     {
         final int len = properties.numProperties();
         if (start < 0) start = 0;
         if (start > len) return;  // failed

         if (propertyList != null)
             remove(propertyList);
         propertyList = new LabelledList(listName);

         if (len > maxProperties)
         {
             final String s = "Page forward/back through properties.  " 
+ maxProperties + " properties shown at a time.";
             if (startField == null)
             {
                 NumberTextField f = new NumberTextField(" Go to ", 
start,1,maxProperties)
                 {
                     public double newValue(double newValue)
                     {
                         int newIndex = (int) newValue;
                         if (newIndex<0) newIndex = 0;
                         if (newIndex >= len) return (int)getValue();
                         // at this point we need to build a new 
properties list!
                         generateProperties(newIndex);
                         return newIndex; // for good measure, though 
it'll be gone by now
                     }
                 };

                 f.setToolTipText(s);
                 numElements.setText(" of " + len + " ");
                 numElements.setToolTipText(s);
                 f.getField().setColumns(4);
                 startField = new Box(BoxLayout.X_AXIS);
                 startField.add(f);
                 startField.add(numElements);
                 startField.add(Box.createGlue());
                 header.add(startField, BorderLayout.CENTER);
             }
         }
         else
         {
             start = 0;
             if (startField!=null) header.remove(startField);
         }

         members = new PropertyField[len];

         int end = start + maxProperties;
         if (end > len) end = len;
         count = end - start;
         for( int i = start ; i < end; i++ )
         {
             if (!properties.isHidden(i))  // don't show if the user 
asked that it be hidden
             {
                 JLabel label = new JLabel(properties.getName(i) + " ");
                 JToggleButton toggle = 
PropertyInspector.getPopupMenu(properties,i,state, makePreliminaryPopup(i));
                 members[i] = makePropertyField(i);
                 propertyList.add(null,
                         label,
                         toggle,
                         members[i],
                         null);

                 // load tooltips
                 String description = properties.getDescription(i);
                 if (description != null)
                 {
                     if (label != null) label.setToolTipText(description);
                     if (toggle != null) 
toggle.setToolTipText(description);    // do we want this one?
                     if (members[i] != null) 
members[i].setToolTipText(description);  // do we want this one?
                 }
             }
             else members[i] = null;
         }
         add(propertyList, BorderLayout.CENTER);
         this.start = start;
         revalidate();
     }

     JButton updateButton = null;
     public void setVolatile(boolean val)
     {
         super.setVolatile(val);
         if (isVolatile())
         {
             if (updateButton!=null)
             {
                 header.remove(updateButton); revalidate();
             }
         }
         else
         {
             if (updateButton==null)
             {
                 updateButton = (JButton) makeUpdateButton();

                 // modify height -- stupid MacOS X 1.4.2 bug has icon 
buttons too big
                 NumberTextField sacrificial = new NumberTextField(1,true);
                 Dimension d = sacrificial.getPreferredSize();
                 d.width = updateButton.getPreferredSize().width;
                 updateButton.setPreferredSize(d);
                 d = sacrificial.getMinimumSize();
                 d.width = updateButton.getMinimumSize().width;
                 updateButton.setMinimumSize(d);

                 // add to header
                 header.add(updateButton,BorderLayout.WEST);
                 revalidate();
             }
         }
     }

     public void updateInspector()
     {
         if (!updating && properties.isVolatile()) // need to rebuild 
each time, YUCK
         {
             remove(propertyList);
             generateProperties(start);
             doEnsuredRepaint(this);
         }
         else for( int i = start ; i < start+count ; i++ )
             if (members[i] != null)
members[i].setValue(properties.betterToString(properties.getValue(i)));
     }
}

Top of Message | Previous Page | Permalink

Advanced Options


Options

Log In

Log In

Get Password

Get Password


Search Archives

Search Archives


Subscribe or Unsubscribe

Subscribe or Unsubscribe


Archives

May 2021
April 2021
March 2021
February 2021
August 2020
July 2020
June 2020
February 2020
August 2019
June 2019
May 2019
April 2019
March 2019
February 2019
January 2019
December 2018
October 2018
August 2018
July 2018
June 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
September 2017
June 2017
May 2017
March 2017
February 2017
January 2017
December 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
January 2016
December 2015
October 2015
September 2015
August 2015
July 2015
June 2015
May 2015
April 2015
March 2015
February 2015
January 2015
December 2014
November 2014
October 2014
September 2014
August 2014
July 2014
June 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
September 2013
August 2013
July 2013
June 2013
May 2013
April 2013
March 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
May 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
September 2010
August 2010
July 2010
June 2010
May 2010
April 2010
February 2010
December 2009
November 2009
October 2009
September 2009
August 2009
July 2009
June 2009
May 2009
April 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
June 2008
May 2008
April 2008
March 2008
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006
October 2006
September 2006
August 2006
July 2006
June 2006
May 2006
April 2006
March 2006
February 2006
January 2006
December 2005
November 2005
October 2005
September 2005
August 2005
July 2005
June 2005
April 2005
March 2005
February 2005
January 2005
December 2004
November 2004
October 2004
September 2004
July 2004
May 2004
April 2004
March 2004

ATOM RSS1 RSS2



LISTSERV.GMU.EDU

CataList Email List Search Powered by the LISTSERV Email List Manager