Source Code Cross Referenced for ResourceBRWRule.java in  » RSS-RDF » Jena-2.5.5 » com » hp » hpl » jena » reasoner » rdfsReasoner1 » 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.reasoner.rdfsReasoner1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /******************************************************************
002:         * File:        ResourceBRWRule.java
003:         * Created by:  Dave Reynolds
004:         * Created on:  28-Jan-03
005:         * 
006:         * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007:         * [See end of file]
008:         * $Id: ResourceBRWRule.java,v 1.13 2008/01/02 12:06:44 andy_seaborne Exp $
009:         *****************************************************************/package com.hp.hpl.jena.reasoner.rdfsReasoner1;
010:
011:        import com.hp.hpl.jena.reasoner.*;
012:        import com.hp.hpl.jena.graph.*;
013:        import com.hp.hpl.jena.vocabulary.*;
014:        import com.hp.hpl.jena.util.iterator.*;
015:
016:        import java.util.*;
017:
018:        /**
019:         * A special case of a backchaing rule to handle the nasty case
020:         * of "anything mentioned in any triple is a Resource".
021:         * 
022:         * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
023:         * @version $Revision: 1.13 $ on $Date: 2008/01/02 12:06:44 $
024:         */
025:        public class ResourceBRWRule extends BRWRule {
026:
027:            /** node form of rdf:type */
028:            private static Node TYPE = RDF.Nodes.type;
029:
030:            /** node form of rdfs:Resource */
031:            private static Node RESOURCE = RDFS.Nodes.Resource;
032:
033:            /**
034:             * Constructor
035:             */
036:            public ResourceBRWRule() {
037:                super (new TriplePattern(null, TYPE, RESOURCE),
038:                        new TriplePattern(null, null, null));
039:            }
040:
041:            /**
042:             * Use the rule to implement the given query. This will
043:             * instantiate the rule against the query, run the new query
044:             * against the whole reasoner+rawdata again and then rewrite the
045:             * results from that query according the rule.
046:             * @param query the query being processed
047:             * @param infGraph link to originating inference graph, may be re-invoked after a pattern rewrite
048:             * @param data the raw data graph which gets passed back to the reasoner as part of the recursive invocation
049:             * @param firedRules set of rules which have already been fired and should now be blocked
050:             * @return a ExtendedIterator which aggregates the matches and rewrites them
051:             * according to the rule
052:             */
053:            public ExtendedIterator execute(TriplePattern query,
054:                    InfGraph infGraph, Finder data, HashSet firedRules) {
055:                RDFSInfGraph bRr = (RDFSInfGraph) infGraph;
056:                if (query.getSubject().isVariable()) {
057:                    // Find all things of type resource
058:                    return new ResourceRewriteIterator(bRr
059:                            .findRawWithContinuation(body, data));
060:                } else {
061:                    // Just check for a specific resource
062:                    Node subj = query.getSubject();
063:                    TriplePattern pattern = new TriplePattern(subj, null, null);
064:                    String var = "s";
065:                    ExtendedIterator it = bRr.findRawWithContinuation(pattern,
066:                            data);
067:                    if (!it.hasNext()) {
068:                        pattern = new TriplePattern(null, null, subj);
069:                        var = "o";
070:                        it = bRr.findRawWithContinuation(pattern, data);
071:                        if (!it.hasNext()) {
072:                            pattern = new TriplePattern(null, subj, null);
073:                            var = "p";
074:                            it = bRr.findRawWithContinuation(pattern, data);
075:                        }
076:                    }
077:                    BRWRule rwrule = new BRWRule(new TriplePattern(Node
078:                            .createVariable(var), TYPE, RESOURCE), body);
079:                    return new RewriteIterator(it, rwrule);
080:                }
081:            }
082:
083:            /**
084:             * Return true if this rule is a a complete solution to the given
085:             * query and the router need look no further
086:             */
087:            public boolean completeFor(TriplePattern query) {
088:                return head.subsumes(query);
089:            }
090:
091:            /**
092:             * Inner class. This implements an iterator that uses the rule to rewrite any
093:             * results from the supplied iterator according to the rule.
094:             */
095:            static class ResourceRewriteIterator extends WrappedIterator {
096:                /** short stack of triples generated but not yet delivered */
097:                private Triple[] lookahead = new Triple[3];
098:
099:                /** the number of values available in lookahead */
100:                private short nAvailable = 0;
101:
102:                /** The set of objects already seen */
103:                protected HashSet seen = new HashSet();
104:
105:                /** 
106:                 * Constructor 
107:                 * @param underlying the iterator whose results are to be rewritten
108:                 * @param rule the BRWRule which defines the rewrite
109:                 */
110:                public ResourceRewriteIterator(Iterator underlying) {
111:                    super (underlying);
112:                }
113:
114:                /**
115:                 * Record a new instance of Resource so long as it has not
116:                 * been seen before
117:                 */
118:                private void push(Node resource) {
119:                    if (seen.add(resource)) {
120:                        lookahead[nAvailable++] = new Triple(resource, TYPE,
121:                                RESOURCE);
122:                    }
123:                }
124:
125:                /**
126:                 * @see Iterator#hasNext()
127:                 */
128:                public boolean hasNext() {
129:                    while (nAvailable == 0 && super .hasNext()) {
130:                        Triple value = (Triple) super .next();
131:                        if (seen.add(value)) {
132:                            push(value.getSubject());
133:                            push(value.getPredicate());
134:                            Node object = value.getObject();
135:                            if (!object.isLiteral()) {
136:                                push(object);
137:                            }
138:
139:                        }
140:                    }
141:                    return nAvailable > 0;
142:                }
143:
144:                /**
145:                 * @see Iterator#next()
146:                 */
147:                public Object next() {
148:                    if (nAvailable == 0 && !hasNext()) {
149:                        throw new NoSuchElementException("No element available");
150:                    }
151:                    return lookahead[--nAvailable];
152:                }
153:
154:            } // End of inner class - ResourceRewriteIterator
155:
156:        }
157:
158:        /*
159:         (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
160:         All rights reserved.
161:
162:         Redistribution and use in source and binary forms, with or without
163:         modification, are permitted provided that the following conditions
164:         are met:
165:
166:         1. Redistributions of source code must retain the above copyright
167:         notice, this list of conditions and the following disclaimer.
168:
169:         2. Redistributions in binary form must reproduce the above copyright
170:         notice, this list of conditions and the following disclaimer in the
171:         documentation and/or other materials provided with the distribution.
172:
173:         3. The name of the author may not be used to endorse or promote products
174:         derived from this software without specific prior written permission.
175:
176:         THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177:         IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178:         OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
179:         IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
180:         INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
181:         NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
182:         DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
183:         THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
184:         (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
185:         THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
186:         */
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.