]> 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 98852bcefafc4962bc90e3e64ae4b15c1b1549d0..ed5578992b69123ace2106ec0e52a66ce7df1c22 100644 (file)
@@ -11,16 +11,20 @@ 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;
 import org.simantics.databoard.adapter.AdaptException;
+import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.binding.error.BindingException;
-import org.simantics.databoard.binding.error.RuntimeBindingConstructionException;
 import org.simantics.databoard.binding.mutable.Variant;
 import org.simantics.databoard.container.DataContainer;
 import org.simantics.databoard.container.DataContainers;
-import org.simantics.databoard.serialization.SerializationException;
+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;
@@ -39,12 +43,9 @@ import org.simantics.graph.compiler.internal.validation.ValidateGraph;
 import org.simantics.graph.query.CompositeGraph;
 import org.simantics.graph.query.Paths;
 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,25 +57,38 @@ 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);
+               TreeMap<String, Variant> metadata = new TreeMap<>();
+               metadata.put(Extensions.CACHED_HASHCODE, new Variant(Bindings.INTEGER, hashCode));
                byte[] buffer = DataContainers.writeFile(
-                               new DataContainer("graph", 1, new Variant(TransferableGraph1.BINDING, tg))
+                               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(
                String Layer0Version,
                        Collection<ISource> sources, 
                        Collection<TransferableGraph1> dependencies,
                        ExternalFileLoader fileLoader,
                        GraphCompilerPreferences preferences) {
-           out.println(preferences);
+           //out.println(preferences);
            
                Collection<Problem> errors = new ArrayList<Problem>();
                GraphStore store = new GraphStore();            
@@ -191,18 +205,19 @@ 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) {
                StringBuilder sb = new StringBuilder();
-           Formatter formatter = new Formatter(sb, Locale.US);
-           formatter.format("%-25s %8.4f ms", taskName, (endTime - beginTime)*1e-6);
-                  
+               @SuppressWarnings("resource")
+               Formatter formatter = new Formatter(sb, Locale.US);
+               formatter.format("%-25s %8.4f ms", taskName, (endTime - beginTime)*1e-6);
+
                out.println(sb.toString());
        }