From 136b514f526fc12d44c5f9f399dde88715ca25c9 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Fri, 16 Dec 2011 23:01:47 +0100 Subject: [PATCH] added a Triple Store based on Nodes that fit to the new storage classes. Added also a first Vocabulary for the node store - Dublin Core. --- source/net/yacy/cora/lod/Node.java | 75 ++++++++++++++ source/net/yacy/cora/lod/TripleStore.java | 98 +++++++++++++++++++ .../yacy/cora/lod/vocabulary/DublinCore.java | 44 +++++++++ .../yacy/cora/lod/vocabulary/Vocabulary.java | 11 +++ 4 files changed, 228 insertions(+) create mode 100644 source/net/yacy/cora/lod/Node.java create mode 100644 source/net/yacy/cora/lod/TripleStore.java create mode 100644 source/net/yacy/cora/lod/vocabulary/DublinCore.java create mode 100644 source/net/yacy/cora/lod/vocabulary/Vocabulary.java diff --git a/source/net/yacy/cora/lod/Node.java b/source/net/yacy/cora/lod/Node.java new file mode 100644 index 000000000..2de6fe11e --- /dev/null +++ b/source/net/yacy/cora/lod/Node.java @@ -0,0 +1,75 @@ +package net.yacy.cora.lod; + +import java.util.Map; +import java.util.HashMap; + +import net.yacy.cora.document.UTF8; +import net.yacy.cora.lod.vocabulary.Vocabulary; + +public class Node extends HashMap implements Map { + + private static final long serialVersionUID = -6715118942251224832L; + + public static final String SUBJECT = "rdf:about"; + + public Node() { + super(); + } + + public Node(byte[] subject) { + super(); + this.put(SUBJECT, subject); + } + + /** + * initialize the triples. + * one of the properties must be the resource SUBJECT + * for a blank node the SUBJECT can be omitted + * @param set + */ + public Node(Map set) { + super(); + this.putAll(set); + } + + public boolean isBlank() { + return !this.containsKey(SUBJECT); + } + + public byte[] getSubject() { + return this.get(SUBJECT); + } + + public void setSubject(byte[] subject) { + this.put(SUBJECT, subject); + } + + public byte[] getObject(Vocabulary predicate) { + return this.get(predicate.getPredicate()); + } + + public byte[] setObject(Vocabulary predicate, byte[] object) { + return this.put(predicate.getPredicate(), object); + } + + public byte[] removePredicate(Vocabulary predicate) { + return this.remove(predicate.getPredicate()); + } + + public byte[] toObject() { + StringBuilder sb = new StringBuilder(this.size() * 50); + sb.append("\n"); + for (Map.Entry entry: this.entrySet()) { + if (entry.getKey().equals(SUBJECT)) continue; + sb.append('<').append(entry.getKey()).append('>'); + sb.append(UTF8.String(entry.getValue())); + sb.append("\n"); + } + sb.append("\n"); + return UTF8.getBytes(sb); + } + +} diff --git a/source/net/yacy/cora/lod/TripleStore.java b/source/net/yacy/cora/lod/TripleStore.java new file mode 100644 index 000000000..8c39307ed --- /dev/null +++ b/source/net/yacy/cora/lod/TripleStore.java @@ -0,0 +1,98 @@ +package net.yacy.cora.lod; + +import java.util.AbstractMap; +import java.util.Iterator; +import java.util.Map; + +import net.yacy.kelondro.blob.MapStore; +import net.yacy.kelondro.order.ByteOrder; +import net.yacy.kelondro.order.CloneableIterator; + +public class TripleStore { + + MapStore store; + + public TripleStore(MapStore store) { + this.store = store; + } + + public void clear() { + this.store.clear(); + } + + public boolean contains(byte[] id) { + return this.store.containsKey(id); + } + + public Node get(byte[] id) { + Map n = this.store.get(id); + if (n == null) return null; + return new Node(n); + } + + public boolean isEmpty() { + return this.store.isEmpty(); + } + + public Node put(byte[] id, Node node) { + Map n = this.store.put(id, node); + if (n == null) return null; + return new Node(n); + } + + public void putAll(TripleStore entries) { + Iterator> i = entries.iterator(); + Map.Entry entry; + while (i.hasNext()) { + entry = i.next(); + this.put(entry.getKey(), entry.getValue()); + } + } + + public Node remove(byte[] id) { + Map n = this.store.remove(id); + if (n == null) return null; + return new Node(n); + } + + public int size() { + return this.store.size(); + } + + public Iterator> iterator() { + final Iterator id = this.idIterator(); + return new Iterator>(){ + + @Override + public boolean hasNext() { + return id.hasNext(); + } + + @Override + public Map.Entry next() { + byte[] key = id.next(); + if (key == null) return null; + return new AbstractMap.SimpleImmutableEntry(key, TripleStore.this.get(key)); + } + + @Override + public void remove() { + id.remove(); + } + + }; + } + + public ByteOrder getOrdering() { + return store.getOrdering(); + } + + public CloneableIterator idIterator() { + return this.store.keyIterator(); + } + + public void close() { + this.store.close(); + } + +} diff --git a/source/net/yacy/cora/lod/vocabulary/DublinCore.java b/source/net/yacy/cora/lod/vocabulary/DublinCore.java new file mode 100644 index 000000000..c67aa6acd --- /dev/null +++ b/source/net/yacy/cora/lod/vocabulary/DublinCore.java @@ -0,0 +1,44 @@ +package net.yacy.cora.lod.vocabulary; + +public enum DublinCore implements Vocabulary { + + Contributor, + Coverage, + Creator, + Date, + Description, + Format, + Identifier, + Language, + Publisher, + Relation, + Rights, + Source, + Subject, + Title, + Type; + + public final static String IDENTIFIER = "http://dublincore.org/documents/2010/10/11/dces/"; + public final static String PREFIX = "dc"; + + private final String predicate; + + private DublinCore() { + this.predicate = PREFIX + ":" + this.name().toLowerCase(); + } + + @Override + public String getIdentifier() { + return IDENTIFIER; + } + + @Override + public String getPrefix() { + return PREFIX; + } + + @Override + public String getPredicate() { + return this.predicate; + } +} diff --git a/source/net/yacy/cora/lod/vocabulary/Vocabulary.java b/source/net/yacy/cora/lod/vocabulary/Vocabulary.java new file mode 100644 index 000000000..a2e7671d4 --- /dev/null +++ b/source/net/yacy/cora/lod/vocabulary/Vocabulary.java @@ -0,0 +1,11 @@ +package net.yacy.cora.lod.vocabulary; + +public interface Vocabulary { + + public String getIdentifier(); + + public String getPrefix(); + + public String getPredicate(); + +}