Hi again,

the method 

        org.argouml.model.mdr.ModelManagementHelperMDRImpl
                .getElement(List<String> path, Object namespace) 

does not work if the root of path starts at the same level as the namespace.
For example, if namespace, is an object 'ns2', whose full path as obtained 
with getPathList() is [model1, ns1, ns2], and path is [ns2, ns3, c1], the
method does not find the model element 'c1' (assuming it exists). 
It will find elements when the root of path is a direct children (one 
level below) the namespace.

I think it would be useful to support the example above. It would enable 
searching elements in models (top-level namespaces) providing a full path 
obtained with getPathList(). Can we support that?

If yes, it is enough to start searching path at index 1 (instead of 0) if
there is a match:

        int i = path.get(0).equals(((Namespace) root).getName()) ? 1 : 0;

Additionally, I believe that the checking of root can be put outside the 
cycle, because getOwnedElement() will never return a null element (right?)
and only namespaces are compared (except for the last name in path).

The complete code is:

    public Object getElement(List<String> path, Object theRootNamespace) {
        ModelElement root = (ModelElement) theRootNamespace;

        if (root == null || !(root instanceof Namespace)) {
            return null;
        }
        // if the toplevel path matches namespace, increase path start
        int i = path.get(0).equals(((Namespace) root).getName()) ? 1 : 0; 

        for (; i < path.size(); i++) {
            String name = path.get(i);
            boolean found = false;
            for (ModelElement me : ((Namespace) root).getOwnedElement()) {
                if (i < path.size() - 1 && !(me instanceof Namespace)) {
                    continue;
                }
                if (name.equals(me.getName())) {
                    root = me;
                    found = true;
                    break;
                }
            }
            if (!found) {
                return null;
            }
        }
        return root;
    }

Finally, note that if we call the method with a full path to be searched in 
a namespace that is not a model (top level namespace) the method still won't

work. Is there any interest in handling these cases?
If so I can post a first implementation immediately. It is slower, but
handles other situations like 'partial paths'.

Regards,
Sergio Lopes.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to