]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph/tests/org/simantics/graph/tests/diff/TestDiff.java
Do not merge TG if it is not set as immutable
[simantics/platform.git] / bundles / org.simantics.graph / tests / org / simantics / graph / tests / diff / TestDiff.java
1 package org.simantics.graph.tests.diff;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Random;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.type.Datatype;
9 import org.simantics.graph.diff.Diff;
10 import org.simantics.graph.representation.External;
11 import org.simantics.graph.representation.Identity;
12 import org.simantics.graph.representation.Internal;
13 import org.simantics.graph.representation.Optional;
14 import org.simantics.graph.representation.TransferableGraph1;
15 import org.simantics.graph.representation.Value;
16
17 public class TestDiff {
18         
19         public static TransferableGraph1 load(String name) throws IOException {
20                 InputStream stream = TestDiff.class.getResourceAsStream(name);
21                 Bindings.getSerializerUnchecked(Datatype.class).skip(stream);
22                 TransferableGraph1 result = (TransferableGraph1)TransferableGraph1.SERIALIZER.deserialize(stream);
23                 stream.close();
24                 return result;
25         }
26         
27         public static int[] randomPermutation(int size) {
28                 Random rand = new Random();
29                 int[] permutation = new int[size];
30                 for(int i=0;i<size;++i)
31                         permutation[i] = i;
32                 for(int i=size-1;i>0;--i) {
33                         int j = rand.nextInt(i+1);
34                         int temp = permutation[i];
35                         permutation[i] = permutation[j];
36                         permutation[j] = temp;
37                 }
38                 return permutation;
39         }
40         
41         public static void shuffle(TransferableGraph1 tg) {
42                 int[] permutation = randomPermutation(tg.resourceCount);
43                 
44                 {
45                         int[] statements = tg.statements;
46                         for(int i=0;i<statements.length;++i) {
47                                 int r = statements[i];
48                                 if(r >= 0)
49                                         statements[i] = permutation[r];                         
50                         }
51                 }
52                 
53                 for(Identity id : tg.identities) {
54                         id.resource = permutation[id.resource];
55                         if(id.definition instanceof External) {
56                                 External def = (External)id.definition;
57                                 def.parent = permutation[def.parent];
58                         }
59                         else if(id.definition instanceof Internal) {
60                                 Internal def = (Internal)id.definition;
61                                 def.parent = permutation[def.parent];
62                         }
63                         else if(id.definition instanceof Optional) {
64                                 Optional def = (Optional)id.definition;
65                                 def.parent = permutation[def.parent];
66                         }
67                 }
68                 
69                 for(Value value : tg.values) {
70                         value.resource = permutation[value.resource];
71                 }
72         }
73         
74         public static void main(String[] args) throws Exception {
75                 TransferableGraph1 graphOld = load("layer0.tg");
76                 TransferableGraph1 graphNew = load("layer0.tg");
77                 shuffle(graphNew);
78                 long begin = System.nanoTime();
79                 Diff diff = new Diff(graphOld, graphNew);
80                 /*TransferableGraphDelta1 result =*/ diff.diff();
81                 long end = System.nanoTime();
82
83                 System.out.println("Total time: " + (end - begin)*1e-6 + "ms");
84                 //result.print();
85         }
86         
87 }