[Mobies-commit] [commit] r4273 - in UDM/trunk: lib src/Udm/PythonAPIGen src/UdmPython tests/test_UdmPythonDS
endre at redhat3.isis.vanderbilt.edu
endre at redhat3.isis.vanderbilt.edu
Mon Mar 10 01:31:04 CDT 2014
Author: endre
Date: Mon Mar 10 01:31:04 2014
New Revision: 4273
Log:
Python Domain Specific API: parent() functions considering compositionparentroles as well.
Modified:
UDM/trunk/lib/UdmPython.py
UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp
UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.h
UDM/trunk/src/UdmPython/UdmPython.cpp
UDM/trunk/tests/test_UdmPythonDS/test.py
Modified: UDM/trunk/lib/UdmPython.py
==============================================================================
--- UDM/trunk/lib/UdmPython.py Sat Mar 8 02:21:14 2014 (r4272)
+++ UDM/trunk/lib/UdmPython.py Mon Mar 10 01:31:04 2014 (r4273)
@@ -5,9 +5,6 @@
return indent
class UdmPython(object):
- @property
- def parent(self):
- return UdmPython(self.impl.parent)
def __init__(self, impl):
if isinstance(impl, UdmPython):
@@ -29,8 +26,16 @@
def _type(self):
import Uml
return Uml.Class.cast(self.impl.type)
-
- def children(self,child_role=None, child_type=None):
+ def _get_parent(self, parent_role=None):
+ #parent_role could be either Udm.Object or UdmPython (or descendants: it is expected to be Uml.CompositionChildRole)
+ parent_role_impl = None
+ if isinstance(parent_role, UdmPython):
+ parent_role_impl = parent_role.impl
+ else:
+ parent_role_impl = parent_role
+ return UdmPython(self.impl.getParent(parent_role_impl))
+
+ def _get_children(self,child_role=None, child_type=None):
#child_type could be either Udm.Object or UdmPython (or descendants: it is expected to be Uml.Class)
#child_role could be either Udm.Object or UdmPython (or descendants: it is expected to be Uml.CompositionChildRole)
#if role is given, return only those children that have that role (ignore kind)
Modified: UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp
==============================================================================
--- UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp Sat Mar 8 02:21:14 2014 (r4272)
+++ UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.cpp Mon Mar 10 01:31:04 2014 (r4273)
@@ -259,10 +259,7 @@
m_output << "class " << cls.name() << is.getAncestorList(cls) << ":" << endl;
m_output << "\t\"\"\"Generated\"\"\"" << endl;
- m_output << "\t at property" << endl;
- m_output << "\tdef parent(self):" << endl;
- m_output << "\t\tparent_o = super(" << cls.name() << ", self).parent" << endl;
- m_output << "\t\treturn globals()[parent_o._type().name](parent_o) " << endl;
+
generateAttributes(cls);
@@ -276,6 +273,7 @@
generateChildrenAccess(cls);
generateAssociations(cls);
+ generateParentAccess(cls);
}
void PythonAPIGen::generateAttributes(::Uml::Class &cls)
@@ -302,6 +300,49 @@
}
}
+void PythonAPIGen::generateParentAccess(::Uml::Class &c)
+{
+ string cl_name = c.name();
+ set < ::Uml::CompositionChildRole> parents = c.childRoles();
+ for( set< ::Uml::CompositionChildRole>::iterator i = parents.begin(); i != parents.end(); i++)
+ {
+ ::Uml::CompositionParentRole the_other = Uml::theOther(*i);
+ ::Uml::Class parent = the_other.target();
+
+ string rel_name = ::Uml::MakeRoleName(the_other);
+ string parent_name = UmlClassCPPName(parent);
+
+ if (the_other.isNavigable())
+ {
+ m_output << "\tdef " << rel_name << "(self):" <<endl;
+ m_output << "\t\t# return parent contained via rolename: " << the_other.name() << ", rel_name: " << rel_name << endl;
+ m_output << "\t\tparent = self._get_parent( "<< cl_name << ".meta_" << rel_name <<")" << endl;
+ m_output << "\t\t" << "return globals()[parent._type().name](parent)" << endl;
+
+ }
+ bool parent_defined = false;
+ set< ::Uml::CompositionParentRole> parroles = Uml::AncestorCompositionPeerParentRoles(c);
+ for( set< ::Uml::CompositionParentRole>::iterator ri = parroles.begin(); ri != parroles.end(); ri++)
+ {
+
+ if(string(ri->name()) == "parent")
+ {
+ parent_defined = true;
+ break;
+ }
+ }
+
+ if (!parent_defined)
+ {
+ m_output << "\tdef parent(self):" <<endl;
+ m_output << "\t\t# return generic parent " << endl;
+ m_output << "\t\tparent = self._get_parent()" << endl;
+ m_output << "\t\t" << "return globals()[parent._type().name](parent)" << endl;
+ }
+ }
+
+}
+
void PythonAPIGen::generateChildrenAccess(::Uml::Class &cls)
{
@@ -326,7 +367,7 @@
if(the_other.max() == 1)
{
m_output << "\t\t# return the single child contained via rolename: " << the_other.name() << ", rel_name: " << rel_name << endl;
- m_output << "\t\tchilds = self.children(child_role=" << cl_name << ".meta_" << rel_name <<")" << endl;
+ m_output << "\t\tchilds = self._get_children(child_role=" << cl_name << ".meta_" << rel_name <<")" << endl;
m_output << "\t\t" << "if len(childs) > 0:" << endl;
m_output << "\t\t\t" << "return globals()[childs[0]._type().name](childs[0])" << endl;
m_output << "\t\t" << "else: " << endl;
@@ -336,7 +377,7 @@
else
{
m_output << "\t\t# return the children contained via rolename: " << the_other.name() << ", rel_name: " << rel_name << endl;
- m_output << "\t\tchilds = self.children(child_role=" << cl_name << ".meta_" << rel_name <<")" << endl;
+ m_output << "\t\tchilds = self._get_children(child_role=" << cl_name << ".meta_" << rel_name <<")" << endl;
m_output << "\t\tlist = []" << endl;
m_output << "\t\t" << "for i in childs:" << endl;
m_output << "\t\t\t" << "list.append(globals()[i._type().name](i))" << endl;
@@ -362,7 +403,7 @@
m_output << "\tdef " << kind_children_name << "_kind_children(self):" << endl;
m_output << "\t\t# return the children of type: " << kind_children_name << endl;
- m_output << "\t\tchilds = self.children(child_type=" << kind_children_name << ".Meta)" << endl;
+ m_output << "\t\tchilds = self._get_children(child_type=" << kind_children_name << ".Meta)" << endl;
m_output << "\t\tlist = []" << endl;
m_output << "\t\t" << "for i in childs:" << endl;
m_output << "\t\t\t" << "list.append(globals()[i._type().name](i))" << endl;
Modified: UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.h
==============================================================================
--- UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.h Sat Mar 8 02:21:14 2014 (r4272)
+++ UDM/trunk/src/Udm/PythonAPIGen/PythonAPIGen.h Mon Mar 10 01:31:04 2014 (r4273)
@@ -36,7 +36,7 @@
void generateAttributes(::Uml::Class &cls);
void generateChildrenAccess(::Uml::Class &cls);
void generateAssociations(::Uml::Class &cls);
-
+ void generateParentAccess(::Uml::Class &cls);
private:
//! The UDM UML diagram.
const ::Uml::Diagram & m_diagram;
Modified: UDM/trunk/src/UdmPython/UdmPython.cpp
==============================================================================
--- UDM/trunk/src/UdmPython/UdmPython.cpp Sat Mar 8 02:21:14 2014 (r4272)
+++ UDM/trunk/src/UdmPython/UdmPython.cpp Mon Mar 10 01:31:04 2014 (r4273)
@@ -417,6 +417,19 @@
}
+
+Udm::Object Object_getParent(Udm::Object& self, Udm::Object& prole)
+{
+ // this functions works in exactly the same way as ObjectImpl::getParent()
+ // get the parent object or null if object has no parent
+ // if role is not NULLROLE, return parent only if the role actually applies
+
+ if (!self) throw std::runtime_error(std::string("Object is null in getParent()"));
+ Udm::Object ret =self.__impl()->getParent(Uml::CompositionParentRole::Cast(prole));
+ return ret;
+
+}
+
object Object_getAssociation(Udm::Object& self, Udm::Object& arole, object _mode)
{
// arole must be non-null.
@@ -836,6 +849,7 @@
.def("_children", Object_children)
.def("getChildren", Object_getChildren)
.def("getAssociation", Object_getAssociation)
+ .def("getParent", Object_getParent)
.def("_adjacent", Object_adjacent)
.def("get_attribute",&Object_attr_by_uml_attr_as_udm_object)
.def("set_attribute",&Object_set_attr_by_uml_attr_as_udm_object)
Modified: UDM/trunk/tests/test_UdmPythonDS/test.py
==============================================================================
--- UDM/trunk/tests/test_UdmPythonDS/test.py Sat Mar 8 02:21:14 2014 (r4272)
+++ UDM/trunk/tests/test_UdmPythonDS/test.py Mon Mar 10 01:31:04 2014 (r4273)
@@ -5,6 +5,8 @@
import Uml # generated UML classes
import LampDiagram # generated Domain Specific API
+import unittest # testing
+
@@ -33,7 +35,7 @@
obj.setIndent(indent_tabs)
print obj
- generic_children = obj.children()
+ generic_children = obj._get_children()
if generic_children:
print UdmPython.indent(indent_tabs) + "I have found the following children at this level: %d" % (len(generic_children))
for child in generic_children:
More information about the Mobies-commit
mailing list