Source Code Cross Referenced for ModelCom.java in  » RSS-RDF » Jena-2.5.5 » com » hp » hpl » jena » rdf » model » impl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » RSS RDF » Jena 2.5.5 » com.hp.hpl.jena.rdf.model.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:            (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
0003:            [See end of file]
0004:            $Id: ModelCom.java,v 1.126 2008/01/15 15:51:19 chris-dollin Exp $
0005:         */
0006:
0007:        package com.hp.hpl.jena.rdf.model.impl;
0008:
0009:        import com.hp.hpl.jena.rdf.model.*;
0010:        import com.hp.hpl.jena.shared.*;
0011:        import com.hp.hpl.jena.shared.impl.*;
0012:        import com.hp.hpl.jena.graph.*;
0013:        import com.hp.hpl.jena.graph.impl.*;
0014:        import com.hp.hpl.jena.graph.query.*;
0015:
0016:        import com.hp.hpl.jena.util.CollectionFactory;
0017:        import com.hp.hpl.jena.util.iterator.*;
0018:        import com.hp.hpl.jena.vocabulary.RDF;
0019:        import com.hp.hpl.jena.datatypes.*;
0020:        import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
0021:        import com.hp.hpl.jena.datatypes.xsd.XSDDateTime;
0022:        import com.hp.hpl.jena.enhanced.*;
0023:
0024:        import java.io.*;
0025:        import java.net.URL;
0026:        import java.util.*;
0027:
0028:        /** Common methods for model implementations.
0029:         *
0030:         * <P>This class implements common methods, mainly convenience methods, for
0031:         *    model implementations.  It is intended use is as a base class from which
0032:         *    model implemenations can be derived.</P>
0033:         *
0034:         * @author bwm
0035:         * hacked by Jeremy, tweaked by Chris (May 2002 - October 2002)
0036:         */
0037:
0038:        public class ModelCom extends EnhGraph implements  Model, PrefixMapping,
0039:                ModelLock {
0040:
0041:            private static final RDFReaderF readerFactory = new RDFReaderFImpl();
0042:            private static final RDFWriterF writerFactory = new RDFWriterFImpl();
0043:            private ModelLock modelLock = null;
0044:
0045:            /**
0046:            	make a model based on the specified graph
0047:             */
0048:            public ModelCom(Graph base) {
0049:                this (base, BuiltinPersonalities.model);
0050:            }
0051:
0052:            public ModelCom(Graph base, Personality personality) {
0053:                super (base, personality);
0054:                withDefaultMappings(defaultPrefixMapping);
0055:            }
0056:
0057:            private static PrefixMapping defaultPrefixMapping = PrefixMapping.Factory
0058:                    .create();
0059:
0060:            public static PrefixMapping getDefaultModelPrefixes() {
0061:                return defaultPrefixMapping;
0062:            }
0063:
0064:            public static PrefixMapping setDefaultModelPrefixes(PrefixMapping pm) {
0065:                PrefixMapping result = defaultPrefixMapping;
0066:                defaultPrefixMapping = pm;
0067:                return result;
0068:            }
0069:
0070:            public QueryHandler queryHandler() {
0071:                return getGraph().queryHandler();
0072:            }
0073:
0074:            public Graph getGraph() {
0075:                return graph;
0076:            }
0077:
0078:            protected static Model createWorkModel() {
0079:                return ModelFactory.createDefaultModel();
0080:            }
0081:
0082:            public RDFNode asRDFNode(Node n) {
0083:                return n.isLiteral() ? (RDFNode) this .getNodeAs(n,
0084:                        Literal.class) : (RDFNode) this .getNodeAs(n,
0085:                        Resource.class);
0086:            }
0087:
0088:            /**
0089:                the ModelReifier does everything to do with reification.
0090:             */
0091:            protected ModelReifier modelReifier = new ModelReifier(this );
0092:
0093:            public Resource getResource(String uri, ResourceF f) {
0094:                try {
0095:                    return f.createResource(getResource(uri));
0096:                } catch (Exception e) {
0097:                    throw new JenaException(e);
0098:                }
0099:            }
0100:
0101:            /**
0102:             * @deprecated Use {@link #addLiteral(Resource,Property,boolean)} instead
0103:             */
0104:            public Model addTyped(Resource s, Property p, boolean o) {
0105:                return addLiteral(s, p, o);
0106:            }
0107:
0108:            public Model addLiteral(Resource s, Property p, boolean o) {
0109:                return add(s, p, createTypedLiteral(o));
0110:            }
0111:
0112:            public Model addLiteral(Resource s, Property p, long o) {
0113:                return add(s, p, createTypedLiteral(o));
0114:            }
0115:
0116:            public Model addLiteral(Resource s, Property p, int o) {
0117:                return add(s, p, createTypedLiteral(o));
0118:            }
0119:
0120:            public Model addLiteral(Resource s, Property p, char o) {
0121:                return add(s, p, createTypedLiteral(o));
0122:            }
0123:
0124:            public Model addLiteral(Resource s, Property p, float o) {
0125:                return add(s, p, createTypedLiteral(o));
0126:            }
0127:
0128:            public Model addLiteral(Resource s, Property p, double o) {
0129:                return add(s, p, createTypedLiteral(o));
0130:            }
0131:
0132:            public Model add(Resource s, Property p, String o) {
0133:                return add(s, p, o, "", false);
0134:            }
0135:
0136:            public Model add(Resource s, Property p, String o,
0137:                    boolean wellFormed) {
0138:                add(s, p, literal(o, "", wellFormed));
0139:                return this ;
0140:            }
0141:
0142:            public Model add(Resource s, Property p, String o, String lang,
0143:                    boolean wellFormed) {
0144:                add(s, p, literal(o, lang, wellFormed));
0145:                return this ;
0146:            }
0147:
0148:            public Model add(Resource s, Property p, String lex,
0149:                    RDFDatatype datatype) {
0150:                add(s, p, literal(lex, datatype));
0151:                return this ;
0152:            }
0153:
0154:            private Literal literal(String s, String lang, boolean wellFormed) {
0155:                return new LiteralImpl(Node.createLiteral(s, lang, wellFormed),
0156:                        this );
0157:            }
0158:
0159:            private Literal literal(String lex, RDFDatatype datatype) {
0160:                return new LiteralImpl(Node.createLiteral(lex, "", datatype),
0161:                        this );
0162:            }
0163:
0164:            public Model add(Resource s, Property p, String o, String l) {
0165:                return add(s, p, o, l, false);
0166:            }
0167:
0168:            public Model addLiteral(Resource s, Property p, Object o) {
0169:                return add(s, p, asObject(o));
0170:            }
0171:
0172:            private RDFNode asObject(Object o) {
0173:                return o instanceof  RDFNode ? (RDFNode) o
0174:                        : createTypedLiteral(o);
0175:            }
0176:
0177:            public Model add(StmtIterator iter) {
0178:                try {
0179:                    getBulkUpdateHandler().add(asTriples(iter));
0180:                } finally {
0181:                    iter.close();
0182:                }
0183:                return this ;
0184:            }
0185:
0186:            public Model add(Model m) {
0187:                return add(m, false);
0188:            }
0189:
0190:            public Model add(Model m, boolean suppressReifications) {
0191:                getBulkUpdateHandler().add(m.getGraph(), !suppressReifications);
0192:                return this ;
0193:            }
0194:
0195:            public RDFReader getReader() {
0196:                return readerFactory.getReader();
0197:            }
0198:
0199:            public RDFReader getReader(String lang) {
0200:                return readerFactory.getReader(lang);
0201:            }
0202:
0203:            public String setReaderClassName(String lang, String className) {
0204:                return readerFactory.setReaderClassName(lang, className);
0205:            }
0206:
0207:            public Model read(String url) {
0208:                readerFactory.getReader().read(this , url);
0209:                return this ;
0210:            }
0211:
0212:            public Model read(Reader reader, String base) {
0213:                readerFactory.getReader().read(this , reader, base);
0214:                return this ;
0215:            }
0216:
0217:            public Model read(InputStream reader, String base) {
0218:                readerFactory.getReader().read(this , reader, base);
0219:                return this ;
0220:            }
0221:
0222:            public Model read(String url, String lang) {
0223:                readerFactory.getReader(lang).read(this , url);
0224:                return this ;
0225:            }
0226:
0227:            public Model read(String url, String base, String lang) {
0228:                try {
0229:                    InputStream is = new URL(url).openStream();
0230:                    try {
0231:                        read(is, base, lang);
0232:                    } finally {
0233:                        is.close();
0234:                    }
0235:                } catch (IOException e) {
0236:                    throw new WrappedIOException(e);
0237:                }
0238:                return this ;
0239:            }
0240:
0241:            public Model read(Reader reader, String base, String lang) {
0242:                readerFactory.getReader(lang).read(this , reader, base);
0243:                return this ;
0244:            }
0245:
0246:            public Model read(InputStream reader, String base, String lang) {
0247:                readerFactory.getReader(lang).read(this , reader, base);
0248:                return this ;
0249:            }
0250:
0251:            /**
0252:                Get the model's writer after priming it with the model's namespace
0253:                prefixes.
0254:             */
0255:            public RDFWriter getWriter() {
0256:                return writerFactory.getWriter();
0257:            }
0258:
0259:            /**
0260:                Get the model's writer after priming it with the model's namespace
0261:                prefixes.
0262:             */
0263:            public RDFWriter getWriter(String lang) {
0264:                return writerFactory.getWriter(lang);
0265:            }
0266:
0267:            public String setWriterClassName(String lang, String className) {
0268:                return writerFactory.setWriterClassName(lang, className);
0269:            }
0270:
0271:            public Model write(Writer writer) {
0272:                getWriter().write(this , writer, "");
0273:                return this ;
0274:            }
0275:
0276:            public Model write(Writer writer, String lang) {
0277:                getWriter(lang).write(this , writer, "");
0278:                return this ;
0279:            }
0280:
0281:            public Model write(Writer writer, String lang, String base) {
0282:                getWriter(lang).write(this , writer, base);
0283:                return this ;
0284:            }
0285:
0286:            public Model write(OutputStream writer) {
0287:                getWriter().write(this , writer, "");
0288:                return this ;
0289:            }
0290:
0291:            public Model write(OutputStream writer, String lang) {
0292:                getWriter(lang).write(this , writer, "");
0293:                return this ;
0294:            }
0295:
0296:            public Model write(OutputStream writer, String lang, String base) {
0297:                getWriter(lang).write(this , writer, base);
0298:                return this ;
0299:            }
0300:
0301:            public Model remove(Statement s) {
0302:                graph.delete(s.asTriple());
0303:                return this ;
0304:            }
0305:
0306:            public Model remove(Resource s, Property p, RDFNode o) {
0307:                graph.delete(Triple.create(s.asNode(), p.asNode(), o.asNode()));
0308:                return this ;
0309:            }
0310:
0311:            public Model remove(StmtIterator iter) {
0312:                getBulkUpdateHandler().delete(asTriples(iter));
0313:                return this ;
0314:            }
0315:
0316:            public Model remove(Model m) {
0317:                return remove(m, false);
0318:            }
0319:
0320:            public Model remove(Model m, boolean suppressReifications) {
0321:                getBulkUpdateHandler().delete(m.getGraph(),
0322:                        !suppressReifications);
0323:                return this ;
0324:            }
0325:
0326:            public Model removeAll() {
0327:                getGraph().getBulkUpdateHandler().removeAll();
0328:                return this ;
0329:            }
0330:
0331:            public Model removeAll(Resource s, Property p, RDFNode o) {
0332:                getGraph().getBulkUpdateHandler().remove(asNode(s), asNode(p),
0333:                        asNode(o));
0334:                return this ;
0335:            }
0336:
0337:            /**
0338:             * @deprecated Use {@link #containsLiteral(Resource,Property,boolean)} instead
0339:             */
0340:            public boolean containsTyped(Resource s, Property p, boolean o) {
0341:                return containsLiteral(s, p, o);
0342:            }
0343:
0344:            public boolean containsLiteral(Resource s, Property p, boolean o) {
0345:                return contains(s, p, createTypedLiteral(o));
0346:            }
0347:
0348:            public boolean containsLiteral(Resource s, Property p, long o) {
0349:                return contains(s, p, createTypedLiteral(o));
0350:            }
0351:
0352:            public boolean containsLiteral(Resource s, Property p, int o) {
0353:                return contains(s, p, createTypedLiteral(o));
0354:            }
0355:
0356:            public boolean containsLiteral(Resource s, Property p, char o) {
0357:                return contains(s, p, createTypedLiteral(o));
0358:            }
0359:
0360:            public boolean containsLiteral(Resource s, Property p, float o) {
0361:                return contains(s, p, createTypedLiteral(o));
0362:            }
0363:
0364:            public boolean containsLiteral(Resource s, Property p, double o) {
0365:                return contains(s, p, createTypedLiteral(o));
0366:            }
0367:
0368:            public boolean contains(Resource s, Property p, String o) {
0369:                return contains(s, p, o, "");
0370:            }
0371:
0372:            public boolean contains(Resource s, Property p, String o, String l) {
0373:                return contains(s, p, literal(o, l, false));
0374:            }
0375:
0376:            public boolean containsLiteral(Resource s, Property p, Object o) {
0377:                return contains(s, p, asObject(o));
0378:            }
0379:
0380:            public boolean containsAny(Model model) {
0381:                return containsAnyThenClose(model.listStatements());
0382:            }
0383:
0384:            public boolean containsAll(Model model) {
0385:                return containsAllThenClose(model.listStatements());
0386:            }
0387:
0388:            protected boolean containsAnyThenClose(StmtIterator iter) {
0389:                try {
0390:                    return containsAny(iter);
0391:                } finally {
0392:                    iter.close();
0393:                }
0394:            }
0395:
0396:            protected boolean containsAllThenClose(StmtIterator iter) {
0397:                try {
0398:                    return containsAll(iter);
0399:                } finally {
0400:                    iter.close();
0401:                }
0402:            }
0403:
0404:            public boolean containsAny(StmtIterator iter) {
0405:                while (iter.hasNext())
0406:                    if (contains(iter.nextStatement()))
0407:                        return true;
0408:                return false;
0409:            }
0410:
0411:            public boolean containsAll(StmtIterator iter) {
0412:                while (iter.hasNext())
0413:                    if (!contains(iter.nextStatement()))
0414:                        return false;
0415:                return true;
0416:            }
0417:
0418:            protected StmtIterator listStatements(Resource S, Property P, Node O) {
0419:                return IteratorFactory.asStmtIterator(graph.find(asNode(S),
0420:                        asNode(P), O), this );
0421:            }
0422:
0423:            public StmtIterator listStatements(Resource S, Property P, RDFNode O) {
0424:                return listStatements(S, P, asNode(O));
0425:            }
0426:
0427:            public StmtIterator listStatements(Resource S, Property P, String O) {
0428:                return O == null ? listStatements(S, P, Node.ANY)
0429:                        : listStatements(S, P, Node.createLiteral(O));
0430:            }
0431:
0432:            public StmtIterator listStatements(Resource S, Property P,
0433:                    String O, String L) {
0434:                return O == null ? listStatements(S, P, Node.ANY)
0435:                        : listStatements(S, P, Node.createLiteral(O, L, false));
0436:            }
0437:
0438:            public StmtIterator listLiteralStatements(Resource S, Property P,
0439:                    boolean O) {
0440:                return listStatements(S, P, createTypedLiteral(O));
0441:            }
0442:
0443:            public StmtIterator listLiteralStatements(Resource S, Property P,
0444:                    long O) {
0445:                return listStatements(S, P, createTypedLiteral(O));
0446:            }
0447:
0448:            public StmtIterator listLiteralStatements(Resource S, Property P,
0449:                    char O) {
0450:                return listStatements(S, P, createTypedLiteral(O));
0451:            }
0452:
0453:            public StmtIterator listlLiteralStatements(Resource S, Property P,
0454:                    float O) {
0455:                return listStatements(S, P, createTypedLiteral(O));
0456:            }
0457:
0458:            public StmtIterator listLiteralStatements(Resource S, Property P,
0459:                    double O) {
0460:                return listStatements(S, P, createTypedLiteral(O));
0461:            }
0462:
0463:            /*
0464:                 list resources with property [was: list subjects with property]
0465:             */
0466:
0467:            public ResIterator listResourcesWithProperty(Property p, boolean o) {
0468:                return listResourcesWithProperty(p, createTypedLiteral(o));
0469:            }
0470:
0471:            public ResIterator listResourcesWithProperty(Property p, char o) {
0472:                return listResourcesWithProperty(p, createTypedLiteral(o));
0473:            }
0474:
0475:            public ResIterator listResourcesWithProperty(Property p, long o) {
0476:                return listResourcesWithProperty(p, createTypedLiteral(o));
0477:            }
0478:
0479:            public ResIterator listResourcesWithProperty(Property p, float o) {
0480:                return listResourcesWithProperty(p, createTypedLiteral(o));
0481:            }
0482:
0483:            public ResIterator listResourcesWithProperty(Property p, double o) {
0484:                return listResourcesWithProperty(p, createTypedLiteral(o));
0485:            }
0486:
0487:            public ResIterator listResourcesWithProperty(Property p, Object o) {
0488:                return listResourcesWithProperty(p, createTypedLiteral(o));
0489:            }
0490:
0491:            public ResIterator listSubjectsWithProperty(Property p, RDFNode o) {
0492:                return listResourcesWithProperty(p, o);
0493:            }
0494:
0495:            public ResIterator listSubjectsWithProperty(Property p, String o) {
0496:                return listSubjectsWithProperty(p, o, "");
0497:            }
0498:
0499:            public ResIterator listSubjectsWithProperty(Property p, String o,
0500:                    String l) {
0501:                return listResourcesWithProperty(p, literal(o, l, false));
0502:            }
0503:
0504:            public Resource createResource(Resource type) {
0505:                return createResource().addProperty(RDF.type, type);
0506:            }
0507:
0508:            public Resource createResource(String uri, Resource type) {
0509:                return getResource(uri).addProperty(RDF.type, type);
0510:            }
0511:
0512:            public Resource createResource(ResourceF f) {
0513:                return createResource(null, f);
0514:            }
0515:
0516:            public Resource createResource(AnonId id) {
0517:                return new ResourceImpl(id, this );
0518:            }
0519:
0520:            public Resource createResource(String uri, ResourceF f) {
0521:                return f.createResource(createResource(uri));
0522:            }
0523:
0524:            /** create a type literal from a boolean value.
0525:             *
0526:             * <p> The value is converted to a string using its <CODE>toString</CODE>
0527:             * method. </p>
0528:             * @param v the value of the literal
0529:             * 
0530:             * @return a new literal representing the value v
0531:             */
0532:            public Literal createTypedLiteral(boolean v) {
0533:                return createTypedLiteral(new Boolean(v));
0534:            }
0535:
0536:            /** create a typed literal from an integer value.
0537:             *
0538:             * @param v the value of the literal
0539:             * 
0540:             * @return a new literal representing the value v
0541:             */
0542:            public Literal createTypedLiteral(int v) {
0543:                return createTypedLiteral(new Integer(v));
0544:            }
0545:
0546:            /** create a typed literal from a long integer value.
0547:             *
0548:             * @param v the value of the literal
0549:             * 
0550:             * @return a new literal representing the value v
0551:             */
0552:            public Literal createTypedLiteral(long v) {
0553:                return createTypedLiteral(new Long(v));
0554:            }
0555:
0556:            /** create a typed literal from a char value.
0557:             *
0558:             * @param v the value of the literal
0559:             * 
0560:             * @return a new literal representing the value v
0561:             */
0562:            public Literal createTypedLiteral(char v) {
0563:                return createTypedLiteral(new Character(v));
0564:            }
0565:
0566:            /** create a typed literal from a float value.
0567:             *
0568:             * @param v the value of the literal
0569:             * 
0570:             * @return a new literal representing the value v
0571:             */
0572:            public Literal createTypedLiteral(float v) {
0573:                return createTypedLiteral(new Float(v));
0574:            }
0575:
0576:            /** create a typed literal from a double value.
0577:             *
0578:             * @param v the value of the literal
0579:             * 
0580:             * @return a new literal representing the value v
0581:             */
0582:            public Literal createTypedLiteral(double v) {
0583:                return createTypedLiteral(new Double(v));
0584:            }
0585:
0586:            /** create a typed literal from a String value.
0587:             *
0588:             * @param v the value of the literal
0589:             * 
0590:             * @return a new literal representing the value v
0591:             */
0592:            public Literal createTypedLiteral(String v) {
0593:                LiteralLabel ll = new LiteralLabel(v);
0594:                return new LiteralImpl(Node.createLiteral(ll), this );
0595:            }
0596:
0597:            /**
0598:             * Create a typed literal xsd:dateTime from a Calendar object. 
0599:             */
0600:            public Literal createTypedLiteral(Calendar cal) {
0601:                Object value = new XSDDateTime(cal);
0602:                LiteralLabel ll = new LiteralLabel(value, "",
0603:                        XSDDatatype.XSDdateTime);
0604:                return new LiteralImpl(Node.createLiteral(ll), this );
0605:
0606:            }
0607:
0608:            /**
0609:             * Build a typed literal from its lexical form. The
0610:             * lexical form will be parsed now and the value stored. If
0611:             * the form is not legal this will throw an exception.
0612:             * 
0613:             * @param lex the lexical form of the literal
0614:             * @param dtype the type of the literal, null for old style "plain" literals
0615:             * @throws DatatypeFormatException if lex is not a legal form of dtype
0616:             */
0617:            public Literal createTypedLiteral(String lex, RDFDatatype dtype)
0618:                    throws DatatypeFormatException {
0619:                return new LiteralImpl(Node.createLiteral(lex, "", dtype), this );
0620:            }
0621:
0622:            /**
0623:             * Build a typed literal from its value form.
0624:             * 
0625:             * @param value the value of the literal
0626:             * @param dtype the type of the literal, null for old style "plain" literals
0627:             */
0628:            public Literal createTypedLiteral(Object value, RDFDatatype dtype) {
0629:                LiteralLabel ll = new LiteralLabel(value, "", dtype);
0630:                return new LiteralImpl(Node.createLiteral(ll), this );
0631:            }
0632:
0633:            /**
0634:             * Build a typed literal from its lexical form. The
0635:             * lexical form will be parsed now and the value stored. If
0636:             * the form is not legal this will throw an exception.
0637:             * 
0638:             * @param lex the lexical form of the literal
0639:             * @param typeURI the uri of the type of the literal, null for old style "plain" literals
0640:             * @throws DatatypeFormatException if lex is not a legal form of dtype
0641:             */
0642:            public Literal createTypedLiteral(String lex, String typeURI) {
0643:                RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(
0644:                        typeURI);
0645:                LiteralLabel ll = LiteralLabel.createLiteralLabel(lex, "", dt);
0646:                return new LiteralImpl(Node.createLiteral(ll), this );
0647:            }
0648:
0649:            /**
0650:             * Build a typed literal from its value form.
0651:             * 
0652:             * @param value the value of the literal
0653:             * @param typeURI the URI of the type of the literal, null for old style "plain" literals
0654:             */
0655:            public Literal createTypedLiteral(Object value, String typeURI) {
0656:                RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(
0657:                        typeURI);
0658:                LiteralLabel ll = new LiteralLabel(value, "", dt);
0659:                return new LiteralImpl(Node.createLiteral(ll), this );
0660:            }
0661:
0662:            /**
0663:             * Build a typed literal label from its value form using
0664:             * whatever datatype is currently registered as the the default
0665:             * representation for this java class. No language tag is supplied.
0666:             * @param value the literal value to encapsulate
0667:             */
0668:            public Literal createTypedLiteral(Object value) {
0669:                // Catch special case of a Calendar which we want to act as if it were an XSDDateTime
0670:                if (value instanceof  Calendar)
0671:                    return createTypedLiteral((Calendar) value);
0672:                LiteralLabel ll = new LiteralLabel(value);
0673:                return new LiteralImpl(Node.createLiteral(ll), this );
0674:            }
0675:
0676:            public Literal createLiteral(String v) {
0677:                return createLiteral(v, "");
0678:            }
0679:
0680:            public Literal createLiteral(String v, String l) {
0681:                return literal(v, l, false);
0682:            }
0683:
0684:            public Literal createLiteral(String v, boolean wellFormed) {
0685:                return literal(v, "", wellFormed);
0686:            }
0687:
0688:            public Literal createLiteral(String v, String l, boolean wellFormed) {
0689:                return literal(v, l, wellFormed);
0690:            }
0691:
0692:            public Statement createLiteralStatement(Resource r, Property p,
0693:                    boolean o) {
0694:                return createStatement(r, p, createTypedLiteral(o));
0695:            }
0696:
0697:            public Statement createLiteralStatement(Resource r, Property p,
0698:                    long o) {
0699:                return createStatement(r, p, createTypedLiteral(o));
0700:            }
0701:
0702:            public Statement createLiteralStatement(Resource r, Property p,
0703:                    int o) {
0704:                return createStatement(r, p, createTypedLiteral(o));
0705:            }
0706:
0707:            public Statement createLiteralStatement(Resource r, Property p,
0708:                    char o) {
0709:                return createStatement(r, p, createTypedLiteral(o));
0710:            }
0711:
0712:            public Statement createLiteralStatement(Resource r, Property p,
0713:                    float o) {
0714:                return createStatement(r, p, createTypedLiteral(o));
0715:            }
0716:
0717:            public Statement createLiteralStatement(Resource r, Property p,
0718:                    double o) {
0719:                return createStatement(r, p, createTypedLiteral(o));
0720:            }
0721:
0722:            public Statement createStatement(Resource r, Property p, String o) {
0723:                return createStatement(r, p, createLiteral(o));
0724:            }
0725:
0726:            public Statement createLiteralStatement(Resource r, Property p,
0727:                    Object o) {
0728:                return createStatement(r, p, asObject(o));
0729:            }
0730:
0731:            public Statement createStatement(Resource r, Property p, String o,
0732:                    boolean wellFormed) {
0733:                return createStatement(r, p, o, "", wellFormed);
0734:            }
0735:
0736:            public Statement createStatement(Resource r, Property p, String o,
0737:                    String l) {
0738:                return createStatement(r, p, o, l, false);
0739:            }
0740:
0741:            public Statement createStatement(Resource r, Property p, String o,
0742:                    String l, boolean wellFormed) {
0743:                return createStatement(r, p, literal(o, l, wellFormed));
0744:            }
0745:
0746:            public Bag createBag() {
0747:                return createBag(null);
0748:            }
0749:
0750:            public Alt createAlt() {
0751:                return createAlt(null);
0752:            }
0753:
0754:            public Seq createSeq() {
0755:                return createSeq(null);
0756:            }
0757:
0758:            /**
0759:                Answer a (the) new empty list
0760:                @return An RDF-encoded list of no elements (ie nil)
0761:             */
0762:            public RDFList createList() {
0763:                return (RDFList) getResource(RDF.nil.getURI())
0764:                        .as(RDFList.class);
0765:            }
0766:
0767:            /**
0768:             * <p>Answer a new list containing the resources from the given iterator, in order.</p>
0769:             * @param members An iterator, each value of which is expected to be an RDFNode.
0770:             * @return An RDF-encoded list of the elements of the iterator
0771:             */
0772:            public RDFList createList(Iterator members) {
0773:                RDFList list = createList();
0774:
0775:                while (members != null && members.hasNext()) {
0776:                    list = list.with((RDFNode) members.next());
0777:                }
0778:
0779:                return list;
0780:            }
0781:
0782:            /**
0783:             * <p>Answer a new list containing the RDF nodes from the given array, in order</p>
0784:             * @param members An array of RDFNodes that will be the members of the list
0785:             * @return An RDF-encoded list 
0786:             */
0787:            public RDFList createList(RDFNode[] members) {
0788:                return createList(Arrays.asList(members).iterator());
0789:            }
0790:
0791:            public RDFNode getRDFNode(Node n) {
0792:                return asRDFNode(n);
0793:            }
0794:
0795:            public Resource getResource(String uri) {
0796:                return IteratorFactory.asResource(makeURI(uri), this );
0797:            }
0798:
0799:            public Property getProperty(String uri) {
0800:                if (uri == null)
0801:                    throw new InvalidPropertyURIException(null);
0802:                return IteratorFactory.asProperty(makeURI(uri), this );
0803:            }
0804:
0805:            public Property getProperty(String nameSpace, String localName) {
0806:                return getProperty(nameSpace + localName);
0807:            }
0808:
0809:            public Seq getSeq(String uri) {
0810:                return (Seq) IteratorFactory.asResource(makeURI(uri),
0811:                        Seq.class, this );
0812:            }
0813:
0814:            public Seq getSeq(Resource r) {
0815:                return (Seq) r.as(Seq.class);
0816:            }
0817:
0818:            public Bag getBag(String uri) {
0819:                return (Bag) IteratorFactory.asResource(makeURI(uri),
0820:                        Bag.class, this );
0821:            }
0822:
0823:            public Bag getBag(Resource r) {
0824:                return (Bag) r.as(Bag.class);
0825:            }
0826:
0827:            static private Node makeURI(String uri) {
0828:                return uri == null ? Node.createAnon() : Node.createURI(uri);
0829:            }
0830:
0831:            public Alt getAlt(String uri) {
0832:                return (Alt) IteratorFactory.asResource(makeURI(uri),
0833:                        Alt.class, this );
0834:            }
0835:
0836:            public Alt getAlt(Resource r) {
0837:                return (Alt) r.as(Alt.class);
0838:            }
0839:
0840:            public long size() {
0841:                return graph.size();
0842:            }
0843:
0844:            public boolean isEmpty() {
0845:                return graph.isEmpty();
0846:            }
0847:
0848:            private void updateNamespace(Set set, Iterator it) {
0849:                while (it.hasNext()) {
0850:                    Node node = (Node) it.next();
0851:                    if (node.isURI()) {
0852:                        String uri = node.getURI();
0853:                        String ns = uri.substring(0, Util.splitNamespace(uri));
0854:                        // String ns = IteratorFactory.asResource( node, this ).getNameSpace();
0855:                        set.add(ns);
0856:                    }
0857:                }
0858:            }
0859:
0860:            private Iterator listPredicates() {
0861:                return getGraph().queryHandler().predicatesFor(Node.ANY,
0862:                        Node.ANY);
0863:            }
0864:
0865:            private Iterator listTypes() {
0866:                Set types = CollectionFactory.createHashedSet();
0867:                ClosableIterator it = graph.find(null, RDF.type.asNode(), null);
0868:                while (it.hasNext())
0869:                    types.add(((Triple) it.next()).getObject());
0870:                return types.iterator();
0871:            }
0872:
0873:            public NsIterator listNameSpaces() {
0874:                Set nameSpaces = CollectionFactory.createHashedSet();
0875:                updateNamespace(nameSpaces, listPredicates());
0876:                updateNamespace(nameSpaces, listTypes());
0877:                return new NsIteratorImpl(nameSpaces.iterator(), nameSpaces);
0878:            }
0879:
0880:            private PrefixMapping getPrefixMapping() {
0881:                return getGraph().getPrefixMapping();
0882:            }
0883:
0884:            public boolean samePrefixMappingAs(PrefixMapping other) {
0885:                return getPrefixMapping().samePrefixMappingAs(other);
0886:            }
0887:
0888:            public PrefixMapping lock() {
0889:                getPrefixMapping().lock();
0890:                return this ;
0891:            }
0892:
0893:            public PrefixMapping setNsPrefix(String prefix, String uri) {
0894:                getPrefixMapping().setNsPrefix(prefix, uri);
0895:                return this ;
0896:            }
0897:
0898:            public PrefixMapping removeNsPrefix(String prefix) {
0899:                getPrefixMapping().removeNsPrefix(prefix);
0900:                return this ;
0901:            }
0902:
0903:            public PrefixMapping setNsPrefixes(PrefixMapping pm) {
0904:                getPrefixMapping().setNsPrefixes(pm);
0905:                return this ;
0906:            }
0907:
0908:            public PrefixMapping setNsPrefixes(Map map) {
0909:                getPrefixMapping().setNsPrefixes(map);
0910:                return this ;
0911:            }
0912:
0913:            public PrefixMapping withDefaultMappings(PrefixMapping other) {
0914:                getPrefixMapping().withDefaultMappings(other);
0915:                return this ;
0916:            }
0917:
0918:            public String getNsPrefixURI(String prefix) {
0919:                return getPrefixMapping().getNsPrefixURI(prefix);
0920:            }
0921:
0922:            public String getNsURIPrefix(String uri) {
0923:                return getPrefixMapping().getNsURIPrefix(uri);
0924:            }
0925:
0926:            public Map getNsPrefixMap() {
0927:                return getPrefixMapping().getNsPrefixMap();
0928:            }
0929:
0930:            public String expandPrefix(String prefixed) {
0931:                return getPrefixMapping().expandPrefix(prefixed);
0932:            }
0933:
0934:            public String qnameFor(String uri) {
0935:                return getPrefixMapping().qnameFor(uri);
0936:            }
0937:
0938:            public String shortForm(String uri) {
0939:                return getPrefixMapping().shortForm(uri);
0940:            }
0941:
0942:            /**
0943:                Service method to update the namespaces of  a Model given the
0944:                mappings from prefix names to sets of URIs.
0945:                
0946:                If the prefix maps to multiple URIs, then we discard it completely.
0947:                
0948:                @param the Model who's namespace is to be updated
0949:                @param ns the namespace map to add to the Model      
0950:             */
0951:            public static void addNamespaces(Model m, Map ns) {
0952:                PrefixMapping pm = m;
0953:                Iterator it = ns.entrySet().iterator();
0954:                while (it.hasNext()) {
0955:                    Map.Entry e = (Map.Entry) it.next();
0956:                    String key = (String) e.getKey();
0957:                    Set values = (Set) e.getValue();
0958:                    Set niceValues = CollectionFactory.createHashedSet();
0959:                    Iterator them = values.iterator();
0960:                    while (them.hasNext()) {
0961:                        String uri = (String) them.next();
0962:                        if (PrefixMappingImpl.isNiceURI(uri))
0963:                            niceValues.add(uri);
0964:                    }
0965:                    if (niceValues.size() == 1)
0966:                        pm.setNsPrefix(key, (String) niceValues.iterator()
0967:                                .next());
0968:                }
0969:            }
0970:
0971:            public StmtIterator listStatements() {
0972:                return IteratorFactory.asStmtIterator(GraphUtil.findAll(graph),
0973:                        this );
0974:            }
0975:
0976:            /**
0977:                add a Statement to this Model by adding its SPO components.
0978:             */
0979:            public Model add(Statement s) {
0980:                add(s.getSubject(), s.getPredicate(), s.getObject());
0981:                return this ;
0982:            }
0983:
0984:            /**
0985:                Add all the statements to the model by converting them to an array of corresponding
0986:                triples and removing those from the underlying graph.
0987:             */
0988:            public Model add(Statement[] statements) {
0989:                getBulkUpdateHandler().add(StatementImpl.asTriples(statements));
0990:                return this ;
0991:            }
0992:
0993:            protected BulkUpdateHandler getBulkUpdateHandler() {
0994:                return getGraph().getBulkUpdateHandler();
0995:            }
0996:
0997:            /**
0998:                Add all the statements to the model by converting the list to an array of
0999:                Statement and removing that.
1000:             */
1001:            public Model add(List statements) {
1002:                getBulkUpdateHandler().add(asTriples(statements));
1003:                return this ;
1004:            }
1005:
1006:            private List asTriples(List statements) {
1007:                List L = new ArrayList(statements.size());
1008:                for (int i = 0; i < statements.size(); i += 1)
1009:                    L.add(((Statement) statements.get(i)).asTriple());
1010:                return L;
1011:            }
1012:
1013:            private Iterator asTriples(StmtIterator it) {
1014:                return new Map1Iterator(mapAsTriple, it);
1015:            }
1016:
1017:            private Map1 mapAsTriple = new Map1() {
1018:                public Object map1(Object s) {
1019:                    return ((Statement) s).asTriple();
1020:                }
1021:            };
1022:
1023:            /**
1024:                remove all the Statements from the model by converting them to triples and
1025:                removing those triples from the underlying graph.        
1026:             */
1027:            public Model remove(Statement[] statements) {
1028:                getBulkUpdateHandler().delete(
1029:                        StatementImpl.asTriples(statements));
1030:                return this ;
1031:            }
1032:
1033:            /**
1034:                Remove all the Statements from the model by converting the List to a
1035:                List(Statement) and removing that.
1036:             */
1037:            public Model remove(List statements) {
1038:                getBulkUpdateHandler().delete(asTriples(statements));
1039:                return this ;
1040:            }
1041:
1042:            public Model add(Resource s, Property p, RDFNode o) {
1043:                modelReifier.noteIfReified(s, p, o);
1044:                graph.add(Triple.create(s.asNode(), p.asNode(), o.asNode()));
1045:                return this ;
1046:            }
1047:
1048:            public ReificationStyle getReificationStyle() {
1049:                return modelReifier.getReificationStyle();
1050:            }
1051:
1052:            /**
1053:                @return an iterator which delivers all the ReifiedStatements in this model
1054:             */
1055:            public RSIterator listReifiedStatements() {
1056:                return modelReifier.listReifiedStatements();
1057:            }
1058:
1059:            /**
1060:                @return an iterator each of whose elements is a ReifiedStatement in this
1061:                    model such that it's getStatement().equals( st )
1062:             */
1063:            public RSIterator listReifiedStatements(Statement st) {
1064:                return modelReifier.listReifiedStatements(st);
1065:            }
1066:
1067:            /**
1068:                @return true iff this model has a reification of _s_ in some Statement
1069:             */
1070:            public boolean isReified(Statement s) {
1071:                return modelReifier.isReified(s);
1072:            }
1073:
1074:            /**
1075:                get any reification of the given statement in this model; make
1076:                one if necessary.
1077:                
1078:                @param s for which a reification is sought
1079:                @return a ReifiedStatement that reifies _s_
1080:             */
1081:            public Resource getAnyReifiedStatement(Statement s) {
1082:                return modelReifier.getAnyReifiedStatement(s);
1083:            }
1084:
1085:            /**
1086:                remove any ReifiedStatements reifying the given statement
1087:                @param s the statement who's reifications are to be discarded
1088:             */
1089:            public void removeAllReifications(Statement s) {
1090:                modelReifier.removeAllReifications(s);
1091:            }
1092:
1093:            public void removeReification(ReifiedStatement rs) {
1094:                modelReifier.removeReification(rs);
1095:            }
1096:
1097:            /**
1098:                create a ReifiedStatement that encodes _s_ and belongs to this Model.
1099:             */
1100:            public ReifiedStatement createReifiedStatement(Statement s) {
1101:                return modelReifier.createReifiedStatement(s);
1102:            }
1103:
1104:            public ReifiedStatement createReifiedStatement(String uri,
1105:                    Statement s) {
1106:                return modelReifier.createReifiedStatement(uri, s);
1107:            }
1108:
1109:            public boolean contains(Statement s) {
1110:                return graph.contains(s.asTriple());
1111:            }
1112:
1113:            public boolean containsResource(RDFNode r) {
1114:                return graph.queryHandler().containsNode(r.asNode());
1115:            }
1116:
1117:            public boolean contains(Resource s, Property p) {
1118:                return contains(s, p, (RDFNode) null);
1119:            }
1120:
1121:            public boolean contains(Resource s, Property p, RDFNode o) {
1122:                return graph.contains(asNode(s), asNode(p), asNode(o));
1123:            }
1124:
1125:            public Statement getRequiredProperty(Resource s, Property p) {
1126:                Statement st = getProperty(s, p);
1127:                if (st == null)
1128:                    throw new PropertyNotFoundException(p);
1129:                return st;
1130:            }
1131:
1132:            public Statement getProperty(Resource s, Property p) {
1133:                StmtIterator iter = listStatements(s, p, (RDFNode) null);
1134:                try {
1135:                    return iter.hasNext() ? iter.nextStatement() : null;
1136:                } finally {
1137:                    iter.close();
1138:                }
1139:            }
1140:
1141:            public static Node asNode(RDFNode x) {
1142:                return x == null ? Node.ANY : x.asNode();
1143:            }
1144:
1145:            private NodeIterator listObjectsFor(RDFNode s, RDFNode p) {
1146:                ClosableIterator xit = graph.queryHandler().objectsFor(
1147:                        asNode(s), asNode(p));
1148:                return IteratorFactory.asRDFNodeIterator(xit, this );
1149:            }
1150:
1151:            private ResIterator listSubjectsFor(RDFNode p, RDFNode o) {
1152:                ClosableIterator xit = graph.queryHandler().subjectsFor(
1153:                        asNode(p), asNode(o));
1154:                return IteratorFactory.asResIterator(xit, this );
1155:            }
1156:
1157:            public ResIterator listSubjects() {
1158:                return listSubjectsFor(null, null);
1159:            }
1160:
1161:            public ResIterator listResourcesWithProperty(Property p) {
1162:                return listSubjectsFor(p, null);
1163:            }
1164:
1165:            public ResIterator listSubjectsWithProperty(Property p) {
1166:                return listResourcesWithProperty(p);
1167:            }
1168:
1169:            public ResIterator listResourcesWithProperty(Property p, RDFNode o) {
1170:                return listSubjectsFor(p, o);
1171:            }
1172:
1173:            public NodeIterator listObjects() {
1174:                return listObjectsFor(null, null);
1175:            }
1176:
1177:            public NodeIterator listObjectsOfProperty(Property p) {
1178:                return listObjectsFor(null, p);
1179:            }
1180:
1181:            public NodeIterator listObjectsOfProperty(Resource s, Property p) {
1182:                return listObjectsFor(s, p);
1183:            }
1184:
1185:            public StmtIterator listStatements(final Selector selector) {
1186:                StmtIterator sts = IteratorFactory.asStmtIterator(
1187:                        findTriplesFrom(selector), this );
1188:                return selector.isSimple() ? sts : new StmtIteratorImpl(sts
1189:                        .filterKeep(asFilter(selector)));
1190:            }
1191:
1192:            /**
1193:                Answer a Filter that filters exactly those things the Selector selects.
1194:                
1195:                @param s a Selector on statements
1196:                @return a Filter that accepts statements that s passes tests on
1197:             */
1198:            public Filter asFilter(final Selector s) {
1199:                return new Filter() {
1200:                    public boolean accept(Object x) {
1201:                        return s.test((Statement) x);
1202:                    }
1203:                };
1204:            }
1205:
1206:            /**
1207:                Answer an [extended] iterator which returns the triples in this graph which
1208:                are selected by the (S, P, O) triple in the selector, ignoring any special
1209:                tests it may do.
1210:                
1211:                @param s a Selector used to supply subject, predicate, and object
1212:                @return an extended iterator over the matching (S, P, O) triples
1213:             */
1214:            public ExtendedIterator findTriplesFrom(Selector s) {
1215:                return graph.find(asNode(s.getSubject()), asNode(s
1216:                        .getPredicate()), asNode(s.getObject()));
1217:            }
1218:
1219:            public boolean supportsTransactions() {
1220:                return getTransactionHandler().transactionsSupported();
1221:            }
1222:
1223:            public Model begin() {
1224:                getTransactionHandler().begin();
1225:                return this ;
1226:            }
1227:
1228:            public Model abort() {
1229:                getTransactionHandler().abort();
1230:                return this ;
1231:            }
1232:
1233:            public Model commit() {
1234:                getTransactionHandler().commit();
1235:                return this ;
1236:            }
1237:
1238:            public Object executeInTransaction(Command cmd) {
1239:                return getTransactionHandler().executeInTransaction(cmd);
1240:            }
1241:
1242:            private TransactionHandler getTransactionHandler() {
1243:                return getGraph().getTransactionHandler();
1244:            }
1245:
1246:            public boolean independent() {
1247:                return true;
1248:            }
1249:
1250:            public Resource createResource() {
1251:                return IteratorFactory.asResource(Node.createAnon(), this );
1252:            }
1253:
1254:            public Resource createResource(String uri) {
1255:                return getResource(uri);
1256:            }
1257:
1258:            public Property createProperty(String uri) {
1259:                return getProperty(uri);
1260:            }
1261:
1262:            public Property createProperty(String nameSpace, String localName) {
1263:                return getProperty(nameSpace, localName);
1264:            }
1265:
1266:            /**
1267:                create a Statement from the given r, p, and o.
1268:             */
1269:            public Statement createStatement(Resource r, Property p, RDFNode o) {
1270:                return new StatementImpl(r, p, o, this );
1271:            }
1272:
1273:            public Bag createBag(String uri) {
1274:                return (Bag) getBag(uri).addProperty(RDF.type, RDF.Bag);
1275:            }
1276:
1277:            public Alt createAlt(String uri) {
1278:                return (Alt) getAlt(uri).addProperty(RDF.type, RDF.Alt);
1279:            }
1280:
1281:            public Seq createSeq(String uri) {
1282:                return (Seq) getSeq(uri).addProperty(RDF.type, RDF.Seq);
1283:            }
1284:
1285:            /**
1286:                Answer a Statement in this Model whcih encodes the given Triple.
1287:                @param t a triple to wrap as a statement
1288:                @return a statement wrapping the triple and in this model
1289:             */
1290:            public Statement asStatement(Triple t) {
1291:                return StatementImpl.toStatement(t, this );
1292:            }
1293:
1294:            public Statement[] asStatements(Triple[] triples) {
1295:                Statement[] result = new Statement[triples.length];
1296:                for (int i = 0; i < triples.length; i += 1)
1297:                    result[i] = asStatement(triples[i]);
1298:                return result;
1299:            }
1300:
1301:            public List asStatements(List triples) {
1302:                List L = new ArrayList(triples.size());
1303:                for (int i = 0; i < triples.size(); i += 1)
1304:                    L.add(asStatement((Triple) triples.get(i)));
1305:                return L;
1306:            }
1307:
1308:            public Model asModel(Graph g) {
1309:                return new ModelCom(g);
1310:            }
1311:
1312:            public StmtIterator asStatements(final Iterator it) {
1313:                return new StmtIteratorImpl(
1314:                        new Map1Iterator(mapAsStatement, it));
1315:            }
1316:
1317:            protected Map1 mapAsStatement = new Map1() {
1318:                public Object map1(Object t) {
1319:                    return asStatement((Triple) t);
1320:                }
1321:            };
1322:
1323:            public StmtIterator listBySubject(Container cont) {
1324:                return listStatements(cont, null, (RDFNode) null);
1325:            }
1326:
1327:            public void close() {
1328:                graph.close();
1329:            }
1330:
1331:            public boolean isClosed() {
1332:                return graph.isClosed();
1333:            }
1334:
1335:            public boolean supportsSetOperations() {
1336:                return true;
1337:            }
1338:
1339:            public Model query(Selector selector) {
1340:                return createWorkModel().add(listStatements(selector));
1341:            }
1342:
1343:            public Model union(Model model) {
1344:                return createWorkModel().add(this ).add(model);
1345:            }
1346:
1347:            /**
1348:                Intersect this with another model. As an attempt at optimisation, we try and ensure
1349:                we iterate over the smaller model first. Nowadays it's not clear that this is a good
1350:                idea, since <code>size()</code> can be expensive on database and inference
1351:                models.
1352:                
1353:             	@see com.hp.hpl.jena.rdf.model.Model#intersection(com.hp.hpl.jena.rdf.model.Model)
1354:             */
1355:            public Model intersection(Model other) {
1356:                return this .size() < other.size() ? intersect(this , other)
1357:                        : intersect(other, this );
1358:            }
1359:
1360:            /**
1361:                Answer a Model that is the intersection of the two argument models. The first
1362:                argument is the model iterated over, and the second argument is the one used
1363:                to check for membership. [So the first one should be "small" and the second one
1364:                "membership cheap".]
1365:             */
1366:            public static Model intersect(Model smaller, Model larger) {
1367:                Model result = createWorkModel();
1368:                StmtIterator it = smaller.listStatements();
1369:                try {
1370:                    return addCommon(result, it, larger);
1371:                } finally {
1372:                    it.close();
1373:                }
1374:            }
1375:
1376:            /**
1377:                Answer the argument result with all the statements from the statement iterator that
1378:                are in the other model added to it.
1379:                
1380:             	@param result the Model to add statements to and return
1381:             	@param it an iterator over the candidate statements
1382:             	@param other the model that must contain the statements to be added
1383:             	@return result, after the suitable statements have been added to it
1384:             */
1385:            protected static Model addCommon(Model result, StmtIterator it,
1386:                    Model other) {
1387:                while (it.hasNext()) {
1388:                    Statement s = it.nextStatement();
1389:                    if (other.contains(s))
1390:                        result.add(s);
1391:                }
1392:                return result;
1393:            }
1394:
1395:            public Model difference(Model model) {
1396:                Model resultModel = createWorkModel();
1397:                StmtIterator iter = null;
1398:                Statement stmt;
1399:                try {
1400:                    iter = listStatements();
1401:                    while (iter.hasNext()) {
1402:                        stmt = iter.nextStatement();
1403:                        if (!model.contains(stmt)) {
1404:                            resultModel.add(stmt);
1405:                        }
1406:                    }
1407:                    return resultModel;
1408:                } finally {
1409:                    iter.close();
1410:                }
1411:            }
1412:
1413:            public String toString() {
1414:                return "<ModelCom  " + getGraph() + " | " + reifiedToString()
1415:                        + ">";
1416:            }
1417:
1418:            public String reifiedToString() {
1419:                return statementsToString(getHiddenStatements()
1420:                        .listStatements());
1421:            }
1422:
1423:            protected String statementsToString(StmtIterator it) {
1424:                StringBuffer b = new StringBuffer();
1425:                while (it.hasNext())
1426:                    b.append(" ").append(it.nextStatement());
1427:                return b.toString();
1428:            }
1429:
1430:            /**
1431:            	a read-only Model with all the statements of this Model and any
1432:            	statements "hidden" by reification. That model is dynamic, ie
1433:            	any changes this model will be reflected that one.
1434:             */
1435:            public Model getHiddenStatements() {
1436:                return modelReifier.getHiddenStatements();
1437:            }
1438:
1439:            /**
1440:                Answer whether or not these two graphs are isomorphic, taking the
1441:                hidden (reification) statements into account.
1442:             */
1443:            public boolean isIsomorphicWith(Model m) {
1444:                Graph L = ModelFactory.withHiddenStatements(this ).getGraph();
1445:                Graph R = ModelFactory.withHiddenStatements(m).getGraph();
1446:                return L.isIsomorphicWith(R);
1447:            }
1448:
1449:            public synchronized ModelLock getModelLock() {
1450:                if (modelLock == null)
1451:                    modelLock = new ModelLockImpl();
1452:                return modelLock;
1453:            }
1454:
1455:            public synchronized Lock getLock() {
1456:                return getModelLock();
1457:            }
1458:
1459:            public void enterCriticalSection(boolean requestReadLock) {
1460:                this .getModelLock().enterCriticalSection(requestReadLock);
1461:            }
1462:
1463:            public void leaveCriticalSection() {
1464:                this .getModelLock().leaveCriticalSection();
1465:            }
1466:
1467:            /**
1468:                Register the listener with this model by registering its GraphListener
1469:                adaption with the underlying Graph.
1470:               
1471:                @param a ModelChangedListener to register for model events
1472:                @return this model, for cascading 
1473:             */
1474:            public Model register(ModelChangedListener listener) {
1475:                getGraph().getEventManager().register(adapt(listener));
1476:                return this ;
1477:            }
1478:
1479:            /**
1480:                Unregister the listener from this model by unregistering its GraphListener
1481:                adaption from the underlying Graph.
1482:                @param  a ModelChangedListener to unregister from model events
1483:                @return this model, for cascading 
1484:             */
1485:            public Model unregister(ModelChangedListener listener) {
1486:                getGraph().getEventManager().unregister(adapt(listener));
1487:                return this ;
1488:            }
1489:
1490:            /**
1491:                Answer a GraphListener that, when fed graph-level update events,
1492:                fires the corresponding model-level event handlers in <code>L</code>.
1493:                @see ModelListenerAdapter
1494:                @param L a model listener to be wrapped as a graph listener
1495:                @return a graph listener wrapping L
1496:             */
1497:            public GraphListener adapt(final ModelChangedListener L) {
1498:                return new ModelListenerAdapter(this , L);
1499:            }
1500:
1501:            public Model notifyEvent(Object e) {
1502:                getGraph().getEventManager().notifyEvent(getGraph(), e);
1503:                return this ;
1504:            }
1505:        }
1506:
1507:        /*
1508:         *  (c) Copyright 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
1509:         *  All rights reserved.
1510:         *
1511:         * Redistribution and use in source and binary forms, with or without
1512:         * modification, are permitted provided that the following conditions
1513:         * are met:
1514:         * 1. Redistributions of source code must retain the above copyright
1515:         *    notice, this list of conditions and the following disclaimer.
1516:         * 2. Redistributions in binary form must reproduce the above copyright
1517:         *    notice, this list of conditions and the following disclaimer in the
1518:         *    documentation and/or other materials provided with the distribution.
1519:         * 3. The name of the author may not be used to endorse or promote products
1520:         *    derived from this software without specific prior written permission.
1521:         * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1522:         * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1523:         * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1524:         * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1525:         * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1526:         * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
1527:         * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
1528:         * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
1529:         * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
1530:         * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1531:         *
1532:         * Model.java
1533:         *
1534:         * Created on 11 March 2001, 16:07
1535:         */
w_w___w._j___a__v_a_2__s___._c__o__m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.