/******************************************************************************/
/**** generePrem : construit une premisse avec morgOp, plus n arguments sup ***/
/******************************************************************************/
chain_list *generePrem(cell,n)
cellList *cell;
short n;

{
short morgOp = morgOper(OPER(cell->val_abl->VALABL));
chain_list *premisse = consExpr(morgOp,cell->entrees);

addQExpr(premisse,createAtom("*a"));
if (n == 2)
	addQExpr(premisse,createAtom("*b"));
return premisse;
}

/******************************************************************************/
/********** genereConc : construit une conclusion *****************************/
/******************************************************************************/
chain_list *genereConc(cell,n)
cellList *cell;
short n;

{
short op = OPER(cell->val_abl->VALABL);
int arite = lengthExpr(cell->val_abl->VALABL);
chain_list *conclusion = createExpr(op);
chain_list *conc2;
tabList *in = cell->entrees;

do
	{
	addQExpr(conclusion,notExpr(createAtom(in->name)));
	in = in->NEXT;
	arite--;
	}
while (arite > 1);
if ((fonc(cell) == AND) || (fonc(cell) == OR))
	conc2 = consExpr(morgOper(op),in);
else conc2 = consExpr(invOper(morgOper(op)),in);
addQExpr(conc2,createAtom("*a"));
if (n == 2)
	addQExpr(conc2,createAtom("b"));
addQExpr(conclusion,conc2);
return conclusion;
}

/******************************************************************************/
/************ existCell : recherche si une cellule existe *********************/
/******************************************************************************/
short existCell(cell,n)
cellList *cell;
int n;

{
char *chaine = (char *)malloc(5);
char *nom = (char *)malloc(6);
char *ariteChar = (char *)malloc(2);
int arite = lengthExpr(cell->val_abl->VALABL);

short op = OPER(cell->val_abl->VALABL);
if ((op == AND) || (op == NAND))
	chaine = operToChar(NOR);
else chaine = operToChar(NAND);
strcpy(nom,chaine);
printf("existCell : nom 1 = %s\n",nom);
switch (arite+n)
	{
	case 2 : ariteChar = "2"; break;
	case 3 : ariteChar = "3"; break;
	case 4 : ariteChar = "4"; break;
	case 5 : ariteChar = "5"; break;
	case 6 : ariteChar = "6"; break;
	case 7 : ariteChar = "7"; break;
	case 8 : ariteChar = "8"; break;
	case 9 : ariteChar = "9"; break;
	}
strcat(nom,ariteChar);
printf("existCell : nom 2 = %s\n",nom);
if (testObj(namealloc(nom),'c'))
	return 1;
return 0;
}

/******************************************************************************/
/*************** decomp : regles de decomposition booleenne *******************/
/******************************************************************************/
chain_list *decomp(cell)
cellList *cell;

{
chain_list *head = NULL,*reg;

if (lengthExpr(cell->val_abl->VALABL) == 2)
	{
	if (existCell(cell,1))
		{
		reg = (chain_list *)malloc(sizeof(chain_list));
		reg->DATA = (char *)generePrem(cell,2);
		printf("generePrem : ");
		displayExpr(CAR(reg));
		reg->NEXT = genereConc(cell,2);
		head = addchain(head,(char *)reg);
		}
	if (existCell(cell,0))
		{
		reg = (chain_list *)malloc(sizeof(chain_list));
		reg->DATA = (char *)generePrem(cell,1);
		reg->NEXT = genereConc(cell,1);
		head = addchain(head,(char *)reg);
		}
	}
else 
	{
	if (existCell(cell,-1))
		{
		reg = (chain_list *)malloc(sizeof(chain_list));
		reg->DATA = (char *)generePrem(cell,1);
		reg->NEXT = genereConc(cell,1);
		head = addchain(head,(char *)reg);
		}
	}
return head;
}

