Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've used a lesser known product called <a href="http://www.freemarker.org/" rel="nofollow">FreeMarker</a> for several projects that needed code generation (such as encode/decode classes for messages). It is a Java based solution in which you generate a memory model and feed it to a template. From their home page:</p> <blockquote> <p>FreeMarker is a "template engine"; a generic tool to generate text output (anything from HTML to autogenerated source code) based on templates. It's a Java package, a class library for Java programmers. It's not an application for end-users in itself, but something that programmers can embed into their products.</p> </blockquote> <p>To use FreeMarker, create a data model and a template to produce the code for the class you are trying to build. This solution has extra learning overhead, but should be easy to learn, and is incredibly useful for future code generation requirements and other projects in the future.</p> <p>Update: Here is a template for the class specified in the question (Note: I have not tested it):</p> <pre><code>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) &lt;#list nodes as node&gt; state = graph.createNode(${node.id}); state.setProperty(GraphProperties.LABEL, "${node.id}"); state.setProperty(GraphProperties.DESCRIPTION, "null"); graph.addNode(state); &lt;/#list&gt; // Creating Graph Edges (Automaton Transitions) &lt;#assign edgeCount = 0&gt; &lt;#list nodes as node1&gt; &lt;#list nodes as node2&gt; edge = graph.createEdge(null, (DirectedNodeInterface) graph.getNode(${node1.id}), (DirectedNodeInterface) graph.getNode(${node2.id})); edge.setProperty((GraphProperties.LABEL), "${edgeCount}"); graph.addEdge(edge); &lt;#assign edgeCount = edgeCount + 1&gt; &lt;/#list&gt; &lt;/#list&gt; } } </code></pre> <p>Your data model should be fairly simple--a Map containing one key who's value is a List of nodes. If you later find your template needs more information, you can change your data model at any time. Any Java object should work within the data model so long as the required fields are public or have public getters.</p> <pre><code>Map&lt;String, Object&gt; root = new HashMap&lt;String, Object&gt;(); List&lt;Integer&gt; nodes = new ArrayList&lt;Integer&gt;(); nodes.add(1); nodes.add(2); ... root.put("nodes", nodes); </code></pre> <p>See <a href="http://www.freemarker.org/docs/pgui_quickstart_createdatamodel.html" rel="nofollow">this</a> page in the FreeMarker manual for a great example for data models using Maps.</p> <p>The next step would be to use the FreeMarker API to combine the template + data model to create the class. Here is an <a href="http://www.freemarker.org/docs/pgui_quickstart_all.html" rel="nofollow">example</a> from the FreeMarker manual I have modified for your case:</p> <pre><code>import freemarker.template.*; import java.util.*; import java.io.*; public class Test { public static void main(String[] args) throws Exception { /* ------------------------------------------------------------------- */ /* You should do this ONLY ONCE in the whole application life-cycle: */ /* Create and adjust the configuration */ Configuration cfg = new Configuration(); cfg.setDirectoryForTemplateLoading( new File("/where/you/store/templates")); cfg.setObjectWrapper(new DefaultObjectWrapper()); /* ------------------------------------------------------------------- */ /* You usually do these for many times in the application life-cycle: */ /* Get or create a template */ Template temp = cfg.getTemplate("test.ftl"); /* Create a data-model */ Map&lt;String, Object&gt; root = new HashMap&lt;String, Object&gt;(); List&lt;Integer&gt; nodes = new ArrayList&lt;Integer&gt;(); nodes.add(1); nodes.add(2); ... root.put("nodes", nodes); /* Merge data-model with template */ Writer out = new OutputStreamWriter(System.out); temp.process(root, out); out.flush(); } } </code></pre> <p>The FreeMarker manual is very helpful and contains many useful examples. See the <a href="http://www.freemarker.org/docs/pgui_quickstart.html" rel="nofollow">Getting Started</a> guide if you're interested in this approach. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload