Skip to content

Commit afa251d

Browse files
authored
Add support for material property groups. (#90)
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.
1 parent b2fe3e6 commit afa251d

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

mph/node.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,12 @@ def java(self):
215215
java = parent.java
216216
if not java:
217217
return None
218-
container = java if parent.is_group() else java.feature()
218+
if parent.is_group():
219+
container = java
220+
elif hasattr(java, 'propertyGroup'):
221+
container = java.propertyGroup()
222+
else:
223+
container = java.feature()
219224
for tag in container.tags():
220225
member = container.get(tag)
221226
if name == escape(member.label()):
@@ -260,6 +265,9 @@ def children(self):
260265
return [self.__class__(self.model, group) for group in self.groups]
261266
elif self.is_group():
262267
return [self/escape(java.get(tag).label()) for tag in java.tags()]
268+
elif hasattr(java, 'propertyGroup'):
269+
return [self/escape(java.propertyGroup(tag).label())
270+
for tag in java.propertyGroup().tags()]
263271
elif hasattr(java, 'feature'):
264272
return [self/escape(java.feature(tag).label())
265273
for tag in java.feature().tags()]
@@ -579,6 +587,8 @@ def create(self, *arguments, name=None):
579587
container = java.feature()
580588
elif hasattr(java, 'uniquetag') and hasattr(java, 'create'):
581589
container = java
590+
elif hasattr(java, 'propertyGroup'):
591+
container = java.propertyGroup()
582592
elif hasattr(java, 'feature'):
583593
container = java.feature()
584594
if not hasattr(container, 'uniquetag'):
@@ -635,7 +645,13 @@ def remove(self):
635645
log.error(error)
636646
raise LookupError(error)
637647
parent = self.parent()
638-
container = parent.java if parent.is_group() else parent.java.feature()
648+
java = parent.java
649+
if parent.is_group():
650+
container = java
651+
elif hasattr(java, 'propertyGroup'):
652+
container = java.propertyGroup()
653+
else:
654+
container = java.feature()
639655
container.remove(self.java.tag())
640656

641657

tests/fixtures.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ def setup_logging():
6262
return
6363
logging.setLogRecordFactory(timed_records)
6464
logging.basicConfig(
65-
level = logging.DEBUG,
66-
format = '[%(timestamp)s] %(message)s')
65+
level = logging.DEBUG,
66+
format = '[%(timestamp)s] %(message)s',
67+
)

tests/test_node.py

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,10 @@ def test_property():
365365
assert plot.property('plotonsecyaxis') == new
366366
plot.property('plotonsecyaxis', old)
367367
assert plot.property('plotonsecyaxis') == old
368+
# Test changing material properties.
369+
assert (material/'Basic').property('relpermittivity') == ['1']
370+
(material/'Basic').property('relpermittivity', 2)
371+
assert (material/'Basic').property('relpermittivity') == ['2']
368372
# Read and write back every node property in the model.
369373
if not client.port:
370374
# Skip test in client-server mode where it's excruciatingly slow.
@@ -485,6 +489,11 @@ def test_create():
485489
Node(model, '').create()
486490
with raises(RuntimeError):
487491
Node(model, 'components/component').create()
492+
material = Node(model, 'materials/medium 1')
493+
material.create('custom', name='custom')
494+
assert (material/'custom').exists()
495+
(material/'custom').property('bulkviscosity', '1')
496+
assert (material/'custom').property('bulkviscosity') == '1'
488497

489498

490499
def test_remove():
@@ -498,6 +507,9 @@ def test_remove():
498507
physics = Node(model, 'physics')
499508
(physics/'Electrostatics 1').remove()
500509
assert not (physics/'Electrostatics 1').exists()
510+
material = Node(model, 'materials/medium 1')
511+
(material/'custom').remove()
512+
assert not (material/'custom').exists()
501513
with logging_disabled():
502514
with raises(PermissionError):
503515
Node(model, '').remove()
@@ -584,28 +596,38 @@ def test_tree():
584596
with capture_stdout() as output:
585597
mph.tree(model, max_depth=1)
586598
expected = '''
587-
capacitor
588-
├─ parameters
589-
├─ functions
590-
├─ components
591-
├─ geometries
592-
├─ views
593-
├─ selections
594-
├─ coordinates
595-
├─ variables
596-
├─ couplings
597-
├─ physics
598-
├─ multiphysics
599-
├─ materials
600-
├─ meshes
601-
├─ studies
602-
├─ solutions
603-
├─ batches
604-
├─ datasets
605-
├─ evaluations
606-
├─ tables
607-
├─ plots
608-
└─ exports
599+
capacitor
600+
├─ parameters
601+
├─ functions
602+
├─ components
603+
├─ geometries
604+
├─ views
605+
├─ selections
606+
├─ coordinates
607+
├─ variables
608+
├─ couplings
609+
├─ physics
610+
├─ multiphysics
611+
├─ materials
612+
├─ meshes
613+
├─ studies
614+
├─ solutions
615+
├─ batches
616+
├─ datasets
617+
├─ evaluations
618+
├─ tables
619+
├─ plots
620+
└─ exports
621+
'''
622+
assert output.text().strip() == dedent(expected).strip()
623+
with capture_stdout() as output:
624+
mph.tree(model/'materials')
625+
expected = '''
626+
materials
627+
├─ medium 1
628+
│ └─ Basic
629+
└─ medium 2
630+
└─ Basic
609631
'''
610632
assert output.text().strip() == dedent(expected).strip()
611633

0 commit comments

Comments
 (0)