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

inverse relation shadow variable NameError: name XXX is not defined #180

Open
MinamiHuang opened this issue Dec 17, 2023 · 1 comment
Open

Comments

@MinamiHuang
Copy link

MinamiHuang commented Dec 17, 2023

@planning_entity
class Device:
    def __init__(self, id, node, controller):
        self.id = id
        self.node = node
        self.controller = controller

    @planning_id
    def get_id(self):
        return self.id

    def get_node():
        return node

    @planning_variable(Controller, ["controller_range"])
    def get_controller():
        return controller

    def set_controller(new_ctr):
        self.controller = new_ctr

@planning_entity
class Controller:
    def __init__(self, id, node, dev_list=None):
        self.id = id
        self.node = node
        if dev_list is None:
            self.dev_list = []
        else:
            self.dev_list = dev_list

    @planning_id
    def get_id(self):
        return self.id

    def get_node():
        return node

    @inverse_relation_shadow_variable(Controller, source_variable_name = "controller")
    def get_dev_list():
        return dev_list

    def set_dev_list(new_list):
        self.dev_list = new_list

If I pass the 'Controller', I get the NameError. If I put the class Controller before Device and omit the param source_type, I receive the message "missing 1 required positional argument: 'source_type' "

Please help me construct the Bi-directional variable, thanks very much.

@ge0ffrey
@Christopher-Chianelli

@Christopher-Chianelli
Copy link
Contributor

I recommend you look at the inverse relation test:

@optapy.planning_entity
class InverseRelationEntity:
def __init__(self, code, value=None):
self.code = code
self.value = value
@optapy.planning_variable(object, ['value_range'])
def get_value(self):
return self.value
def set_value(self, value):
self.value = value
@optapy.planning_entity
class InverseRelationValue:
def __init__(self, code, entities=None):
self.code = code
if entities is None:
self.entities = []
else:
self.entities = entities
@optapy.inverse_relation_shadow_variable(InverseRelationEntity, source_variable_name='value')
def get_entities(self):
return self.entities
def set_entities(self, entities):
self.entities = entities

In particular, you would use object for get_controller, and Device for get_dev_list.

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

No branches or pull requests

2 participants