/* * (c) Copyright 2007, 2008, 2009 Hewlett-Packard Development Company, LP * (c) Copyright 2010 Talis Information Ltd. * (c) Copyright 2010 Epimorphics Ltd. * * All rights reserved. * [See end of file] */ package dev; import java.util.ArrayList ; import java.util.Iterator ; import java.util.List ; import matcher.Match ; import matcher.PatternMatch ; import matcher.View ; import org.openjena.atlas.lib.Bytes ; import org.openjena.atlas.logging.Log ; import storage.varrecord.VarRecordBuffer ; import structure.radix.RadixIndex ; import com.hp.hpl.jena.graph.Node ; import com.hp.hpl.jena.iri.IRI ; import com.hp.hpl.jena.iri.IRIFactory ; import com.hp.hpl.jena.iri.Violation ; import com.hp.hpl.jena.iri.ViolationCodes ; import com.hp.hpl.jena.sparql.core.BasicPattern ; import com.hp.hpl.jena.sparql.core.Var ; import com.hp.hpl.jena.sparql.engine.optimizer.reorder.PatternTriple ; import com.hp.hpl.jena.sparql.sse.SSE ; import com.hp.hpl.jena.tdb.base.record.Record ; import com.hp.hpl.jena.tdb.base.record.RecordFactory ; import com.hp.hpl.jena.tdb.index.bplustree.BPlusTreeParams ; public class RunAFS { static { Log.setLog4j() ; } public static void exit(int code) { System.out.println("DONE") ; System.exit(code) ; } public static void main(String ... args) throws Exception { { RecordFactory[] recordFactories = { new RecordFactory(3*8,0), new RecordFactory(4*8,0), new RecordFactory(16,8) } ; for ( RecordFactory recordFactory : recordFactories ) { System.out.println("Record Factory: "+recordFactory) ; int[] sizes = { 1024, 1024-8, (8*1024-16), (8*1024-8) } ; for ( int x : sizes ) { int order = BPlusTreeParams.calcOrder(x, recordFactory) ; BPlusTreeParams bptParams = new BPlusTreeParams(order, recordFactory) ; System.out.printf("Block size: %d: %s\n", x, bptParams) ; } System.out.println() ; } exit(0) ; } RecordFactory recordFactory = new RecordFactory(4,0) ; RadixIndex idx = new RadixIndex(recordFactory) ; for ( int i : new int[] {1,2}) //,3,4,5,6} ) { Record r = recordFactory.create() ; Bytes.setInt(i, r.getKey()) ; idx.add(r) ; idx.radix.print() ; } Record r0 = recordFactory.create() ; Bytes.setInt(0, r0.getKey()) ; Record r9 = recordFactory.create() ; Bytes.setInt(9, r9.getKey()) ; Iterator iter = idx.iterator() ; for ( ; iter.hasNext() ; ) { System.out.println(iter.next()) ; } System.out.println("DONE") ; System.exit(0) ; // String s = DigestUtils.md5Hex("fred") ; // System.out.println(s) ; } static IRIFactory iriFactory = new IRIFactory(); static { // IRIFactory.iriImplementation() ... iriFactory.useSpecificationIRI(true); iriFactory.useSchemeSpecificRules("*",true); iriFactory.shouldViolation(false,true); // Moderate it -- allow unwise chars and any scheme name. iriFactory.setIsError(ViolationCodes.UNWISE_CHARACTER,false); iriFactory.setIsWarning(ViolationCodes.UNWISE_CHARACTER,false); iriFactory.setIsError(ViolationCodes.UNREGISTERED_IANA_SCHEME,false); iriFactory.setIsWarning(ViolationCodes.UNREGISTERED_IANA_SCHEME,false); iriFactory.create(""); } private static void iriTest(String iriStr) { System.out.println("IRI = "+iriStr) ; IRIFactory factory = iriFactory ; IRI iri = factory.create(iriStr) ; System.out.println("IRI => "+iri.toDisplayString()) ; Iterator iter = iri.violations(true) ; for ( ; iter.hasNext(); ) { Violation v = iter.next(); String x = (v.isError()?"ERROR":"WARN ") ; int code = v.getViolationCode() ; // // Treat these with low priority. // if ( code == Violation.LOWERCASE_PREFERRED || // code == Violation.PERCENT_ENCODING_SHOULD_BE_UPPERCASE ) // { // continue ; // } System.out.printf("%s %s\n", x, v.getShortMessage()) ; } } private static void varBlock() { byte[] d1 = {1,2,3,4} ; byte[] d2 = {(byte)0xFF, (byte)0xFE} ; byte[] d3 = {5,4,5,4,3,2,3,2,1,1} ; byte[] d4 = {(byte)0xFF, (byte)0xFE, (byte)0xFD, (byte)0xFC, (byte)0xFB} ; { VarRecordBuffer vrb = new VarRecordBuffer(50) ; vrb.dump(); vrb.put(d1,d2) ; vrb.dump(); vrb.put(d3,d4) ; vrb.dump(); System.out.println(vrb) ; Record r = vrb.get(0) ; System.out.println(r) ; System.exit(0) ; } VarRecordBuffer vrb = new VarRecordBuffer(40) ; byte[] k1 = new byte[]{1,2} ; byte[] v1 = new byte[]{(byte)0xA9, (byte)0xA8, (byte)0xA7 }; //, (byte)0xA6} ; byte[] k2 = new byte[]{3,4} ; byte[] v2 = new byte[]{(byte)0xFF, (byte)0xFE, (byte)0xFD, (byte)0xFC} ; byte[] k3 = new byte[]{5,6} ; byte[] v3 = new byte[]{(byte)0xBF, (byte)0xBE} ;//, (byte)0xBD, (byte)0xBC} ; //byte[] v3 = new byte[100] ; divider() ; boolean PRINT = false ; if ( PRINT ) System.out.println("k="+str(k1)+", v="+str(v1)) ; vrb.put(0, k1, v1) ; if ( PRINT ) vrb.dump(); if ( PRINT ) System.out.println(vrb) ; if ( PRINT )divider() ; // Push up. if ( PRINT ) System.out.println("k="+str(k2)+", v="+str(v2)) ; vrb.put(0, k2, v2) ; if ( PRINT ) vrb.dump(); if ( PRINT ) System.out.println(vrb) ; if ( PRINT ) divider() ; if ( PRINT ) System.out.println("k="+str(k3)+", v="+str(v3)) ; vrb.put(2, k3, v3) ; if ( PRINT ) vrb.dump(); if ( PRINT ) System.out.println(vrb) ; PRINT = false ; if ( PRINT ) divider() ; if ( PRINT ) vrb.dump(); if ( PRINT ) System.out.println(vrb) ; vrb.delete(1) ; if ( PRINT ) vrb.dump(); System.out.println(vrb) ; } private static String str(byte[] bytes) { StringBuilder sb = new StringBuilder() ; char sep = 0 ; for ( byte b : bytes ) { if ( sep != 0 ) sb.append(sep) ; else sep = ' ' ; sb.append(String.format("%02X", b)) ; } return sb.toString() ; } public static void matcher() { // Start of a multi-TP matcher for BGPs. Node x = Node.createURI("http://example/x") ; Node p = Node.createURI("http://example/p") ; Node q = Node.createURI("http://example/q") ; Node z = Node.createURI("http://example/z") ; List views = new ArrayList() ; { View _view = new View("View 1") ; // (?V :p ?W1 ) (?V :q ?W2) _view.patterns.add(new PatternTriple(Var.alloc("V"), p, Var.alloc("W1"))) ; _view.patterns.add(new PatternTriple(Var.alloc("V"), q, Var.alloc("W2"))) ; views.add(_view) ; } { View _view = new View("View 2") ; // (?A2 :p ?B) (?A1 :q ?B) _view.patterns.add(new PatternTriple(Var.alloc("A1"), z, Var.alloc("B"))) ; _view.patterns.add(new PatternTriple(Var.alloc("A2"), q, Var.alloc("B"))) ; views.add(_view) ; } // { // View _view = new View("View 3") ; // // (?X :q ?Y) // _view.patterns.add(new PatternTriple(Var.alloc("X"), q, Var.alloc("Y"))) ; // views.add(_view) ; // } match(views, "(bgp (triple ?x :p :z) (triple ?x :Z ?z2) (triple ?x :q ?z2))") ; match(views, "(bgp (triple ?x :Z ?z2) (triple ?x :q ?z2) (triple ?x :p :z) )") ; match(views, "(bgp (triple ?x :q ?z2) (triple ?x :z ?z2))") ; match(views, "(bgp (triple ?x :p :z))") ; System.out.println("-----------") ; System.exit(0) ; } static void match(List views, String str) { String s1 = "(prefix ((: )) " ; String s2 = ")" ; BasicPattern pattern = SSE.parseBGP(s1+str+s2) ; List matches = PatternMatch.match(views, pattern, false) ; System.out.println("Input: "+str) ; if ( matches == null ) System.out.println("No match") ; else { if ( matches.size() == 0 ) System.out.println("Zero matches") ; for ( Match match : matches ) { System.out.println("Match: "+match) ; } } System.out.println() ; } // ---- Helpers private static final String NL = "\n" ; public static String divider = "---- ----" ; private static String nextDivider = null ; public static void divider() { if ( nextDivider != null ) System.out.println(nextDivider) ; nextDivider = divider ; } } /* * (c) Copyright 2007, 2008, 2009 Hewlett-Packard Development Company, LP * (c) Copyright 2010 Talis Information Ltd. * (c) Copyright 2010 Epimorphics Ltd. * 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. */