/*
 * $Id$
 *
 * JSDAI(TM), a way to implement STEP, ISO 10303
 * Copyright (C) 1997-2008, LKSoftWare GmbH, Germany
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License
 * version 3 as published by the Free Software Foundation (AGPL v3).
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * JSDAI is a registered trademark of LKSoftWare GmbH, Germany
 * This software is also available under commercial licenses.
 * See also http://www.jsdai.net/
 */

// %modified: 1016210368345 %

/* Generated By:JJTree: Do not edit this line. X_RuleDecl.java */
package jsdai.expressCompiler;
import jsdai.lang.*;

public class X_RuleDecl
  extends SimpleNode {
  jsdai.SExtended_dictionary_schema.EGlobal_rule global_rule;

  public X_RuleDecl(int id) {
    super(id);
  }

  public X_RuleDecl(Compiler2 p, int id) {
    super(p, id);
  }

  /** Accept the visitor. **/
  public Object jjtAccept(Compiler2Visitor visitor, Object data)
                   throws SdaiException {



    return visitor.visit(this, data);
  }

  
  public Object childrenAccept(Compiler2Visitor visitor, Object data)
                            throws SdaiException {
    JavaClass jc = ( JavaClass )data;

    if (children != null) {


      for (int i = 0; i < children.length; ++i) {
				if(children[i] instanceof X_WhereClause && (jc.entity == global_rule)){

/*
					// trying moving it here, so that init() method is not printed before rule class
					// init(_context) method in RULE
					String parameters = "SdaiContext _context";
					String signature = "\tpublic void init(" + parameters + ") throws SdaiException {\n\t\tif (_already_initialized) return;\n\t\t_already_initialized = true;\n\t\t";
					jc.pw.println(signature);
	 				for (int j = 0; j < jc.init_rule.size(); j++) {
  	 				jc.pw.println("\t\t" + (String)jc.init_rule.elementAt(j));
    			}

*/
						// close init(_context); method in rule declaration
						jc.pw.println("\t}");
						children[i].jjtAccept(visitor, data);
				}else{
						children[i].jjtAccept(visitor, data);
				}

        if (jc != null) {
          if (jc.active) {
							if (children[i] instanceof X_AlgorithmHead) {


		if (!(jc.in_function || jc.in_procedure)) {
			if (!jc.declare_rule.equals("")) {
				jc.pw.println(jc.declare_rule);
				jc.declare_rule = "";
			}
		}

								if (jc.entity == global_rule) {
									// init(_context) method in RULE
									String parameters = "SdaiContext _context";
									String signature = "\tpublic void init(" + parameters + ") throws SdaiException {\n\t\tif (_already_initialized) return;\n\t\t_already_initialized = true;\n\t\t";
									jc.pw.println("// global rule: " + global_rule.getName(null));
									jc.pw.println(signature);
		      				for (int j = 0; j < jc.init_rule.size(); j++) {
  			    				jc.pw.println("\t\t" + (String)jc.init_rule.elementAt(j));
      						}
								}
							}
          }
        }
      }
    }

    return data;
  }

}
