From 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 Mon Sep 17 00:00:00 2001 From: yzhou Date: Tue, 21 Apr 2015 10:34:27 +0100 Subject: initial version --- src/uk/ac/ox/cs/pagoda/model/AnswerTerm.java | 10 +++ src/uk/ac/ox/cs/pagoda/model/IRI.java | 28 ++++++++ src/uk/ac/ox/cs/pagoda/model/Literal.java | 28 ++++++++ src/uk/ac/ox/cs/pagoda/model/Trie.java | 95 ++++++++++++++++++++++++++++ 4 files changed, 161 insertions(+) create mode 100644 src/uk/ac/ox/cs/pagoda/model/AnswerTerm.java create mode 100644 src/uk/ac/ox/cs/pagoda/model/IRI.java create mode 100644 src/uk/ac/ox/cs/pagoda/model/Literal.java create mode 100644 src/uk/ac/ox/cs/pagoda/model/Trie.java (limited to 'src/uk/ac/ox/cs/pagoda/model') 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 @@ +package uk.ac.ox.cs.pagoda.model; + +public abstract class AnswerTerm { + + protected static Trie instances = new Trie(); + protected static int SkolemCounter = 0; + protected static int OriginalCounter = 0; + + public abstract String toString(); +} 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 @@ +package uk.ac.ox.cs.pagoda.model; + +import uk.ac.ox.cs.pagoda.util.Namespace; + +public class IRI extends AnswerTerm { + + int id; + String iri; + + private IRI(String iri, int id) { + this.iri = iri; + this.id = id; + } + + public IRI create(String iri) { + IRI instance = (IRI) instances.find(iri); + if (instance != null) return instance; + instance = new IRI(iri, iri.startsWith(Namespace.PAGODA_ANONY) ? --SkolemCounter : ++OriginalCounter); + instances.insert(iri, instance); + return instance; + } + + @Override + public String toString() { + return "<" + iri + ">"; + } + +} 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 @@ +package uk.ac.ox.cs.pagoda.model; + +public class Literal extends AnswerTerm { + + int id; + String lexicalForm, datatype; + + public Literal(String lexicalForm2, String datatype2, int i) { + this.lexicalForm = lexicalForm2; + this.datatype = datatype2; + this.id = i; + } + + public static Literal create(String lexicalForm, String datatype) { + String key = lexicalForm + "^^" + datatype; + Literal instance = (Literal) instances.find(key); + if (instance != null) return instance; + instance = new Literal(lexicalForm, datatype, ++OriginalCounter); + instances.insert(key, instance); + return instance; + } + + @Override + public String toString() { + return "\"" + lexicalForm + "\"^^" + datatype; + } + +} 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 @@ +package uk.ac.ox.cs.pagoda.model; + +public class Trie { + + private TrieNode root = new TrieNode(""); + private Status findStatus; + + public void insert(String key, AnswerTerm term) { + findStatus.lastNode.insert(key, findStatus.lastIndex, term); + } + + public AnswerTerm find(String key) { + findStatus = root.find(key, 0); + return findStatus.term; + } + +} + +class TrieNode { + + String relative; + TrieNode[] children = new TrieNode[256]; + + public TrieNode(String s) { + relative = s; + } + + public void insert(String key, int index, AnswerTerm term) { + int nextChar = (int) key.charAt(index); + if (children[nextChar] == null) { + children[nextChar] = new TrieLeaf(key.substring(index), term); + } + else { + TrieNode next = children[nextChar]; + int len = next.isPrefixOf(key, index); + if (len == next.relative.length()) + insert(key, index + len, term); + else { + TrieNode newNext = new TrieNode(next.relative.substring(0, len)); + next.relative = next.relative.substring(len); + children[nextChar] = newNext; + newNext.children[(int) next.relative.charAt(0)] = next; + insert(key, index + len, term); + } + } + } + + private int isPrefixOf(String key, int index) { + int i = 0; + for (int j = index; i < relative.length() && j < key.length(); ++i, ++j) + if (relative.charAt(i) != relative.charAt(j)) + break; + return i; + } + + public Status find(String key, int index) { + TrieNode next = children[(int) key.charAt(index)]; + if (next == null) + return new Status(this, index, null); + + if (next instanceof TrieLeaf) + if (next.relative.equals(key.substring(index))) + return new Status(this, index, ((TrieLeaf) next).term); + else { + return new Status(this, index, null); + } + return find(key, index + next.relative.length()); + } + +} + +class TrieLeaf extends TrieNode { + + AnswerTerm term; + + public TrieLeaf(String s, AnswerTerm term) { + super(s); + this.term = term; + } + +} + +class Status { + + AnswerTerm term; + TrieNode lastNode; + int lastIndex; + + public Status(TrieNode trieNode, int index, AnswerTerm term2) { + this.lastNode = trieNode; + this.lastIndex = index; + this.term = term2; + } + +} \ No newline at end of file -- cgit v1.2.3