Skip to content

Implemented toplogical sorting for totals #65

Closed
wants to merge 1 commit into from

4 participants

@amenk
amenk commented Aug 15, 2012

The sorting of totals is incorrect with causes lots of problem when new modules that define own totals are added.

For details see http://stackoverflow.com/a/9258826/288568

This patch implementes a toplogic sort to fix this problem.

For a unit test I suggest the following code ($result is the result of the _getSortedCollectorCodes array for any input)

        $this->assertEquals(count($configArray), count($result));

        // check if all conditions where matched
        // as we do have only a partial order, it does not make sense
        // to check the definite result
        foreach($configArray as $code=>$data) {
            $_code = $data['_code'];
            $codeIndex = array_search($_code, $result);
            if (!isset($configArray[$_code])) continue;
            foreach($data['before'] as $beforeCode) {
                if (!isset($configArray[$beforeCode])) continue;
                $bIndex = array_search($beforeCode, $result);
                $this->assertTrue($codeIndex < $bIndex, "Condition not fullfilled: $codeIndex < $bIndex");
            }
            foreach($data['after'] as $afterCode) {
                if (!isset($configArray[$afterCode])) continue;
                $aIndex = array_search($afterCode, $result);
                $this->assertTrue($codeIndex > $aIndex, "Condition not fullfilled: $codeIndex > $aIndex");
            }
        }
@IvanChepurnyi

Hi Amenk.
Check this pull request submitted by me: #49

It has already a fixed solution with integration test that confirmes that fix is working.

@amenk
amenk commented Aug 20, 2012

Hi Ivan,

thanks for the comment - I wish I saw that pull request earlier...

Alex

@magento-team

@amenk
Thank you for yet another contribution.

Recently (see pull request #49) we have implemented a library Magento_Data_Graph, which allows depth-first search (DFS). The topological search algorithm is known to be based on the DFS algorithm -- would you try redo your contribution to add the search algorithm to graph class and then utilize it for sorting sales totals?

@amenk
amenk commented Aug 31, 2012

Okay. Can you give me a ping as soon as the Magento_Data_Graph is available in the git - I did not find it.

But I think the "drawback" of using the topological sort will be that if the ordering is not possible it would fail. See the discussion in #49 - so I do not know if it will be possible to still "sell" if there are problems.

@magento-team

@amenk Magento_Data_Graph is available since Sep 5, 2012.

As you mentioned, there are drawbacks in a case of ambiguous input data. Feel free to close the ticket, if you think this ticket is no longer actual after the contribution #49 has been accepted.

@amenk
amenk commented Sep 25, 2012

I will try to implement a check when sorting the data as an addition to #49 and log an exception when there are contradictions.

@magento-team

Hello Amenk, any luck with implementing this one?

@amenk
amenk commented Oct 17, 2012

Did not have a time yet.

I thought again about your suggestion in the other ticket to implement it in a CI test. Not sure about that.

I think if I integrate it with the Magento_Data_Graph you would use my functionality only to verify and log an exception but use Ivan's patch to do the actual sorting?

Is such a double-check and double-sorting intended ?

What do you think?

@magento-team

In the particular case with checkout -- yes, we'd have to use this algorithm only to verify and log an exception.

But also we intend to utilize topological sorting in future for other places:

  • Across the board, where sorting elements is based on "before" and "after" attributes
  • In core layout and sales totals, admin menu, PEAR packages (Mage_Connect), module dependencies, admin system configuration (system.xml)
@magento-team

Is such a double-check and double-sorting intended ?
Yes, it is intended as we described in Ivan's ticket: #49 (comment)

@amenk
amenk commented Oct 17, 2012

Alright. What algorithms are used in those cases currently.

Please be aware, that the algorithm I suggest will fail if there are any contradictions. (which I do not think is a problem - but basically the discussion is the same like in #49) You mentioned also sales totals - isn't that the same use case?

@kirmorozov

Here's the code for sorting one way,
http://pubsvn.ez.no/doxygen/trunk/html/ezptopologicalsort_8php_source.html
And before is the same is opposite depends on for constructor.
Cant you just copy over GPL code?

@amenk
amenk commented Oct 18, 2012

I actually used the code from http://www.calcatraz.com/blog/php-topological-sort-function-384 which is without license, means public domain.

@magento-team

But also we intend to utilize topological sorting in future for other places:
... sales totals ...

You mentioned also sales totals - isn't that the same use case?

Right. It was listed there by mistake, please disregard that part.

@magento-team

Hi. Would you be able to contribute topological sorting in Magento_Data_Graph? (and if you can add test/cases that would be great)

@magento-team

Closing the thread due to no recent activity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.