aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/model
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/model')
-rw-r--r--src/uk/ac/ox/cs/pagoda/model/AnswerTerm.java10
-rw-r--r--src/uk/ac/ox/cs/pagoda/model/IRI.java28
-rw-r--r--src/uk/ac/ox/cs/pagoda/model/Literal.java28
-rw-r--r--src/uk/ac/ox/cs/pagoda/model/Trie.java95
4 files changed, 161 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/model/AnswerTerm.java b/src/uk/ac/ox/cs/pagoda/model/AnswerTerm.java
new file mode 100644
index 0000000..777a35c
--- /dev/null
+++ b/src/uk/ac/ox/cs/pagoda/model/AnswerTerm.java
@@ -0,0 +1,10 @@
1package uk.ac.ox.cs.pagoda.model;
2
3public abstract class AnswerTerm {
4
5 protected static Trie instances = new Trie();
6 protected static int SkolemCounter = 0;
7 protected static int OriginalCounter = 0;
8
9 public abstract String toString();
10}
diff --git a/src/uk/ac/ox/cs/pagoda/model/IRI.java b/src/uk/ac/ox/cs/pagoda/model/IRI.java
new file mode 100644
index 0000000..7dc5242
--- /dev/null
+++ b/src/uk/ac/ox/cs/pagoda/model/IRI.java
@@ -0,0 +1,28 @@
1package uk.ac.ox.cs.pagoda.model;
2
3import uk.ac.ox.cs.pagoda.util.Namespace;
4
5public class IRI extends AnswerTerm {
6
7 int id;
8 String iri;
9
10 private IRI(String iri, int id) {
11 this.iri = iri;
12 this.id = id;
13 }
14
15 public IRI create(String iri) {
16 IRI instance = (IRI) instances.find(iri);
17 if (instance != null) return instance;
18 instance = new IRI(iri, iri.startsWith(Namespace.PAGODA_ANONY) ? --SkolemCounter : ++OriginalCounter);
19 instances.insert(iri, instance);
20 return instance;
21 }
22
23 @Override
24 public String toString() {
25 return "<" + iri + ">";
26 }
27
28}
diff --git a/src/uk/ac/ox/cs/pagoda/model/Literal.java b/src/uk/ac/ox/cs/pagoda/model/Literal.java
new file mode 100644
index 0000000..0a4dff2
--- /dev/null
+++ b/src/uk/ac/ox/cs/pagoda/model/Literal.java
@@ -0,0 +1,28 @@
1package uk.ac.ox.cs.pagoda.model;
2
3public class Literal extends AnswerTerm {
4
5 int id;
6 String lexicalForm, datatype;
7
8 public Literal(String lexicalForm2, String datatype2, int i) {
9 this.lexicalForm = lexicalForm2;
10 this.datatype = datatype2;
11 this.id = i;
12 }
13
14 public static Literal create(String lexicalForm, String datatype) {
15 String key = lexicalForm + "^^" + datatype;
16 Literal instance = (Literal) instances.find(key);
17 if (instance != null) return instance;
18 instance = new Literal(lexicalForm, datatype, ++OriginalCounter);
19 instances.insert(key, instance);
20 return instance;
21 }
22
23 @Override
24 public String toString() {
25 return "\"" + lexicalForm + "\"^^" + datatype;
26 }
27
28}
diff --git a/src/uk/ac/ox/cs/pagoda/model/Trie.java b/src/uk/ac/ox/cs/pagoda/model/Trie.java
new file mode 100644
index 0000000..eb9e71b
--- /dev/null
+++ b/src/uk/ac/ox/cs/pagoda/model/Trie.java
@@ -0,0 +1,95 @@
1package uk.ac.ox.cs.pagoda.model;
2
3public class Trie {
4
5 private TrieNode root = new TrieNode("");
6 private Status findStatus;
7
8 public void insert(String key, AnswerTerm term) {
9 findStatus.lastNode.insert(key, findStatus.lastIndex, term);
10 }
11
12 public AnswerTerm find(String key) {
13 findStatus = root.find(key, 0);
14 return findStatus.term;
15 }
16
17}
18
19class TrieNode {
20
21 String relative;
22 TrieNode[] children = new TrieNode[256];
23
24 public TrieNode(String s) {
25 relative = s;
26 }
27
28 public void insert(String key, int index, AnswerTerm term) {
29 int nextChar = (int) key.charAt(index);
30 if (children[nextChar] == null) {
31 children[nextChar] = new TrieLeaf(key.substring(index), term);
32 }
33 else {
34 TrieNode next = children[nextChar];
35 int len = next.isPrefixOf(key, index);
36 if (len == next.relative.length())
37 insert(key, index + len, term);
38 else {
39 TrieNode newNext = new TrieNode(next.relative.substring(0, len));
40 next.relative = next.relative.substring(len);
41 children[nextChar] = newNext;
42 newNext.children[(int) next.relative.charAt(0)] = next;
43 insert(key, index + len, term);
44 }
45 }
46 }
47
48 private int isPrefixOf(String key, int index) {
49 int i = 0;
50 for (int j = index; i < relative.length() && j < key.length(); ++i, ++j)
51 if (relative.charAt(i) != relative.charAt(j))
52 break;
53 return i;
54 }
55
56 public Status find(String key, int index) {
57 TrieNode next = children[(int) key.charAt(index)];
58 if (next == null)
59 return new Status(this, index, null);
60
61 if (next instanceof TrieLeaf)
62 if (next.relative.equals(key.substring(index)))
63 return new Status(this, index, ((TrieLeaf) next).term);
64 else {
65 return new Status(this, index, null);
66 }
67 return find(key, index + next.relative.length());
68 }
69
70}
71
72class TrieLeaf extends TrieNode {
73
74 AnswerTerm term;
75
76 public TrieLeaf(String s, AnswerTerm term) {
77 super(s);
78 this.term = term;
79 }
80
81}
82
83class Status {
84
85 AnswerTerm term;
86 TrieNode lastNode;
87 int lastIndex;
88
89 public Status(TrieNode trieNode, int index, AnswerTerm term2) {
90 this.lastNode = trieNode;
91 this.lastIndex = index;
92 this.term = term2;
93 }
94
95} \ No newline at end of file