Friday, August 23, 2013

Jedi Nation

In Star Wars when Naboo, Leia's homeworld is destroyed by the Death Star, Obi Wan slumps over in the Millennium Falcon and utters something like, "I feel as if ten thousand souls suddenly screamed out, and then were silenced."

Hmm, is Obi Wan using his Jedi powers to snoop on the universe? One could see it that way, but the verb "snoop" carries the connotation that he did not have the snoopee's best interests at heart. And Obi Wan was all heart.

Hence the Jedi Nation.

Let us take the current climate of anti-terrorist snooping. It brings to mind images of kicked down doors in the middle of night, subjects disappearing based on flimsy wiretap evidence. But what if we take the Jedi approach? What if the next day, Obi Wan shows up to the subjects apartment, offers to buy her some coffee and says, "I got a feeling from the Force that you're feeling a but unhappy with the current government and some other stuff. You know all these wars over oil piss me off too. Want to talk to someone about it? Are you in a bad spot right now? I could help you with your rent for awhile. Are you looking for work? Let me measure your mitochondria. You're a bit old to start the training, but the force runs strong in you. We'll make a Jedi out of you yet!"

If she still resists, Obi Wan can try Jedi mind tricks. "Theses are not the innocent civilians you are looking for." If she tries to kill Obi Wan, then he might be forced to slice her up with his light-saber in self defense.

Wednesday, August 14, 2013

Flatten nested if statements with the opposite condition

Have you ever found yourself nesting if, for and other flow-control statements seemingly endlessly into the dreaded arrow formation?
EG:
if this:
    if that:
        for x in X:
            if yuck:
                ...
            else:
                ...
                continue
    else:
        while Y:
            if foobar:
                ...
            else:
                ...
                break
else:
    ...
One of the easiest ways to collapse if statements is using the opposite condition and return, continue or break.
EG:
if not this:
    ...
    return
if not that:
    while Y:
        if not foobar:
            ...
            break
        ...
    return
for x in X:
    if not yuck:
        ...
        continue
    ...
Some refactoring tools will help you do this, but it good programming practice to avoid nested flow-control statements when possible. Even though in this case it only eliminated 2 lines, it is more compact, lines are not as long, so wrapping/continuing/splitting commands is not an issue, and in general the code should run faster, since it only executes as much code as is needed before returning, continuing or breaking.

Monday, August 12, 2013

SifterClient

Wow! Someone took an interest in one of my side projects. A long time ago, we started using Sifter at work, so I decided to make an Android app, called SifterReader to access it from my phone. It was a fun little side project, but we stopped using Sifter so I abandoned it. Now it's been revived. That's nice!

Thursday, August 1, 2013

importing UML models from Modelio to Papyrus

Skipping the discussion of class diagrams, UML and round trip engineering, let's just say after dismissing Umlet and Violet, Argo, NClass and StarUML, and all of the non-Windows or commercial offerings you have settled between Modelio and Papyrus. You like the ease of Modelio, but its freemium service means you can't generate code, and Papyrus + Acceleo does just that.
  1. Make your diagram in Medelio
  2. From the model explorer, right click and select XMI --> export
  3. Browse to a location outside of your workspace and save it with the EMF UML 3.0.0, but unclick adding Modelio notations please! Also save it with the *.uml extension instead *.xmi.
  4. Now start eclipse and show the Papyrus perspective.
  5. if you already have a project, that's fine, just start a new Papyrus model.
  6. Create a folder in your project called XMI and import the files you exported from Modelio - there should be 2 of them, a profile and your model.
  7. Double click the model that you just created (e.g. model.di) and it should open in the main window, and hopefully you will see it in the model explorer too.
  8. Right click and select import, then import package from user model.
  9. In the window that opens, select your profile and model packages, and then import all. I'm not 100% positive that you need to import the profile, but it doesn't hurt, and I had issues when I tried to just copy the model components from the Modelio UML model to Papyrus.
  10. Ta-daa, you should now see two packages in your model explorer one is the default profile and the other is your Modelio model.
  11. Now at the top level model, create a class diagram (or any of the 9 UML diagrams). This is the most important step!
  12. Drag and drop the first class into the diagram window.
  13. Expand that class in the model explorer and select the class attributes and drag and drop them into the first section of the class below the title.
  14. Repeat for the operations, but drop them into the second box.
  15. Drag over any associations.
  16. You may need to remove and re-add any stereotypes as for some reason they were not showing up in the diagram.
Voila! you've transferred your model and your diagram!
Fork me on GitHub