6
$\begingroup$

Is it possible in Mathematica to write a program inside a graph? For example if you have a bunch of different If[] statements that you need to embed within one another, instead of writing them all within one another and getting very confused as to where in the control flow you are up to, is it possible to somehow embed these within a graph, so that you can more clearly keep track of precisely where you are?

Thank you

$\endgroup$
3
  • $\begingroup$ You can certainly use a Graph object in such a way, e.g., putting the "logic" in vertex/edge custom properties. But, you'd need to handle what MMA does with it (a simple parser), since on their own they have no meaning to MMA (they're custom, after all). I'd also suggest if nesting of If is such it's confusing, a different control-of-flow construct, or a refactoring of code, might be in order. $\endgroup$
    – ciao
    Commented Mar 1, 2014 at 19:48
  • $\begingroup$ Do you mean that instead of writing code you'd like to use a flowchart to describe control flow within the program? $\endgroup$
    – Szabolcs
    Commented Mar 1, 2014 at 20:47
  • $\begingroup$ Yes, @Szabolcs, exactly. $\endgroup$
    – Aron
    Commented Mar 1, 2014 at 23:31

2 Answers 2

6
$\begingroup$

Let me know if this doesn't cut as an answer.

If your goal is a visual programming language with flow-chart paradigm - it doesn't have to be a Graph that Wolfram Language (WL) uses, but could be another dedicated flow-language that can drive WL computations.

Mohamed Zaghloul is developing Mantis add-on tools for Grasshopper to link it with the Mathematica kernel. I like the idea - so this is why I give detailed information. I am sure some folks could use it.

You already can use it - just get all components.

Screen shots of the environment - yellow cells feature actual WL code:

enter image description here

enter image description here

enter image description here

$\endgroup$
1
$\begingroup$

Here's a completely contrived example that gets to the gist of my comment. I store the "logic" of the nested If as custom vertex properties, and simply follow the results, highlighting the appropriate edges. Clearly, code can use the results for control of flow, and that flow can be visualized with the graph. If you really wanted to do this kind of thing, packaging up some functions to parse your logic and create the graph/properties/query API would make sense.

ClearAll[gr, x, y, z]

Manipulate[
 With[{x = x, y = y, z = z}, 
  HighlightGraph[
   gr, {a \[DirectedEdge] (res = ReleaseHold@PropertyValue[{gr, a}, "Logic"]), 
    res \[DirectedEdge] 
     ReleaseHold@PropertyValue[{gr, res}, "Logic"]}]], {x, {1, 2}}, {y, {1, 2}}, {z, {1, 2}}, 
 Initialization :> {gr = 
    Graph[{Property[a, "Logic" -> Hold[If[x == 1, b, c]]], 
      Property[b, "Logic" -> Hold[If[y == 1, d, e, d]]], 
      Property[c, "Logic" -> Hold[If[z == 1, f, g, f]]], d, e, f, 
      g}, {a \[DirectedEdge] b, a \[DirectedEdge] c, 
      b \[DirectedEdge] d, b \[DirectedEdge] e, c \[DirectedEdge] f, 
      c \[DirectedEdge] g}, VertexLabels -> "Name"]}]

enter image description here

$\endgroup$
3
  • $\begingroup$ Thank you @rasher, but is there a way to then compile this graph into a usable function? or will this only work for illustrative purposes? $\endgroup$
    – Aron
    Commented Mar 2, 2014 at 13:08
  • $\begingroup$ @Aron: this was, as I said, to illustrate the idea in the comment, and as said there, you'd have to write the framework yourself to do such things. MMA is not a data-flow language, nor a graph-based language, so if you want/need that you need to do it yourself (I'm not aware of any packages in those areas either). $\endgroup$
    – ciao
    Commented Mar 2, 2014 at 22:41
  • $\begingroup$ Ok, thank you, @rasher $\endgroup$
    – Aron
    Commented Mar 2, 2014 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.