public static void defineConstructor(IndentWriter writer, IXMLElement constructor) throws IOException { assert (DebugMsg.debugMsg("ClassWriter", "Begin ClassWriter::defineConstructor")); IXMLElement klass = constructor.getParent(); SharedWriter.generateFileLocation(writer, klass); if (klass == null) { throw new RuntimeException("missing class for constructor"); } String longname = XMLUtil.nameOf(klass); writer.write("void " + longname + "::"); generateSignature(writer, constructor); writer.write(" {\n"); writer.indent(); // If the constructor has a call to the super class, invoke it. assert (DebugMsg.debugMsg( "ClassWriter", "ClassWriter::defineConstructor - getting children named 'super'")); Vector superCalls = constructor.getChildrenNamed("super"); assert (DebugMsg.debugMsg( "ClassWriter", "ClassWriter::defineConstructor - " + superCalls.size() + " children named retrieved")); if (!superCalls.isEmpty()) { assert (DebugMsg.debugMsg("ClassWriter", "ClassWriter::defineConstructor - calling super")); if (superCalls.size() > 1) writer.write("!ERROR: AT MOST ONE CALL TO SUPER ALLOWED\n"); IXMLElement superCall = (IXMLElement) superCalls.elementAt(0); String superClass = ModelAccessor.getParentClassName(klass); String superCppClass = ModelAccessor.getCppClass(superClass); writer.write(superCppClass + "::constructor("); String comma = ""; Enumeration arguments = superCall.enumerateChildren(); while (arguments.hasMoreElements()) { IXMLElement argument = (IXMLElement) arguments.nextElement(); String value = ModelAccessor.getValue(argument); if (argument.getName().equals("value") && XMLUtil.getAttribute(argument, "type").equals("string")) writer.write(comma + "\"" + value + "\""); else writer.write(comma + value); comma = ", "; } writer.write(");\n"); } Set allocatedMemberVariables = new HashSet(); // Store names as we go for reference /* Now capture names of constructor arguments */ Set constructorArguments = new HashSet(); // Store for names of constructor arguments { Vector args = constructor.getChildrenNamed("arg"); assert (DebugMsg.debugMsg( "ClassWriter", "ClassWriter::defineConstructor - getting " + args.size() + " arguments")); for (int i = 0; i < args.size(); i++) { IXMLElement arg = (IXMLElement) args.elementAt(i); String argName = XMLUtil.getAttribute(arg, "name"); constructorArguments.add(argName); } } Vector constructorAssignments = constructor.getChildrenNamed("assign"); Vector classVariables = klass.getChildrenNamed("var"); // Use the set below to track when an assignment is being made more than once for same variable Set assignmentsMade = new HashSet(); for (int i = 0; i < constructorAssignments.size(); i++) { IXMLElement element = (IXMLElement) constructorAssignments.elementAt(i); String target = XMLUtil.getAttribute(element, "name"); if (assignmentsMade.contains(target)) { writer.write("!ERROR: Duplicate assignment for " + target + "\n"); } else { assignmentsMade.add(target); // String type = getVariableType(classVariables, target); // trust in the parser, for it will assign the correct types String type = XMLUtil.getAttribute(element, "type"); IXMLElement sourceElement = element.getChildAtIndex(0); if (sourceElement.getName().equals("new")) { // Handle object allocation assert (DebugMsg.debugMsg( "ClassWriter", "ClassWriter::defineConstructor - allocating object for " + target)); String sourceType = XMLUtil.typeOf(sourceElement); writer.write(target + " = addVariable("); writer.write( sourceType + "Domain((new " + sourceType + "(m_id, \"" + target + "\"))->getId(), \"" + sourceType + "\")"); writer.write(", " + makeObjectVariableNameString(target) + ");\n"); writer.write( "Id<" + type + ">(singleton(" + target + "))->constructor(" + buildArguments( classVariables, constructorArguments, allocatedMemberVariables, sourceElement) + ");\n"); // Invoke initialization writer.write( "Id<" + type + ">(singleton(" + target + "))->handleDefaults();\n"); // Default variable setup } else { // Handle variable allocation String value = ModelAccessor.getValue(sourceElement); if (sourceElement.getName().equals("id") && type.equals(NddlXmlStrings.x_string)) value = XMLUtil.escapeQuotes(value); else if (sourceElement.getName().equals(NddlXmlStrings.x_symbol) || XMLUtil.getAttribute(sourceElement, "type").equals(NddlXmlStrings.x_string)) value = "LabelStr(\"" + XMLUtil.escapeQuotes(value) + "\")"; else if (ModelAccessor.isNumericPrimitive(type) && !type.equals(NddlXmlStrings.x_boolean)) { // Set both bounds to singleton value value = value + ", " + value; } writer.write(target + " = addVariable("); if (allocatedMemberVariables.contains( value)) // If we are assigning one member to another, we obtain the base domain for it writer.write(value + "->baseDomain()"); else writer.write(ModelAccessor.getDomain(type) + "(" + value + ", \"" + type + "\")"); writer.write(", " + makeObjectVariableNameString(target) + ");\n"); } // Add member to the set allocatedMemberVariables.add(target); } } writer.unindent(); writer.write("}\n"); assert (DebugMsg.debugMsg("ClassWriter", "End ClassWriter::defineConstructor")); }
public static void generateFactory(IndentWriter writer, IXMLElement constructor) throws IOException { IXMLElement klass = constructor.getParent(); SharedWriter.generateFileLocation(writer, klass); if (klass == null) throw new RuntimeException("missing class for factory"); String longname = XMLUtil.nameOf(klass) + "Factory" + s_factoryCounter++; writer.write("class " + longname + ": public ObjectFactory {\n"); writer.write("public:\n"); writer.indent(); writer.write(longname + "(const LabelStr& name): ObjectFactory(name){}\n"); writer.unindent(); writer.write("private:\n"); writer.indent(); writer.write("ObjectId createInstance(const PlanDatabaseId& planDb,\n"); writer.write(" const LabelStr& objectType, \n"); writer.write(" const LabelStr& objectName,\n"); writer.write( " const std::vector<const AbstractDomain*>& arguments) const {\n"); writer.indent(); Vector constructorAssignments = constructor.getChildrenNamed("arg"); String klassName = XMLUtil.nameOf(klass); String constructorArguments = ""; String comma = ""; // Do some type checking - at least the size must match! writer.write("check_error(arguments.size() == " + constructorAssignments.size() + ");\n"); // Process arguments, defining local variables and giving them initial values for (int i = 0; i < constructorAssignments.size(); i++) { IXMLElement element = (IXMLElement) constructorAssignments.elementAt(i); String target = XMLUtil.getAttribute(element, "name"); String type = XMLUtil.getAttribute(element, "type"); writer.write("check_error(AbstractDomain::canBeCompared(*arguments[" + i + "], \n"); writer.write( " planDb->getConstraintEngine()->getCESchema()->baseDomain(\"" + type + "\")), \n"); writer.write( " \"Cannot convert \" + arguments[" + i + "]->getTypeName().toString() + \" to " + type + "\");\n"); writer.write("check_error(arguments[" + i + "]->isSingleton());\n"); String localVarType = makeArgumentType( type); // Some conversion may be required for Id's or strings or enumerations // Declare local variable for current argument writer.write( localVarType + " " + target + "((" + localVarType + ")arguments[" + i + "]->getSingletonValue());\n\n"); constructorArguments = constructorArguments + comma + target; comma = ", "; } // Now do the declaration and allocation of the instance writer.write( klassName + "Id instance = (new " + klassName + "(planDb, objectType, objectName))->getId();\n"); writer.write("instance->constructor(" + constructorArguments + ");\n"); writer.write("instance->handleDefaults();\n"); writer.write("return instance;\n"); writer.unindent(); writer.write("}\n"); writer.unindent(); writer.write("};\n"); // Generate all appropriate registration informationLabelStr Vector factoryNames = makeFactoryNames(constructor); for (int i = 0; i < factoryNames.size(); i++) { String factoryName = (String) factoryNames.elementAt(i); SchemaWriter.addFactory( "REGISTER_OBJECT_FACTORY(id," + longname + ", " + factoryName + ");\n"); } }
/** * The pattern we have is to generate the schema through an implementation of thie schema() * function which will be invoked by the client. */ public static void generate(IndentWriter writer) throws IOException { writer.write("namespace NDDL {\n"); writer.indent(); // Implement expected initialization hooks writer.write("// Boot-strap code to initialize schema\n"); writer.write( "extern \"C\" SchemaId loadSchema(const SchemaId& schema,const RuleSchemaId& ruleSchema)\n{\n"); writer.indent(); String modelName = ModelAccessor.getModelName(); if (modelName == null) throw new RuntimeException("Failed to set model name. Bug in NddlCompiler"); writer.write("SchemaId id = schema;\n"); // Register Constraints writer.write("// Register Constraints\n"); for (Iterator it = s_constraintRegistrations.iterator(); it.hasNext(); ) { String constraint = (String) it.next(); writer.write(constraint + ";\n"); } writer.write("// Invoke commands to populate schema with type definitions\n"); // Iterate over all type definition commands for (int i = 0; i < s_objectTypeCommands.size(); i++) { writer.write("id->" + s_objectTypeCommands.elementAt(i) + ";\n"); } // Iterate over all type definition commands for (int i = 0; i < s_commands.size(); i++) { writer.write("id->" + s_commands.elementAt(i) + ";\n"); } // Force allocation of type factories. This should do a mapping for external names // to internal writer.write("// Force allocation of model specific type factories\n"); // Allocate Factories writer.write("// Allocate factories\n"); for (Iterator it = s_factoryAllocations.iterator(); it.hasNext(); ) { String command = (String) it.next(); writer.write(command); } // Iterate over all rules and allocate singletons - this registers rules writer.write("// Allocate rules\n"); Set ruleNames = RuleWriter.getRules(); for (Iterator it = ruleNames.iterator(); it.hasNext(); ) { String ruleName = (String) it.next(); writer.write("ruleSchema->registerRule((new " + ruleName + "())->getId());\n"); } writer.write("return id;\n"); writer.unindent(); writer.write("}\n\n"); writer.unindent(); writer.write("}\n"); }