Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variant logic for '+'-prefixed board variants #136

Closed
wants to merge 4 commits into from
Closed

Fix variant logic for '+'-prefixed board variants #136

wants to merge 4 commits into from

Conversation

romavis
Copy link

@romavis romavis commented Aug 24, 2020

Hello,

I've found something that looks like a bug in board variant processing. Readme.md describes the following example:

e.g. if we have a PCB with three components that have the following values in the fit_field field:

    C1 -> "-production,+test"
    C2 -> "+production,+test"
    R1 -> ""
    R2 -> "-test"

If the script is run with the flag --variant test, then C1, C2 and R1 will be loaded.

However, with existing code this doesn't quite work. The relevant piece in Component.isFitted() method is:

            if opt.startswith("+") and opt[1:] not in self.prefs.pcbConfig:
                return False

Which means that when it checks opt == '+production' with self.prefs.pcbConfig == ['test'] (the only variant specified on command line), the component will be discarded, which is not what we want here... On the next loop iteration opt value will be +test that should allow component to stay in the BOM, but it never checks for that.

This PR should fix the issue. Sorry for removal of unrelated trailing whitespace in a few places - it's just how my editor is configured...

Copy link
Contributor

@set-soft set-soft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have a good point here.
Ok, let me see this. First, I tried your code on the regression tests of my fork and it failed 3 out of the 5 tests. So even when you are right about the problem something is not quite ok ;-)

Let me try to understand the definitions:

  • C1: Your definition is confusing, why -production? This isn't needed. If this is a component that will be added only for test using +test should be enough. Note that - is exclude, but + isn't just include, is include only.
  • R1: Is a default component, found in all variants.
  • R2: Will be excluded from test

The problem comes with C2. You say only added to production and only added to test, which generates the confusion, but we could interpret as only added to (test or production). And here is where I think you have a strong point.

In order to make your code work I'll suggest using the following for the second loop:

        exclusive = False
        for opt in opts:
           # Options that start with '+' are fitted only for certain configurations  
            if opt.startswith("+"):
                exclusive = True
                if opt[1:] in variants:
                    return True
        # No match
        return not exclusive

Also note that now the two loops can be joined.
In case you are interested I added 3 regression tests using your example here: KiBot

The tests are test_int_bom_variant_t2_* I run them using:

$ make single_test SINGLE_TEST=test_int_bom_variant_t2

Your example is here: kibom-variant_2.sch
Let me know if this is properly interpreted.

# Variants logic
opts = check.split(",")
# Variants logic (comma-separated list)
opts = [s.strip() for s in check.split(",")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a loop here? The current code uses another (recycled) loop for this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't want to duplicate calls to strip() in both loops, so factored it out.

if fields:
for f in fields.getChildren():
fieldNames.append(f.get('field', 'name'))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not reinserting the spaces to make the patch more readable? you can searate the space removing in other patch.


return True
# If no decision has been made, component is not fitted
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? the config field is used by other stuff, just think about this:

Config="-V2,DNC"

And you are not specifying variants. With this False the component will be excluded, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the behavior is definitely wrong. I will look into it...

set-soft added a commit to INTI-CMNB/KiBot that referenced this pull request Aug 24, 2020
The variants logic for BoMs when a component resquested to be only
added to more than one variant.
This is related to SchrodingersGat/KiBoM#136 issue.
@romavis
Copy link
Author

romavis commented Aug 24, 2020

I will follow up with feedback for rest of your questions a bit later, but for now I just want to mention that the usecase mentioned in my message is not something that I've invented myself - it was quoted from README.md in this repository.

The problem comes with C2. You say only added to production and only added to test, which generates the confusion, but we could interpret as only added to (test or production). And here is where I think you have a strong point.

From my point of view there's no confusion here. Documentation (README.md) defines an expected behavior which is not followed by the implementation. Whether the behavior stipulated by README makes sense is a different question, but IMHO it does :-)

set-soft added a commit to INTI-CMNB/KiBoM that referenced this pull request Sep 11, 2020
The problem was reported by @romavis
Personally I didn't like his two PRs, both had flaws and the changes in
the code are larger than this patch.
Fixes SchrodingersGat#136
set-soft added a commit to INTI-CMNB/KiBoM that referenced this pull request Sep 11, 2020
The problem was reported by @romavis
Personally I didn't like his two PRs, both had flaws and the changes in
the code are larger than this patch.
So now we have two options ;-)
Fixes SchrodingersGat#136
Copy link
Contributor

@set-soft set-soft left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @romavis !
I think you removed the check for DNF values mixed with other values and separated by comma.
I'm proposing an alternative solution, using the code I suggested before.
This PR is applied to the following fork: https://github.com/INTI-CMNB/KiBoM
BTW: if you are using variants I'll sugest you visiting: https://inti-cmnb.github.io/kibot_variants_arduprog/

Comment on lines -337 to -338
if opt in DNF:
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this check?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another good catch! I got confused by check above which does almost the same, but that's for space-separated options...

@romavis
Copy link
Author

romavis commented Sep 20, 2020

I've pushed a commit to restore a missing DNF check. Please let me know if you see any more issues with this fix.

@SchrodingersGat
Copy link
Owner

@romavis thanks for this improvement, looks like there was a flaw in the logic here.

@set-soft do you have any further comments on this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants