[Solved] Loop through group which is member of group

Hi!

I’m trying to loop through a group (using ‘forEach’) where the group object is coming from another ‘forEach’ loop.

Explanation:
The item setup is something like this:

Group  grpProfiles
Group  grpProfile1 (grpProfiles)
Group  grpProfile2 (grpProfiles)

Number Profile1_Item1 (grpProfile1)
Number Profile1_Item2 (grpProfile1)
Number Profile1_Item3 (grpProfile1)

Number Profile2_Item1 (grpProfile2)
Number Profile2_Item2 (grpProfile2)
Number Profile2_Item3 (grpProfile2)

Now a rule which is fired by the change of any item:

rule "Profile check"
when
    Profile1_Item1_Time1 changed or
    Profile1_Item2_Time1 changed or
    Profile1_Item3_Time1 changed or
    Profile2_Item1_Time1 changed or
    Profile2_Item2_Time1 changed or
    Profile2_Item3_Time1 changed

then
    // loop through all profiles (all members of group 'grpProfiles')
    grpProfiles.members.forEach[ profile |
    	logInfo("MyRules", "processing profile '{}'", profile.name)

        // now loop through all items in the profile
        profile.members.forEach[ setting |
            logInfo("MyRules", "processing setting '{}'", setting.name)
    	]
    ]

    logInfo("MyRules.test", "finished rule 'Heating profile check'")
end

While the outer loop runs fine, the inner loop gives an error:

[ERROR] [.script.engine.ScriptExecutionThread] - Rule 'Profile check': The name '<XFeatureCallImplCustom>.members' cannot be resolved to an item or type.

My idea is, that the outer loop gives me the group members as type “Object” which probably does not have a method called ‘forEach’. But in this case, the group members of ‘grpProfiles’ are really also groups.

Is there a way to get this script construct running? Any kind of type cast, so the ‘Object’ is considered as a real group.

Thank’s for any reply.

Update: Found solution by reading some interesting posts from Rich regarding “Design pattern”.
@rlkoshak: Thank you very much, very helpful!

Just for anyone who is interested in:

    // loop through all profiles (all members of group 'grpProfiles')
    grpProfiles.members.forEach[ profile |
    	logInfo("MyRules", "processing profile '{}'", profile.name)

        // get group object
        val gProfile = grpProfiles.members.filter[ p | p.name == profile.name ].head as GroupItem

        // now loop through all items in the profile
        gProfile.members.forEach[ setting |
            logInfo("MyRules", "processing setting '{}'", setting.name)
    	]
    ]

That’s it.

4 Likes