Java: treat String as a command without using exec

snotnose

Ars Praefectus
3,454
Subscriptor
I wish I'd found Graphstream last week, it does everything I need from a display standpoint. For example:
Code:
public class DemoGraphstream {
    void main() {
        System.setProperty("org.graphstream.ui", "swing");
        Graph graph = new SingleGraph("Tutorial 1");
        graph.addNode("A");
        graph.addNode("B");
        graph.addNode("C");
        graph.addEdge("AB", "A", "B");
        graph.addEdge("BC", "B", "C");
        graph.addEdge("CA", "C", "A");
        graph.display();
}
I have a parser that returns a List<String> of addNode and addEdge lines. Which is way cool, I can now skip the copy-paste I was doing. Except, now that I have the string "graph.addNode("A");", what do I do with it?

It occurs to me my parser can just return the list of nodes and edges, instead of strings. Which I will do tomorrow.

But now I wanna know, in Java (or Kotlin), how do I treat a string like a line of code?
 

Fruity Pebbles

Ars Praetorian
595
Subscriptor
Instead of generating code strings you could generate lists of parameters. Have the parser return two lists, a List<String> that contains the node parameters and a List<List<String>> that contains the edge parameters. Then the code is something like

Code:
for each name in nodeList
    graph.addNode(name)
for each list in edgeList
    graph.addEdge(list[0], list[1], list[2])
 

ramases

Ars Tribunus Angusticlavius
9,015
Subscriptor++
But now I wanna know, in Java (or Kotlin), how do I treat a string like a line of code?

The JVM ecosystem has a ... strong cultural aversion to this type of data/code commingling, due to the dangers it brings.

The traditional way to have dynamic code generation and execution on the JVM is to use something like Javassist (https://github.com/jboss-javassist/javassist) or ByteBuddy (https://github.com/raphw/byte-buddy) to generate a type with whatever behaviour you want it to have, then call it.

If you really want to, you can use something like JShell or BeanShell.
 

snotnose

Ars Praefectus
3,454
Subscriptor
The answers above cover it. I just find it amusing you're trying to do something very similar to what I'm currently working on, but I'm in C++. My graph inputs are circuit netlists. I ended up writing my own simple graph implementation because the one in boost was just incomprehensible.
I strongly recommend Graphstream.

Jgrapht - looks good but after a week I still couldn't figure out how to display my graph correctly. It's like getting an Ikea dresser except it has no instructions and you've never seen a dresser before so you don't know what it should look like.
gelphi - almost worked but it's a separate program
Graphstream - Works like a champ, but looks to be abandoned. No updates in 5 years and a lot of dead links at the website. Nowhere near as hard to figure out from the source as Jgrapht.

Turns out graph manipulation is easy, displaying that graph not so much.

I'm using Kotlin, not C++, YMMV.
 
Last edited: