Currently, calculating is done with 2 input values (see "Calculate a value" action). The result is assigned to a new variable. This is very limited and leads to issues like #1386370: How to: sum of integer values in a loop.

Therefore I'd like to start discussion about an alternative way of calculating with Rules: use lists to collect input values and do arithmetics with them.

=== A simple use case: adding up 3 values (v1, v2, v3) ===

  1. create a new list L
  2. append v1 to L
  3. append v2 to L
  4. append v3 to L
  5. calculate total of L

This would be even more powerful when thinking of loops and calculating with hundrets of numeric values in an optimized way (fast, low ressource consumption).

=== Common math operations should be supported ===

  • total
  • average
  • min
  • max
  • count

Other operations (eg. statistical) could be added later on.

CommentFileSizeAuthor
#1 calculating_with_lists.zip1.05 KBAnonymous (not verified)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Component: Rules Engine » Rules Core
Status: Active » Needs work
FileSize
1.05 KB

A part of it has already been implemented within Italango's Rules Bonus Pack (http://drupal.org/project/rb). It contains actions to sum up and to multiply numeric list items.

In addition to that, please find attached a custom module I put together as a proof of concept and a starting point. The attached module supports total, average and count of lists.

Any feedback appreciated!

mitchell’s picture

Title: Calculating with lists » Calculating with lists: sum, mean, count, min, max
Status: Needs work » Needs review

Thank you for contributing this, Shanapoo! I tested this a while back and remember it worked as expected; maybe I could find or recreate the example exports I made, but they were very basic, just testing / demo stuff. Real use cases would be a lot more interesting. I think the majority of what I intended to discuss was how to merge this without creating a new actions group and how components might be used as an alternative to each one of these being coded in separate actions, but I forget any concrete suggestions and will need to re-review this another day. (I'm returning now for the sake of #1415628: How to: make data variables available for calculations.) Anyone else have ideas on how to move this forward?

zhangtaihao’s picture

I propose one of the following options:

  • Try to get this into Rules Bonus Pack D7.
  • Incorporate this into Rules as a single data action.
  • Document how to construct these as reusable components in Common Configurations.

Overall, I think the approach for the action should be similar to "Calculate a value", i.e. implement one action and make the aggregation function (sum, mean, count, min, max) an option.

I absolutely believe this is essential for a lot of people. Then again, this exists really since the component overview UX leaves something to be desired, otherwise a module/site/consultancy could easily grow sets of utilities neatly organized into little packages in the component overview (not that I don't appreciate your efforts, @mitchell, especially given you've been putting a lot of work into Rules UI Mockups to try to revamp it).

mitchell’s picture

> Try to get this into Rules Bonus Pack D7.
This isn't possible because Itangalo has left the building, so it's currently unmaintained. He's greater than Elvis in my book, and it's super unfortunate that in his new role as a maths prof, he's had to become inactive on d.o :( . I've started trying to pick up the issue queue a bit and merging its feature requests with Rules' own developments (ie #1367332: Loops: batch processing). I'd also like to look at rb_views and rb's example features in other issues. The other major reason not to go this direction is that rb doesn't have a release schedule.

> Incorporate this into Rules as a single data action.
In general, I think this this is an improved approach for a number of actions and especially in this case, and I like the approach about doing it similar to "Calculate a value", but I would like to see what you think about components first.

> Document how to construct these as reusable components in Common Configurations.
I'm in favor of including them with Rules, possibly as default configurations, but wrt implementing them as inter-dependent components, I find it a bit odd that sum and mean are coded entirely separately. It's not a big difference at all, but wouldn't this be a better approach looking forward? I don't have any concrete examples, but I find "Other operations (eg. statistical) could be added later on" to be a very telling comment.

> the component overview UX leaves something to be desired...
Wow. This issue is a lot more interesting to me now. Are you leading to something along the lines of 'Math Components'? When I made the Rules Overview with Vertical Tabs mockup following the Denver BoF, I hadn't yet made the switchover to using components 99% of the time, so I'd love to hear more about what you think might be some potential improvements to the components overview screen. That reaction rules overview is based solely on a suggestion that we use vertical tabs for each tag. I believe organizing components similarly may be an even more significant improvement in light of this issue.

IWasBornToWin’s picture

I installed the new module but can't see there the setting is within rules. I've tried actions - calculate a value and set data value. Thanks

mitchell’s picture

Status: Needs review » Active

See also: #1284266: Condition: "list has count".
Taking off 'needs review' for patches.

#5: Probably clear cache.. after each module installation.

maxplus’s picture

Hi Shnapoo,

I installed your module "calculating_with_lists.zip" and I'm already using the function "List item count" and it works great.
I use it to count the number of referenced nodes from an Entity Reference Field.

Before I wrote my own loops but this is much more streamlined

Thanks!

Max

_ib’s picture

Thanks! Just installed and working beautifully. I'm using it to count the number of users who have created posts (content A) referencing a particular book (content B). Each user can create multiple posts, but each post can reference only one book. I got to the point where I had a list of unique UIDs, but no idea how to do an item count. You saved me a lot of time and frustration, that's for sure.

FYI for people restricting a list to unique items: I had to install the patch provided here #1817942: Enforce uniqueness causes error "in_array() expects parameter 2 to be array" to stop Rules giving a warning message. The problem had nothing to do with your patch, obvs. :)

Once again: many, many thanks for an extremely useful patch!

MrPaulDriver’s picture

Issue summary: View changes

Did this initiative get anywhere? I have installed the module linked in the opening post and it is doing exactly what I need. (struggling with loops)

It would great to see it properly supported.