Update of /cvsroot/javaowl/JavaOWL/src/org/javaowl/models/prevalence In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18474/src/org/javaowl/models/prevalence Modified Files: PrevalentGraph.java AddTripleCommand.java StorageGraph.java DeleteTripleCommand.java Added Files: TripleUtil.java Removed Files: StorageUtil.java Log Message: Changed serialization to externalization. --- NEW FILE: TripleUtil.java --- /* * (c) Copyright 2003, Hewlett-Packard Development Company, LP [See end of file] */ package org.javaowl.models.prevalence; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import com.hp.hpl.jena.datatypes.RDFDatatype; import com.hp.hpl.jena.datatypes.TypeMapper; import com.hp.hpl.jena.db.RDFRDBException; import com.hp.hpl.jena.graph.Node; import com.hp.hpl.jena.graph.Node_Literal; import com.hp.hpl.jena.graph.Node_URI; import com.hp.hpl.jena.graph.Node_Variable; import com.hp.hpl.jena.graph.Triple; import com.hp.hpl.jena.graph.impl.LiteralLabel; import com.hp.hpl.jena.rdf.model.AnonId; public class TripleUtil { /* * The following routines are responsible for encoding nodes as database * structures. For each node type stored (currently, literals, URI, blank), * there are two possible encodings depending on the node size. Small nodes * may be stored within a statement table. If the node is long (will not fit * within the statement table), it is be stored in a separate table for that * node type. * * In addition, for resources (URI, blank nodes), the URI may be optionally * compressed. Below, the possibilites are enumerated. * * Literal Encoding in Statement Tables Short Literal: * Lv:[langLen]:[datatypeLen]:[langString][datatypeString]value[:] Long * Literal: Lr:dbid Literal Encoding in Long Literal Table Literal: * Lv:[langLen]:[datatypeLen]:[langString][datatypeString]head[:] hash tail * * Comments: L indicates a literal v indicates a value r indicates a * reference to another table : is used as a delimiter. note that MySQL * trims trailing white space for certain VARCHAR columns so an extra * delimiter is appended when necessary for those columns. it is not * required for dbid, however. dbid references the long literal table * langLen is the length of the language identifier for the literal * langString is the language identifier datatypeLen is the length of the * datatype for the literal datatypeString is the datatype for the literal * value is the lexical form of the string head is a prefix of value that * can be indexed hash is the CRC32 hash value for the tail tail is the * remainder of the value that cannot be indexed * * * * URI Encoding in Statement Tables Short URI: Uv:[pfx_dbid]:URI[:] Long * URI: Ur:[pfx_dbid]:dbid URI Encoding in Long URI Table URI: Uv:head[:] * hash tail * * Comments: U indicates a URI pfx_dbid references the prefix table. if the * prefix is too short (i.e., the length of the prefix is less than * URI_COMPRESS_LENGTH), the URI is not compressed and pfx_dbid is null. URI * is the complete URI other notation same as for literal encoding * * Blank Node Encoding in Statement Tables Short URI: Bv:[pfx_dbid]:bnid[:] * Long URI: Br:[pfx_dbid]:dbid Blank Encoding in Long URI Table URI: * Bv:head[:] hash tail * * Comments: B indicates a blank node bnid is the blank node identifier * other notation same as above Note: currently, blank nodes are always * stored uncompressed (pfix_dbid is null). * * Variable Node Encoding in Statement Tables Variable Node: Vv:name * * Comments: V indicates a variable node v indicates a value name is the * variable name Note: the length must be less than LONG_OBJECT_LENGTH * * ANY Node Encoding in Statement Tables Variable Node: Av: * * Prefix Encoding in Prefix Table Prefix: Pv:val[:] [hash] [tail] * * Comments: P indicates a prefix other notation same as above hash and tail * are only required for long prefixes. * */ private static String RDB_CODE_URI = "U"; private static String RDB_CODE_BLANK = "B"; private static String RDB_CODE_LITERAL = "L"; private static String RDB_CODE_VARIABLE = "V"; private static String RDB_CODE_ANY = "A"; private static String RDB_CODE_VALUE = "v"; private static String RDB_CODE_DELIM = ":"; private static char RDB_CODE_DELIM_CHAR = ':'; private static String nodeToString(Node node) throws RDFRDBException { String res; if (node.isURI()) { String uri = new String(((Node_URI) node).getURI()); if (uri.startsWith(RDB_CODE_URI)) { throw new RDFRDBException("URI Node looks like a blank node: " + uri); } String pfx = RDB_CODE_DELIM + RDB_CODE_DELIM; String qname = uri; res = RDB_CODE_URI + RDB_CODE_VALUE + pfx + qname; } else if (node.isLiteral()) { Node_Literal litNode = (Node_Literal) node; LiteralLabel ll = litNode.getLiteral(); String lval = ll.getLexicalForm(); String lang = ll.language(); String dtype = ll.getDatatypeURI(); String ld = litLangTypeToRDBString(lang, dtype); res = RDB_CODE_LITERAL + RDB_CODE_VALUE + RDB_CODE_DELIM + ld + lval; } else if (node.isBlank()) { String bnID = node.getBlankNodeId().toString(); String delims = "::"; res = RDB_CODE_BLANK + RDB_CODE_VALUE + delims + bnID; } else if (node.isVariable()) { String name = ((Node_Variable) node).getName(); res = RDB_CODE_VARIABLE + RDB_CODE_VALUE + RDB_CODE_DELIM + name; } else if (node.equals(Node.ANY)) { res = RDB_CODE_ANY + RDB_CODE_VALUE + RDB_CODE_DELIM; } else { throw new RDFRDBException("Expected Concrete Node, got " + node.toString()); } return res; } private static Node stringToNode(String rdbString) throws RDFRDBException { Node res = null; int len = rdbString.length(); if (len < 3) throw new RDFRDBException("Bad RDBString Header: " + rdbString); String nodeType = rdbString.substring(0, 1); String valType = rdbString.substring(1, 2); if ((!valType.equals(RDB_CODE_VALUE)) || (rdbString.charAt(2) != RDB_CODE_DELIM_CHAR)) throw new RDFRDBException("Bad RDBString Header: " + rdbString); int pos = 3; if (nodeType.equals(RDB_CODE_URI)) { ParseInt pi = new ParseInt(pos); String prefix = ""; rdbStringParseInt(rdbString, pi, false); pos = pi.pos + 1; String qname = rdbString.substring(pos, len); res = Node.createURI(prefix + qname); } else if (nodeType.equals(RDB_CODE_LITERAL)) { //TODO: Parse to correct datatype ParseInt pi = new ParseInt(pos); String litString = null; litString = rdbString.substring(pos, len); len = litString.length(); String lang; String dType; int langLen = 0; int dTypeLen = 0; LiteralLabel llabel; pi.pos = 0; rdbStringParseInt(litString, pi, false); if (pi.val == null) langLen = 0; else langLen = pi.val.intValue(); pi.pos = pi.pos + 1; rdbStringParseInt(litString, pi, false); if (pi.val == null) dTypeLen = 0; else dTypeLen = pi.val.intValue(); pos = pi.pos + 1; if ((pos + langLen + dTypeLen) > len) throw new RDFRDBException("Malformed Literal: " + litString); lang = litString.substring(pos, pos + langLen); pos = pos + langLen; dType = litString.substring(pos, pos + dTypeLen); pos = pos + dTypeLen; String val = litString.substring(pos); if ((dType == null) || (dType.equals(""))) { llabel = new LiteralLabel(val, lang == null ? "" : lang); } else { RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(dType); llabel = new LiteralLabel(val, lang == null ? "" : lang, dt); } res = Node.createLiteral(llabel); } else if (nodeType.equals(RDB_CODE_BLANK)) { String bstr = rdbString.substring(4, len); res = Node.createAnon(new AnonId(bstr)); } else if (nodeType.equals(RDB_CODE_VARIABLE)) { String vname = rdbString.substring(3, len); res = Node.createVariable(vname); } else if (nodeType.equals(RDB_CODE_ANY)) { res = Node.ANY; } else throw new RDFRDBException("Invalid RDBString Prefix, " + rdbString); return res; } private final static class ParseInt { int pos; Integer val; ParseInt(int p) { pos = p; } } private static void rdbStringParseInt(String rdbString, ParseInt pi, boolean toEnd) { int npos = toEnd ? rdbString.length() : rdbString.indexOf( RDB_CODE_DELIM_CHAR, pi.pos); if (npos < 0) { throw new RDFRDBException("Bad RDB String: " + rdbString); } String intStr = rdbString.substring(pi.pos, npos); pi.pos = npos; if (intStr.equals("")) pi.val = null; else try { pi.val = new Integer(intStr); } catch (NumberFormatException e1) { throw new RDFRDBException("Bad RDB String: " + rdbString); } return; } /** * Encode a literal node's lang and datatype as a string of the form * ":[langLen]:[datatypeLen]:[langString][dataTypeString]" * * @return the string. */ private static String litLangTypeToRDBString(String lang, String dtype) throws RDFRDBException { String res = RDB_CODE_DELIM; res = ((lang == null) ? "" : Integer.toString(lang.length())) + RDB_CODE_DELIM; res = res + ((dtype == null) ? "" : Integer.toString(dtype.length())) + RDB_CODE_DELIM; res = res + (lang == null ? "" : lang) + (dtype == null ? "" : dtype); return res; } public static void writeTriple(Triple triple, ObjectOutput out) throws RDFRDBException, IOException { out.writeUTF(nodeToString(triple.getSubject())); out.writeUTF(nodeToString(triple.getPredicate())); out.writeUTF(nodeToString(triple.getObject())); } public static Triple readTriple(ObjectInput in) throws RDFRDBException, IOException { Node subject = TripleUtil.stringToNode(in.readUTF()); Node predicate = TripleUtil.stringToNode(in.readUTF()); Node object = TripleUtil.stringToNode(in.readUTF()); return new Triple(subject, predicate, object); } } /* * (c) Copyright 2000, 2001 Hewlett-Packard Development Company, LP All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. 2. Redistributions in * binary form must reproduce the above copyright notice, this list of * conditions and the following disclaimer in the documentation and/or other * materials provided with the distribution. 3. The name of the author may not * be used to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ Index: DeleteTripleCommand.java =================================================================== RCS file: /cvsroot/javaowl/JavaOWL/src/org/javaowl/models/prevalence/DeleteTripleCommand.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DeleteTripleCommand.java 27 Jul 2004 16:20:00 -0000 1.2 --- DeleteTripleCommand.java 28 Jul 2004 06:54:07 -0000 1.3 *************** *** 20,38 **** package org.javaowl.models.prevalence; ! import com.hp.hpl.jena.graph.Node; ! import com.hp.hpl.jena.graph.Triple; import org.prevayler.Command; import org.prevayler.PrevalentSystem; ! import java.io.IOException; ! import java.io.ObjectInputStream; ! import java.io.ObjectOutputStream; ! import java.io.Serializable; ! ! class DeleteTripleCommand implements Command { ! private transient Triple triple; DeleteTripleCommand(Triple triple) { this.triple = triple; --- 20,42 ---- package org.javaowl.models.prevalence; ! import java.io.Externalizable; ! import java.io.IOException; ! import java.io.ObjectInput; ! import java.io.ObjectOutput; ! import java.io.Serializable; import org.prevayler.Command; import org.prevayler.PrevalentSystem; ! import com.hp.hpl.jena.graph.Triple; ! class DeleteTripleCommand implements Command, Externalizable { + private static final long serialVersionUID = -4089954040389955762L; + + private Triple triple; + + public DeleteTripleCommand() {} + DeleteTripleCommand(Triple triple) { this.triple = triple; *************** *** 44,59 **** } ! private void writeObject(ObjectOutputStream out) throws IOException { ! out.writeUTF(StorageUtil.nodeToRDBString(triple.getSubject())); ! out.writeUTF(StorageUtil.nodeToRDBString(triple.getPredicate())); ! out.writeUTF(StorageUtil.nodeToRDBString(triple.getObject())); } ! private void readObject(ObjectInputStream in) throws IOException, ! ClassNotFoundException { ! Node subject = StorageUtil.rdbStringToNode(in.readUTF()); ! Node predicate = StorageUtil.rdbStringToNode(in.readUTF()); ! Node object = StorageUtil.rdbStringToNode(in.readUTF()); ! this.triple = new Triple(subject, predicate, object); } } --- 48,57 ---- } ! public void writeExternal(ObjectOutput out) throws IOException { ! TripleUtil.writeTriple(triple, out); } ! public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { ! this.triple = TripleUtil.readTriple(in); } } --- StorageUtil.java DELETED --- Index: StorageGraph.java =================================================================== RCS file: /cvsroot/javaowl/JavaOWL/src/org/javaowl/models/prevalence/StorageGraph.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** StorageGraph.java 26 Jul 2004 22:55:02 -0000 1.2 --- StorageGraph.java 28 Jul 2004 06:54:07 -0000 1.3 *************** *** 20,24 **** --- 20,31 ---- package org.javaowl.models.prevalence; + import java.io.Externalizable; + import java.io.IOException; + import java.io.ObjectInput; + import java.io.ObjectOutput; + import java.util.Iterator; + import com.hp.hpl.jena.graph.Graph; + import com.hp.hpl.jena.graph.GraphUtil; import com.hp.hpl.jena.graph.Triple; import com.hp.hpl.jena.graph.Node; *************** *** 37,46 **** import com.hp.hpl.jena.util.iterator.ExtendedIterator; import org.prevayler.implementation.AbstractPrevalentSystem; ! class StorageGraph extends AbstractPrevalentSystem implements Graph { private final Graph graph = new GraphMem(ReificationStyle.Standard); public boolean dependsOn(Graph other) { return graph.dependsOn(other); --- 44,58 ---- import com.hp.hpl.jena.util.iterator.ExtendedIterator; + import org.prevayler.AlarmClock; import org.prevayler.implementation.AbstractPrevalentSystem; ! class StorageGraph extends AbstractPrevalentSystem implements Graph, Externalizable { + private static final long serialVersionUID = 792795079693766654L; + private final Graph graph = new GraphMem(ReificationStyle.Standard); + public StorageGraph() {} + public boolean dependsOn(Graph other) { return graph.dependsOn(other); *************** *** 53,57 **** } public BulkUpdateHandler getBulkUpdateHandler() { ! return graph.getBulkUpdateHandler(); } public Capabilities getCapabilities() { --- 65,69 ---- } public BulkUpdateHandler getBulkUpdateHandler() { ! throw new UnsupportedOperationException(); } public Capabilities getCapabilities() { *************** *** 97,99 **** --- 109,126 ---- graph.add(t); } + + public void writeExternal(ObjectOutput out) throws IOException { + out.writeInt(size()); + out.writeObject(clock()); + for (Iterator it = GraphUtil.findAll(graph); it.hasNext(); ) { + TripleUtil.writeTriple((Triple) it.next(), out); + } + } + public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { + int size = in.readInt(); + clock((AlarmClock) in.readObject()); + for(int i = 0; i < size; i++) { + add(TripleUtil.readTriple(in)); + } + } } Index: PrevalentGraph.java =================================================================== RCS file: /cvsroot/javaowl/JavaOWL/src/org/javaowl/models/prevalence/PrevalentGraph.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** PrevalentGraph.java 26 Jul 2004 22:55:02 -0000 1.2 --- PrevalentGraph.java 28 Jul 2004 06:54:07 -0000 1.3 *************** *** 21,28 **** --- 21,31 ---- import java.io.IOException; + import java.util.Iterator; + import java.util.List; import org.prevayler.implementation.SnapshotPrevayler; import com.hp.hpl.jena.graph.Graph; + import com.hp.hpl.jena.graph.GraphUtil; import com.hp.hpl.jena.graph.Triple; import com.hp.hpl.jena.graph.Node; *************** *** 39,43 **** import com.hp.hpl.jena.util.iterator.ExtendedIterator; ! public class PrevalentGraph implements Graph, Runnable{ private final SnapshotPrevayler prevayler; --- 42,46 ---- import com.hp.hpl.jena.util.iterator.ExtendedIterator; ! public class PrevalentGraph implements Graph, BulkUpdateHandler, Runnable{ private final SnapshotPrevayler prevayler; *************** *** 47,50 **** --- 50,54 ---- prevayler = new SnapshotPrevayler(new StorageGraph(), directory); storageGraph = (StorageGraph) prevayler.system(); + new Thread(this).start(); } *************** *** 52,55 **** --- 56,62 ---- for (;;) { try { + prevayler.takeSnapshot(); + System.err.println("Snapshot taken at " + new java.util.Date() + "..."); + try { Thread.sleep(1000 * 60 * 60 * 24); *************** *** 57,62 **** //Ignore } - prevayler.takeSnapshot(); - System.err.println("Snapshot taken at " + new java.util.Date() + "..."); } catch (IOException e) { e.printStackTrace(); --- 64,67 ---- *************** *** 78,82 **** public BulkUpdateHandler getBulkUpdateHandler() { ! return storageGraph.getBulkUpdateHandler(); } --- 83,87 ---- public BulkUpdateHandler getBulkUpdateHandler() { ! return this; } *************** *** 146,148 **** --- 151,201 ---- } } + + public void add(Triple[] triples) { + for (int i = 0; i < triples.length; i++) { + add(triples[i]); + } + } + + public void add(List triples) { + add(triples.iterator()); + } + + public void add(Iterator it) { + while (it.hasNext()) { + add((Triple) it.next()); + } + } + + public void add(Graph graph, boolean withReifications) { + add(GraphUtil.findAll(graph)); + } + + public void add(Graph graph) { + add(graph, false); + } + + public void delete(Triple[] triples) { + for (int i = 0; i < triples.length; i++) { + delete(triples[i]); + } + } + + public void delete(List triples) { + delete(triples.iterator()); + } + + public void delete(Iterator it) { + while (it.hasNext()) { + delete((Triple) it.next()); + } + } + + public void delete(Graph graph) { + delete(graph, false); + } + + public void delete(Graph graph, boolean withReifications) { + delete(GraphUtil.findAll(graph)); + } } Index: AddTripleCommand.java =================================================================== RCS file: /cvsroot/javaowl/JavaOWL/src/org/javaowl/models/prevalence/AddTripleCommand.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** AddTripleCommand.java 27 Jul 2004 16:20:00 -0000 1.6 --- AddTripleCommand.java 28 Jul 2004 06:54:07 -0000 1.7 *************** *** 20,38 **** package org.javaowl.models.prevalence; ! import com.hp.hpl.jena.graph.Node; ! import com.hp.hpl.jena.graph.Triple; import org.prevayler.Command; import org.prevayler.PrevalentSystem; ! import java.io.Serializable; ! import java.io.ObjectInputStream; ! import java.io.ObjectOutputStream; ! import java.io.IOException; ! ! class AddTripleCommand implements Command { ! private transient Triple triple; AddTripleCommand(Triple triple) { this.triple = triple; --- 20,42 ---- package org.javaowl.models.prevalence; ! import java.io.Externalizable; ! import java.io.IOException; ! import java.io.ObjectInput; ! import java.io.ObjectOutput; ! import java.io.Serializable; import org.prevayler.Command; import org.prevayler.PrevalentSystem; ! import com.hp.hpl.jena.graph.Triple; ! class AddTripleCommand implements Command, Externalizable { + private static final long serialVersionUID = -8900004762584176922L; + + private Triple triple; + + public AddTripleCommand() {} + AddTripleCommand(Triple triple) { this.triple = triple; *************** *** 44,59 **** } ! private void writeObject(ObjectOutputStream out) throws IOException { ! out.writeUTF(StorageUtil.nodeToRDBString(triple.getSubject())); ! out.writeUTF(StorageUtil.nodeToRDBString(triple.getPredicate())); ! out.writeUTF(StorageUtil.nodeToRDBString(triple.getObject())); } ! private void readObject(ObjectInputStream in) throws IOException, ! ClassNotFoundException { ! Node subject = StorageUtil.rdbStringToNode(in.readUTF()); ! Node predicate = StorageUtil.rdbStringToNode(in.readUTF()); ! Node object = StorageUtil.rdbStringToNode(in.readUTF()); ! this.triple = new Triple(subject, predicate, object); } } --- 48,61 ---- } ! public void writeExternal(ObjectOutput out) throws IOException { ! TripleUtil.writeTriple(triple, out); } ! public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { ! try { ! this.triple = TripleUtil.readTriple(in); ! } catch (Exception e) { ! e.printStackTrace(); ! } } } |