Yeah, I myself was just about to edit my post to say that it was root. I misread the trigger as "is_heir" instead of "is_heir_of". I think it must be root, as root is the ai arranging the marriage. And that is what seems logical.
Also would mean that an heiress in a foreign court still could be marriaged away as long as the ruler isn't closely related to her, which I think is fine.
What I have atm is the following. After having looked at it then I think that Tsadiq's suggestion is sound, since it should prevent patri becoming rare and make sure that matri only happens for female rulers and heiresses. Though is gonna need testing, of course.
Code:ai_wants_matrilineal_marriage = { OR = { scope:secondary_actor = { is_female = yes OR = { is_ruler = yes is_heir_of = root } } AND = { scope:secondary_actor = { is_female = yes } OR = { has_realm_law = female_only_law has_realm_law = female_preference_law faith = { has_doctrine_parameter = female_dominated_law } } } } }
Just wanted to point out, that this doesn´t work as intended. An unmarried female ruler will enter a normal, e.g. patrilineal marriage.
So far your initial solution works best; With female rulers arranging matrilineal marriages and rulers in general arranging matrilineal marriages for daughters who are first or second in the line of inheritance.