1 6 7 package com.hp.hpl.jena.rdf.model.impl; 8 9 import com.hp.hpl.jena.rdf.model.*; 10 import com.hp.hpl.jena.shared.*; 11 import com.hp.hpl.jena.graph.*; 12 import com.hp.hpl.jena.graph.compose.*; 13 import com.hp.hpl.jena.graph.impl.GraphBase; 14 import com.hp.hpl.jena.util.iterator.*; 15 16 22 public class ModelReifier 23 { 24 private ModelCom model; 25 public Reifier reifier; 26 27 32 private static boolean copyingReifications = false; 33 34 38 public ModelReifier( ModelCom model ) 39 { 40 this.model = model; 41 this.reifier = model.asGraph().getReifier(); 42 } 43 44 public ReificationStyle getReificationStyle() 45 { return reifier.getStyle(); } 46 47 53 public static Model withHiddenStatements( Model m ) 54 { 55 Graph mGraph = m.getGraph(); 56 Graph hiddenTriples = getHiddenTriples( m ); 57 return new ModelCom( new DisjointUnion( mGraph, hiddenTriples ) ); 58 } 59 60 64 protected static Graph getHiddenTriples( Model m ) 65 { 66 Graph mGraph = m.getGraph(); 67 final Reifier r = mGraph.getReifier(); 68 return new GraphBase() 69 { 70 public ExtendedIterator graphBaseFind( TripleMatch m ) 71 { return r.findEither( m, true ); } 72 }; 73 } 74 75 79 public Model getHiddenStatements() 80 { return new ModelCom( getHiddenTriples( model ) ); } 81 82 87 public ReifiedStatement createReifiedStatement( Statement s ) 88 { return createReifiedStatement( null, s ); } 89 90 98 public ReifiedStatement createReifiedStatement( String uri, Statement s ) 99 { return ReifiedStatementImpl.create( model, uri, s ); } 100 101 107 public Resource getAnyReifiedStatement( Statement s ) 108 { 109 RSIterator it = listReifiedStatements( s ); 110 if (it.hasNext()) 111 try { return it.nextRS(); } finally { it.close(); } 112 else 113 return createReifiedStatement( s ); 114 } 115 116 121 public boolean isReified( FrontsTriple s ) 122 { return reifier.hasTriple( s.asTriple() ); } 123 124 129 public void removeAllReifications( FrontsTriple s ) 130 { reifier.remove( s.asTriple() ); } 131 132 137 public void removeReification( ReifiedStatement rs ) 138 { reifier.remove( rs.asNode(), rs.getStatement().asTriple() ); } 139 140 145 public RSIterator listReifiedStatements() 146 { return new RSIteratorImpl( findReifiedStatements() ); } 147 148 154 public RSIterator listReifiedStatements( FrontsTriple s ) 155 { return new RSIteratorImpl( findReifiedStatements( s.asTriple() ) ); } 156 157 161 public void noteIfReified( RDFNode s, RDFNode p, RDFNode o ) 162 { 163 if (copyingReifications) 164 { 165 noteIfReified( s ); 166 noteIfReified( p ); 167 noteIfReified( o ); 168 } 169 } 170 171 175 private void noteIfReified( RDFNode n ) 176 { 177 if (n.canAs( ReifiedStatement.class )) 178 { 179 ReifiedStatement rs = (ReifiedStatement) n.as( ReifiedStatement.class ); 180 createReifiedStatement( rs.getURI(), rs.getStatement() ); 181 } 182 } 183 184 188 protected final Map1 mapToRS = new Map1() 189 { 190 public Object map1( Object node ) { return getRS( (Node) node ); } 191 }; 192 193 private ExtendedIterator findReifiedStatements() 194 { return reifier .allNodes() .mapWith( mapToRS ); } 195 196 private ExtendedIterator findReifiedStatements( Triple t ) 197 { return reifier .allNodes( t ) .mapWith( mapToRS ); } 198 199 204 private ReifiedStatement getRS( Node n ) 205 { 206 Triple t = reifier.getTriple( n ); 207 Statement s = model.asStatement( t ); 208 return ReifiedStatementImpl.create( model, n, s ); 209 } 210 } 211 212 241 242 | Popular Tags |