/*
 * Copyright (C) 1999-2006 <a href="mailto:paschke@in.tum.de">Adrian Paschke</a>
 * 
 * The ECA Interpreter is part of the Rule Based Service Level Agreement (RBSLA)
 * framework, available at 
 * <a href=" http://ibis.in.tum.de/staff/paschke/rbsla/index.htm">RBSLA</a>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package rbsla;

import org.apache.log4j.Logger;
import rbsla.eca.*;
import rbsla.wrapper.ProvaWrapper;
import rbsla.utils.ConsoleWriter;

/**
 * Starts the RBSLA system with ContractLog and reactive rules from the console. 
 * With a command line parameter you can
 * choose which Knowledge Definition you want to use.
 * 
 * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
 * @version 0.1 <1 Sept 2005>
 * @since 0.1
 */
public class RBSLAStart
{

	public static boolean RBSLAflag = true;
	public static boolean getRBSLAflag() {return RBSLAflag;}
	public static void setRBSLAflag(boolean flag) {RBSLAflag=flag;}
	
    private static Logger logger = Logger.getLogger(RBSLAStart.class);

    public RBSLAStart(ActiveKnowledgeTask _startDaemon, ProvaWrapper prova)
    {		
		// print console output with console printer - parses eca console output
		prova.setPrintWriter(new ConsoleWriter(System.out)); 
		prova.showPrintWriter(true); //print console output
		startDaemon = _startDaemon; 
        startDaemon.setActiveKnowledgeBase(new ActiveKnowledgeBase(prova));
		startDaemon.showTimer(true); // show the timer on console
    }

    public static void main(String[] argv)
    {

        if (argv.length != 2)
        {
            System.out.println("RBSLA - please specify a deamon for reactive rule processing and/or a Prova file for execution.");
            System.out.println("rbsla.ConsoleStart rbsla.eca.DaemonMultithreading XXX.prova");
			System.out.println("rbsla.ConsoleStart rbsla.eca.Daemon XXX.prova");
			return;
        }

        logger.info("");
        logger.info("----------------------------------------------");
        logger.info("                Start RBSLA System");
        logger.info("----------------------------------------------");

        ProvaWrapper myProva = null;

        try
        {		
			RBSLAStart.RBSLAflag = false; // RBSLA start
            myProva = new ProvaWrapper(new String[] { argv[1] });			
        } catch (Exception E)
        {
            System.out.println("The Prova file " + argv[1]
                    + " can not be loaded.");
            System.exit(1);
        }


        ActiveKnowledgeTask ant;
        try
        {
            ant = (ActiveKnowledgeTask) Class.forName(argv[0]).newInstance();
        } catch (Exception ex)
        {
            System.out.println("Can not find daemon for reactive rule processing or Prova file.\r\n");
            System.out.println("RBSLA - please specify a deamon and a Prova file for execution.");
            System.out.println("rbsla.ConsoleStart rbsla.eca.DaemonMultithreading XXX.prova");
			System.out.println("rbsla.ConsoleStart rbsla.eca.Daemon XXX.prova");
            return;
        }
		
		
		
		new RBSLAStart(ant, myProva).run();
		
		System.exit(0);
       
    }

    public void run()
    {
        startDaemon.run();
			
		return;
    }

    private ActiveKnowledgeTask startDaemon;
}