Skip to content

Commit

Permalink
Add support for material property groups. (#90)
Browse files Browse the repository at this point in the history
As pointed out in issue #78, the material property groups, such as
"Basic" for the material "medium 1" in the demo model, did not show up
in the model tree as displayed by `mph.tree()`. We also could not
access those properties via the `Node.property()` method.

We fix that by also considering "property groups" (in addition to
"feature groups") as Java containers for children of a given node
when traversing the model tree.
  • Loading branch information
john-hen committed Aug 31, 2022
1 parent b2fe3e6 commit afa251d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 26 deletions.
20 changes: 18 additions & 2 deletions mph/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ def java(self):
java = parent.java
if not java:
return None
container = java if parent.is_group() else java.feature()
if parent.is_group():
container = java
elif hasattr(java, 'propertyGroup'):
container = java.propertyGroup()
else:
container = java.feature()
for tag in container.tags():
member = container.get(tag)
if name == escape(member.label()):
Expand Down Expand Up @@ -260,6 +265,9 @@ def children(self):
return [self.__class__(self.model, group) for group in self.groups]
elif self.is_group():
return [self/escape(java.get(tag).label()) for tag in java.tags()]
elif hasattr(java, 'propertyGroup'):
return [self/escape(java.propertyGroup(tag).label())
for tag in java.propertyGroup().tags()]
elif hasattr(java, 'feature'):
return [self/escape(java.feature(tag).label())
for tag in java.feature().tags()]
Expand Down Expand Up @@ -579,6 +587,8 @@ def create(self, *arguments, name=None):
container = java.feature()
elif hasattr(java, 'uniquetag') and hasattr(java, 'create'):
container = java
elif hasattr(java, 'propertyGroup'):
container = java.propertyGroup()
elif hasattr(java, 'feature'):
container = java.feature()
if not hasattr(container, 'uniquetag'):
Expand Down Expand Up @@ -635,7 +645,13 @@ def remove(self):
log.error(error)
raise LookupError(error)
parent = self.parent()
container = parent.java if parent.is_group() else parent.java.feature()
java = parent.java
if parent.is_group():
container = java
elif hasattr(java, 'propertyGroup'):
container = java.propertyGroup()
else:
container = java.feature()
container.remove(self.java.tag())


Expand Down
5 changes: 3 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ def setup_logging():
return
logging.setLogRecordFactory(timed_records)
logging.basicConfig(
level = logging.DEBUG,
format = '[%(timestamp)s] %(message)s')
level = logging.DEBUG,
format = '[%(timestamp)s] %(message)s',
)
66 changes: 44 additions & 22 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ def test_property():
assert plot.property('plotonsecyaxis') == new
plot.property('plotonsecyaxis', old)
assert plot.property('plotonsecyaxis') == old
# Test changing material properties.
assert (material/'Basic').property('relpermittivity') == ['1']
(material/'Basic').property('relpermittivity', 2)
assert (material/'Basic').property('relpermittivity') == ['2']
# Read and write back every node property in the model.
if not client.port:
# Skip test in client-server mode where it's excruciatingly slow.
Expand Down Expand Up @@ -485,6 +489,11 @@ def test_create():
Node(model, '').create()
with raises(RuntimeError):
Node(model, 'components/component').create()
material = Node(model, 'materials/medium 1')
material.create('custom', name='custom')
assert (material/'custom').exists()
(material/'custom').property('bulkviscosity', '1')
assert (material/'custom').property('bulkviscosity') == '1'


def test_remove():
Expand All @@ -498,6 +507,9 @@ def test_remove():
physics = Node(model, 'physics')
(physics/'Electrostatics 1').remove()
assert not (physics/'Electrostatics 1').exists()
material = Node(model, 'materials/medium 1')
(material/'custom').remove()
assert not (material/'custom').exists()
with logging_disabled():
with raises(PermissionError):
Node(model, '').remove()
Expand Down Expand Up @@ -584,28 +596,38 @@ def test_tree():
with capture_stdout() as output:
mph.tree(model, max_depth=1)
expected = '''
capacitor
├─ parameters
├─ functions
├─ components
├─ geometries
├─ views
├─ selections
├─ coordinates
├─ variables
├─ couplings
├─ physics
├─ multiphysics
├─ materials
├─ meshes
├─ studies
├─ solutions
├─ batches
├─ datasets
├─ evaluations
├─ tables
├─ plots
└─ exports
capacitor
├─ parameters
├─ functions
├─ components
├─ geometries
├─ views
├─ selections
├─ coordinates
├─ variables
├─ couplings
├─ physics
├─ multiphysics
├─ materials
├─ meshes
├─ studies
├─ solutions
├─ batches
├─ datasets
├─ evaluations
├─ tables
├─ plots
└─ exports
'''
assert output.text().strip() == dedent(expected).strip()
with capture_stdout() as output:
mph.tree(model/'materials')
expected = '''
materials
├─ medium 1
│ └─ Basic
└─ medium 2
└─ Basic
'''
assert output.text().strip() == dedent(expected).strip()

Expand Down

0 comments on commit afa251d

Please sign in to comment.