int partially_expanded(State *s){/* Partially expanded */
  return s->move.o_pm&2;
}
void reexpand(State *s){ /* Set as partially or as fully expanded */
  s->move.o_pm=(s->move.o_pm&2)?(s->move.o_pm&!2):(s->move.o_pm|2);
}

StateItem *partially_expand (State *index) {   
    Trans *t, *t_aux; /* A pointer to a transition */
    char m, ot, ot_aux, o_pm_aux; /* ot is the state of the current process, m? */
    short II, From, To, tt, II_aux, tt_aux; /* The first 3 are pid, tt is type of process */
    StateItem *current_item,*aux_item;
    unsigned char successor_index,successor_index_aux;
    unsigned char timeout;
    unsigned char *this_p_aux;

    sprintf(message,"%s expanding state at depth %d...\n",!partially_expanded(index)?"Partially":"Fully",index->g);
    index->uncompress();
    if(interactive || DEBUG_P) printf(message);
    /* Global variable now points to (index->ps) for comp. with SPIN */
    now=index->ps;

    /* See if the trans from pred was atomic */
    if(!(index->move.o_t)){ /* No move => root */
      /* Try to exectue all currently active processes */
      From=now->_nr_pr-1;
      To=0;
    }else if(index->move.o_t->atom&2){ /* If transition was atomic...*/
      /* Continue executing the same process as in the previous trans. */
      if(interactive || DEBUG_P) printf("\tPrev. trans was atomic\n");
      From=To=index->move.pr; atomic_transitions++;
    } else {
      /* Try to exectue all currently active processes */
      From=now->_nr_pr-1;
      To=0;
    }
 
    successor_index=0;
    trpt=&index->move;
    trpt->tau = trpt->tau & ~(16|32|64); /* make sure these are off */
    /* Prepare potential successor */
    current_item=new StateItem;
    current_item->the_state=new State;
    *(current_item->the_state->ps)=*(index->ps);
    /* Global variable now points to potiential successor for comp. with SPIN */
    now=current_item->the_state->ps;
    //printf("\tLooping in processes %d..%d\n",From,To);
    /* Timeout things */
    trpt->tau=(trpt->tau)&(~1);
  TRY_TIMEOUT:
    /* Loop in processes */
    for(II=From; II>=To; II-=1){
      trpt->o_pm&=~1; /* For else. Process not moved */
      /* Get type and state of the process */
      this_p=pptr(II); /* Get pointer to II within state vector */
      tt=(short) ((P0 *)pptr(II))->_p; /* Get state of II */
      ot=(unsigned char) ((P0 *)pptr(II))->_t; /* Get type of II */

      //printf("\tLooping in transitions...\n");
      /* Loop in each possible transition for the current process and state */
      for(t=trans[ot][tt]; t; t=t->nxt){
	if( (!partially_expanded(index) && !(My_Heuristic->is_dangerous(t))) ||
	    (partially_expanded(index) && My_Heuristic->is_dangerous(t)) )
	  continue;
	//printf("\tTrying to move forward %d...\n",t->forw);
#include FORWARD_MOVES /* move_forward(II,ot,tt,t) */
	if(!m) continue;
	if(now->boq!=-1){
	  this_p_aux=this_p; II_aux=II; tt_aux=tt; ot_aux=ot; t_aux=t; o_pm_aux=trpt->o_pm;
	  /* Prepare potential successor */
	  aux_item=current_item;
	  current_item=new StateItem;
	  current_item->the_state=new State;
	  *(current_item->the_state->ps)=*(aux_item->the_state->ps);
	  now=current_item->the_state->ps;
	  for(II=From; II>=To; II-=1){
	    trpt->o_pm&=~1; /* For else. Process not moved */
	    /* Get type and state of the process */
	    this_p=pptr(II); /* Get pointer to II within state vector */
	    tt=(short) ((P0 *)pptr(II))->_p; /* Get state of II */
	    ot=(unsigned char) ((P0 *)pptr(II))->_t; /* Get type of II */  
	    for(t=trans[ot][tt]; t; t=t->nxt){
#include FORWARD_MOVES /* move_forward(II,ot,tt,t) */
	      if(!m) continue;
	      rendezvous_boq_states++;
	      delete current_item->the_state;
	      delete current_item;
	      current_item=aux_item;
	      this_p=this_p_aux; II=II_aux; tt=tt_aux; ot=ot_aux; t=t_aux; trpt->o_pm=o_pm_aux;
	      now=current_item->the_state->ps;
	      this_p=pptr(II); /* Get pointer to II within state vector */
	      goto SUCCESSFULL_MOVE;
	    }
	  }
	  delete current_item->the_state;
	  delete current_item;
	  current_item=aux_item;
	  this_p=this_p_aux; II=II_aux; tt=tt_aux; ot=ot_aux; t=t_aux; trpt->o_pm=o_pm_aux;
	  *(current_item->the_state->ps)=*(index->ps);
	  now=current_item->the_state->ps;
	  this_p=pptr(II); /* Get pointer to II within state vector */
	} else {
	SUCCESSFULL_MOVE:
	  trpt->o_pm|=1; /* Process II moved */
	  if(interactive || DEBUG_P) printf("\tMove succeded: ");
	  if(interactive || DEBUG_P) printf("proc %d(%d) exec (%d--->%d) : %s\n", II, ot, tt, t->st, t->tp);
	  if (t->st > 0){
	    /* Set up new state for the process */
	    ((P0 *)pptr(II))->_p = t->st;
	  }
	  /* Construct successor */
	  //printf("\tConstruct Successor\n");
	  current_item->the_state->g=index->g+1;
	  current_item->the_state->pred=index;
	  current_item->the_state->move.o_t=t;
	  current_item->the_state->move.st=tt;
	  current_item->the_state->move.pr=II;
	  current_item->the_state->move.tau=0;
	  current_item->the_state->move.o_pm=0;
	  current_item->the_state->h=Heuristic(*current_item->the_state);
	  current_item->the_state->compress();
	  successor_index++;
	  /* Prepare potential successor */
	  //printf("Prepare potential successor\n");
	  aux_item=new StateItem;
	  aux_item->next=current_item;
	  current_item=aux_item;
	  current_item->the_state=new State;
	  *(current_item->the_state->ps)=*(index->ps);
	  /* Global variable now points to potiential successor for comp. with SPIN */
	  now=current_item->the_state->ps;
	  this_p=pptr(II); /* Get pointer to II within state vector */
	}
      }
    }
    if(successor_index==0 && !((trpt->tau)&1)){
      trpt->tau=(trpt->tau)|1;
      now=current_item->the_state->ps;
     goto TRY_TIMEOUT;
    }
    if(interactive || DEBUG_P)
      printf("\tExpansion done: %d successor(s) found\n",successor_index);
    if(current_item){
      aux_item=current_item;
      current_item=current_item->next;
      delete aux_item->the_state;
      delete aux_item; /* delete unused state */
    }
/*     if(interactive || DEBUG_P){ */
/*       successor_index_aux=successor_index; */
/*       for(aux_item=current_item; aux_item; aux_item=aux_item->next){ */
/* 	printf("\t\tSuccesssor %d: proc %d exec (%d--->%d) : %s\n", successor_index_aux,aux_item->the_state->move.pr,aux_item->the_state->move.st, aux_item->the_state->move.o_t->st, aux_item->the_state->move.o_t->tp); */
/* 	successor_index_aux--; */
/*       } */
/*     } */
    if(interactive && successor_index>=2){
      do{
	printf("\tSelect succcessor 1..%d: ",successor_index);
	scanf("%d",&successor_index_aux);
      }while(successor_index_aux<1 || successor_index_aux>successor_index);
      for(successor_index=1; successor_index!=successor_index_aux; successor_index++){
	current_item=current_item->next;
      }
      current_item->next=(StateItem *)0;
      printf("\tSelected: proc %d exec (%d--->%d) : %s\n", current_item->the_state->move.pr,current_item->the_state->move.st, current_item->the_state->move.o_t->st, current_item->the_state->move.o_t->tp);
    }
    index->compress();
    reexpand(index);
    return current_item;
  };
