/*
 * $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/
 */

/* Generated By:JJTree: Do not edit this line. X_IdentifiedByClause.java */

package jsdai.expressCompiler;

import jsdai.lang.*;
import jsdai.SExtended_dictionary_schema.*;

public class X_IdentifiedByClause extends SimpleNode {
  public X_IdentifiedByClause(int id) {
    super(id);
  }

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


  /** Accept the visitor. **/
  public Object jjtAccept(Compiler2Visitor visitor, Object data) throws jsdai.lang.SdaiException {
    return visitor.visit(this, data);
  }

  public Object childrenAccept(Compiler2Visitor visitor, Object data)
                            throws jsdai.lang.SdaiException {
    JavaClass jc = ( JavaClass )data;
		java.io.PrintWriter pw = jc.pw;

		SdaiIterator spi = jc.asp.createIterator();
		while (spi.next()) {
			ESource_parameter sp = (ESource_parameter)jc.asp.getCurrentMemberObject(spi);
			String sp_name = sp.getName(null);
			pw.println("\t\tint src_par_id_" +sp_name +" = ((Integer)src_id_map.get(\"" +sp_name +"\")).intValue();");
		}
		//		pw.println("\t\tArrayList alist = new ArrayList();");
		pw.println("\t\tHashSet alist = new HashSet();");
		pw.println("\t\tfor (int i = 0; i < binding_inst_extent.size(); i++) {");
		pw.println("\t\t\t// Classification to equivalence classes");
    if (children != null) {
      for (int i = 0; i < children.length; ++i) {
// System.out.println("what is this: " + children[i]);
					children[i].jjtAccept(visitor, data);
			}
		}

		pw.println("\t\t\tVector eq_class = new Vector();");
		pw.println("\t\t\tfor (int j = 0; j < binding_inst_extent.size(); j++) {");

		if (!jc.single_map_call) {
			pw.println("\t\t\t\tif (i == j) {");
			pw.println("\t\t\t\t\teq_class.add(new Integer(j));");
			pw.println("\t\t\t\t\tcontinue;");
			pw.println("\t\t\t\t}");
		}
		// second pass
		jc.second_pass = true;
    if (children != null) {
      for (int i = 0; i < children.length; ++i) {
					children[i].jjtAccept(visitor, data);
			}
		}
		jc.second_pass = false;

		boolean first_id_parameter = true;
    if (children != null) {
      for (int i = 0; i < children.length; ++i) {
					if(first_id_parameter){
							pw.print("\t\t\t\tif((Value.alloc(ExpressTypes.BOOLEAN_TYPE).equal(_context, identified_by_0, identified_by_0_current).getAsBoolean())");
							first_id_parameter = false;
					}	else
							pw.print(" && (Value.alloc(ExpressTypes.BOOLEAN_TYPE).equal(_context, identified_by_" +i +", identified_by_" +i +"_current).getAsBoolean())");							
			}
		}
		pw.println("){");
		pw.println("\t\t\t\t\teq_class.add(new Integer(j));");
		pw.println("\t\t\t\t}");
		pw.println("\t\t\t}");
		pw.println("\t\t\talist.add(eq_class);");
		if (jc.single_map_call) {
			pw.println("\t\t\tbreak;");
		}
		pw.println("\t\t}");
		return data;
	}
}
