All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.exigen.ie.constrainer.impl.IntExpArrayElement1 Maven / Gradle / Ivy

package com.exigen.ie.constrainer.impl;
import com.exigen.ie.constrainer.EventOfInterest;
import com.exigen.ie.constrainer.Failure;
import com.exigen.ie.constrainer.IntExp;
import com.exigen.ie.constrainer.IntExpArray;
import com.exigen.ie.constrainer.IntVar;
import com.exigen.ie.constrainer.Observer;
import com.exigen.ie.constrainer.Subject;
import com.exigen.ie.tools.Reusable;
import com.exigen.ie.tools.ReusableFactory;
import com.exigen.ie.tools.ReusableImpl;

/*
 * == Changed
 * March-2010: Error was found and fixed by Roman - see MaxTest.java
 * Test 3 triggered an ArrayIndexOutOfBoundsException
 * at IntExpArrayElement1:935, resulting in a
 * NullPointerException at Var:120.
 * "j<=max" replaced by "j arrayElementMax)
      index.removeValue(idx);
    return true;
  }
} // ~RemoveFromElementMinIterator

/**
 * Remove all indexes from the _index where max LT min(_ary[idx]).
 */
static class RemoveFromElementMaxIterator extends ReusableImpl implements IntExp.IntDomainIterator
{
  static ReusableFactory _factory = new ReusableFactory()
  {
      protected Reusable createNewElement()
      {
        return new RemoveFromElementMaxIterator();
      }
  };

  static RemoveFromElementMaxIterator getIterator(IntExp index, IntExpArray ary, int max)
  {
    RemoveFromElementMaxIterator it = (RemoveFromElementMaxIterator) _factory.getElement();
    it.index = index;
    it.ary = ary;
    it.max = max;
    return it;
  }

  IntExp index;
  IntExpArray ary;
  int max;

  public boolean doSomethingOrStop(int idx) throws Failure
  {
    int arrayElementMin = ary.elementAt(idx).min();
    if(max < arrayElementMin)
      index.removeValue(idx);
    return true;
  }
} // ~RemoveFromElementMaxIterator


/**
 * Remove all indexes from the _index where _ary[idx] != value.
 */
static class SetValueFromElementIterator extends ReusableImpl implements IntExp.IntDomainIterator
{
  static ReusableFactory _factory = new ReusableFactory()
  {
      protected Reusable createNewElement()
      {
        return new SetValueFromElementIterator();
      }
  };

  static SetValueFromElementIterator getIterator(IntExp index, IntExpArray ary, int value)
  {
    SetValueFromElementIterator it = (SetValueFromElementIterator) _factory.getElement();
    it.index = index;
    it.ary = ary;
    it.value = value;
    return it;
  }

  IntExp index;
  IntExpArray ary;
  int value;

  public boolean doSomethingOrStop(int idx) throws Failure
  {
    if(!ary.elementAt(idx).contains(value))
      index.removeValue(idx);
    return true;
  }

} // ~SetValueFromElementIterator

/**
 * finds  min(_ary[idx]) and max(_ary[idx]).
 */
static class FindMinMaxIterator extends ReusableImpl implements IntExp.IntDomainIterator
{
  static ReusableFactory _factory = new ReusableFactory()
  {
      protected Reusable createNewElement()
      {
        return new FindMinMaxIterator();
      }
  };

  IntExpArray ary;
  int min = Integer.MAX_VALUE;
  int max = -Integer.MAX_VALUE;
  IntExp index;

  static FindMinMaxIterator getIterator(IntExp index, IntExpArray ary)
  {
    FindMinMaxIterator it = (FindMinMaxIterator) _factory.getElement();
    it.index = index;
    it.ary = ary;
    return it;
  }

  public boolean doSomethingOrStop(int idx) throws Failure
  {
    if (ary.elementAt(idx).max() > max)
      max = ary.elementAt(idx).max();
    if (ary.elementAt(idx).min() < min)
      min = ary.elementAt(idx).min();
    return true;
  }
}
//~FindMinMaxIterator

/*==============================================================================
EOF Functional iterators
============================================================================*/

static int findIndex(IntExp index, IntExpArray ary, int value)
{
  FindValueIterator it = FindValueIterator.getIterator(index, ary, value);
  try{ index.iterateDomain(it); } catch(Failure f){}

  int foundIndex = it.foundIndex;

  it.free();

  return foundIndex;
}

static boolean indexHasValue(IntExp index, IntExpArray ary, int value)
{
  return findIndex(index,ary,value) >= 0;
}

static IntExpArray makeExtraction(IntExp index, IntExpArray ary){
  CopyElementsIterator iter = CopyElementsIterator.getIterator(index, ary);
  try{index.iterateDomain(iter);} catch(Failure f){}
  //iter.free();
  return iter.extract;
}
/**
 * An Mapping that scan index for a value.
 */
static class SimpleMapping implements Mapping
{
  IntExp   _index;
  IntExp   _element;
  IntExpArray _ary;
  AryElementsObserver[] _observers;

  public SimpleMapping(IntExp index, IntExp element, IntExpArray ary, AryElementsObserver[] observers)
  {
    _index = index;
    _element = element;
    _ary = ary;
    _observers = observers;
  }

  public void arrayElementValue(int value, int idx) throws Failure{
    if ( (value > _element.max()) || (value < _element.min()) )
        _index.removeValue(idx);
  }

  public void arrayElementMax(int oldmax, int max, int idx) throws Failure{
    if (_element.min() > max)
      _index.removeValue(idx);
  }

  public void arrayElementMin(int oldmin, int min, int idx) throws Failure{
    if (_element.max() < min)
      _index.removeValue(idx);
  }

  public void arrayElementRemove(int value, int idx) throws Failure{;}

  public void indexValue(int value) throws Failure
  {
    for (int i=0; i<_ary.size(); i++){
      if ( (_observers[i] != null) && (i!=value))
        _ary.get(value).detachObserver(_observers[i]);
    }
    _element.setMin(_ary.get(value).min());
    _element.setMax(_ary.get(value).max());
  }

  public void indexMax(int oldmax, int max) throws Failure{
    for (int i=oldmax; i>max; i--){
      if ( (_observers[i] != null) )
        _ary.get(i).detachObserver(_observers[i]);
    }
    updateResultDomainFromIndex();
  }

  public void indexMin(int oldmin, int min) throws Failure{
    for (int i=oldmin; i _element.max())
      _index.removeValue(idx);
    else
      decreaseUsageCounter(oldmin, min-1, idx);
    IntVar varCopy = (IntVar)copyOfAry.get(idx);
    varCopy.setMin(min);
  }

  public void arrayElementMax(int oldmax, int max, int idx) throws Failure{
    if (max < _element.min())
      _index.removeValue(idx);
    else
      decreaseUsageCounter(max+1, oldmax, idx);
    IntVar varCopy = (IntVar)copyOfAry.get(idx);
    varCopy.setMax(max);
  }

  public void arrayElementRemove(int removedValue, int idx) throws Failure{
    decreaseUsageCounter(removedValue);
    IntVar varCopy = (IntVar)copyOfAry.get(idx);
    varCopy.removeValue(removedValue);
  }

  public void arrayElementValue(int value, int idx) throws Failure{
    IntVar varCopy = (IntVar)copyOfAry.get(idx);
    if ( (value < _element.min()) || (value > _element.max()) )
      _index.removeValue(idx);
    else{
      for (int i=varCopy.min(); i < value; i++){
        if (varCopy.contains(i))
          decreaseUsageCounter(i);
      }
      for (int i=varCopy.max(); i > value; i--){
        if (varCopy.contains(i))
          decreaseUsageCounter(i);
      }
    }
    varCopy.setValue(value);
  }

  void synchronizedUsageCounterWithIndex(int valueBeingSet){

  }

  void detachObservers(int start, int end){
    for (int i=start; i<=end; i++){
      if (_copyOfIndex.contains(i))
        _ary.get(i).detachObserver(_observers[i]);
    }
  }
/**/
  public void indexValue(int value) throws Failure{
    /*IntVar exp = (IntVar)copyOfAry.get(value);
    java.util.Iterator iter = valueToArrayIdx.keySet().iterator();
    while(iter.hasNext()){
      Integer tmp = (Integer)iter.next();
      int curValue = tmp.intValue();
      Integer idx = (Integer)valueToArrayIdx.get(tmp);
      if (_ary.get(value).contains(curValue)){
        _valuesUsed.get(idx.intValue()).setMax(1);
      }else{
        _valuesUsed.get(idx.intValue()).setMax(0);
      }
    }*/

    _element.setMin(_ary.get(value).min());
    _element.setMax(_ary.get(value).max());

    for (int i=0; i<_ary.size(); i++){
      if (_copyOfIndex.contains(i) && (i!=value))
        _ary.get(i).detachObserver(_observers[i]);
    }

    _copyOfIndex.setValue(value);
  }

  public void indexMax(int oldmax, int max) throws Failure{
    FindMinMaxIterator iter = FindMinMaxIterator.getIterator(_index, _ary);
    _index.iterateDomain(iter);
    if ( (iter.max < _element.max()) && (iter.min > _element.min()) ){
      _element.setMax(iter.max);
      _element.setMin(iter.min);
    }
    else{
      for (int i=oldmax; i>max; i--){
        if (_copyOfIndex.contains(i))
          decreaseUsageCounter(copyOfAry.get(i).min(), copyOfAry.get(i).max(), i);
      }
    }

    detachObservers(max+1, oldmax);
    _copyOfIndex.setMax(max);
  }/**/

  public void indexMin(int oldmin, int min) throws Failure{
    FindMinMaxIterator iter = FindMinMaxIterator.getIterator(_index, _ary);
    _index.iterateDomain(iter);
    if ( (iter.max < _element.max()) || (iter.min > _element.min()) ){
      _element.setMax(iter.max);
      _element.setMin(iter.min);
    }
    else{
      for (int i=oldmin; i _element.max()) || (value < _element.min()) ){
        _index.removeValue(idx);
      }*/
    }
    else
    {
      if ((type &  IntEvent.MIN) != 0)
      {
        /*if (_element.max() < e.min())
          _index.removeValue(idx);*/
        _m.arrayElementMin(e.oldmin(), e.min(), idx);
      }
      if ((type &  IntEvent.MAX) != 0)
      {
        /*if (_element.min() > e.max())
          _index.removeValue(idx);*/
        _m.arrayElementMax(e.oldmax(), e.max(), idx);
      }
      if ((type &  IntEvent.REMOVE) != 0)
        {
          int nRemoves = e.numberOfRemoves();
          int min = e.min();
          int max = e.max();
          for (int i = 0; i < nRemoves; ++i)
          {
            int removedValue = e.removed(i);
            if(min <= removedValue && removedValue <= max)
              _m.arrayElementRemove(removedValue, idx);
          }
        }
    }
  } // ~update()

  public int subscriberMask()
  {
    return IntEvent.ALL;
  }

  public Object master()
  {
    return IntExpArrayElement1.this;
  }
} //~ AryElementsObserver



public IntExpArrayElement1(IntExpArray ary, IntExp indexExp)
{
  super(ary.constrainer());
  _ary = ary;
  _indexExp = indexExp;

  _aryElementsObservers = new AryElementsObserver[ary.size()];

  if(constrainer().showInternalNames())
  {
    _name = "" + _ary.name() + "[" + _indexExp.name() + "]";
  }

  try
  {
    createIndex();
    createElement();
    // Propagate events BEFORE attaching the observers.
    constrainer().propagate();
  }
  catch(Exception e)
  {
    throw new RuntimeException( "Invalid elementAt-expression: "+ary+"["+indexExp+"]. "
                                + e.getClass().getName() + ": " + e.getMessage() );
  }

  createMapping();

  _index.attachObserver(new IndexObserver());
  _element.attachObserver(new ElementObserver());

  for (int i=0;i<_ary.size();i++){
    if (_index.contains(i)){
      _aryElementsObservers[i] = new AryElementsObserver(i);
      _ary.get(i).attachObserver(_aryElementsObservers[i]);
    }
  }
}

/**
 * Returns element-domain as an array of different sorted values.
 */
int[] elementDomain()
{
  int arMin = _ary.min();
  int arMax = _ary.max();
  int[] values= new int[arMax-arMin+1];
  int valCounter = 0;

  class IntExpComparator implements java.util.Comparator{
    public int compare(Object a1, Object a2){

      if (((IntExp)a1).min() < ((IntExp)a2).min())
        return -1;
      if (((IntExp)a1).min() == ((IntExp)a2).min())
        return 0;
      return 1;
    }
  };

  IntExpArray tmp = makeExtraction(_index, _ary);
  tmp.sort(new IntExpComparator());

  for (int i=tmp.get(0).min(); i<=tmp.get(0).max(); i++){
    if (tmp.get(0).contains(i))
    values[valCounter++] = i;
  }

  for(int i=1; i < tmp.size(); i++)
  {
    IntExp curElem = tmp.get(i);
    int min = curElem.min();
    int max = curElem.max();
    if (min < values[valCounter-1])
      min = values[valCounter-1]+1;
    for (int j=min;j 1){
            space = "     ";
            s = s + " " + _vals[i] + space;
          }
          else{
            space = "   ";
            s = s + _vals[i] + space;
          }

        }
        System.out.println("\n" + s + "\n" + _arr);
        return null;
      }
   }


    IntExpArray array = new IntExpArray(C, 9);
    for (int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy