OSDN Git Service

graphviz output of the SilkSchema
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Tue, 7 Jul 2009 02:43:38 +0000 (02:43 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Tue, 7 Jul 2009 02:43:38 +0000 (02:43 +0000)
git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3444 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd

src/main/java/org/xerial/silk/schema/SilkAttribute.java
src/main/java/org/xerial/silk/schema/SilkClass.java
src/main/java/org/xerial/silk/schema/SilkModule.java
src/main/java/org/xerial/silk/schema/SilkSchema.java
src/main/java/org/xerial/util/StringUtil.java
src/main/java/org/xerial/util/graph/Automaton.java
src/main/java/org/xerial/util/graph/GraphvizHelper.java
src/test/java/org/xerial/silk/schema/SilkSchemaTest.java
src/test/java/org/xerial/silk/schema/schema.silk
src/test/java/org/xerial/util/graph/LatticeTest.java

index 4bb4aab..2964204 100644 (file)
@@ -31,9 +31,8 @@ package org.xerial.silk.schema;
  * \r
  */\r
 public class SilkAttribute {\r
-    public static final String DEFAULT_TYPE = "default";\r
 \r
-    public String typeName = DEFAULT_TYPE;\r
+    public String typeName = null;\r
     public String name;\r
     public boolean isArray = false;\r
 \r
index add0b2d..93d01c3 100644 (file)
@@ -24,6 +24,7 @@
 //--------------------------------------\r
 package org.xerial.silk.schema;\r
 \r
+import java.util.ArrayList;\r
 import java.util.List;\r
 \r
 /**\r
@@ -38,8 +39,8 @@ public class SilkClass {
     public String parent = null;\r
 \r
     public List<String> mixin;\r
-    public List<SilkAttribute> attribute;\r
+    public List<SilkAttribute> attribute = new ArrayList<SilkAttribute>();\r
 \r
-    public List<SilkIndexTarget> index;\r
+    public List<SilkIndexTarget> index = new ArrayList<SilkIndexTarget>();\r
 \r
 }\r
index 77c042c..ef0f9cc 100644 (file)
@@ -24,6 +24,7 @@
 //--------------------------------------\r
 package org.xerial.silk.schema;\r
 \r
+import java.util.ArrayList;\r
 import java.util.List;\r
 \r
 /**\r
@@ -37,5 +38,5 @@ public class SilkModule {
 \r
     public String name = ROOT_MODULE_NAME;\r
 \r
-    public List<SilkClass> classDef;\r
+    public List<SilkClass> classDef = new ArrayList<SilkClass>();\r
 }\r
index 89170a3..83a5c68 100644 (file)
@@ -26,7 +26,10 @@ package org.xerial.silk.schema;
 \r
 import java.io.IOException;\r
 import java.io.Reader;\r
+import java.io.StringWriter;\r
+import java.util.ArrayList;\r
 import java.util.List;\r
+import java.util.Properties;\r
 \r
 import org.antlr.runtime.ANTLRReaderStream;\r
 import org.antlr.runtime.CommonTokenStream;\r
@@ -39,13 +42,14 @@ import org.xerial.silk.schema.impl.SilkSchemaLexer;
 import org.xerial.silk.schema.impl.SilkSchemaParser;\r
 import org.xerial.util.StringUtil;\r
 import org.xerial.util.antlr.ANTLRUtil;\r
+import org.xerial.util.graph.GraphvizHelper;\r
 import org.xerial.util.log.Logger;\r
 \r
 public class SilkSchema {\r
     private static Logger _logger = Logger.getLogger(SilkSchema.class);\r
 \r
-    public List<SilkModule> module;\r
-    public List<SilkRelation> relation;\r
+    public List<SilkModule> module = new ArrayList<SilkModule>();\r
+    public List<SilkRelation> relation = new ArrayList<SilkRelation>();\r
 \r
     /**\r
      * parse the input schema file and return the parse result in the form of\r
@@ -87,4 +91,69 @@ public class SilkSchema {
 \r
     }\r
 \r
+    public String toGraphviz() {\r
+        StringWriter buf = new StringWriter();\r
+        GraphvizHelper g = new GraphvizHelper(buf);\r
+        g.beginDigraph("G");\r
+        g.graphOption("font=Arial");\r
+\r
+        int moduleCount = 1;\r
+        for (SilkModule eachModule : module) {\r
+            String moduleID = String.format("m%d", moduleCount++);\r
+            g.node(moduleID, eachModule.name);\r
+\r
+            for (SilkClass eachClass : eachModule.classDef) {\r
+                String classID = String.format("c_%s", eachClass.name);\r
+\r
+                g.node(classID, eachClass.name);\r
+                // parent\r
+                if (eachClass.parent != null) {\r
+                    String parentID = String.format("c_%s", eachClass.parent);\r
+\r
+                    Properties prop = new Properties();\r
+                    prop.put("arrowhead", StringUtil.doubleQuote("none"));\r
+                    prop.put("arrowtail", StringUtil.doubleQuote("empty"));\r
+                    prop.put("colro", "gray");\r
+                    g.edge(parentID, classID, prop);\r
+                }\r
+                else {\r
+                    // module to class\r
+                    g.edge(moduleID, classID);\r
+                }\r
+\r
+                // attributes\r
+                int attributeCount = 1;\r
+                for (SilkAttribute eachAttribute : eachClass.attribute) {\r
+                    String attribID = String.format("%s_a%d", classID, attributeCount++);\r
+                    {\r
+                        String label = eachAttribute.name;\r
+                        if (eachAttribute.typeName != null)\r
+                            label += String.format(" [%s]", eachAttribute.typeName);\r
+\r
+                        Properties prop = new Properties();\r
+                        prop.put("label", StringUtil.doubleQuote(label));\r
+                        prop.put("shape", "box");\r
+                        g.node(attribID, prop);\r
+                    }\r
+\r
+                    if (eachAttribute.isArray) {\r
+                        Properties edgeProp = new Properties();\r
+                        edgeProp.put("color", "red");\r
+                        g.edge(classID, attribID, edgeProp);\r
+                    }\r
+                    else\r
+                        g.edge(classID, attribID);\r
+\r
+                }\r
+\r
+            }\r
+        }\r
+\r
+        g.endDigraph();\r
+\r
+        g.flush();\r
+\r
+        return buf.toString();\r
+    }\r
+\r
 }\r
index a6d8282..8ec164e 100644 (file)
@@ -35,14 +35,12 @@ import java.util.regex.Pattern;
  * @author leo
  * 
  */
-public class StringUtil
-{
+public class StringUtil {
     public static final String SINGLE_QUOTE = "\'";
     public static final String DOUBLE_QUOTE = "\"";
     public static final String NEW_LINE = System.getProperty("line.separator");
 
-    private StringUtil()
-    {}
+    private StringUtil() {}
 
     /**
      * Concatenates all elements in the given collection c into a single string
@@ -54,8 +52,7 @@ public class StringUtil
      *            a concatenator: ex. ", ", "." etc.
      * @return a concatenated string
      */
-    public static <T> String join(Collection<T> c, String concatinator)
-    {
+    public static <T> String join(Collection<T> c, String concatinator) {
         if (c == null)
             return "";
         int size = c.size();
@@ -64,8 +61,7 @@ public class StringUtil
 
         Iterator<T> it = c.iterator();
         StringBuilder buf = new StringBuilder();
-        for (int i = 0; it.hasNext() && i < size - 1; i++)
-        {
+        for (int i = 0; it.hasNext() && i < size - 1; i++) {
             Object data = it.next();
             if (data != null)
                 buf.append(data.toString());
@@ -91,8 +87,7 @@ public class StringUtil
      *            a concatenator: ex. ", ", "." etc.
      * @return the concatenated string
      */
-    public static String join(Object[] c, String concatinator)
-    {
+    public static String join(Object[] c, String concatinator) {
         if (c == null)
             return "";
         int size = c.length;
@@ -100,8 +95,7 @@ public class StringUtil
             return "";
 
         StringBuilder buf = new StringBuilder();
-        for (int i = 0; i < size - 1; i++)
-        {
+        for (int i = 0; i < size - 1; i++) {
             Object data = c[i];
             buf.append(data != null ? data.toString() : "");
             buf.append(concatinator);
@@ -119,19 +113,20 @@ public class StringUtil
      *            strings.
      * @return the quoted message
      */
-    public static String quote(String message, String quotationMark)
-    {
+    public static String quote(String message, String quotationMark) {
         if (message == null)
             return message;
-        return quotationMark + message + quotationMark;
+        return String.format("%s%s%s", quotationMark, message, quotationMark);
     }
 
-    public static String unquote(String message)
-    {
+    public static String doubleQuote(String message) {
+        return quote(message, DOUBLE_QUOTE);
+    }
+
+    public static String unquote(String message) {
         if (message == null)
             return message;
-        if (message.length() > 1)
-        {
+        if (message.length() > 1) {
             if ((message.charAt(0) == '"' && message.charAt(message.length() - 1) == '"')
                     || (message.charAt(0) == '\'' && message.charAt(message.length() - 1) == '\''))
                 return message.substring(1, message.length() - 1);
@@ -141,19 +136,16 @@ public class StringUtil
 
     static Pattern _whiteSpacePattern = Pattern.compile("[ \t\n\r]*");
 
-    static public boolean isWhiteSpace(String s)
-    {
+    static public boolean isWhiteSpace(String s) {
         Matcher m = _whiteSpacePattern.matcher(s);
         return m.matches();
     }
 
-    public static String concatinateWithTab(Object... data)
-    {
+    public static String concatinateWithTab(Object... data) {
         return StringUtil.join(data, "\t");
     }
 
-    public static String newline()
-    {
+    public static String newline() {
         return NEW_LINE;
     }
 }
index 354f245..9b134b0 100644 (file)
@@ -46,25 +46,21 @@ import org.xerial.util.StringUtil;
  * @param <State>\r
  * @param <Symbol>\r
  */\r
-public class Automaton<State, Symbol>\r
-{\r
+public class Automaton<State, Symbol> {\r
     private List<AutomatonListener<State, Symbol>> listenerList = new ArrayList<AutomatonListener<State, Symbol>>();\r
     private IndexedSet<State> stateSet = new IndexedSet<State>();\r
     private Set<State> terminalState = new HashSet<State>();\r
     private HashMap<State, HashMap<Symbol, State>> transition = new HashMap<State, HashMap<Symbol, State>>();\r
     private HashMap<State, State> transitionForOtherInput = new HashMap<State, State>();\r
 \r
-    private class CursorImpl implements AutomatonCursor<State, Symbol>\r
-    {\r
+    private class CursorImpl implements AutomatonCursor<State, Symbol> {\r
         State currentState;\r
 \r
-        public CursorImpl(State initialState)\r
-        {\r
+        public CursorImpl(State initialState) {\r
             currentState = initialState;\r
         }\r
 \r
-        public boolean canAccept(Symbol input)\r
-        {\r
+        public boolean canAccept(Symbol input) {\r
             if (canAcceptAnySymbol())\r
                 return true;\r
 \r
@@ -75,44 +71,38 @@ public class Automaton<State, Symbol>
             return getTransitionTableOf(currentState).containsKey(input);\r
         }\r
 \r
-        public Set<Symbol> getAcceptableSymbolSet()\r
-        {\r
+        public Set<Symbol> getAcceptableSymbolSet() {\r
             return getAcceptableSymbolSetOf(currentState);\r
         }\r
 \r
-        public boolean canAcceptAnySymbol()\r
-        {\r
+        public boolean canAcceptAnySymbol() {\r
             return transitionForOtherInput.containsKey(currentState);\r
         }\r
 \r
-        public State getState()\r
-        {\r
+        public State getState() {\r
             return currentState;\r
         }\r
 \r
-        public boolean isTerminal()\r
-        {\r
+        public boolean isTerminal() {\r
             return terminalState.contains(currentState);\r
         }\r
 \r
-        public AutomatonCursor<State, Symbol> reset(State state)\r
-        {\r
+        public AutomatonCursor<State, Symbol> reset(State state) {\r
             if (!hasState(state))\r
-                throw new XerialError(XerialErrorCode.INVALID_INPUT, "no such state found: " + state);\r
+                throw new XerialError(XerialErrorCode.INVALID_INPUT, "no such state found: "\r
+                        + state);\r
             currentState = state;\r
             return this;\r
         }\r
 \r
-        public State transit(Symbol input)\r
-        {\r
+        public State transit(Symbol input) {\r
             Map<Symbol, State> transitionTable = getTransitionTableOf(currentState);\r
             State nextState = transitionTable.get(input);\r
             if (nextState == null)\r
                 return otherTransit(input);\r
 \r
             // notify the transition to listeners\r
-            for (AutomatonListener<State, Symbol> each : listenerList)\r
-            {\r
+            for (AutomatonListener<State, Symbol> each : listenerList) {\r
                 each.onTansit(currentState, input, nextState);\r
             }\r
 \r
@@ -120,52 +110,45 @@ public class Automaton<State, Symbol>
             return currentState;\r
         }\r
 \r
-        private State otherTransit(Symbol input)\r
-        {\r
-            if (transitionForOtherInput.containsKey(currentState))\r
-            {\r
+        private State otherTransit(Symbol input) {\r
+            if (transitionForOtherInput.containsKey(currentState)) {\r
                 currentState = transitionForOtherInput.get(currentState);\r
                 return currentState;\r
             }\r
-            else\r
-            {\r
-                throw new XerialError(XerialErrorCode.INVALID_INPUT, "cannot accept the symbol:" + input);\r
+            else {\r
+                throw new XerialError(XerialErrorCode.INVALID_INPUT, "cannot accept the symbol:"\r
+                        + input);\r
             }\r
         }\r
 \r
     }\r
 \r
-    private Map<Symbol, State> getTransitionTableOf(State state)\r
-    {\r
+    private Map<Symbol, State> getTransitionTableOf(State state) {\r
         HashMap<Symbol, State> result = transition.get(state);\r
-        if (result == null)\r
-        {\r
+        if (result == null) {\r
             result = new HashMap<Symbol, State>();\r
             transition.put(state, result);\r
         }\r
         return result;\r
     }\r
 \r
-    public AutomatonCursor<State, Symbol> cursor(State initialState)\r
-    {\r
+    public AutomatonCursor<State, Symbol> cursor(State initialState) {\r
         return new CursorImpl(initialState);\r
     }\r
 \r
-    public Set<State> getStateSet()\r
-    {\r
+    public Set<State> getStateSet() {\r
         return stateSet;\r
     }\r
 \r
-    public Set<Symbol> getAcceptableSymbolSetOf(State state)\r
-    {\r
+    public Set<Symbol> getAcceptableSymbolSetOf(State state) {\r
         if (!hasState(state))\r
-            throw new XerialError(XerialErrorCode.INVALID_INPUT, String.format("state %s not found", state));\r
+            throw new XerialError(XerialErrorCode.INVALID_INPUT, String.format(\r
+                    "state %s not found", state));\r
 \r
         return getTransitionTableOf(state).keySet();\r
     }\r
 \r
-    public Set<State> getAdjacentStates(State from)\r
-    {\r
+    public Set<State> getAdjacentStates(State from) {\r
         HashSet<State> result = new HashSet<State>();\r
         if (transitionForOtherInput.containsKey(from))\r
             result.add(transitionForOtherInput.get(from));\r
@@ -174,95 +157,81 @@ public class Automaton<State, Symbol>
         return result;\r
     }\r
 \r
-    public void addState(State state)\r
-    {\r
+    public void addState(State state) {\r
         stateSet.add(state);\r
     }\r
 \r
-    public void addTerminalState(State state)\r
-    {\r
+    public void addTerminalState(State state) {\r
         addState(state);\r
         terminalState.add(state);\r
     }\r
 \r
-    public void addTransition(State from, Symbol input, State to)\r
-    {\r
+    public void addTransition(State from, Symbol input, State to) {\r
         addState(from);\r
         addState(to);\r
 \r
         getTransitionTableOf(from).put(input, to);\r
     }\r
 \r
-    public void addStarTransition(State from, State to)\r
-    {\r
+    public void addStarTransition(State from, State to) {\r
         addState(from);\r
         addState(to);\r
 \r
         transitionForOtherInput.put(from, to);\r
     }\r
 \r
-    public void addListener(AutomatonListener<State, Symbol> listener)\r
-    {\r
+    public void addListener(AutomatonListener<State, Symbol> listener) {\r
         this.listenerList.add(listener);\r
     }\r
 \r
-    public void remvoeListener(AutomatonListener<State, Symbol> listener)\r
-    {\r
+    public void remvoeListener(AutomatonListener<State, Symbol> listener) {\r
         this.listenerList.remove(listener);\r
     }\r
 \r
-    public void clearListner()\r
-    {\r
+    public void clearListner() {\r
         this.listenerList.clear();\r
     }\r
 \r
-    public boolean hasState(State state)\r
-    {\r
+    public boolean hasState(State state) {\r
         return stateSet.contains(state);\r
     }\r
 \r
-    public String toString()\r
-    {\r
+    public String toString() {\r
         ArrayList<String> edgeData = new ArrayList<String>();\r
-        for (State from : transition.keySet())\r
-        {\r
+        for (State from : transition.keySet()) {\r
             int fromID = stateSet.getID(from);\r
-            for (Entry<Symbol, State> eachTransition : getTransitionTableOf(from).entrySet())\r
-            {\r
-                edgeData.add(String.format("%s -> %s [%s]", from, eachTransition.getValue(), eachTransition.getKey()));\r
+            for (Entry<Symbol, State> eachTransition : getTransitionTableOf(from).entrySet()) {\r
+                edgeData.add(String.format("%s -> %s [%s]", from, eachTransition.getValue(),\r
+                        eachTransition.getKey()));\r
             }\r
 \r
-            if (transitionForOtherInput.containsKey(from))\r
-            {\r
-                edgeData.add(String.format("%s -> %s [*]", from, transitionForOtherInput.get(from)));\r
+            if (transitionForOtherInput.containsKey(from)) {\r
+                edgeData\r
+                        .add(String.format("%s -> %s [*]", from, transitionForOtherInput.get(from)));\r
             }\r
         }\r
 \r
-        return String\r
-                .format("node:\n%s\nedge:\n%s", StringUtil.join(stateSet, ",\n"), StringUtil.join(edgeData, ",\n"));\r
+        return String.format("node:\n%s\nedge:\n%s", StringUtil.join(stateSet, ",\n"), StringUtil\r
+                .join(edgeData, ",\n"));\r
     }\r
 \r
-    public String toGraphviz()\r
-    {\r
+    public String toGraphviz() {\r
         StringWriter writer = new StringWriter();\r
         GraphvizHelper g = new GraphvizHelper(writer);\r
         g.beginDigraph("G");\r
-        for (State each : stateSet)\r
-        {\r
+        for (State each : stateSet) {\r
             g.node(stateSet.getID(each), each.toString());\r
         }\r
 \r
-        for (State from : transition.keySet())\r
-        {\r
+        for (State from : transition.keySet()) {\r
             int fromID = stateSet.getID(from);\r
-            for (Entry<Symbol, State> eachTransition : getTransitionTableOf(from).entrySet())\r
-            {\r
-                g.edge(fromID, stateSet.getID(eachTransition.getValue()), eachTransition.getKey().toString());\r
+            for (Entry<Symbol, State> eachTransition : getTransitionTableOf(from).entrySet()) {\r
+                g.edge(fromID, stateSet.getID(eachTransition.getValue()), eachTransition.getKey()\r
+                        .toString());\r
             }\r
 \r
-            if (transitionForOtherInput.containsKey(from))\r
-            {\r
-                g.edge(fromID, transitionForOtherInput.get(from), "*");\r
+            if (transitionForOtherInput.containsKey(from)) {\r
+                g.edge(Integer.toString(fromID), transitionForOtherInput.get(from).toString(), "*");\r
             }\r
         }\r
 \r
index 5319f83..57c2d44 100644 (file)
@@ -27,7 +27,12 @@ package org.xerial.util.graph;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.io.Writer;
+import java.util.ArrayList;
+import java.util.Properties;
 import java.util.Stack;
+import java.util.Map.Entry;
+
+import org.xerial.util.StringUtil;
 
 /**
  * A helper class for generateing Graphviz's dot file
@@ -35,21 +40,18 @@ import java.util.Stack;
  * @author leo
  * 
  */
-public class GraphvizHelper
-{
+public class GraphvizHelper {
     private PrintWriter _out;
     private Stack<GraphvizComponent> _componentStack = new Stack<GraphvizComponent>();
 
     /**
      * 
      */
-    public GraphvizHelper(OutputStream out)
-    {
+    public GraphvizHelper(OutputStream out) {
         _out = new PrintWriter(out);
     }
 
-    public GraphvizHelper(Writer out)
-    {
+    public GraphvizHelper(Writer out) {
         _out = new PrintWriter(out);
     }
 
@@ -59,13 +61,11 @@ public class GraphvizHelper
      * 
      * @param graphName
      */
-    public void beginDigraph(String graphName)
-    {
+    public void beginDigraph(String graphName) {
         pushComponent(new Digraph(graphName));
     }
 
-    public void endDigraph()
-    {
+    public void endDigraph() {
         assert !_componentStack.empty();
         GraphvizComponent lastComponent = popComponent();
         assert lastComponent instanceof Digraph;
@@ -73,86 +73,99 @@ public class GraphvizHelper
         flush();
     }
 
-    public void node(Object nodeName, Object label)
-    {
-        _out.println(String.format("%s [label=\"%s\"];", nodeName.toString(), label.toString()));
+    public void node(String nodeID, String label) {
+        _out.println(String.format("%s [label=\"%s\"];", nodeID, label.toString()));
+    }
+
+    public void node(int nodeID, String label) {
+        _out.println(String.format("%d [label=\"%s\"];", nodeID, label.toString()));
+    }
+
+    public void node(String nodeID, Properties options) {
+        _out.println(String.format("%s [%s]", nodeID, format(options)));
+    }
+
+    private String format(Properties options) {
+        ArrayList<String> buf = new ArrayList<String>(options.size());
+        for (Entry<Object, Object> each : options.entrySet()) {
+            buf.add(String.format("%s=%s", each.getKey(), each.getValue()));
+        }
+
+        return StringUtil.join(buf, ", ");
+    }
+
+    public void graphOption(String option) {
+        _out.println(String.format("graph [%s];", option));
+    }
+
+    public void edge(String fromNodeID, String toNodeID) {
+        _out.println(fromNodeID + " -> " + toNodeID + ";");
+    }
+
+    public void edge(String fromNodeID, String toNodeID, Properties prop) {
+        _out.println(String.format("%s -> %s [%s];", fromNodeID, toNodeID, format(prop)));
     }
 
-    public void option(String option)
-    {
-        _out.println(String.format("[];", option));
+    public void edge(int fromNodeID, int toNodeID) {
+        _out.println(fromNodeID + " -> " + toNodeID + ";");
     }
 
-    public void edge(Object nodeFrom, Object nodeTo)
-    {
-        _out.println(nodeFrom.toString() + " -> " + nodeTo.toString() + ";");
+    public void edge(String fromNodeID, String toNodeID, String label) {
+        _out.println(String.format("%s -> %s [label=\"%s\"];", fromNodeID, toNodeID, label));
     }
 
-    public void edge(Object nodeFrom, Object nodeTo, String label)
-    {
-        _out.println(String.format("%s -> %s [label=\"%s\"];", nodeFrom.toString(), nodeTo.toString(), label));
+    public void edge(int fromNodeID, int toNodeID, String label) {
+        _out.println(String.format("%d -> %d [label=\"%s\"];", fromNodeID, toNodeID, label));
     }
 
-    public void endOutput()
-    {
-        while (!_componentStack.empty())
-        {
+    public void endOutput() {
+        while (!_componentStack.empty()) {
             GraphvizComponent lastComponent = popComponent();
             lastComponent.leave(_out);
         }
         _out.flush();
     }
 
-    public void flush()
-    {
+    public void flush() {
         _out.flush();
     }
 
-    private void pushComponent(GraphvizComponent component)
-    {
+    private void pushComponent(GraphvizComponent component) {
         _componentStack.add(component);
         component.enter(_out);
     }
 
-    private GraphvizComponent popComponent()
-    {
+    private GraphvizComponent popComponent() {
         assert !_componentStack.empty();
         return _componentStack.pop();
     }
 
-    class GraphvizComponent
-    {
+    class GraphvizComponent {
         /**
          * 
          */
-        public void enter(PrintWriter out)
-        {}
+        public void enter(PrintWriter out) {}
 
         /**
          * 
          */
-        public void leave(PrintWriter out)
-        {}
+        public void leave(PrintWriter out) {}
     }
 
-    class Digraph extends GraphvizComponent
-    {
+    class Digraph extends GraphvizComponent {
         private String _graphName;
 
-        public Digraph(String graphName)
-        {
+        public Digraph(String graphName) {
             _graphName = graphName;
         }
 
         @Override
-        public void enter(PrintWriter out)
-        {
+        public void enter(PrintWriter out) {
             out.println("digraph " + _graphName + "{");
         }
 
         @Override
-        public void leave(PrintWriter out)
-        {
+        public void leave(PrintWriter out) {
             out.println("}");
         }
 
index 60486e4..4612816 100644 (file)
@@ -84,14 +84,16 @@ public class SilkSchemaTest {
         }\r
 \r
         // confirm relation\r
-        assertNotNull(schema.relation);\r
-        assertEquals(1, schema.relation.size());\r
-        SilkRelation r = schema.relation.get(0);\r
-        assertEquals("Alignment", r.name);\r
-        assertEquals(2, r.attribute.size());\r
+        //        assertNotNull(schema.relation);\r
+        //        assertEquals(1, schema.relation.size());\r
+        //        SilkRelation r = schema.relation.get(0);\r
+        //        assertEquals("Alignment", r.name);\r
+        //        assertEquals(2, r.attribute.size());\r
 \r
         _logger.info(Lens.toJSON(schema));\r
 \r
+        _logger.info(schema.toGraphviz());\r
+\r
     }\r
 \r
 }\r
index 7e78cf1..520d307 100644 (file)
@@ -13,7 +13,8 @@ end
 # region on a genome sequence\r
 class Locus\r
   :id \r
-  integer :start, :end \r
+  integer :start\r
+  integer :end \r
 \r
   index interval :start, :end\r
   index keyword :id\r
@@ -22,6 +23,8 @@ end
 # gene data with sequence data\r
 class Gene < Locus\r
   sequence :sequence \r
+\r
+  # A gene may have many exons and CDSs\r
   Exon* :exon\r
   CDS* :cds\r
 end\r
@@ -36,6 +39,9 @@ end
 class Reference < Locus\r
   Coordinate :coodinate\r
   sequence :sequence \r
+  \r
+  # Reference has many reads\r
+  Read* :read\r
 end\r
 \r
 # short-read data\r
@@ -46,11 +52,5 @@ class Read
 end \r
 \r
 \r
-# Alignment of reads to a reference genome\r
-relation Alignment\r
-  Reference :reference\r
-  Read* :read \r
-end\r
-\r
 \r
 end \r
index 02a9229..50828e1 100644 (file)
 //--------------------------------------
 package org.xerial.util.graph;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -38,21 +35,17 @@ import org.junit.Test;
 import org.xerial.util.StopWatch;
 import org.xerial.util.log.Logger;
 
-public class LatticeTest
-{
+public class LatticeTest {
     private static Logger _logger = Logger.getLogger(LatticeTest.class);
 
     @Before
-    public void setUp() throws Exception
-    {}
+    public void setUp() throws Exception {}
 
     @After
-    public void tearDown() throws Exception
-    {}
+    public void tearDown() throws Exception {}
 
     @Test
-    public void latticeNode()
-    {
+    public void latticeNode() {
         Lattice<String> lattice = new Lattice<String>();
         LatticeNode<String> emptyNode = lattice.emptyNode();
         LatticeNode<String> aNode = emptyNode.next("A");
@@ -60,7 +53,7 @@ public class LatticeTest
         assertTrue(aNode.contains("A"));
         assertFalse(aNode.contains("B"));
         assertFalse(aNode.contains("B")); // double check
-        
+
         HashSet<String> answer = new HashSet<String>();
         answer.add("A");
 
@@ -90,7 +83,6 @@ public class LatticeTest
         for (String s : acdNode)
             assertTrue(answer.contains(s));
 
-
         LatticeNode<String> acdMinusDNode = acdNode.back("D");
         assertEquals(acNode, acdMinusDNode);
         assertNotSame(acNode, acdNode);
@@ -104,8 +96,7 @@ public class LatticeTest
     }
 
     @Test
-    public void latticePerformance()
-    {
+    public void latticePerformance() {
         Lattice<String> lattice = new Lattice<String>();
         LatticeNode<String> emptyNode = lattice.emptyNode();
 
@@ -113,25 +104,23 @@ public class LatticeTest
         int N = 100000;
 
         timer.reset();
-        for (int i = 0; i < N; i++)
-        {
-            emptyNode.next("A").next("B").next("C").next("D").next("E").next("F").back("F").next("F").back("F").back(
-                    "E").back("D").back("C").back("B").back("A");
+        for (int i = 0; i < N; i++) {
+            emptyNode.next("A").next("B").next("C").next("D").next("E").next("F").back("F").next(
+                    "F").back("F").back("E").back("D").back("C").back("B").back("A");
         }
         _logger.debug(timer.getElapsedTime());
 
-        LatticeNode<String> emptyNode2 = emptyNode.next("A").next("B").next("C").next("D").back("D").back("C")
-                .back("B").back("A");
+        LatticeNode<String> emptyNode2 = emptyNode.next("A").next("B").next("C").next("D")
+                .back("D").back("C").back("B").back("A");
         assertEquals(emptyNode, emptyNode2);
 
         emptyNode2 = emptyNode.next("A").next("B").back("B").back("A");
         assertEquals(emptyNode, emptyNode2);
 
     }
-    
+
     @Test
-    public void latticeCursor()
-    {
+    public void latticeCursor() {
         Lattice<String> lattice = new Lattice<String>();
         LatticeCursor<String> cursor = lattice.cursor();
 
@@ -155,13 +144,11 @@ public class LatticeTest
         assertTrue(!cursor.contains("A"));
         assertTrue(cursor.contains("B"));
         assertTrue(!cursor.contains("C"));
-        
+
     }
-    
-    
+
     @Test
-    public void loopBack()
-    {
+    public void loopBack() {
         Lattice<String> lattice = new Lattice<String>();
         LatticeCursor<String> cursor = lattice.cursor();
         cursor.next("A"); // {A}
@@ -179,11 +166,9 @@ public class LatticeTest
         cursor.next("B"); // {B, D}
         _logger.debug(cursor.getNode());
     }
-    
 
     @Test
-    public void pathRepeat()
-    {
+    public void pathRepeat() {
         Lattice<String> lattice = new Lattice<String>();
         LatticeCursor<String> cursor = lattice.cursor();
         ArrayList<String> path = new ArrayList<String>();
@@ -197,19 +182,16 @@ public class LatticeTest
         StopWatch timer = new StopWatch();
         int N = 1000;
         timer.reset();
-        for (int i = 0; i < N; i++)
-        {
-            for (String each : path)
-            {
+        for (int i = 0; i < N; i++) {
+            for (String each : path) {
                 cursor.next(each);
             }
-            for (int p = path.size() - 1; p >= 0; --p)
-            {
+            for (int p = path.size() - 1; p >= 0; --p) {
                 cursor.back(path.get(p));
             }
         }
         _logger.debug(timer.getElapsedTime());
-        
+
     }
 
 }