/* Author: Jeff Dalton <J.Dalton@ed.ac.uk>
 * Updated: Mon Jul  2 15:15:03 2001 by Jeff Dalton
 * Copyright: (c) 2000, AIAI, University of Edinburgh
 */

package ix.icore.domain;

import java.util.*;

import ix.icore.domain.event.*;
import ix.util.*;
import ix.util.lisp.*;

/**
 * A Domain contains descriptions of process products and actions.
 */

public class Domain extends SchemaTable {

    // public SchemaTable schemaTable = new SchemaTable(); // s.b. protected

    protected LListCollector productSchemas = new LListCollector();

    protected LListCollector listeners = new LListCollector();

    public Domain() {
	super();
    }

    /** Merges two domains */
    public void takeFrom(Domain other) {
	Iterator i;
	// Schemas
	for (i = other.getAllSchemas().iterator(); i.hasNext();) {
	    addSchema((Schema)i.next());
	}
	// Product schemas
	for (i = other.getAllProductSchemas().iterator(); i.hasNext();) {
	    addProductSchema((ProductSchema)i.next());
	}
	// Initial facts
    }

    public boolean isEmpty() {
	return super.isEmpty() && productSchemas.isEmpty();
    }

    public void clear() {
	super.clear();
	productSchemas.clear();
    }

    public void addSchema(Schema s) {
	super.addSchema(s);
	fireSchemaAdded(s);
    }

    public void addInitialFact(PVPair fact) {
    }

    public void addProductSchema(ProductSchema s) {
	productSchemas.addElement(s);
    }

    public LList getAllProductSchemas() {
	return productSchemas.contents();
    }

    public void addDomainListener(DomainListener listener) {
	listeners.addElement(listener);
    }

    public void fireSchemaAdded(Schema s) {
	SchemaEvent event = new SchemaEvent(this, s);
	for (Enumeration e = listeners.elements(); e.hasMoreElements();) {
	    DomainListener listener = (DomainListener)e.nextElement();
	    listener.schemaAdded(event);
	}
    }

    public void analyseDomain() {

	checkSchemaReferences();

    }

}


// Issues:
// * Should it be possible to do new Domain(String fileName)?
// * get/setSchemas() and get/setInitialFacts()?
// * Should analyseDomain() exist as something the client has to call
//   or be something that's done automatically.
// * Should Domain contain a SchemaTable rather than be a subclass?
// * Are the initial facts just defaults?  Should we bother?
