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.shared.impl.*; 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.graph.impl.*; 14 import com.hp.hpl.jena.graph.query.*; 15 16 import com.hp.hpl.jena.util.CollectionFactory; 17 import com.hp.hpl.jena.util.iterator.*; 18 import com.hp.hpl.jena.vocabulary.RDF; 19 import com.hp.hpl.jena.datatypes.*; 20 import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; 21 import com.hp.hpl.jena.datatypes.xsd.XSDDateTime; 22 import com.hp.hpl.jena.enhanced.*; 23 24 import java.io.*; 25 import java.net.URL ; 26 import java.util.*; 27 28 37 38 public class ModelCom 39 extends EnhGraph 40 implements Model, PrefixMapping, ModelLock 41 { 42 43 private RDFReaderF readerFactory = new RDFReaderFImpl(); 44 private RDFWriterF writerFactory = new RDFWriterFImpl(); 45 private ModelLock modelLock = null ; 46 47 50 public ModelCom( Graph base ) 51 { this( base, BuiltinPersonalities.model ); } 52 53 public ModelCom( Graph base, Personality personality ) 54 { super( base, personality ); 55 withDefaultMappings( defaultPrefixMapping ); } 56 57 private static PrefixMapping defaultPrefixMapping = PrefixMapping.Factory.create(); 58 59 public static PrefixMapping getDefaultModelPrefixes() 60 { return defaultPrefixMapping; } 61 62 public static PrefixMapping setDefaultModelPrefixes( PrefixMapping pm ) 63 { PrefixMapping result = defaultPrefixMapping; 64 defaultPrefixMapping = pm; 65 return result; } 66 67 public QueryHandler queryHandler() 68 { return getGraph().queryHandler(); } 69 70 public Graph getGraph() 71 { return graph; } 72 73 protected static Model createWorkModel() 74 { return ModelFactory.createDefaultModel(); } 75 76 public RDFNode asRDFNode( Node n ) 77 { 78 return n.isLiteral() 79 ? (RDFNode) this.getNodeAs( n, Literal.class ) 80 : (RDFNode) this.getNodeAs( n, Resource.class ); 81 } 82 83 86 protected ModelReifier modelReifier = new ModelReifier( this ); 87 88 public Resource getResource(String uri, ResourceF f) { 89 try { 90 return f.createResource(getResource(uri)); 91 } catch (Exception e) { 92 throw new JenaException(e); 93 } 94 } 95 96 public Model add(Resource s, Property p, boolean o) { 97 return add(s, p, String.valueOf( o ) ); 98 } 99 100 public Model add(Resource s, Property p, long o) { 101 return add(s, p, String.valueOf( o ) ); 102 } 103 104 public Model add(Resource s, Property p, char o) { 105 return add(s, p, String.valueOf( o ) ); 106 } 107 108 public Model add(Resource s, Property p, float o) { 109 return add(s, p, String.valueOf( o ) ); 110 } 111 112 public Model add(Resource s, Property p, double o) { 113 return add(s, p, String.valueOf( o ) ); 114 } 115 116 public Model add(Resource s, Property p, String o) { 117 return add( s, p, o, "", false ); 118 } 119 120 public Model add(Resource s, Property p, String o, boolean wellFormed) 121 { 122 add(s, p, literal(o, "", wellFormed)); 123 return this; 124 } 125 126 public Model add(Resource s, Property p, String o, String lang, 127 boolean wellFormed) { 128 add(s, p, literal(o, lang, wellFormed)); 129 return this; 130 } 131 132 private Literal literal( String s, String lang, boolean wellFormed ) 133 { return new LiteralImpl( Node.createLiteral( s, lang, wellFormed), this ); } 134 135 public Model add(Resource s, Property p, String o, String l) 136 { return add( s, p, o, l, false ); } 137 138 143 private RDFNode ensureRDFNode( Object o ) 144 { 145 return o instanceof RDFNode 146 ? (RDFNode) o 147 : literal( o.toString(), null, false ) 148 ; 149 } 150 151 public Model add(Resource s, Property p, Object o) { 152 return add( s, p, ensureRDFNode( o ) ); 153 } 154 155 public Model add( StmtIterator iter ) { 156 try { getBulkUpdateHandler().add( asTriples( iter ) ); } 157 finally { iter.close(); } 158 return this; 159 } 160 161 public Model add( Model m ) 162 { return add( m, false ); } 163 164 public Model add( Model m, boolean suppressReifications ) { 165 getBulkUpdateHandler().add( m.getGraph(), !suppressReifications ); 166 return this; 167 } 168 169 public RDFReader getReader() { 170 return readerFactory.getReader(); 171 } 172 173 public RDFReader getReader(String lang) { 174 return readerFactory.getReader(lang); 175 } 176 177 public String setReaderClassName(String lang, String className) { 178 return readerFactory.setReaderClassName(lang, className); 179 } 180 181 public Model read(String url) { 182 readerFactory .getReader() .read(this, url); 183 return this; 184 } 185 186 public Model read(Reader reader, String base) { 187 readerFactory .getReader() .read(this, reader, base); 188 return this; 189 } 190 191 public Model read(InputStream reader, String base) { 192 readerFactory .getReader() .read(this, reader, base); 193 return this; 194 } 195 196 public Model read(String url, String lang) { 197 readerFactory. getReader(lang) .read(this, url); 198 return this; 199 } 200 201 public Model read( String url, String base, String lang ) 202 { 203 try 204 { 205 InputStream is = new URL ( url ) .openStream(); 206 try { read( is, base, lang ); } 207 finally { is.close(); } 208 } 209 catch (IOException e) { throw new WrappedIOException( e ); } 210 return this; 211 } 212 213 public Model read(Reader reader, String base, String lang) 214 { 215 readerFactory .getReader(lang) .read(this, reader, base); 216 return this; 217 } 218 219 public Model read(InputStream reader, String base, String lang) 220 { 221 readerFactory .getReader(lang) .read(this, reader, base); 222 return this; 223 } 224 225 229 public RDFWriter getWriter() { 230 return writerFactory.getWriter(); 231 } 232 233 237 public RDFWriter getWriter(String lang) { 238 return writerFactory.getWriter(lang); 239 } 240 241 242 public String setWriterClassName(String lang, String className) { 243 return writerFactory.setWriterClassName(lang, className); 244 } 245 246 public Model write(Writer writer) 247 { 248 getWriter() .write(this, writer, ""); 249 return this; 250 } 251 252 public Model write(Writer writer, String lang) 253 { 254 getWriter(lang) .write(this, writer, ""); 255 return this; 256 } 257 258 public Model write(Writer writer, String lang, String base) 259 { 260 getWriter(lang) .write(this, writer, base); 261 return this; 262 } 263 264 public Model write( OutputStream writer ) 265 { 266 getWriter() .write(this, writer, ""); 267 return this; 268 } 269 270 public Model write(OutputStream writer, String lang) 271 { 272 getWriter(lang) .write(this, writer, ""); 273 return this; 274 } 275 276 public Model write(OutputStream writer, String lang, String base) 277 { 278 getWriter(lang) .write(this, writer, base); 279 return this; 280 } 281 282 public Model remove(Statement s) { 283 graph.delete(s.asTriple()); 284 return this; 285 } 286 287 public Model remove( StmtIterator iter ) 288 { 289 getBulkUpdateHandler().delete( asTriples( iter ) ); 290 return this; 291 } 292 293 public Model remove( Model m ) 294 { return remove( m, false ); } 295 296 public Model remove( Model m, boolean suppressReifications ) 297 { 298 getBulkUpdateHandler().delete( m.getGraph(), !suppressReifications ); 299 return this; 300 } 301 302 public Model removeAll() 303 { 304 getGraph().getBulkUpdateHandler().removeAll(); 305 return this; 306 } 307 308 public Model removeAll( Resource s, Property p, RDFNode o ) 309 { 310 getGraph().getBulkUpdateHandler().remove( asNode( s ), asNode( p ), asNode( o ) ); 311 return this; 312 } 313 314 public boolean contains( Resource s, Property p, boolean o ) 315 { return contains(s, p, String.valueOf( o ) ); } 316 317 public boolean contains( Resource s, Property p, long o ) 318 { return contains(s, p, String.valueOf( o ) ); } 319 320 public boolean contains( Resource s, Property p, char o ) 321 { return contains(s, p, String.valueOf( o ) ); } 322 323 public boolean contains( Resource s, Property p, float o ) 324 { return contains(s, p, String.valueOf( o ) ); } 325 326 public boolean contains( Resource s, Property p, double o ) 327 { return contains(s, p, String.valueOf( o ) ); } 328 329 public boolean contains(Resource s, Property p, String o) 330 { return contains(s, p, o, "" ); } 331 332 public boolean contains(Resource s, Property p, String o, String l) 333 { return contains( s, p, literal( o, l, false ) ); } 334 335 public boolean contains(Resource s, Property p, Object o) 336 { return contains( s, p, ensureRDFNode( o ) ); } 337 338 public boolean containsAny(StmtIterator iter) { 339 while (iter.hasNext()) { 340 if (contains(iter.nextStatement())) return true; 341 } 342 return false; 343 } 344 345 public boolean containsAll(StmtIterator iter) { 346 while (iter.hasNext()) { 347 if (!contains(iter.nextStatement())) return false; 348 } 349 return true; 350 } 351 352 public boolean containsAny(Model model) { 353 StmtIterator iter = model.listStatements(); 354 try { return containsAny(iter); } 355 finally { iter.close(); } 356 } 357 358 public boolean containsAll(Model model) { 359 StmtIterator iter = model.listStatements(); 360 try { return containsAll(iter); } 361 finally { iter.close(); } 362 } 363 364 public StmtIterator listStatements(Resource subject, 365 Property predicate, 366 RDFNode object) 367 { return listStatements( new SimpleSelector( subject, predicate, object ) ); } 368 369 public StmtIterator listStatements(Resource subject, 370 Property predicate, 371 boolean object) 372 { return listStatements( new SimpleSelector( subject, predicate, object ) ); } 373 374 public StmtIterator listStatements(Resource subject, 375 Property predicate, 376 long object) 377 { return listStatements( new SimpleSelector( subject, predicate, object ) ); } 378 379 public StmtIterator listStatements(Resource subject, 380 Property predicate, 381 char object) 382 { return listStatements( new SimpleSelector( subject, predicate, object ) ); } 383 384 public StmtIterator listStatements(Resource subject, 385 Property predicate, 386 float object) 387 { return listStatements( new SimpleSelector( subject, predicate, object ) ); } 388 389 public StmtIterator listStatements(Resource subject, 390 Property predicate, 391 double object) 392 { return listStatements( new SimpleSelector(subject, predicate, object ) ); } 393 394 public StmtIterator listStatements(Resource subject, 395 Property predicate, 396 String object) 397 { return listStatements( new SimpleSelector(subject, predicate, object ) ); } 398 399 public StmtIterator listStatements(Resource subject, 400 Property predicate, 401 String object, 402 String lang) 403 { return listStatements( new SimpleSelector(subject, predicate, object, lang ) ); } 404 405 public ResIterator listSubjectsWithProperty(Property p, boolean o) 406 { 407 return listSubjectsWithProperty(p, String.valueOf( o ) ); 408 } 409 410 public ResIterator listSubjectsWithProperty(Property p, long o) 411 { 412 return listSubjectsWithProperty(p, String.valueOf( o ) ); 413 } 414 415 public ResIterator listSubjectsWithProperty(Property p, char o) 416 { 417 return listSubjectsWithProperty(p, String.valueOf( o ) ); 418 } 419 420 public ResIterator listSubjectsWithProperty(Property p, float o) 421 { 422 return listSubjectsWithProperty(p, String.valueOf( o ) ); 423 } 424 425 public ResIterator listSubjectsWithProperty(Property p, double o) 426 { 427 return listSubjectsWithProperty(p, String.valueOf( o ) ); 428 } 429 430 public ResIterator listSubjectsWithProperty(Property p, String o) 431 { 432 return listSubjectsWithProperty( p, o, "" ); 433 } 434 435 public ResIterator listSubjectsWithProperty(Property p, String o, String l) 436 { 437 return listSubjectsWithProperty(p, literal( o, l, false ) ); 438 } 439 440 public ResIterator listSubjectsWithProperty(Property p, Object o) 441 { 442 return listSubjectsWithProperty( p, ensureRDFNode( o ) ); 443 } 444 445 public Resource createResource(Resource type) { 446 return createResource().addProperty(RDF.type, type); 447 } 448 449 public Resource createResource(String uri,Resource type) 450 { 451 return getResource(uri) 452 .addProperty(RDF.type, type); 453 } 454 455 public Resource createResource(ResourceF f) { 456 return createResource(null, f); 457 } 458 459 public Resource createResource( AnonId id ) 460 { return new ResourceImpl( id, this ); } 461 462 public Resource createResource(String uri, ResourceF f) { 463 return f.createResource( createResource( uri ) ); 464 } 465 466 467 475 public Literal createTypedLiteral(boolean v) { 476 return createTypedLiteral(new Boolean (v)); 477 } 478 479 485 public Literal createTypedLiteral(int v) { 486 return createTypedLiteral(new Integer (v)); 487 } 488 489 495 public Literal createTypedLiteral(long v) { 496 return createTypedLiteral(new Long (v)); 497 } 498 499 505 public Literal createTypedLiteral(char v) { 506 return createTypedLiteral(new Character (v)); 507 } 508 509 515 public Literal createTypedLiteral(float v) { 516 return createTypedLiteral(new Float (v)); 517 } 518 519 525 public Literal createTypedLiteral(double v) { 526 return createTypedLiteral(new Double (v)); 527 } 528 529 535 public Literal createTypedLiteral(String v) { 536 LiteralLabel ll = new LiteralLabel(v); 537 return new LiteralImpl(Node.createLiteral(ll), this); 538 } 539 540 543 public Literal createTypedLiteral(Calendar cal) { 544 Object value = new XSDDateTime(cal); 545 LiteralLabel ll = new LiteralLabel(value, "", XSDDatatype.XSDdateTime); 546 return new LiteralImpl(Node.createLiteral(ll), this); 547 548 } 549 550 559 public Literal createTypedLiteral(String lex, RDFDatatype dtype) 560 throws DatatypeFormatException { 561 LiteralLabel ll = new LiteralLabel(lex, "", dtype); 562 return new LiteralImpl(Node.createLiteral(ll), this); 563 } 564 565 571 public Literal createTypedLiteral(Object value, RDFDatatype dtype) { 572 LiteralLabel ll = new LiteralLabel(value, "", dtype); 573 return new LiteralImpl( Node.createLiteral(ll), this ); 574 } 575 576 585 public Literal createTypedLiteral(String lex, String typeURI) { 586 RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(typeURI); 587 LiteralLabel ll = new LiteralLabel(lex, "", dt); 588 return new LiteralImpl( Node.createLiteral(ll), this ); 589 } 590 591 597 public Literal createTypedLiteral(Object value, String typeURI) { 598 RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(typeURI); 599 LiteralLabel ll = new LiteralLabel(value, "", dt); 600 return new LiteralImpl(Node.createLiteral(ll), this); 601 } 602 603 609 public Literal createTypedLiteral(Object value) { 610 if (value instanceof Calendar) { 612 return createTypedLiteral((Calendar)value); 613 } 614 LiteralLabel ll = new LiteralLabel(value); 615 return new LiteralImpl(Node.createLiteral(ll), this); 616 } 617 618 public Literal createLiteral(boolean v) 619 { return createLiteral(String.valueOf(v), ""); } 620 621 public Literal createLiteral(int v) { 622 return createLiteral(String.valueOf(v), ""); 623 } 624 625 public Literal createLiteral(long v) { 626 return createLiteral(String.valueOf(v), ""); 627 } 628 629 public Literal createLiteral(char v) { 630 return createLiteral(String.valueOf(v), ""); 631 } 632 633 public Literal createLiteral(float v) { 634 return createLiteral(String.valueOf(v), ""); 635 } 636 637 public Literal createLiteral(double v) { 638 return createLiteral(String.valueOf(v), ""); 639 } 640 641 public Literal createLiteral(String v) { 642 return createLiteral(v, ""); 643 } 644 645 public Literal createLiteral(String v, String l) { 646 return literal(v, l, false); 647 } 648 649 public Literal createLiteral(String v, boolean wellFormed) { 650 return literal(v, "", wellFormed); 651 } 652 653 public Literal createLiteral(String v, String l, boolean wellFormed) { 654 return literal(v, l, wellFormed); 655 } 656 657 public Literal createLiteral(Object v) { 658 return createLiteral(v.toString(), ""); 659 } 660 661 public Statement createStatement(Resource r, Property p, boolean o) 662 { 663 return createStatement(r, p, createLiteral(o)); 664 } 665 666 public Statement createStatement(Resource r, Property p, long o) 667 { 668 return createStatement(r, p, createLiteral(o)); 669 } 670 671 public Statement createStatement(Resource r, Property p, char o) 672 { 673 return createStatement(r, p, createLiteral(o)); 674 } 675 676 public Statement createStatement(Resource r, Property p, float o) 677 { 678 return createStatement(r, p, createLiteral(o)); 679 } 680 681 public Statement createStatement(Resource r, Property p, double o) 682 { 683 return createStatement(r, p, createLiteral(o)); 684 } 685 686 public Statement createStatement(Resource r, Property p, String o) 687 { 688 return createStatement(r, p, createLiteral(o)); 689 } 690 691 public Statement createStatement(Resource r, Property p, Object o) 692 { 693 return createStatement( r, p, ensureRDFNode( o ) ); 694 } 695 696 public Statement createStatement(Resource r, Property p, String o, 697 boolean wellFormed) { 698 return createStatement( r, p, o, "", wellFormed ); 699 } 700 701 public Statement createStatement(Resource r, Property p, String o, String l) 702 { 703 return createStatement( r, p, o, l, false ); 704 } 705 706 public Statement createStatement(Resource r, Property p, String o, String l, 707 boolean wellFormed) { 708 return createStatement(r, p, literal(o,l,wellFormed)); 709 } 710 711 public Bag createBag() { 712 return createBag(null); 713 } 714 715 public Alt createAlt() { 716 return createAlt(null); 717 } 718 719 public Seq createSeq() { 720 return createSeq(null); 721 } 722 723 727 public RDFList createList() { 728 Resource list = getResource( RDF.nil.getURI() ); 729 730 return (RDFList) list.as( RDFList.class ); 731 } 732 733 734 739 public RDFList createList( Iterator members ) { 740 RDFList list = createList(); 741 742 while (members != null && members.hasNext()) { 743 list = list.with( (RDFNode) members.next() ); 744 } 745 746 return list; 747 } 748 749 750 755 public RDFList createList( RDFNode[] members ) { 756 return createList( Arrays.asList( members ).iterator() ); 757 } 758 759 public RDFNode getRDFNode( Node n ) 760 { 761 return n.isURI() || n.isBlank() 762 ? (RDFNode) new ResourceImpl( n, this ) 763 : (RDFNode) new LiteralImpl( n, this ); 764 } 765 766 public Resource getResource(String uri) { 767 return IteratorFactory.asResource(makeURI(uri),this); 768 } 769 770 public Property getProperty(String uri) { 771 if ( uri == null ) 772 throw new InvalidPropertyURIException( null ); 773 return IteratorFactory.asProperty(makeURI(uri),this); 774 } 775 776 public Property getProperty(String nameSpace,String localName) 777 { 778 return getProperty(nameSpace+localName); 779 } 780 781 public Seq getSeq(String uri) { 782 return (Seq)IteratorFactory.asResource(makeURI(uri),Seq.class, this); 783 } 784 785 public Seq getSeq(Resource r) { 786 return (Seq) r.as( Seq.class ); 787 } 788 789 public Bag getBag(String uri) { 790 return (Bag)IteratorFactory.asResource(makeURI(uri),Bag.class, this); 791 } 792 793 public Bag getBag(Resource r) { 794 return (Bag) r.as( Bag.class ); 795 } 796 797 static private Node makeURI(String uri) { 798 if ( uri == null ) 799 return Node.createAnon(new AnonId()); 800 else 801 return Node.createURI(uri); 802 } 803 804 public Alt getAlt(String uri) { 805 return (Alt)IteratorFactory.asResource(makeURI(uri),Alt.class, this); 806 } 807 808 public Alt getAlt(Resource r) { 809 return (Alt) r.as( Alt.class ); 810 } 811 812 public long size() { 813 return graph.size(); 814 } 815 816 public boolean isEmpty() 817 { return graph.isEmpty(); } 818 819 private void updateNamespace( Set set, Iterator it ) 820 { 821 while (it.hasNext()) 822 { 823 Node node = (Node) it.next(); 824 if (node.isURI()) 825 { 826 String uri = node.getURI(); 827 String ns = uri.substring( 0, Util.splitNamespace( uri ) ); 828 set.add( ns ); 830 } 831 } 832 } 833 834 private Iterator listPredicates() 835 { 836 return getGraph().queryHandler().predicatesFor( Node.ANY, Node.ANY ); 841 } 842 843 private Iterator listTypes() 844 { 845 Set types = CollectionFactory.createHashedSet(); 846 ClosableIterator it = graph.find( null, RDF.type.asNode(), null ); 847 while (it.hasNext()) types.add( ((Triple) it.next()).getObject() ); 848 return types.iterator(); 849 } 850 851 public NsIterator listNameSpaces() { 852 Set nameSpaces = CollectionFactory.createHashedSet(); 853 updateNamespace( nameSpaces, listPredicates() ); 854 updateNamespace( nameSpaces, listTypes() ); 855 return new NsIteratorImpl(nameSpaces.iterator(), nameSpaces); 856 } 857 858 private PrefixMapping getPrefixMapping() 859 { return getGraph().getPrefixMapping(); } 860 861 public PrefixMapping lock() 862 { 863 getPrefixMapping().lock(); 864 return this; 865 } 866 867 public PrefixMapping setNsPrefix( String prefix, String uri ) 868 { 869 getPrefixMapping().setNsPrefix( prefix, uri ); 870 return this; 871 } 872 873 public PrefixMapping removeNsPrefix( String prefix ) 874 { 875 getPrefixMapping().removeNsPrefix( prefix ); 876 return this; 877 } 878 879 public PrefixMapping setNsPrefixes( PrefixMapping pm ) 880 { 881 getPrefixMapping().setNsPrefixes( pm ); 882 return this; 883 } 884 885 public PrefixMapping setNsPrefixes( Map map ) 886 { 887 getPrefixMapping().setNsPrefixes( map ); 888 return this; 889 } 890 891 public PrefixMapping withDefaultMappings( PrefixMapping other ) 892 { 893 getPrefixMapping().withDefaultMappings( other ); 894 return this; 895 } 896 897 public String getNsPrefixURI( String prefix ) 898 { return getPrefixMapping().getNsPrefixURI( prefix ); } 899 900 public String getNsURIPrefix( String uri ) 901 { return getPrefixMapping().getNsURIPrefix( uri ); } 902 903 public Map getNsPrefixMap() 904 { return getPrefixMapping().getNsPrefixMap(); } 905 906 public String expandPrefix( String prefixed ) 907 { return getPrefixMapping().expandPrefix( prefixed ); } 908 909 public String usePrefix( String uri ) 910 { return getPrefixMapping().shortForm( uri ); } 911 912 public String qnameFor( String uri ) 913 { return getPrefixMapping().qnameFor( uri ); } 914 915 public String shortForm( String uri ) 916 { return getPrefixMapping().shortForm( uri ); } 917 918 927 public static void addNamespaces( Model m, Map ns ) 928 { 929 PrefixMapping pm = m; 930 Iterator it = ns.entrySet().iterator(); 931 while (it.hasNext()) 932 { 933 Map.Entry e = (Map.Entry) it.next(); 934 String key = (String ) e.getKey(); 935 Set values = (Set) e.getValue(); 936 Set niceValues = CollectionFactory.createHashedSet(); 937 Iterator them = values.iterator(); 938 while (them.hasNext()) 939 { 940 String uri = (String ) them.next(); 941 if (PrefixMappingImpl.isNiceURI( uri )) niceValues.add( uri ); 942 } 943 if (niceValues.size() == 1) 944 pm.setNsPrefix( key, (String ) niceValues.iterator().next() ); 945 } 946 } 947 948 public StmtIterator listStatements() { 949 return IteratorFactory.asStmtIterator( GraphUtil.findAll( graph ), this); 950 } 951 952 955 public Model add(Statement s) { 956 add( s.getSubject(), s.getPredicate(), s.getObject() ); 957 return this; 958 } 959 960 964 public Model add( Statement [] statements ) 965 { 966 getBulkUpdateHandler().add( StatementImpl.asTriples( statements ) ); 967 return this; 968 } 969 970 protected BulkUpdateHandler getBulkUpdateHandler() 971 { return getGraph().getBulkUpdateHandler(); } 972 973 977 public Model add( List statements ) 978 { 979 getBulkUpdateHandler().add( asTriples( statements ) ); 980 return this; 981 } 982 983 private List asTriples( List statements ) 984 { 985 List L = new ArrayList( statements.size() ); 986 for (int i = 0; i < statements.size(); i += 1) 987 L.add( ((Statement) statements.get(i)).asTriple() ); 988 return L; 989 } 990 991 private Iterator asTriples( StmtIterator it ) 992 { return new Map1Iterator( mapAsTriple, it ); } 993 994 private Map1 mapAsTriple = new Map1() 995 { public Object map1( Object s ) { return ((Statement) s).asTriple(); } }; 996 997 1001 public Model remove( Statement [] statements ) 1002 { 1003 getBulkUpdateHandler().delete( StatementImpl.asTriples( statements ) ); 1004 return this; 1005 } 1006 1007 1011 public Model remove( List statements ) 1012 { 1013 getBulkUpdateHandler().delete( asTriples( statements ) ); 1014 return this; 1015 } 1016 1017 public Model add(Resource s,Property p,RDFNode o) { 1018 modelReifier.noteIfReified( s, p, o ); 1019 graph.add( Triple.create( s.asNode(), p.asNode(), o.asNode() ) ); 1020 return this; 1021 } 1022 1023 public ReificationStyle getReificationStyle() 1024 { return modelReifier.getReificationStyle(); } 1025 1026 1029 public RSIterator listReifiedStatements() 1030 { return modelReifier.listReifiedStatements(); } 1031 1032 1036 public RSIterator listReifiedStatements( Statement st ) 1037 { return modelReifier.listReifiedStatements( st ); } 1038 1039 1042 public boolean isReified( Statement s ) 1043 { return modelReifier.isReified( s ); } 1044 1045 1052 public Resource getAnyReifiedStatement(Statement s) 1053 { return modelReifier.getAnyReifiedStatement( s ); } 1054 1055 1059 public void removeAllReifications( Statement s ) 1060 { modelReifier.removeAllReifications( s ); } 1061 1062 public void removeReification( ReifiedStatement rs ) 1063 { modelReifier.removeReification( rs ); } 1064 1065 1068 public ReifiedStatement createReifiedStatement( Statement s ) 1069 { return modelReifier.createReifiedStatement( s ); } 1070 1071 public ReifiedStatement createReifiedStatement( String uri, Statement s ) 1072 { return modelReifier.createReifiedStatement( uri, s ); } 1073 1074 public boolean contains( Statement s ) 1075 { return graph.contains( s.asTriple() ); } 1076 1077 public boolean containsResource( RDFNode r ) 1078 { return graph.queryHandler().containsNode( r.asNode() ); } 1079 1080 public boolean contains( Resource s, Property p ) { 1081 ClosableIterator it = graph.find( asNode( s ), asNode( p ), null ); 1082 try { return it.hasNext(); } finally { it.close(); } 1083 } 1084 1085 public boolean contains( Resource s, Property p, RDFNode o ) 1086 { return graph.contains( asNode( s ), asNode( p ), asNode( o ) ); } 1087 1088 public Statement getRequiredProperty( Resource s, Property p ) 1089 { Statement st = getProperty( s, p ); 1090 if (st == null) throw new PropertyNotFoundException( p ); 1091 return st; } 1092 1093 public Statement getProperty( Resource s, Property p ) 1094 { 1095 StmtIterator iter = listStatements( s, p, (RDFNode) null ); 1096 try { return iter.hasNext() ? iter.nextStatement() : null; } 1097 finally { iter.close(); } 1098 } 1099 1100 public static Node asNode( RDFNode x ) 1101 { return x == null ? Node.ANY : x.asNode(); } 1102 1103 private NodeIterator listObjectsFor( RDFNode s, RDFNode p ) 1104 { 1105 ClosableIterator xit = graph.queryHandler().objectsFor( asNode( s ), asNode( p ) ); 1106 return IteratorFactory.asRDFNodeIterator( xit, this ); 1107 } 1108 1109 private ResIterator listSubjectsFor( RDFNode p, RDFNode o ) 1110 { 1111 ClosableIterator xit = graph.queryHandler().subjectsFor( asNode( p ), asNode( o ) ); 1112 return IteratorFactory.asResIterator( xit, this ); 1113 } 1114 1115 public ResIterator listSubjects() 1116 { return listSubjectsFor( null, null ); } 1117 1118 public ResIterator listSubjectsWithProperty(Property p) 1119 { return listSubjectsFor( p, null ); } 1120 1121 public ResIterator listSubjectsWithProperty(Property p, RDFNode o) 1122 { return listSubjectsFor( p, o ); } 1123 1124 public NodeIterator listObjects() 1125 { return listObjectsFor( null, null ); } 1126 1127 public NodeIterator listObjectsOfProperty(Property p) 1128 { return listObjectsFor( null, p ); } 1129 1130 public NodeIterator listObjectsOfProperty(Resource s, Property p) 1131 { return listObjectsFor( s, p ); } 1132 1133 public StmtIterator listStatements(final Selector selector) 1134 { 1135 StmtIterator sts = IteratorFactory.asStmtIterator( findTriplesFrom( selector ), this ); 1136 return selector.isSimple() 1137 ? sts 1138 : new StmtIteratorImpl( sts .filterKeep ( asFilter( selector ) ) ) 1139 ; 1140 } 1141 1142 1148 public Filter asFilter( final Selector s ) 1149 { return new Filter() 1150 { public boolean accept( Object x ) { return s.test( (Statement) x ); } }; 1151 } 1152 1153 1154 1162 public ExtendedIterator findTriplesFrom( Selector s ) 1163 { 1164 return graph.find 1165 ( asNode( s.getSubject() ), asNode( s.getPredicate() ), asNode( s.getObject() ) ); 1166 } 1167 1168 public boolean supportsTransactions() 1169 { return getTransactionHandler().transactionsSupported(); } 1170 1171 public Model begin() 1172 { getTransactionHandler().begin(); return this; } 1173 1174 public Model abort() 1175 { getTransactionHandler().abort(); return this; } 1176 1177 public Model commit() 1178 { getTransactionHandler().commit(); return this; } 1179 1180 public Object executeInTransaction( Command cmd ) 1181 { return getTransactionHandler().executeInTransaction( cmd ); } 1182 1183 private TransactionHandler getTransactionHandler() 1184 { return getGraph().getTransactionHandler(); } 1185 1186 public boolean independent() { 1187 return true; 1188 } 1189 1190 public Resource createResource() { 1191 return IteratorFactory.asResource(Node.createAnon(new AnonId()),this); 1192 } 1193 1194 public Resource createResource(String uri) { 1195 return getResource(uri); 1196 } 1197 1198 public Property createProperty(String uri) { 1199 return getProperty(uri); 1200 } 1201 1202 public Property createProperty(String nameSpace, String localName) 1203 { 1204 return getProperty(nameSpace, localName); 1205 } 1206 1207 1210 public Statement createStatement(Resource r, Property p, RDFNode o) 1211 { 1212 return new StatementImpl( r, p, o, this ); 1213 } 1214 1215 public Bag createBag(String uri) { 1216 return (Bag) getBag(uri).addProperty(RDF.type, RDF.Bag); 1217 } 1218 1219 public Alt createAlt(String uri) { 1220 return (Alt) getAlt(uri).addProperty(RDF.type, RDF.Alt); 1221 } 1222 1223 public Seq createSeq(String uri) { 1224 return (Seq) getSeq(uri).addProperty(RDF.type, RDF.Seq); 1225 } 1226 1227 1232 public Statement asStatement( Triple t ) 1233 { return StatementImpl.toStatement( t, this ); } 1234 1235 public Statement [] asStatements( Triple [] triples ) 1236 { 1237 Statement [] result = new Statement [triples.length]; 1238 for (int i = 0; i < triples.length; i += 1) result[i] = asStatement( triples[i] ); 1239 return result; 1240 } 1241 1242 public List asStatements( List triples ) 1243 { 1244 List L = new ArrayList( triples.size() ); 1245 for (int i = 0; i < triples.size(); i += 1) L.add( asStatement( (Triple) triples.get(i) ) ); 1246 return L; 1247 } 1248 1249 public Model asModel( Graph g ) 1250 { return new ModelCom( g ); } 1251 1252 public StmtIterator asStatements( final Iterator it ) 1253 { return new StmtIteratorImpl( new Map1Iterator( mapAsStatement, it ) ); } 1254 1255 protected Map1 mapAsStatement = new Map1() 1256 { public Object map1( Object t ) { return asStatement( (Triple) t ); } }; 1257 1258 public StmtIterator listBySubject( Container cont ) 1259 { return listStatements( cont, null, (RDFNode) null ); } 1260 1261 public void close() 1262 { graph.close(); } 1263 1264 public boolean supportsSetOperations() 1265 {return true;} 1266 1267 public Model query( Selector selector ) 1268 { return createWorkModel() .add( listStatements( selector ) ); } 1269 1270 public Model union( Model model ) 1271 { return createWorkModel() .add(this) .add( model ); } 1272 1273 1281 public Model intersection( Model other ) 1282 { return this.size() < other.size() ? intersect( this, other ) : intersect( other, this ); } 1283 1284 1290 public static Model intersect( Model smaller, Model larger ) 1291 { 1292 Model result = createWorkModel(); 1293 StmtIterator it = smaller.listStatements(); 1294 try { return addCommon( result, it, larger ); } 1295 finally { it.close(); } 1296 } 1297 1298 1307 protected static Model addCommon( Model result, StmtIterator it, Model other ) 1308 { 1309 while (it.hasNext()) 1310 { 1311 Statement s = it.nextStatement(); 1312 if (other.contains( s )) result.add( s ); 1313 } 1314 return result; 1315 } 1316 1317 public Model difference(Model model) { 1318 Model resultModel = createWorkModel(); 1319 StmtIterator iter = null; 1320 Statement stmt; 1321 try { 1322 iter = listStatements(); 1323 while (iter.hasNext()) { 1324 stmt = iter.nextStatement(); 1325 if (! model.contains(stmt)) { 1326 resultModel.add(stmt); 1327 } 1328 } 1329 return resultModel; 1330 } finally { 1331 iter.close(); 1332 } 1333 } 1334 1335 public String toString() 1336 { return "<ModelCom " + getGraph() + " | " + reifiedToString() + ">"; } 1337 1338 public String reifiedToString() 1339 { return statementsToString( getHiddenStatements().listStatements() ); } 1340 1341 protected String statementsToString( StmtIterator it ) 1342 { 1343 StringBuffer b = new StringBuffer (); 1344 while (it.hasNext()) b.append( " " ).append( it.nextStatement() ); 1345 return b.toString(); 1346 } 1347 1352 public Model getHiddenStatements() 1353 { return modelReifier.getHiddenStatements(); } 1354 1355 1359 public boolean isIsomorphicWith( Model m ) 1360 { 1361 Graph L = ModelFactory.withHiddenStatements( this ).getGraph(); 1362 Graph R = ModelFactory.withHiddenStatements( m ).getGraph(); 1363 return L.isIsomorphicWith( R ); 1364 } 1365 1366 public synchronized ModelLock getModelLock() 1367 { 1368 if ( modelLock == null ) 1369 modelLock = new ModelLockImpl() ; 1370 return modelLock ; 1371 } 1372 1373 public void enterCriticalSection(boolean requestReadLock) 1374 { 1375 this.getModelLock().enterCriticalSection(requestReadLock) ; 1376 } 1377 1378 public void leaveCriticalSection() 1379 { 1380 this.getModelLock().leaveCriticalSection() ; 1381 } 1382 1383 1390 public Model register( ModelChangedListener listener ) 1391 { 1392 getGraph().getEventManager().register( adapt( listener ) ); 1393 return this; 1394 } 1395 1396 1402 public Model unregister( ModelChangedListener listener ) 1403 { 1404 getGraph().getEventManager().unregister( adapt( listener ) ); 1405 return this; 1406 } 1407 1408 1415 public GraphListener adapt( final ModelChangedListener L ) 1416 { return new ModelListenerAdapter( this, L ); } 1417 1418 public Model notifyEvent( Object e ) 1419 { 1420 getGraph().getEventManager().notifyEvent( getGraph(), e ); 1421 return this; 1422 } 1423 } 1424 1425 1454 | Popular Tags |