Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ComponentEntity.addComponent() doesn't copy the component #195

Open
MichaelClerx opened this issue Sep 19, 2017 · 5 comments
Open

ComponentEntity.addComponent() doesn't copy the component #195

MichaelClerx opened this issue Sep 19, 2017 · 5 comments
Labels
New feature Additional feature

Comments

@MichaelClerx
Copy link
Contributor

The docstring for ComponentEntity.addComponent(const ComponentPtr &c) says a copy of the component argument will be made

https://github.com/cellml/libcellml/blob/develop/src/api/libcellml/componententity.h#L51-L58

This doesn't seem to be the case:

    x = ComponentEntity()
    y = Component()
    z = Component()
    y.addComponent(z)
    self.assertTrue(y.containsComponent(z)) --> True!
    x.addComponent(y)
    x.removeComponent(z, True)
    self.assertTrue(y.containsComponent(z)) --> False!
@MichaelClerx
Copy link
Contributor Author

There's other methods that claim to make copies. We should check these all and add tests for this.

@MichaelClerx MichaelClerx added the New feature Additional feature label Aug 15, 2018
@hsorby
Copy link
Contributor

hsorby commented Oct 18, 2019

After having a discussion with @nickerso we think the following behaviour should be observed:

    parent = Component()
    child1 = Component()
    child2 = Component()
    child3 = Component()

    parent.addComponent(child1);
    parent.addComponent(child2);

    self.assertTrue(2 == parent.componentCount())
    self.assertTrue(0 == child1.componentCount())
    self.assertTrue(0 == child2.componentCount())

    # Expect child3 to be added no problem.
    self.assertTrue(child1.addComponent(child3))

    self.assertTrue(2 == parent.componentCount())
    self.assertTrue(1 == child1.componentCount())
    self.assertTrue(0 == child2.componentCount())

    # child3 already has a parent so cannot be added to
    # another component.
    self.assertFalse(child2.addComponent(child3)

    # Hierarchy of the model has not changed after attempting
    # to add child3 to child2.
    self.assertTrue(2 == parent.componentCount())
    self.assertTrue(1 == child1.componentCount())
    self.assertTrue(0 == child2.componentCount())

@agarny
Copy link
Contributor

agarny commented Oct 18, 2019

Hmm... doesn't the following go against what we have recently agreed on and implemented?

# child3 already has a parent so cannot be added to
# another component.
self.assertFalse(child2.addComponent(child3))

I would expect child2.addComponent(child3) to return true and therefore have:

self.assertTrue(2 == parent.componentCount())
self.assertTrue(0 == child1.componentCount())
self.assertTrue(1 == child2.componentCount())

@hsorby
Copy link
Contributor

hsorby commented Oct 18, 2019

Yes it does go against what has just been implemented. On reflection I don't like the current implementation. To me it is doing a move but we are calling it an add.

I feel it is more straight forward to add components this way. We can then add convenience methods to help with moving components or multiple additions of say an imported component.

@agarny
Copy link
Contributor

agarny commented Oct 18, 2019

Hmm... fair points. Otherwise, yes, it might be nice to have convenience methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New feature Additional feature
Projects
None yet
Development

No branches or pull requests

3 participants