aboutsummaryrefslogtreecommitdiff
path: root/external/uk/ac/ox/cs/data/Comparator.java
diff options
context:
space:
mode:
Diffstat (limited to 'external/uk/ac/ox/cs/data/Comparator.java')
-rw-r--r--external/uk/ac/ox/cs/data/Comparator.java131
1 files changed, 131 insertions, 0 deletions
diff --git a/external/uk/ac/ox/cs/data/Comparator.java b/external/uk/ac/ox/cs/data/Comparator.java
new file mode 100644
index 0000000..5b61a81
--- /dev/null
+++ b/external/uk/ac/ox/cs/data/Comparator.java
@@ -0,0 +1,131 @@
1package uk.ac.ox.cs.data;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.OutputStreamWriter;
8import java.util.Collection;
9import java.util.HashSet;
10import java.util.Scanner;
11import java.util.Set;
12
13import org.semanticweb.owlapi.model.OWLAxiom;
14import org.semanticweb.owlapi.model.OWLClassExpression;
15import org.semanticweb.owlapi.model.OWLDataFactory;
16import org.semanticweb.owlapi.model.OWLEquivalentClassesAxiom;
17import org.semanticweb.owlapi.model.OWLOntology;
18import uk.ac.ox.cs.pagoda.owl.OWLHelper;
19import uk.ac.ox.cs.pagoda.util.Utility;
20
21public class Comparator {
22
23 public static void main(String[] args) throws IOException {
24 compareFiles(args);
25 }
26
27 public static void compareFiles(String[] args) throws IOException {
28 String directory = "/users/yzhou/workspace/pagoda/";
29 String name1 = "abox1.txt", name2 = "abox2.txt";
30
31 args = (directory + name1 + " " +
32 directory + name2 + " " +
33 directory + "diff.dlog").split("\\ ");
34
35 Scanner s1 = new Scanner(new File(args[0])), s2 = new Scanner(new File(args[1]));
36 HashSet<String> h1 = new HashSet<String>(), h2 = new HashSet<String>();
37 while (s1.hasNextLine()) h1.add(s1.nextLine());
38 s1.close();
39 while (s2.hasNextLine()) h2.add(s2.nextLine().replace("an-minus.owl", "an.owl"));
40 s2.close();
41
42 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[2])));
43
44 writer.write("Elements in " + name1 + ", but not in " + name2);
45 writer.newLine();
46 for (String line: h1)
47 if (!h2.contains(line)) {
48 writer.write(line);
49 writer.newLine();
50 }
51
52 writer.write("--------------------------------------------------------");
53 writer.newLine();
54
55 writer.write("Elements in " + name2 + ", but not in " + name1);
56 writer.newLine();
57 for (String line: h2)
58 if (!h1.contains(line)) {
59 writer.write(line);
60 writer.newLine();
61 }
62
63 writer.close();
64 }
65
66
67 public void compareOntologies(String[] args) throws IOException {
68 String directory = "/home/scratch/yzhou/ontologies/fly/auxiliary/datalog/";
69 String name1 = "eq/elho.owl", name2 = "noEQ/elho.owl";
70
71 args = (directory + name1 + " " +
72 directory + name2 + " " +
73 directory + "diff.owl").split("\\ ");
74
75 OWLOntology o1 = OWLHelper.loadOntology(args[0]);
76 OWLOntology o2 = OWLHelper.loadOntology(args[1]);
77
78 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(args[2])));
79
80 writer.write("Elements in " + name1 + ", but not in " + name2);
81 writer.newLine();
82 writer.write(compareOntologies(o1, o2));
83
84 writer.write("--------------------------------------------------------");
85 writer.newLine();
86
87 writer.write("Elements in " + name2 + ", but not in " + name1);
88 writer.newLine();
89 writer.write(compareOntologies(o2, o1));
90
91 writer.close();
92 }
93
94 private static String compareOntologies(OWLOntology o1, OWLOntology o2) {
95 StringBuilder sb = new StringBuilder();
96
97 Set<String> axioms = new HashSet<String>();
98 OWLDataFactory factory1 = o1.getOWLOntologyManager().getOWLDataFactory();
99 OWLDataFactory factory2 = o2.getOWLOntologyManager().getOWLDataFactory();
100
101 for (OWLAxiom a: o2.getAxioms())
102 for (OWLAxiom axiom: process(a, factory2)){
103 axioms.add(axiom.toString());
104 }
105
106 for (OWLAxiom a: o1.getAxioms()) {
107 for (OWLAxiom axiom: process(a, factory1))
108 if (!axioms.contains(axiom.toString()))
109 sb.append(axiom.toString()).append(Utility.LINE_SEPARATOR);
110 }
111
112 return sb.toString();
113 }
114
115 private static Collection<OWLAxiom> process(OWLAxiom axiom, OWLDataFactory factory) {
116 Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
117 OWLEquivalentClassesAxiom equiv;
118 if (axiom instanceof OWLEquivalentClassesAxiom) {
119 equiv = (OWLEquivalentClassesAxiom) axiom;
120 for (OWLClassExpression exp1: equiv.getClassExpressions())
121 for (OWLClassExpression exp2: equiv.getClassExpressions())
122 if (!exp1.equals(exp2))
123 axioms.add(factory.getOWLSubClassOfAxiom(exp1, exp2));
124 }
125 else
126 axioms.add(axiom);
127
128 return axioms;
129 }
130
131}