Shop OBEX P1 Docs P2 Docs Learn Events
Convert SPIN to FLOWCHART? — Parallax Forums

Convert SPIN to FLOWCHART?

DavidMDavidM Posts: 630
edited 2010-11-24 17:40 in Propeller 1
Hi,

Is it possible to "Convert" a SPIN Project and all its objects to a flow chart?

I have seen a few that work with C and most popular languages, but it would be really great to do this with SPIN.



reagards

DaveM

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-23 17:59
    There's no fundamental reason why this can't be done although you'd need to produce a separate flowchart for each cog used. It would be a lot of work to do and you'd have to be pretty inspired to put the effort in. I doubt there would be many people who'd be interested in using it.
  • DavidMDavidM Posts: 630
    edited 2010-11-23 18:24
    HI Mike,

    thnaks for th ereply

    I have seen this app..


    http://www.aivosto.com/visustin.html

    They claim to provide a service for custom languages for about $1500.00,

    its a pity you cant add your own language!

    thanks

    Dave M
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-11-23 20:31
    DaveM,

    Let's examine your premises. Why a flowchart? What would it communicate that the source code itself cannot? Flowcharts were fine for spaghetti code, like Fortran. But, even in the '70s, flowcharts were beginning to be frowned upon as a way to illustrate a program's logic and flow of control. Now, here we are forty years later, with well-structured languages like Spin that are virtually self-documenting. I have difficulty imagining what a flowchart could add to that -- except perhaps to mollify some pointy-haired manager-type who needs a PowerPoint slide to show his own bosses.

    'Sorry for the screed; but seriously: flowcharts are so last century -- and certainly nothing I'd spend $1500 to produce. Save your money, don't stress over flowcharting, and just enjoy programming Spin. :)

    -Phil
  • DavidMDavidM Posts: 630
    edited 2010-11-23 20:51
    Hi Phil,


    You make good points,

    I am currently working on a BIG project ( as far as spin is concerned) so far i have.

    3500 lines of spin code
    15 Objects ( many are used several times ( i.e Numbers/serial/shift etc))
    7 cogs

    My MAIN OBJECT ( TOP OBJECT) has over 1000 lines so far, and one problem that i have is that if a make a changed to something, its hard to tell where I have used that attribute ( i.e variable, methods etc) only a FIND helps me here

    Also I have very complex IF statements ( especially in my main loop) , I have also many "MODES" in my app, which I have to be aware of when making changes.

    Yesterday I was trying to see where I can COMBINE some cogs ( as I need to make room for some more cogs), nowhere in the IDE does it show you anything of how the cogs are implements/used.

    I was after some kind of "VISUAL" means of viewing the Structure of my code.

    regards

    Dave M
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-11-23 21:01
    DavidM,

    Please don't take this the wrong way. But sometimes, once you've added -- and added still more -- to a program, it becomes so unwieldy that a complete rewrite is the only way bring order back to your project. It sounds like you may be very close to that point -- if not already there.

    I've been there many times myself. But I've found that complete restructurings not only improve efficiency but serve to clarify my thinking. Granted, rewriting thousands of code lines would be no mean feat -- especially if you're on a deadline. But you also have to consider maintenance issues down the road...

    -Phil
  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-23 21:04
    Unfortunately, something like a flowchart will be of little help for you. These don't handle modes very well nor would they handle objects in any useful way. Proper documentation is a difficult task, sometimes made worse by a program that needs to be rewritten because it's grown piece by piece and, as you've noticed, needs to have portions collapsed onto one another. Typically, each major routine needs to have documentation about what global variables are used and the meaning and use of each of the parameters and result.
  • DavidMDavidM Posts: 630
    edited 2010-11-23 21:04
    DaveM,

    just enjoy programming Spin. :)

    -Phil


    To this point, I am actually starting to NOT ENJOY Spin! When the project becomes big ( and I have worked on apps with over 10,000 lines of code) its very hard to scroll around looking for bits of code,

    Its would be nice to have.

    1) A means of LOCKING/HIGHLIGHTING of METHODS/Lines of code to say that that method has been done/tested etc
    2) A way of keeping methods in a LIST manner but still belong to the MAIN OBJECT.
    3) GLOBAL VARIABLES
    4) A WHERE Used LIST when the FIND feature is implemented ( Similar to the way REALBASIC does this)
    5) Some kind of COG VIEW, where it show the FLOW of how cogs are used, i.e TEMP COG, Permanent CoG etc
    6) I would really like a TYPE AHEAD Feature, that works across all objects

    7) I would like some way of FLAGGING Lines or sections of code that need to be worked on, Like a to do list,

    8) I would like to be able to expand/collapse methods/branching etc ( I know BST has this but BST does not have Branching lines, which make it TOO HARD to use. and I dont know if this will ever be done.

    9) Dont ask me about debugging!

    I have more issues, but its kind of TABOO here to ask for IDE Features!

    Dave M
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2010-11-23 21:14
    DaveM wrote:
    I have more issues, but its kind of TABOO here to ask for IDE Features!
    Well, not taboo, exactly; in my experience, "futile" probably comes closer [sigh].

    BTW, I'm not sure what you mean about a lack of global variables, since Spin has them -- unless you mean cross-object global variables. For those you need to write accessor/modifier methods, in keeping with object encapsulation.

    How long is your longest method? Does it fit on one screen? If not, you might consider breaking it down into smaller pieces. Methods that are "too big to fail" probably will.

    -Phil
  • DavidMDavidM Posts: 630
    edited 2010-11-23 22:48
    HI Phil,

    Global variables...( my opinion)

    Define ONCE in the MAIN OBJECT, then it can be used directly in any other object. ( and yes , I know about semaphores/lock and similar issues) I would only have ONE Modifier method for a given variable, but many READ calls.

    Currently I have to PASS Pointers around all the time, and I use lots of pointers.

    I have considered a smaller main object and also breaking up the main objects ( pulling out the methods into other objects, BUT I only do that for things that are commonly used.

    If I were to make separate objects this would be even more difficult because of all the referencing! I have tried that approach.

    having said all of that My current large applications works very well, I am wanting to make extra features. ( which I have planned for a while)

    I wish I could post my code but its for production units and my competition must note ever see this.
  • DavidMDavidM Posts: 630
    edited 2010-11-23 22:56
    How long is your longest method? Does it fit on one screen? If not, you might consider breaking it down into smaller pieces.

    -Phil

    My longest method ( which is my main loop) is about 150 lines ( I use lts of space between lines in this case about 50 blank lines) , I have most lines commented.

    What I have done is, say I have an IF Statement that then needs to do something that requires more than one line, I would usually make that into a separate method and place it down at the bottom of my code. ( after I test it of course)

    This approach leaves not much left but DECISION LOGIC and this make it easier to read, its the decision logic that drives me nuts ( but remember my project is complicated as it has many user features and Modes.

    thanks

    Dave M
  • DavidMDavidM Posts: 630
    edited 2010-11-23 22:59
    Mike Green wrote: »
    Unfortunately, something like a flowchart will be of little help for you. These don't handle modes very well nor would they handle objects in any useful way.


    Hi Mike,

    When I say "Flow Chart" I probably mean GRAPHICAL VIEW? of some sort, maybe a way to "VISUALISE" cog usage, A way to visualise receptive tasks, and how one method talks to another. I probably don't need all the code visualise, I some just want to visualise the "Structure"

    Another view I mean is something like a BLOCK DIAGRAM similar to that use in describing how a processor chip ( propeller chip) functions.

    thanks

    Dave M
  • DavidMDavidM Posts: 630
    edited 2010-11-24 00:58
    DavidM,

    Please don't take this the wrong way. But sometimes, once you've added -- and added still more -- to a program, it becomes so unwieldy that a complete rewrite is the only way bring order back to your project. It sounds like you may be very close to that point -- if not already there.

    I've been there many times myself. But I've found that complete restructurings not only improve efficiency but serve to clarify my thinking. Granted, rewriting thousands of code lines would be no mean feat -- especially if you're on a deadline. But you also have to consider maintenance issues down the road...

    -Phil

    Hi Phil,

    Yes, I AM ON A DEADLINE!(YUK), JUST 1 MORE WEEK Till I have to sell something, WRITE THE OPERATION MANUAL ( YUK!), CREATE THE WEBSITE ( EVEN YUCKIER!) , finish off my RealBasic Application ( for PC& MAC) etc BUT we do have orders for our products, about 25K! ( YUM!) we have spent about 35K ( YUK!)on components pcb's etc. but its all very exiting!

    re restructuring, I have already done that too!, I have improved many parts of my code many times over, there seems to be NO END POINT to refining, And I know I still can go further. if I have to.!

    thanks

    Dave m
  • David BDavid B Posts: 592
    edited 2010-11-24 13:06
    I'd suggest getting out a stack of blank paper and start sketching out your own flowchart.

    I remember one project I worked on (automating relational database replication) where the number of system states and possible state transitions were just too many for my mind to keep track of at once, yet if my script missed doing the right thing for any state, it could result in a failure of the system.

    All it took was a one page handwritten flowchart to show all possible state transitions, and the chart succeeded in discovering a couple of possible state transitions that I hadn't considered.

    This was nothing fancy, just rectangular boxes and arrows and text, but it detected basic design bugs that I wasn't able to see in the code itself. So it proved its value to me.

    Although to be clear, this was not "code to flowchart", as you first asked about, this was more "system needs to flowchart". In the end, the code implemented the chart, not the other way around.

    Like you, I had already made a first pass at the code, but after making the flowchart, it was much easier to follow the program flow and fix up existing code to be sure that the right things were being done.
  • DavidMDavidM Posts: 630
    edited 2010-11-24 13:25
    David B wrote: »
    I'd suggest getting out a stack of blank paper and start sketching out your own flowchart.

    I remember one project I worked on (automating relational database replication) where the number of system states and possible state transitions were just too many for my mind to keep track of at once, yet if my script missed doing the right thing for any state, it could result in a failure of the system.

    All it took was a one page handwritten flowchart to show all possible state transitions, and the chart succeeded in discovering a couple of possible state transitions that I hadn't considered.

    This was nothing fancy, just rectangular boxes and arrows and text, but it detected basic design bugs that I wasn't able to see in the code itself. So it proved its value to me.

    Although to be clear, this was not "code to flowchart", as you first asked about, this was more "system needs to flowchart". In the end, the code implemented the chart, not the other way around.

    Like you, I had already made a first pass at the code, but after making the flowchart, it was much easier to follow the program flow and fix up existing code to be sure that the right things were being done.

    HI Dave B,

    yes just a bout everything you have stated here is what I was trying to say!


    I DID get Paper and pen and started sketching my BLOCK's, first I had to check how many cogs I was using, and to see were I could COMBINE com cogs tother, I can now that I actually checked this.

    thanks

    Dave M
  • Heater.Heater. Posts: 21,230
    edited 2010-11-24 13:40
    That's the thing. If you make a flow chart of a tangled mess of code you just end up with a tangled mess of flow chart. The result is probably not easier to understand and worse still it probably contains errors. If the flow chart were mechanically generated it might be accurate but is sure to be unreadable.

    If your app has modes and states perhaps you need a state transition diagram. Which should have been drawn up before coding started.

    Given that Spin somewhat object oriented a picture of how the objects hang together can help you visualise where everything is, or perhaps where it should be:)

    Given that The Prop has multiple processes going on concurrently a data flow diagram can help make things clear.
  • Toby SeckshundToby Seckshund Posts: 2,027
    edited 2010-11-24 13:50
    I had looked at the blurb on Flowcode, for the AVR,PIC and ARM and thought that it would be great for simple stuff, but might get a nightmare on anything complex.

    http://www.matrixmultimedia.com/Flowcode3a-X.php
  • Heater.Heater. Posts: 21,230
    edited 2010-11-24 13:58
    Exactly, take any moderately complicated piece of code, does not even have to be very big. Run it through the flowcharting, auto-documenting, whatever tool of choice. Is it likely to be more understandable than what you started with?

    For example, if the operation of the fast fourier transform code below is not clear to you what tool would possibly help?

    That's not to say that there are not diagrams that can be drawn help explain how that thing works.
    /*
       This computes an in-place complex-to-complex FFT 
       x and y are the real and imaginary arrays of 2^m points.
       dir =  1 gives forward transform
       dir = -1 gives reverse transform 
    */
    short FFT(short int dir,long m,double *x,double *y)
    {
       long n,i,i1,j,k,i2,l,l1,l2;
       double c1,c2,tx,ty,t1,t2,u1,u2,z;
    
       /* Calculate the number of points */
       n = 1;
       for (i=0;i<m;i++) 
          n *= 2;
    
       /* Do the bit reversal */
       i2 = n >> 1;
       j = 0;
       for (i=0;i<n-1;i++) {
          if (i < j) {
             tx = x[i];
             ty = y[i];
             x[i] = x[j];
             y[i] = y[j];
             x[j] = tx;
             y[j] = ty;
          }
          k = i2;
          while (k <= j) {
             j -= k;
             k >>= 1;
          }
          j += k;
       }
    
       /* Compute the FFT */
       c1 = -1.0; 
       c2 = 0.0;
       l2 = 1;
       for (l=0;l<m;l++) {
          l1 = l2;
          l2 <<= 1;
          u1 = 1.0; 
          u2 = 0.0;
          for (j=0;j<l1;j++) {
             for (i=j;i<n;i+=l2) {
                i1 = i + l1;
                t1 = u1 * x[i1] - u2 * y[i1];
                t2 = u1 * y[i1] + u2 * x[i1];
                x[i1] = x[i] - t1; 
                y[i1] = y[i] - t2;
                x[i] += t1;
                y[i] += t2;
             }
             z =  u1 * c1 - u2 * c2;
             u2 = u1 * c2 + u2 * c1;
             u1 = z;
          }
          c2 = sqrt((1.0 - c1) / 2.0);
          if (dir == 1) 
             c2 = -c2;
          c1 = sqrt((1.0 + c1) / 2.0);
       }
    
       /* Scaling for forward transform */
       if (dir == 1) {
          for (i=0;i<n;i++) {
             x[i] /= n;
             y[i] /= n;
          }
       }
       
       return(TRUE);
    
    }
    
  • DavidMDavidM Posts: 630
    edited 2010-11-24 14:01
    Hi Guys,

    if you do a google image search using this text "block diagrams for code" you get heaps of images that show many different diagrams for code/electronics etc.

    dave M
  • Heater.Heater. Posts: 21,230
    edited 2010-11-24 14:09
    DavidM,

    That is true. How does it help?
  • DavidMDavidM Posts: 630
    edited 2010-11-24 14:10
    HI Heater,

    I know what you mean, I don't really need to digram the smallest pieces of code, its the relationship say between objects, methods, variables etc

    With that in mind one could then show the same diagram during different MODES or STATES



    Dave M
  • DavidMDavidM Posts: 630
    edited 2010-11-24 14:26
    I found a small movie that shows CODE to DIAGRAM

    http://www.safeactivation.com/movie/structure_chart_from_c.mp4

    Although this particular application looks complicated to set up the result is kind of what I am talking about

    I work with realbasic as well I think there already some app that work with realbasic

    thanks

    Dave M
  • Mike GreenMike Green Posts: 23,101
    edited 2010-11-24 15:19
    @DavidM -
    I don't think anyone so far has suggested that what you want is not impossible to do. Clearly, you could have some kind of tool that would go through a Spin program and make a list of methods, objects, constants, and variables referenced by each of the methods in the program. There could also be a tool that would take these lists and produce a chart like that shown in your video. Tools like these have been built before. The Sphinx Spin compiler source code is available and, other than lacking floating point constant expressions, it's pretty close to the Propeller Tool and BST in capability, so it could be used as a model for a program structure processing tool.

    The real question is who would do the substantial amount of work needed to make the tools. If it's not you, how would it get done? Parallax isn't going to do it. They're busy enough with the existing list of relatively minor enhancements to the Propeller Tool. Given Phil and Heaters' observations, both experienced programmers, it sounds like you're not going to see this sort of tool for Spin in the near future. So what do you do?

    For the future, you establish some rules for yourself about how you document all methods in a program. You come up with a format for comments for what calls what and what references what and you do the work while you write your new programs. If the format is clean enough, it might be easy to write a Python or Perl or similar program to go through the source program, extract the comments, and build a database and print reports.

    For the present, you can do the same, but after the fact. It sounds like you've got a deadline, so it's probably not going to happen for this program.
  • DavidMDavidM Posts: 630
    edited 2010-11-24 15:32
    Mike Green wrote: »
    @DavidM -
    It sounds like you've got a deadline, so it's probably not going to happen for this program.

    Yes Mike, you hit the nail on the head, But it never hurts to discuss things,


    thanks

    Dave M
  • obrienmobrienm Posts: 65
    edited 2010-11-24 16:54
    DavidM,
    The diagramming standard for software development since 1998 has been UML. The key to the spec was out of practicality in communicating the design and "ideally" analysing the algorithm before it was coded up. We used UML extensively when working with overseas teams (they actually drive the spec more).
    UML is to software design in the same way that schematics and datasheets are to hardware design.

    There are around 10-14 diagrams depending on the spec version 2.0 to 2.2 but in practice 4-5 are used (Class, activity, state, sequence and use-case(UC diagrams might make you laugh - but the others are essential). In your case you would benefit from sequence(time flow) state(behavior) and activity(object/function/flow interaction) which together are a replacement for flowcharts. The class diagram shows the static structure of class/spin/modules by illustrating all 3 inheritance(A gets all functionality from B + some from A), encapsulation(A cannot see inside B) and polymorphism(A can act as a B) aspects of OOD. Spin - not being a true OO language has encapsulation and a form of polymorphism via overriding/wrapping objects - but not inheritance (A type of B)
    You can do UML on paper, whiteboard or on a tool like Visio or $even Rational Developer. You can also generate UML directly from C++/C#/Java/Android/Smalltalk code in IDE's like Eclipse - but this will likely not help you as generated diagrams are usually not easy to read. I find it good to manually create subsets of the core loops or functionality and class/object hierarchy to understand these better on paper or a tool.

    As Mike writes -concurrency whether via multiple cogs sharing resources or threads in other virtual machine oriented languages - is very-very hard to diagram.

    History: Around 1998 Booch, Rumbaugh and Jacobson(We used his methods until 98) merged their FC and DFD replacements and joined Rational Software who along with ObjectTime (real time modelling and UML) were merged with IBM where the spec was further refined. Essentially the original details of the 1999 release are still used in the 6 core diagrams.

    I don't know if this helps but the spec may have some benefit to you.

    http://en.wikipedia.org/wiki/Unified_Modeling_Language
    (some diagrams in use)
    Sequence: (dynamic)
    http://1.bp.blogspot.com/_yohDTnOfkgY/TNYVMRWX4cI/AAAAAAAAADI/1ttfp1JWzKQ/s1600/eclipselink_uml_design_v20080125_batch_sequence.gif
    Class: (static)
    http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/metamodel_api#Metamodel_Implementation
    http://wiki.eclipse.org/EclipseLink/Development/Architecture/EclipseLink/ClassDiagram
    http://forums.parallax.com/attachment.php?attachmentid=75641&d=1290483082

    thank you
    /michael
  • DavidMDavidM Posts: 630
    edited 2010-11-24 17:40
    HI ObrienM


    Thats lots of good info you have posted ! much appreciated , I like reading stuff like this, remember I do realbasic programming as well, which means OOP, I try to implement what I learn in this language with my SPIN programming,and it helps a lot.


    That wiki link is a good read!


    thanks Mate!
    Dave M
Sign In or Register to comment.