Code Monkey home page Code Monkey logo

Comments (7)

mballance avatar mballance commented on September 20, 2024

Hi @ShraddhaDevaiya,
Okay, this is a bit of an interesting example because it mixes plain-python control-flow constructs with constraint constructs. While this does work in some cases, I would generally recommend against it.
The first 'foreach' in the constraint block will be simplified down to:

                with vsc.foreach(self.a_list, idx=True) as i:
                    self.a_list[i] in vsc.rangelist(10,11,12,13)

This is because 'c' is not a PyVSC variable. Depending on the application, this might be desired.

In the second case, because we're forming a constraint between two arrays, it is necessary to use the PyVSC constraint if/else. I've updated your example as follows and can confirm that elements of a_list are in [10..13] and elements of temp_list are '1'.

        @vsc.randobj
        class my_s(object):
            def __init__(self):
                self.a_list = vsc.rand_list_t(vsc.uint8_t(),7)
                self.temp_list = vsc.rand_list_t(vsc.uint8_t(),7)
                self.c = 0
    
            @vsc.constraint
            def ab_c(self):
                with vsc.foreach(self.a_list, idx=True) as i:
                    if self.c:
                        self.a_list[i] in vsc.rangelist(5,6,7,8)
                    else:
                        self.a_list[i] in vsc.rangelist(10,11,12,13)

                with vsc.foreach(self.temp_list, idx=True) as i:
                    with vsc.if_then(self.a_list[i].inside(vsc.rangelist(6,7))):
                        self.temp_list[i] == 0
                    with vsc.else_then: 
                        self.temp_list[i] == 1

        my = my_s()
        my.randomize()        

I'll leave this open for now, since I'd like to see if the solve-failure message could better indicate what is happening here.

Best Regards,
Matthew

from pyvsc.

ShraddhaDevaiya avatar ShraddhaDevaiya commented on September 20, 2024

Hi @mballance,
Yeah it is working this way. Thanks for helping!

Regards,
Shraddha Devaiya.

from pyvsc.

ShraddhaDevaiya avatar ShraddhaDevaiya commented on September 20, 2024

Hi @mballance,

I am trying to use this if_then construct with some other case combining this scenario but, when I am writing else_if for comparison of Enum type, then it is giving me an error.
Following is the code for it:

class my_e(Enum):
    A = 0
    B = 1
    C = 2
    D = 3

@vsc.randobj
class my_s(object):
    def __init__(self):
        self.name = my_e.A
        self.a = vsc.rand_uint8_t(0)
        self.b = vsc.rand_list_t(vsc.enum_t(my_e), 4)
    

    @vsc.constraint
    def ab_c(self):
        with vsc.foreach(self.b, idx = True) as i:
            with vsc.if_then(self.b[i] == my_e.A):
                self.a == 0
            with vsc.else_if(self.b[i] == my_e.B):
                self.a == 1
              
my = my_s()

# Randomize
for i in range(5):
    my.randomize()
    print("ITERATION : ",i+1)
    print(my.a, list(my.b))

And it is giving an error like following:

Traceback (most recent call last):
  File "3usecase.py", line 31, in <module>
    my.randomize()
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/rand_obj.py", line 107, in randomize
    Randomizer.do_randomize([model])
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/randomizer.py", line 831, in do_randomize
    constraint_l.extend(ArrayConstraintBuilder.build(
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/array_constraint_builder.py", line 45, in build
    m.accept(builder)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/field_composite_model.py", line 143, in accept
    v.visit_composite_field(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/model_visitor.py", line 65, in visit_composite_field
    c.accept(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/constraint_block_model.py", line 42, in accept
    v.visit_constraint_block(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_copy_builder.py", line 85, in visit_constraint_block
    super().visit_constraint_block(c)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/model_visitor.py", line 101, in visit_constraint_block
    self.visit_constraint_scope(c)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_override_visitor.py", line 26, in visit_constraint_scope
    cc.accept(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/constraint_foreach_model.py", line 47, in accept
    v.visit_constraint_foreach(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/array_constraint_builder.py", line 72, in visit_constraint_foreach
    c.accept(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/model/constraint_if_else_model.py", line 62, in accept
    visitor.visit_constraint_if_else(self)
  File "/home/adduser/.local/lib/python3.8/site-packages/vsc/visitors/constraint_copy_builder.py", line 142, in visit_constraint_if_else
    for cs in c.false_c.constraint_l:
AttributeError: 'ConstraintIfElseModel' object has no attribute 'constraint_l'

if we comment else_if block then it is working. So, can you please help me?

Thanks & Regards,
Shraddha Devaiya.

from pyvsc.

ShraddhaDevaiya avatar ShraddhaDevaiya commented on September 20, 2024

Hi @mballance,
Did you get a chance to look into this?

Thanks & Regards,
Shraddha Devaiya.

from pyvsc.

mballance avatar mballance commented on September 20, 2024

Hi @ShraddhaDevaiya ,
Apologies, I have not had a chance yet. I'll do my best to investigate over the weekend.

Best Regards,
Matthew

from pyvsc.

mballance avatar mballance commented on September 20, 2024

Hi @ShraddhaDevaiya,
I've corrected the issue -- which was specific to using else-if inside a foreach constraint. The testcase now runs and the fix is available both on GitHub and in the precompiled PyPi package.
I'll leave this issue open until you can confirm.

Best Regards,
Matthew

from pyvsc.

ShraddhaDevaiya avatar ShraddhaDevaiya commented on September 20, 2024

Hi @mballance ,
It is working now, Thanks for helping !

Regards,
Shraddha Devaiya.

from pyvsc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.