Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7908239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:50:43+00:00 2026-06-03T11:50:43+00:00

I have a class with a graph inside. I iterate the graph and create

  • 0

I have a class with a graph inside. I iterate the graph and create a string that builds the graph, and then I just write that string into a Java file.
Is there a better way of doing this, i read about JDT and CodeModel but I really am needing some hint of how to get used with it.

EDIT

I am doing a regular expression code generator, so far I have converted the Regular Expression into a DFA represented in a directedgraph (using grail library). When I have the DFA the next step is to generate a class that have three methods, 1st one builds the same graph (DFA), 2nd method moves from one node to another, and the third method matches if the input string is accepted one. Only first method is changing depending on the regularexpression input, the other two are static and same for each generated java class.

My string based approach looks like:

 import grail.interfaces.DirectedEdgeInterface;
 import grail.interfaces.DirectedGraphInterface;
 import grail.interfaces.DirectedNodeInterface;
 import grail.interfaces.EdgeInterface;
 import grail.iterators.EdgeIterator;
 import grail.iterators.NodeIterator;
 import grail.properties.GraphProperties;
 import grail.setbased.SetBasedDirectedGraph;

 public class ClassName {

private SetBasedDirectedGraph graph = new SetBasedDirectedGraph();
private static DirectedNodeInterface state;
private static DirectedNodeInterface currentState;
protected DirectedEdgeInterface edge;

public ClassName() {
    buildGraph();
}

protected void buildGraph() {

    // Creating Graph Nodes (Automaton States)

    state = graph.createNode(3);
    state.setProperty(GraphProperties.LABEL, "3");
    state.setProperty(GraphProperties.DESCRIPTION, "null");
    graph.addNode(state);
    state = graph.createNode(2);
    state.setProperty(GraphProperties.LABEL, "2");
    state.setProperty(GraphProperties.DESCRIPTION, "null");
    graph.addNode(state);
    state = graph.createNode(1);
    state.setProperty(GraphProperties.LABEL, "1");
    state.setProperty(GraphProperties.DESCRIPTION, "Accepted");
    graph.addNode(state);
    state = graph.createNode(0);
    state.setProperty(GraphProperties.LABEL, "0");
    state.setProperty(GraphProperties.DESCRIPTION, "Initial");
    graph.addNode(state);
            .....


    // Creating Graph Edges (Automaton Transitions)

    edge = graph.createEdge(null, (DirectedNodeInterface) graph.getNode(2),
            (DirectedNodeInterface) graph.getNode(1));
    edge.setProperty((GraphProperties.LABEL), "0");
    graph.addEdge(edge);
    edge = graph.createEdge(null, (DirectedNodeInterface) graph.getNode(2),
            (DirectedNodeInterface) graph.getNode(2));
    edge.setProperty((GraphProperties.LABEL), "1");
    graph.addEdge(edge);
    edge = graph.createEdge(null, (DirectedNodeInterface) graph.getNode(1),
            (DirectedNodeInterface) graph.getNode(1));
    edge.setProperty((GraphProperties.LABEL), "0");
    graph.addEdge(edge);
    edge = graph.createEdge(null, (DirectedNodeInterface) graph.getNode(1),
            (DirectedNodeInterface) graph.getNode(3));
    edge.setProperty((GraphProperties.LABEL), "1");
    graph.addEdge(edge);
    edge = graph.createEdge(null, (DirectedNodeInterface) graph.getNode(0),
            (DirectedNodeInterface) graph.getNode(1));
    edge.setProperty((GraphProperties.LABEL), "0");
    graph.addEdge(edge);
    edge = graph.createEdge(null, (DirectedNodeInterface) graph.getNode(0),
            (DirectedNodeInterface) graph.getNode(2));
    edge.setProperty((GraphProperties.LABEL), "1");
    graph.addEdge(edge);
}
}  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T11:50:50+00:00Added an answer on June 3, 2026 at 11:50 am

    Another solution would be to stick to the current technology but provide a small layer with the builder pattern. To implement the builder you need a small one time effort, but get much better readable code.

    I implemented the first part of your code. With the proper builder you could write:

    graph = new GraphBuilder()
        .createNode(3).setLabel("3").setDescription("null").add()
        .createNode(2).setLabel("2").setDescription("null").add()
        .createNode(1).setLabel("1").setDescription("Accepted").add()
        .createNode(0).setLabel("0").setDescription("Initial").add()
        // unimplemented start
        .createEdge(2, 1).setLabel("0").add()
        .createEdge(2, 2).setLabel("1").add()
        .createEdge(1, 1).setLabel("0").add()
        .createEdge(1, 3).setLabel("1").add()
        .createEdge(0, 1).setLabel("0").add()
        .createEdge(0, 2).setLabel("1").add()
        // unimplemented end
        .build();
    

    Much more readable, isn’t it? To get this you need two builders. First comes the GraphBuilder:

    package at.corba.test.builder;
    
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    /**
     * Builder for generating graphs.
     * @author ChrLipp
     */
    public class GraphBuilder {
        /** List of StateBuilder, accesable via nodeNumber. */
        Map<Integer, StateBuilder> stateBuilderMap = new LinkedHashMap<Integer, StateBuilder>();
    
        /**
         * Delegates node-specific building to NodeBuilder.
         * @param nodeNumber Number of node to create
         * @return NodeBuilder for the node instance to create.
         */
        public StateBuilder createNode(final int nodeNumber) {
            StateBuilder builder = new StateBuilder(this);
            stateBuilderMap.put(nodeNumber, builder);
            return  builder;
        }
    
        /**
         * Builder function to initialise the graph.
         */
        public SetBasedDirectedGraph build() {
            SetBasedDirectedGraph graph = new SetBasedDirectedGraph();
    
            for (int key : stateBuilderMap.keySet()) {
                StateBuilder builder = stateBuilderMap.get(key);
                State state = graph.createNode(key);
                state = builder.build(state);
                graph.addNode(state);
            }
    
            return graph;
        }
    }
    

    and than the StateBuilder:

    package at.corba.test.builder;
    
    import java.util.HashMap;
    import java.util.Map;
    
    /**
     * Builder for generating states.
     * @author ChrLipp
     */
    public class StateBuilder {
        /** Parent builder */
        private final GraphBuilder graphBuilder;
    
        /** Properties for this node */
        Map<GraphProperties, String> propertyMap = new HashMap<GraphProperties, String>();
    
        /**
         * ctor.
         * @param graphBuilder  Link to parent builder
         * @param nodeNumber    Node to create
         */
        public StateBuilder(final GraphBuilder graphBuilder)  {
            this.graphBuilder = graphBuilder;
        }
    
        /**
         * Property setter for property Label.
         * @param label value for property label
         * @return current NodeBuilder instance for method chaining
         */
        public StateBuilder setLabel(final String label) {
            propertyMap.put(GraphProperties.LABEL, label);
            return this;
        }
    
        /**
         * Property setter for description Label.
         * @param description value for description label
         * @return current NodeBuilder instance for method chaining
         */
        public StateBuilder setDescription(final String description) {
            propertyMap.put(GraphProperties.DESCRIPTION, description);
            return this;
        }
    
        /**
         * DSL function to close the node section and to return control to the parent builder.
         * @return
         */
        public GraphBuilder add() {
            return graphBuilder;
        }
    
        /**
         * Builder function to initialise the node.
         * @return newly generated node
         */
        public State build(final State state) {
            for (GraphProperties key : propertyMap.keySet()) {
                String value = propertyMap.get(key);
                state.setProperty(key, value);
            }
    
            return state;
        }
    }
    

    You would do the same for edges, but I did not implement this 🙂 .
    In Groovy it is even more easier to create builders (my implementation is a builder written in Java), see for example Make a builder.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class Vertex{ Graph _graph; float x; float y; string key; //and some
I have class method that returns a list of employees that I can iterate
I have a function (assign) in a class (graph) in a header file. The
In Python, I have a Graph class that has a dictionary of vertex objects.
I have a graph class that employs two dimension vertices using a map with
I have a class that does an implementation of a graph (template class) in
I have an app that does an iteration to create points on a graph
I have a class Graph with two lists types namely nodes and edges I
I have Vertex template in vertex.h. From my graph.h: 20 template<class edgeDecor, class vertexDecor,
I have class with a member function that takes a default argument. struct Class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.