[Mobies-commit] [commit] r4085 - UDM/trunk/src/Udm/JavaAPIGen

ksmyth at redhat1.isis.vanderbilt.edu ksmyth at redhat1.isis.vanderbilt.edu
Mon Aug 13 09:19:15 CDT 2012


Author: ksmyth
Date: Mon Aug 13 09:19:14 2012
New Revision: 4085

Log:
JavaAPIGen: if a class is inherited multiple times, generate implementation only once. A class may be contained via inheritance many times, generate getters once. Fully-qualify java.lang.String, so paradigms may define a String class. Fix Java namespace error with multiple inheritance across Udm namespaces. Fixes CyPhyML generation.

Modified:
   UDM/trunk/src/Udm/JavaAPIGen/ClassGen.cpp
   UDM/trunk/src/Udm/JavaAPIGen/FactoryGen.cpp
   UDM/trunk/src/Udm/JavaAPIGen/Utils.cpp
   UDM/trunk/src/Udm/JavaAPIGen/UtilsGen.cpp

Modified: UDM/trunk/src/Udm/JavaAPIGen/ClassGen.cpp
==============================================================================
--- UDM/trunk/src/Udm/JavaAPIGen/ClassGen.cpp	Mon Aug 13 09:18:55 2012	(r4084)
+++ UDM/trunk/src/Udm/JavaAPIGen/ClassGen.cpp	Mon Aug 13 09:19:14 2012	(r4085)
@@ -7,6 +7,9 @@
 #include <cstring>
 
 #include <sstream>
+#include <algorithm>
+#include <deque>
+#include <iterator>
 
 //! Constructor.
 /*!
@@ -52,23 +55,40 @@
   cg.accessAttributes( );
   cg.associations( );
 
-  set< ::Uml::Class> bases = m_cl.baseTypes();
-  set< ::Uml::Class>::iterator basesIt = bases.begin();
-  for (; basesIt != bases.end(); basesIt++)
+  set<Uml::Class> bases = m_cl.baseTypes();
+  set<Uml::Class> inheritedBases;
+
+  deque< ::Uml::Class> basesDeque;
+  basesDeque.push_back(m_cl);
+  while (!basesDeque.empty())
   {
-	  if (basesIt == bases.begin())
-		  // These methods are defined via inheritance
-		  continue;
+	  Uml::Class base = basesDeque.front();
+	  basesDeque.pop_front();
+	  std::set<Uml::Class> basesBases = base.baseTypes();
+	  std::copy(basesBases.begin(), basesBases.end(), std::back_inserter(basesDeque));
+	  std::copy(basesBases.begin(), basesBases.end(), std::inserter(inheritedBases, inheritedBases.begin()));
+  }
+
+  Uml::Class firstBase = *bases.begin();
+  set<Uml::Class> firstBaseAncestors = Uml::AncestorClasses(firstBase);
+  vector<Uml::Class> baseClassesToGenerate;
+  baseClassesToGenerate.resize(inheritedBases.size() + 1);
+  set_difference(inheritedBases.begin(), inheritedBases.end(), firstBaseAncestors.begin(), firstBaseAncestors.end(), baseClassesToGenerate.begin());
 
+  vector<Uml::Class>::iterator basesIt = baseClassesToGenerate.begin();
+  for (; *basesIt; basesIt++)
+  {
 	  // TODO: lowercase this?
-    string pck = Utils::getPackageSignature( *basesIt, m_ns_path, m_package_name );
+    string pck = Utils::getPackageSignature(*basesIt, m_ns_path, m_package_name);
+	pck = pck.substr(0, pck.length() - 1);
+	pck = Utils::toPackageName(pck);
 	std::stringstream ioutput; // The method declarations for the interface will be in a base interface, so we ignore them
+
 	CG<stringstream> cg(*basesIt, pck, m_output, ioutput, basesIt->name(), m_ns_path);
 	m_output << " // Methods for " << basesIt->name() << endl;
 	cg.accessChildren();
 	cg.accessAttributes();
 	cg.associations();
-    m_base_name = pck + (string)bases.begin()->name();
   }
 
 
@@ -140,8 +160,6 @@
   // generate the signature of the class
   m_output << "public ";
 
-  if ( m_cl.isAbstract() )
-    m_output << "abstract ";
 
   m_output << "class " << m_cl_name << " extends " << m_base_name << " implements I" << m_cl_name << endl;
   m_ioutput << "public interface I" << m_cl_name << " ";
@@ -163,10 +181,10 @@
 
   if ( !m_cl.isAbstract() )
   {
-    m_output << "\tpublic static final String META_TYPE = \"" << m_cl_name << "\";" << endl;
-    m_output << "\tpublic static final String META_TYPE_NS = \"" << m_ns_path_orig << "\";" << endl;
-    m_ioutput << "\tpublic static final String META_TYPE = \"" << m_cl_name << "\";" << endl;
-    m_ioutput << "\tpublic static final String META_TYPE_NS = \"" << m_ns_path_orig << "\";" << endl;
+    m_output << "\tpublic static final java.lang.String META_TYPE = \"" << m_cl_name << "\";" << endl;
+    m_output << "\tpublic static final java.lang.String META_TYPE_NS = \"" << m_ns_path_orig << "\";" << endl;
+    m_ioutput << "\tpublic static final java.lang.String META_TYPE = \"" << m_cl_name << "\";" << endl;
+    m_ioutput << "\tpublic static final java.lang.String META_TYPE_NS = \"" << m_ns_path_orig << "\";" << endl;
     m_output << "\tprivate static UdmPseudoObject metaClass;" << endl;
     m_output << endl;
   }
@@ -304,16 +322,22 @@
 {
   m_output << "\t/* Accessing children */" << endl << endl;
 
+  set<Uml::Class> allContainedClasses;
+
   // set of contained classes
   set< ::Uml::Class> conts = Uml::ContainedClasses( m_cl );
   for ( set< ::Uml::Class>::iterator c_i = conts.begin(); c_i != conts.end(); c_i++ )
   {
     // the descendants of the contained class
     set < ::Uml::Class> conts_der = Uml::DescendantClasses( *c_i );
-    for ( set< ::Uml::Class>::iterator c_d_i = conts_der.begin(); c_d_i != conts_der.end(); c_d_i++ )
+	std::copy(conts_der.begin(), conts_der.end(), std::inserter(allContainedClasses, allContainedClasses.begin()));
+  }
+
+  {
+    for ( set< ::Uml::Class>::iterator c_d_i = allContainedClasses.begin(); c_d_i != allContainedClasses.end(); c_d_i++ )
     {
       if ( c_d_i->isAbstract() )
-        continue;
+        continue; // FIXME: KMS this looks wrong
 
       ::Uml::Composition comp = Uml::matchChildToParent( *c_d_i, m_cl );
       string c_i_name = getInterfaceIfNeeded(*c_d_i);
@@ -424,7 +448,7 @@
             m_output << "\t/**" << endl;
             m_output << "\t *  Composition role name <code>" << ccr_name << "</code>." << endl;
             m_output << "\t */" << endl;
-            m_output << "\tpublic static final String " << Utils::getCCRString(*ccrs_i) << " = \"" << (string)ccrs_i->name() << "\";";
+            m_output << "\tpublic static final java.lang.String " << Utils::getCCRString(*ccrs_i) << " = \"" << (string)ccrs_i->name() << "\";";
             m_output << endl << endl;
 
             //getRoleName
@@ -492,8 +516,8 @@
     m_output << "\t/**" << endl;
     m_output << "\t * Attribute for <code>" << att_name << "</code>." << endl;
     m_output << "\t */" << endl;
-    m_output << "\tpublic static final String " << att_name << " = \"" << att_name << "\";" << endl;
-    m_ioutput << "\tpublic static final String " << att_name << " = \"" << att_name << "\";" << endl;
+    m_output << "\tpublic static final java.lang.String " << att_name << " = \"" << att_name << "\";" << endl;
+    m_ioutput << "\tpublic static final java.lang.String " << att_name << " = \"" << att_name << "\";" << endl;
     m_output << endl;
 
     // type of the attribute
@@ -504,12 +528,12 @@
     {
         if ( array )
         {
-          jtype = "String []";
+          jtype = "java.lang.String []";
           upo_call = "StrValues";
         }
         else
         {
-          jtype = "String";
+          jtype = "java.lang.String";
           upo_call = "StringVal";
         }
     }
@@ -617,7 +641,7 @@
       m_ioutput << "\tpublic void set" << ar_name << "(" << pkg_name << tname << " a) throws UdmException;" << endl;
       m_output << "\t{" << endl;
       m_output << "\t\tUdmPseudoObjectContainer container = new UdmPseudoObjectContainer(1);" << endl;
-      m_output << "\t\tcontainer.setAt(0, a);" << endl;
+      m_output << "\t\tcontainer.setAt(0, (UdmPseudoObject)a);" << endl;
       m_output << "\t\tsetAssociation(\"" << ar_name << "\", container, UdmHelper.TARGET_FROM_CLASS);" << endl;
       m_output << "\t}" << endl;
       m_output << endl;

Modified: UDM/trunk/src/Udm/JavaAPIGen/FactoryGen.cpp
==============================================================================
--- UDM/trunk/src/Udm/JavaAPIGen/FactoryGen.cpp	Mon Aug 13 09:18:55 2012	(r4084)
+++ UDM/trunk/src/Udm/JavaAPIGen/FactoryGen.cpp	Mon Aug 13 09:19:14 2012	(r4085)
@@ -194,7 +194,7 @@
   m_example_output << "\t */" << endl;
   m_example_output << "\tpublic void testOpenFromString_" << m_root_name << "_" << containerDescriptionFunc( ) << "() \n\t\tthrows UdmException " << endl;
   m_example_output << "\t{" << endl; 
-  m_example_output << "\t\tString xmlString = \"<...>\";" << endl;
+  m_example_output << "\t\tjava.lang.String xmlString = \"<...>\";" << endl;
   m_example_output << "\t\t// open the data network" << endl;
   m_example_output << "\t\t" << Utils::toPackageName( m_package_name ) << "." << m_root_name << "StringFactory gtf \n\t\t\t= FactoryRepository.get" << namespaceOrDiagramName( ) << m_root_name << "StringFactory();" << endl;
   m_example_output << "\t\t" << Utils::toPackageName( m_package_name ) << "."  << m_root_name << " root \n\t\t\t= gtf.open(xmlString);" << endl;
@@ -206,7 +206,7 @@
   m_example_output << "\t\tgtf.checkConstraints();" << endl;
   m_example_output << "\t\t" << endl;
   m_example_output << "\t\t// close and save the data network" << endl;
-  m_example_output << "\t\tString result = gtf.save();" << endl;
+  m_example_output << "\t\tjava.lang.String result = gtf.save();" << endl;
   m_example_output << "\t\tSystem.out.println(result);" << endl;
   m_example_output << "\t}" << endl;
 
@@ -217,7 +217,7 @@
   m_example_output << containerDescriptionDoc( ) << endl;
   m_example_output << "\t * @throws UdmException" << endl;
   m_example_output << "\t */" << endl;
-  m_example_output << "\tpublic void testOpenFromStream_" << m_root_name << "_" << containerDescriptionFunc( ) << "(InputStream in) \n\t\tthrows UdmException " << endl;
+  m_example_output << "\tpublic void testOpenFromStream_" << m_root_name << "_" << containerDescriptionFunc( ) << "(java.io.InputStream in) \n\t\tthrows UdmException " << endl;
   m_example_output << "\t{" << endl; 
   m_example_output << "\t\t// open the data network" << endl;
   m_example_output << "\t\t" << Utils::toPackageName( m_package_name ) << "." << m_root_name << "StringFactory gtf \n\t\t\t= FactoryRepository.get" << namespaceOrDiagramName( ) << m_root_name << "StringFactory();" << endl;
@@ -232,7 +232,7 @@
   m_example_output << "\t\tgtf.checkConstraints();" << endl;
   m_example_output << "\t\t" << endl;
   m_example_output << "\t\t// close and save the data network" << endl;
-  m_example_output << "\t\tInputStream result = gtf.saveAsStream();" << endl;
+  m_example_output << "\t\tjava.io.InputStream result = gtf.saveAsStream();" << endl;
   m_example_output << "\t\t" << endl;
   m_example_output << "\t}" << endl;
 }
@@ -359,10 +359,10 @@
   m_output << "public class " << m_root_name << m_type << "Factory" << endl;
   m_output << "{" << endl;
   m_output << "\t// resource information" << endl;
-  m_output << "\tprivate static final String packagePath = \"/edu/vanderbilt/isis/meta/\";" << endl;
-  m_output << "\tprivate static final String xmlMetaFile =\"" << m_inputfile << "\";" << endl;
-  m_output << "\tprivate static final String xsdMetaFile =\"" << containerDescriptionFunc( ) << ".xsd\";" << endl;
-  m_output << "\tprivate static final String metaName =\"" << m_diag_name << "\";" << endl;
+  m_output << "\tprivate static final java.lang.String packagePath = \"/edu/vanderbilt/isis/meta/\";" << endl;
+  m_output << "\tprivate static final java.lang.String xmlMetaFile =\"" << m_inputfile << "\";" << endl;
+  m_output << "\tprivate static final java.lang.String xsdMetaFile =\"" << containerDescriptionFunc( ) << ".xsd\";" << endl;
+  m_output << "\tprivate static final java.lang.String metaName =\"" << m_diag_name << "\";" << endl;
   m_output << "\t// the wrapped " << Utils::toLower( m_type ) << " factory instance" << endl;
   m_output << "\tprivate Udm" << m_type << "Factory factory;" << endl;
   m_output << endl;
@@ -465,7 +465,7 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic " << m_root_name << " create" << "(String instanceFileName) \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic " << m_root_name << " create" << "(java.lang.String instanceFileName) \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\t" << m_root_name << " root =" << endl;
   m_output << "\t\t\tnew " << m_root_name << "(" << endl;
@@ -521,7 +521,7 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic " << m_root_name << " open(String instanceFileName) \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic " << m_root_name << " open(java.lang.String instanceFileName) \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\t" << m_root_name << " root = \n\t\t\tnew " << m_root_name << "(" << endl;
   m_output << "\t\t\t\tfactory.openExistingDataNetwork(instanceFileName)" << endl;
@@ -539,7 +539,7 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic " << m_root_name << " open(InputStream xmlStream) \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic " << m_root_name << " open(java.io.InputStream xmlStream) \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\t" << m_root_name << " root = \n\t\t\tnew " << m_root_name << "(" << endl;
   m_output << "\t\t\t\tfactory.openExistingDataNetworkFromStream(xmlStream)" << endl;
@@ -561,7 +561,7 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic " << m_root_name << " open(String xmlString) \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic " << m_root_name << " open(java.lang.String xmlString) \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\t" << m_root_name << " root = \n\t\t\tnew " << m_root_name << "(" << endl;
   m_output << "\t\t\t\tfactory.openExistingDataNetwork(xmlString)" << endl;
@@ -579,7 +579,7 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic " << m_root_name << " open(InputStream xmlStream) \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic " << m_root_name << " open(java.io.InputStream xmlStream) \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\t" << m_root_name << " root = \n\t\t\tnew " << m_root_name << "(" << endl;
   m_output << "\t\t\t\tfactory.openExistingDataNetworkFromStream(xmlStream)" << endl;
@@ -625,7 +625,7 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic void saveAs(String instanceFileName) \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic void saveAs(java.lang.String instanceFileName) \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\tfactory.saveAs(instanceFileName);" << endl;
   m_output << "\t\tUdmHelper.ClearXsdStorage();" << endl;
@@ -644,10 +644,10 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic String save() \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic java.lang.String save() \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\tfactory.closeWithUpdate();" << endl;
-  m_output << "\t\tString result = factory.saveAs();" << endl;
+  m_output << "\t\tjava.lang.String result = factory.saveAs();" << endl;
   m_output << "\t\tUdmHelper.ClearXsdStorage();" << endl;
   m_output << "\t\tfactory.unloadDiagram();" << endl;
   m_output << "\t\treturn result;" << endl;
@@ -666,10 +666,10 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic InputStream saveAsStream() \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic java.io.InputStream saveAsStream() \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\tfactory.closeWithUpdate();" << endl;
-  m_output << "\t\tInputStream result = factory.saveAsStream();" << endl;
+  m_output << "\t\tjava.io.InputStream result = factory.saveAsStream();" << endl;
   m_output << "\t\tUdmHelper.ClearXsdStorage();" << endl;
   m_output << "\t\tfactory.unloadDiagram();" << endl;
   m_output << "\t\treturn result;" << endl;
@@ -687,7 +687,7 @@
   m_output << "\t * @throws  UdmException If any Udm related exception occurred" << endl;
   m_output << "\t */ " << endl;
   // function signature
-  m_output << "\tpublic String checkConstraints() \n\t\t throws UdmException" << endl;
+  m_output << "\tpublic java.lang.String checkConstraints() \n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
   m_output << "\t\treturn (factory.getDataNetwork().checkConstraints());" << endl;
   m_output << "\t}" << endl;

Modified: UDM/trunk/src/Udm/JavaAPIGen/Utils.cpp
==============================================================================
--- UDM/trunk/src/Udm/JavaAPIGen/Utils.cpp	Mon Aug 13 09:18:55 2012	(r4084)
+++ UDM/trunk/src/Udm/JavaAPIGen/Utils.cpp	Mon Aug 13 09:19:14 2012	(r4085)
@@ -85,15 +85,9 @@
 */
 string Utils::toPackageName ( const string & packagePath )
 {
-   string ret ( packagePath );
-
-   for ( unsigned int i = 0; i < ret.size(); i++ )
-   {
-      if ( ret[i] == '/' )
-         ret[i] = '.';
-   }
-
-   return ret;
+	string ret(packagePath);
+	std::replace(ret.begin(), ret.end(), '/', '.');
+	return ret;
 }
 
 //! Creates a directory structure from a package signature .
@@ -171,7 +165,7 @@
 
 //! Returns the package signature for the specific class.
 /*!
-  If the namespace path of cl differs form the namespace path specified by the 
+  If the namespace path of cl differs from the namespace path specified by the 
   parameter current_ns_path, then it returns the the abolute path 
   (package signature) of cl.
 */
@@ -180,20 +174,26 @@
   , const string & pckg_hierarcy)
 
 {
-  string ret;
-
-  ::Uml::Namespace cl_ns = cl.parent_ns();
-  string cl_ns_path = cl_ns ? cl_ns.getPath2("::", false) : "";
-  cl_ns_path = Utils::toLower(cl_ns_path);
-
-  if ( cl_ns_path != Utils::toLower(current_ns_path) )
-  {
-    string pck_name = Utils::toPackageName( pckg_hierarcy );
-    pck_name.resize( pck_name.length( ) - current_ns_path.length( ) );
-	ret = pck_name + UdmUtil::replace_delimiter(cl_ns_path, "::", ".") + ".";
-  }
-
-  return ret;
+	ASSERT(pckg_hierarcy[pckg_hierarcy.length()-1] != '/');
+	ASSERT(pckg_hierarcy[pckg_hierarcy.length()-1] != '.');
+	ASSERT(current_ns_path[current_ns_path.length()-1] != '.');
+	::Uml::Namespace cl_ns = cl.parent_ns();
+	string cl_ns_path = cl_ns ? cl_ns.getPath2(".", false) : "";
+	cl_ns_path = Utils::toLower(cl_ns_path);
+	string ret;
+	ret = Utils::toPackageName(pckg_hierarcy);
+	if (current_ns_path.length())
+	{
+		ret.resize(ret.length() - (current_ns_path.length() + 1));
+	}
+	if (cl_ns_path.length())
+	{
+		ret += ".";
+		ret += cl_ns_path;
+	}
+	ret += ".";
+	ASSERT(ret[ret.length()-1] == '.');
+	return ret;
 }
 
 //! Returns the first non-abstract ancestor of the class specified by the parameter

Modified: UDM/trunk/src/Udm/JavaAPIGen/UtilsGen.cpp
==============================================================================
--- UDM/trunk/src/Udm/JavaAPIGen/UtilsGen.cpp	Mon Aug 13 09:18:55 2012	(r4084)
+++ UDM/trunk/src/Udm/JavaAPIGen/UtilsGen.cpp	Mon Aug 13 09:19:14 2012	(r4085)
@@ -114,7 +114,7 @@
   m_output << "\tpublic static UdmPseudoObject wrapWithSubclass(UdmPseudoObject obj, Diagram diagram)";
   m_output <<  "\n\t\t throws UdmException" << endl;
   m_output << "\t{" << endl;
-  m_output << "\t\tString type = obj.getType();" << endl;
+  m_output << "\t\tjava.lang.String type = obj.getType();" << endl;
 
   for ( classes_i = classes.begin(); classes_i != classes.end(); classes_i++ )
   {


More information about the Mobies-commit mailing list