1 6 7 package com.hp.hpl.jena.graph.query; 8 9 import com.hp.hpl.jena.graph.*; 10 11 import java.util.*; 12 13 31 public class SimpleTripleSorter implements TripleSorter 32 { 33 private Triple [] result; 34 private int putIndex; 35 private Set bound; 36 private List remaining; 37 38 42 public SimpleTripleSorter() 43 {} 44 45 51 public Triple [] sort( Triple[] ts ) 52 { return new SimpleTripleSorter( ts ) .sort(); } 53 54 59 protected SimpleTripleSorter( Triple [] triples ) 60 { 61 this(); 62 this.bound = new HashSet(); 63 this.result = new Triple[triples.length]; 64 this.remaining = new ArrayList( Arrays.asList( triples ) ); 65 } 66 67 75 protected Triple [] sort() 76 { 77 while (remaining.size() > 0) accept( findMostBinding( findLightest( remaining ) ) ); 78 return result; 79 } 80 81 85 protected void accept( Triple t ) 86 { 87 result[putIndex++] = t; 88 bind( t ); 89 remaining.remove( t ); 90 } 91 92 99 protected List findLightest( List candidates ) 100 { 101 List lightest = new ArrayList(); 102 int minWeight = 100; 103 for (int i = 0; i < candidates.size(); i +=1) 104 { 105 Triple t = (Triple) candidates.get(i); 106 int w = weight( t ); 107 if (w < minWeight) 108 { 109 lightest.clear(); 110 lightest.add( t ); 111 minWeight = w; 112 } 113 else if (w == minWeight) 114 lightest.add( t ); 115 } 116 return lightest; 117 } 118 119 122 protected Triple findMostBinding( List candidates ) 123 { 124 int maxBinding = -1; 125 Triple mostBinding = null; 126 for (int i = 0; i < candidates.size(); i += 1) 127 { 128 Triple t = (Triple) candidates.get(i); 129 int count = bindingCount( t ); 130 if (count > maxBinding) { mostBinding = t; maxBinding = count; } 131 } 132 return mostBinding; 133 } 134 135 142 protected int bindingCount( Triple t ) 143 { 144 int count = 0; 145 for (int i = 0; i < remaining.size(); i += 1) 146 { 147 Triple other = (Triple) remaining.get(i); 148 if (other != t) count += bindingCount( t, other ); 149 } 150 return count; 151 } 152 153 156 protected int bindingCount( Triple t, Triple other ) 157 { 158 return 159 bindingCount( t.getSubject(), other ) 160 + bindingCount( t.getPredicate(), other ) 161 + bindingCount( t.getObject(), other ) ; 162 } 163 164 protected int bindingCount( Node n, Triple o ) 165 { 166 return n.isVariable() 167 ? bc( n, o.getSubject() ) + bc( n, o.getPredicate() ) + bc( n, o.getObject() ) 168 : 0; 169 } 170 171 174 protected int bc( Node n, Node other ) 175 { return n.equals( other ) ? 1 : 0; } 176 177 180 protected void bind( Triple t ) 181 { 182 bind( t.getSubject() ); 183 bind( t.getPredicate() ); 184 bind( t.getObject() ); 185 } 186 187 protected void bind( Node n ) 188 { if (n.isVariable()) bound.add( n ); } 189 190 198 protected int weight( Triple t ) 199 { 200 return weight( t.getSubject() ) + weight( t.getPredicate() ) + weight( t.getObject() ); 201 } 202 203 220 protected int weight( Node n ) 221 { return n.isConcrete() ? 0 : n.equals( Node.ANY ) ? 5 : bound.contains( n ) ? 1 : 4; } 222 } 223 224 225 | Popular Tags |