/*****************************************************************************
 *
 * FILE : defjobs.c - default reasoning module
 *
 * ***************************************************************************
 * SECTION : defjobs.c - lib of default logic tasks supported by the program
 *
 * programmer - Pawel Cholewinski
 *
 * date 04/19/93 ; modified 01/15/94
 * modified 19 Apr 1999 by Neil Moore <neil@cs.uky.edu>
 * modified 20 Aug 1999 by Neil Moore <neil@cs.uky.edu>
 * modified  5 Sep 1999 by Neil Moore <neil@cs.uky.edu>
 *
 * Part of sderes.  See COPYRIGHT for copyright information.
 *
 *****************************************************************************/

#include "proto.h"
#include "global.h"
#include "default.h"
#include "token.h"


int w[MAXWSIZE];
int preq[MAXDSIZE];
int just[MAXDSIZE];
int conc[MAXDSIZE];
int ad[MAXDSIZE];

int  dttable[DTSIZE];
int wno, dno,  wbegin, wend, dbegin, dend;

int extlimit=0;

int GenDef[MAXDSIZE];
int Reduct[MAXDSIZE];

int dffno, pbegin, pend;

char *defatomlex[2*MAXDSIZE];
int deffacts[2*MAXDSIZE];
int n_strat;
int stratum[MAXDSIZE];


/*************************************************************************
 * SECTION : deflib.c - library of basic routines for default reasoning
 *
 * programmer - Pawel Cholewinski
 *
 * date 10/28/92
 *
 *************************************************************************/

/*
 * =========================================================================
 * form_prover_structures : scan coded theory, find base formulas (W),
 *	  prerequisits, justifications and conclusions of all defaults.
 *	  store them as pointers to their first symbol in global tables
 *	  w, d, preq, just, conc respectively
 * =========================================================================
 */
int form_prover_structures()
{
	int i,j=0;

	i=wbegin+1;
			/* search for W formulas */
	if (dttable[i]==SEMI) i++;
	while (i!=wend) {
		w[j++] = i;
		i = rewindft(i,SEMI);
		i++;
		}
	w[j]= NONE; wno=j;

	j=0;
	i=dbegin+1;
			/* search for defaults */
	if (dttable[i]==SEMI) i++;
	while(i!=dend) {
		if (dttable[i]==COLON)
			preq[j]= NONE;
		else
			preq[j]=i;
		i = rewindft(i,COLON);
		if (dttable[i]!=COLON && verbosity > 0)
			fprintf(stderr, "rewindft error (COLON) %d\n",i);
		i++;
		if (dttable[i]==ENT)
			just[j]= NONE;
		else
			just[j]=i;
		i = rewindft(i,ENT);
		if (dttable[i]!=ENT && verbosity > 0)
			fprintf(stderr,"rewindft error (ENT) %d\n",i);
		i++;
		conc[j]=i;
		i = rewindft(i, SEMI);
		i++; j++;
	        preq[j]=just[j]=conc[j]= NONE;
	}
        dno = j;
	return 0;
}

/*
 * ========================================================================
 * rewindft : rewind theory to first match given token, exit on error
 *   -parameters: position - current position,
 *		  token    - token to be found
 *   -returnes:   token position
 * ========================================================================
 */
int rewindft(position, token)
	int position, token;
{
	int i;
	i=position;
	while ((dttable[i]!=token) && (i<DTSIZE)) {
	   if (dttable[i]==VAR) i = i+2;
	   else i = i+1;
	}
	if (i==DTSIZE) {
		if(verbosity > 0)
			fprintf(stderr, "token %d expected after %d\n",token,position);
		error("theory syntax error, bye...",1);
	}
	return i;
}


/*
 * ========================================================================
 * Bomega : computes Tomega of the Horn program - reduct of input program
 *	    stored in Reduct, sends results to array Textnsn
 * ========================================================================
 */
int Bomega(void)
{
   int start=0, k=0;

   while (Reduct[start]==0 && start < dno) start++;
   while (k>=0) {
      k = new_monrule_first_fireable(start,dno);
      if (k<0) return start;
      else {
	ad[k] = 1;
	if (GenDef[k]==0) return -1;
	store_facts(conc[k]);
	Reduct[k] = 0;
	while (start<dno && Reduct[start]==0) start++;
      }
   }
   return start;
}

/*
 * ========================================================================
 * default_is_consistent : test if d number cn is consistent with 
 *		   interpretation stored in Cextnsn
 * ========================================================================
 */
int default_is_consistent(dn)
   int dn;
{
   int k;
   
   k = just[dn];
   if (k<0) return 1; /* no justification in default dn */
   while (1) {
      if (prove(k,0) == 1) return 0;
      while (dttable[k] != COMMA && dttable[k] != ENT) k++;
      if (dttable[k] == COMMA) k++;
      else return 1;
   }
}
	 

/*
 * ========================================================================
 * monrule_first_fireable : selects first not used d from Reduct which is
 *		    fireable wrt Textnsn
 * ========================================================================
 */
int new_monrule_first_fireable(int st, int lim)
{
   int k;

   k = st;
   while (k < lim) 
	if (Reduct[k]==0) k++;
	else if (prove(preq[k],1)==1) return k;
	else k++;
   return -1;
}
