UdmComparator::UmlAssociationRoleSet UdmComparator::getAllUmlAssociationRoles( Uml::Class umlClass ) { UmlAssociationRoleSet umlAssociationRoleSet; if ( umlClass.stereotype() == "Connection" ) { Uml::Association umlAssociation = umlClass.association(); if ( umlAssociation != Udm::null ) { umlAssociationRoleSet = umlAssociation.AssociationRole_kind_children(); } } else { umlAssociationRoleSet = umlClass.associationRoles(); } UmlClassSet umlBaseClassSet = umlClass.baseTypes(); for( UmlClassSet::iterator ucsItr = umlBaseClassSet.begin() ; ucsItr != umlBaseClassSet.end() ; ++ucsItr ) { UmlAssociationRoleSet baseClassUmlAssociationRoleSet = getAllUmlAssociationRoles( *ucsItr ); umlAssociationRoleSet.insert( baseClassUmlAssociationRoleSet.begin(), baseClassUmlAssociationRoleSet.end() ); } return umlAssociationRoleSet; }
void GenerateCSApi(const Uml::Diagram &diagram, ostream &output, string fname) { string hname = diagram.name(); string::iterator i; for(i = hname.begin(); i != hname.end(); i++) { if(!isalnum(*i)) *i = '_'; } output << "// CS API file " << fname << ".cs generated from diagram " << (string)diagram.name() << endl; output << "// generated on " << GetTime().c_str() << endl << endl; output << "using System;" << endl; output << "using System.Collections;" << endl <<endl << endl; output << "namespace " << hname << endl << "{" << endl << endl; set<Uml::Class> classes = diagram.classes(); set<Uml::Class>::iterator c; for ( c = classes.begin(); c!= classes.end(); c++) { Uml::Class cl = *c; set<Uml::Class> bases = cl.baseTypes(); if (bases.size() > 1) throw udm_exception("C# UDM does not support multiple inheritence!"); bool derived = (bases.size() > 0); //class declaration, inheritence if (derived) { Uml::Class base = *(bases.begin()); output <<"\tpublic class " << (string)cl.name() << " : " << (string)base.name() << endl; } else output <<"\tpublic class " << (string) cl.name() << " : " << "Udm.Object" << endl; string cn = (string)cl.name(); //static meta field output << "\t{" << endl;; if (derived) output <<"\t\tpublic static new Uml.Class meta;"<<endl; else output <<"\t\tpublic static Uml.Class meta;"<<endl; //static NULL field output <<"\t\tpublic static readonly new " << cn << " NULL = new " << cn << "();" <<endl; output << endl; //constructors output <<"\t\tpublic " << cn << "() {}" << endl; output <<"\t\tpublic " << cn << "(Udm.ObjectImpl impl) : base(impl) {}" <<endl; output <<"\t\tpublic " << cn << "(" << cn <<" master) : base(master) {}" <<endl; output << endl; //creators if (derived) output <<"\t\tpublic static new " << cn << " Create(Udm.Object parent)" << endl; else output <<"\t\tpublic static " << cn << " Create(Udm.Object parent)" << endl; output <<"\t\t{" <<endl; output <<"\t\t\treturn " << cn << ".Cast((Udm.Object.Create(ref meta, parent, Uml.CompositionChildRole.NULL)));" <<endl; output <<"\t\t}" <<endl; if (derived) output <<"\t\tpublic static new " << cn << " Create(Udm.Object parent, Uml.CompositionChildRole ccr)" << endl; else output <<"\t\tpublic static " << cn << " Create(Udm.Object parent, Uml.CompositionChildRole ccr)" << endl; output <<"\t\t{" <<endl; output <<"\t\t\treturn " << cn << ".Cast((Udm.Object.Create(ref meta, parent, ccr)));" <<endl; output <<"\t\t}" <<endl; //cast if (derived) output <<"\t\tpublic static new " << cn << " Cast(Udm.Object o)" << endl; else output <<"\t\tpublic static " << cn << " Cast(Udm.Object o)" << endl; output <<"\t\t{" <<endl; output <<"\t\t\tif (Uml.UmlExt.IsObjectDerivedFromClass(o, meta))" << endl; output <<"\t\t\t\treturn new " << cn << "(o.__impl());" << endl; output <<"\t\t\telse throw new Udm.udm_exception(\"Invalid cast!\");" << endl; output <<"\t\t}" <<endl; set<Uml::Attribute> attributes = cl.attributes(); set<Uml::Attribute>::iterator a; for ( a = attributes.begin(); a != attributes.end(); a++) { string aname = (*a).name(); string atype = (*a).type(); string cs_atype; bool multiple = ((long)(*a).max() != 0) && ((long)(*a).max() != 1); if (atype == "Boolean") cs_atype = "bool"; else if (atype == "Real") cs_atype = "double"; else if (atype == "Integer") cs_atype = "long"; else if (atype == "String") cs_atype = "string"; else throw new udm_exception(string("Invalid attribute type: ") + atype); //check for the same attribute in ascendants output << "\t\tpublic static Uml.Attribute meta_" << aname << ";"<< endl; if (!multiple) output << "\t\tpublic " << cs_atype << " " << aname << endl; else output << "\t\tpublic " << cs_atype << "[] " << aname << endl; output << "\t\t{" << endl; if (!multiple) { output << "\t\t\tget {return impl.get" << atype << "Attribute(meta_" << aname <<");}" << endl; output << "\t\t\tset {impl.set" << atype << "Attribute(meta_"<< aname <<", value);}" <<endl; } else { output << "\t\t\tget {return impl.get" << atype << "Attributes(meta_" << aname <<");}" << endl; output << "\t\t\tset {impl.set" << atype << "Attributes(meta_"<< aname <<", value);}" <<endl; } output << "\t\t}" << endl; } output << endl; //assoc. roles set<Uml::AssociationRole> aroles = cl.associationRoles(); set<Uml::AssociationRole>::iterator b_i; for (b_i = aroles.begin(); b_i != aroles.end(); b_i++) { Uml::AssociationRole arole = Uml::theOther((*b_i)); string arname = MakeRoleName(arole); bool multiple = ((arole.max() != 1) && (arole.max() != 0)); Uml::Class target = arole.target(); string tname = target.name(); Uml::Association parent = arole.parent(); Uml::Class assocClass = parent.assocClass(); if (assocClass) output << "\t\tpublic static Uml.AssociationRole meta_" << arname << ", meta_" <<arname <<"_rev ;" << endl; else output << "\t\tpublic static Uml.AssociationRole meta_" << arname << ";" << endl; if (!multiple) { output << "\t\tpublic " << tname << " " << arname << endl; output << "\t\t{" << endl; if (assocClass) { output << "\t\t\tget{ return (" << tname <<")(getPointer(meta_" << arname << ", \"" << diagram.name() << "\", Udm.UdmAssocMode.CLASSFROMTARGET));}" <<endl; output << "\t\t\tset{ setPointer(meta_" << arname << ", value, Udm.UdmAssocMode.CLASSFROMTARGET);}" <<endl; } else { output << "\t\t\tget{ return (" << tname <<")(getPointer(meta_" << arname << ", \"" << diagram.name() << "\", Udm.UdmAssocMode.TARGETFROMPEER));}" <<endl; output << "\t\t\tset{ setPointer(meta_" << arname << ", value, Udm.UdmAssocMode.TARGETFROMPEER);}" <<endl; } output << "\t\t}" << endl; } else { output << "\t\tpublic Hashtable " <<arname << endl; output << "\t\t{" << endl; if (assocClass) { output << "\t\t\tget{ return (getPointers(meta_" << arname << ", \"" << diagram.name() << "\", Udm.UdmAssocMode.CLASSFROMTARGET));}" <<endl; output << "\t\t\tset{ setPointers(meta_" << arname << ", value, Udm.UdmAssocMode.CLASSFROMTARGET);}" <<endl; } else { output << "\t\t\tget{ return (getPointers(meta_" << arname << ", \"" << diagram.name() << "\", Udm.UdmAssocMode.TARGETFROMPEER));}" <<endl; output << "\t\t\tset{ setPointers(meta_" << arname << ", value, Udm.UdmAssocMode.TARGETFROMPEER);}" <<endl; } output << "\t\t}" << endl; } } //assoc. class Uml::Association ass = cl.association(); if (ass) { set<Uml::Class>::iterator bas_i; for (bas_i = bases.begin(); bas_i != bases.end(); bas_i++) { Uml::Association new_ass = bas_i->association(); if (new_ass) throw udm_exception("An association class has two associations?"); } set<Uml::AssociationRole> ass_roles = ass.roles(); set<Uml::AssociationRole>::iterator ar_i; for (ar_i = ass_roles.begin(); ar_i != ass_roles.end(); ar_i++) { Uml::AssociationRole arole = *ar_i; Uml::Class target = arole.target(); string tname = target.name(); string arname = MakeRoleName(arole); output << "\t\tpublic static Uml.AssociationRole meta_" << arname << "_end_;" << endl; output << "\t\tpublic " << tname << " " << arname << endl; output << "\t\t{" << endl; output << "\t\t\tget{ return (" << tname <<")(getPointer(meta_" << arname << "_end_, \"" << diagram.name() << "\", Udm.UdmAssocMode.TARGETFROMCLASS));}" <<endl; output << "\t\t\tset{ setPointer(meta_" << arname << "_end_, value, Udm.UdmAssocMode.TARGETFROMCLASS);}" <<endl; output << "\t\t}" << endl; output << endl; } } output << endl; set<Uml::Class> childrenkinds; //parentroles set<Uml::CompositionParentRole> cprs = cl.parentRoles(); set<Uml::CompositionParentRole>::iterator c_i; for (c_i = cprs.begin(); c_i != cprs.end(); c_i++) { Uml::CompositionChildRole ccr = Uml::theOther(*c_i); string ccr_name = MakeRoleName(ccr); //ccr.name(); string tname = ((Uml::Class)ccr.target()).name(); childrenkinds.insert((Uml::Class)ccr.target()); bool multiple = ((ccr.max() != 0 ) && (ccr.max() != 1 )); output << "\t\tpublic static Uml.CompositionChildRole meta_" << ccr_name << ";" <<endl; if (!multiple) { output << "\t\tpublic " << tname << " " << ccr_name << endl; output << "\t\t{" << endl; output << "\t\t\tget{ return (" << tname <<")(getChild(meta_" << ccr_name << ", Uml.Class.NULL, \"" << diagram.name() << "\"));}" <<endl; output << "\t\t\tset{ setChild(meta_" << ccr_name << ", value);}" <<endl; output << "\t\t}" << endl; } else { output << "\t\tpublic Hashtable " <<ccr_name << endl; output << "\t\t{" << endl; output << "\t\t\tget{ return (getChildren(meta_" << ccr_name << ", Uml.Class.NULL, \"" << diagram.name() << "\"));}" <<endl; output << "\t\t\tset{ setChildren(meta_" << ccr_name << ", value);}" <<endl; output << "\t\t}" << endl; } } //__kind_children calls set<Uml::Class>::iterator f_i, g_i; for (f_i = classes.begin(); f_i != classes.end(); f_i++) { for (g_i = childrenkinds.begin(); g_i != childrenkinds.end(); g_i++) { if (Uml::IsDerivedFrom(*g_i, *f_i) || Uml::IsDerivedFrom(*f_i, *g_i)) { string kindname = f_i->name(); output << endl; output << "\t\tpublic Hashtable " << kindname << "_kind_children" << endl; output << "\t\t{" << endl; output << "\t\t\tget{ return (getChildren(Uml.CompositionChildRole.NULL, " << kindname << ".meta, \"" << diagram.name() << "\"));}" <<endl; //no setter for kind_children calls output << "\t\t}" << endl; break; } } } output << endl; //childroles set<Uml::CompositionChildRole> ccrs = cl.childRoles(); set<Uml::CompositionChildRole>::iterator d_i; bool was_parent = false; for (d_i = ccrs.begin(); d_i != ccrs.end(); d_i++) { Uml::CompositionParentRole cpr = Uml::theOther(*d_i); string cpr_name = MakeRoleName(cpr); string tname = ((Uml::Class)cpr.target()).name(); bool new_required = false; new_required = derived && (cpr_name == "parent"); if (!was_parent && cpr_name == "parent") was_parent = true; output << "\t\tpublic static Uml.CompositionParentRole meta_" << cpr_name << ";" <<endl; if (new_required) output << "\t\tpublic new " << tname << " " << cpr_name << endl; else output << "\t\tpublic " << tname << " " << cpr_name << endl; output << "\t\t{" << endl; output << "\t\t\tget{ return (" << tname <<")(getParent(meta_" << cpr_name << ", \"" << diagram.name() << "\"));}" <<endl; output << "\t\t\tset{ setParent(meta_" << cpr_name << ", value);}" <<endl; output << "\t\t}" << endl; } if (!was_parent) { if (derived) output << "\t\tpublic new Udm.Object parent" <<endl; else output << "\t\tpublic Udm.Object parent" <<endl; output << "\t\t{" << endl; output << "\t\t\tget{ return getParent(Uml.CompositionParentRole.NULL, \"" << diagram.name() <<"\");}" <<endl; output << "\t\t\tset{ setParent(Uml.CompositionParentRole.NULL, value);}" << endl; output << "\t\t}" << endl; } output << "\t}" << endl;//end of class } }