0

I am new to Python. Can someone explain where the functor value is coming from. Functor = msg.functor, but there is no explanation for the word functor, I've tried googling it and came up with nothing. Also the .val keyword isn't explained anywhere, I'm probably being dim but i cannot find any examples.

def process_action(self, msg, sender):  
    assert msg.get_type() == pedroclient.PObject.structtype
    functor = msg.functor
    assert functor.get_type() == pedroclient.PObject.atomtype
    cmd_type = functor.val
    cmd = msg.args[0]
    if cmd_type == 'stop_':
        assert cmd.get_type() == pedroclient.PObject.structtype
        cmd_functor = cmd.functor.val
        #if cmd_functor in ['pickup', 'putdown']:
        self.stop_arm(cmd.args[0].val)
        #else:
        #    self.stop_arm(cmd.args[0].val)

    elif cmd_type in ['start_', 'mod_']:
        self.start_animate()
        assert cmd.get_type() == pedroclient.PObject.structtype
        cmd_functor = cmd.functor.val
        if cmd_functor == 'pickup':
            self.pickup(cmd.args[0].val, cmd.args[1].val, sender)
        elif cmd_functor == 'putdown':
            if cmd.args[1].get_type() == pedroclient.PObject.inttype:
                self.putdown_on_block(cmd.args[0].val, cmd.args[1].val, 
                                      sender)
            else:
                self.putdown_on_table(cmd.args[0].val, cmd.args[1].val, 
                                      sender)
        elif cmd_functor == 'go_home':
             self.go_home(cmd.args[0].val)

Edit: Sorry there is a lot more code, I have skimmed it as much as i could.

def process_msg(self, term):
    msg = term.args[2]
    sender_process = term.args[1].args[0].args[1].val
    robotID = int(sender_process[-1])-1
    #print msg
    if str(msg) == "initialise_":
        robotID = int(sender_process[-1])-1

def data_cb(self, event):
    self.env.process_msg(event.notification)

The best I can understand it is that the functor is an attribute of msg, which in turn is the argument of system input, where system input is a event notification. Am i correct or completely going of in the wrong direction.

MFree
  • 13
  • 1
  • 3
  • 3
    There's part of this code missing isn't there? Where's the definition of `msg`? What are you passing onto this function? – Bernardo Meurer Mar 13 '16 at 19:15
  • 2
    `functor = msg.functor` tells you that `functor` is an attribute of the object `msg`. Next thing you'll have to do is find out what `msg` is. – timgeb Mar 13 '16 at 19:16
  • Cheers, yeah there was code missing, thanks both of you. – MFree Mar 13 '16 at 20:18

4 Answers4

1

Searching via Google I have found documentation, which shall probably explain part of your : The Pedro 1.6 Reference Manual

Regarding the functor I did not find a not about that (but did not spend much time on it). It is likely, Pedro is used as messaging system for sending commands to some (real or virtual) robot. The robot is controlled by messages, which have some structure, including functor, being probably identification of what type of action shall be performed, and values, which are are defining parameters of that action.

Jan Vlcinsky
  • 40,901
  • 12
  • 95
  • 96
1

Since Python allows passing functions as values a Functor is basically an object which is a function. Note that on the code that you post you are comparing many times which function is this.

On the line cmd_type = functor.val you actually attributed to cmd_type the function itself and later on you compare it to know which function it is exactly.

if cmd_type == 'stop_':
is the function stop()?

elif cmd_type in ['start_', 'mod_']:
is one of these functions, start(), mod()?

And so on... Further reading here

Edit: I found this topic to be very related to understand the concept of functors, though it doesn't quotes python but python has a lot of Functional programming features.

1

Basic stuff - msg & functor might be in these places:

  • Defined in this file
  • In a local module that is imported by the import statements at the top of this file, this would be a .py file in the directory structure of the app
  • In an installed package from someone else

.val - this is most likely an attribute containing data. It is not a Python keyword of any kind that I can find. I'm sure you thought that it is probably short for "value". It is either defined in the objects it is a part of (functor, cmd.args[]) or in some superior object that they import and thus inherit val from.

JackW327
  • 239
  • 2
  • 11
  • Thank you, it should occurred to me that it might have been imported from a different module, seeing as i was importing them. – MFree Mar 13 '16 at 20:15
0

functor is an attribute of the msg object; without seeing the definition of that object we can't tell you anything more. Similarly, val is in turn an attribute of msg.

Daniel Roseman
  • 567,968
  • 59
  • 825
  • 842