]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/GraphCompiler.java
Removed org.simantics.ltk[.antlr] bundles, exact import for antlr
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / GraphCompiler.java
index 26663b08a2a5f288e9f38753ff53162045a82c02..ed5578992b69123ace2106ec0e52a66ce7df1c22 100644 (file)
@@ -11,6 +11,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Formatter;
 import java.util.Locale;
+import java.util.TreeMap;
 
 import org.simantics.databoard.Bindings;
 import org.simantics.databoard.Files;
@@ -20,6 +21,10 @@ import org.simantics.databoard.binding.error.BindingException;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.container.DataContainer;
 import org.simantics.databoard.container.DataContainers;
+import org.simantics.graph.compiler.internal.ltk.FileSource;
+import org.simantics.graph.compiler.internal.ltk.ISource;
+import org.simantics.graph.compiler.internal.ltk.Location;
+import org.simantics.graph.compiler.internal.ltk.Problem;
 import org.simantics.graph.compiler.internal.parsing.Parsing;
 import org.simantics.graph.compiler.internal.procedures.AddConsistsOf;
 import org.simantics.graph.compiler.internal.procedures.ApplyTemplates;
@@ -41,10 +46,6 @@ import org.simantics.graph.query.TransferableGraphConversion;
 import org.simantics.graph.representation.Extensions;
 import org.simantics.graph.representation.TransferableGraph1;
 import org.simantics.graph.store.GraphStore;
-import org.simantics.ltk.FileSource;
-import org.simantics.ltk.ISource;
-import org.simantics.ltk.Location;
-import org.simantics.ltk.Problem;
 
 public class GraphCompiler {
        
@@ -56,23 +57,29 @@ public class GraphCompiler {
        }
        
        public static TransferableGraph1 read(InputStream stream) throws AdaptException, IOException {
-               DataContainer container = DataContainers.readFile(new DataInputStream(stream));
-               stream.close();
-               return (TransferableGraph1)container.content.getValue(TransferableGraph1.BINDING);
+               try (InputStream in = stream) {
+                       DataContainer container = DataContainers.readFile(new DataInputStream(stream));
+                       return (TransferableGraph1)container.content.getValue(TransferableGraph1.BINDING);
+               }
        }
        
        public static InputStream write(TransferableGraph1 tg) throws BindingException, IOException {
                Binding binding = TransferableGraph1.BINDING;
                int hashCode = binding.hashValue(tg);
-               tg.extensions.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode));
-               try {
-                       byte[] buffer = DataContainers.writeFile(
-                                       new DataContainer("graph", 1, new Variant(TransferableGraph1.BINDING, tg))
-                                       );
-                       return new ByteArrayInputStream(buffer);
-               } finally {
-                       tg.extensions.remove(Extensions.CACHED_HASHCODE);
-               }
+               TreeMap<String, Variant> metadata = new TreeMap<>();
+               metadata.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode));
+               byte[] buffer = DataContainers.writeFile(
+                               new DataContainer("graph", 1, metadata, new Variant(binding, tg))
+                               );
+               return new ByteArrayInputStream(buffer);
+       }
+       
+       public static void write(File file, TransferableGraph1 tg) throws BindingException, IOException {
+               Binding binding = TransferableGraph1.BINDING;
+               int hashCode = binding.hashValue(tg);
+               TreeMap<String, Variant> metadata = new TreeMap<>();
+               metadata.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode));
+               DataContainers.writeFile(file, new DataContainer("graph", 1, metadata, new Variant(binding, tg)));
        }
        
        public static CompilationResult compile(
@@ -81,7 +88,7 @@ public class GraphCompiler {
                        Collection<TransferableGraph1> dependencies,
                        ExternalFileLoader fileLoader,
                        GraphCompilerPreferences preferences) {
-           out.println(preferences);
+           //out.println(preferences);
            
                Collection<Problem> errors = new ArrayList<Problem>();
                GraphStore store = new GraphStore();            
@@ -198,11 +205,11 @@ public class GraphCompiler {
        }
        
        private static void run(Runnable runnable) {
-               long beginTime = System.nanoTime();
+               //long beginTime = System.nanoTime();
                runnable.run();
-               long endTime = System.nanoTime();
+               //long endTime = System.nanoTime();
                
-               reportTime(runnable.getClass().getSimpleName(), beginTime, endTime);
+               //reportTime(runnable.getClass().getSimpleName(), beginTime, endTime);
        }
        
        public static void reportTime(String taskName, long beginTime, long endTime) {