001: /*
002: * LICENSE INFORMATION
003: * Copyright 2005-2007 by FZI (http://www.fzi.de).
004: * Licensed under a BSD license (http://www.opensource.org/licenses/bsd-license.php)
005: * <OWNER> = Max Völkel
006: * <ORGANIZATION> = FZI Forschungszentrum Informatik Karlsruhe, Karlsruhe, Germany
007: * <YEAR> = 2007
008: *
009: * Project information at http://semweb4j.org/rdf2go
010: */
011: package org.ontoware.rdf2go.model.impl;
012:
013: import org.ontoware.rdf2go.model.Statement;
014: import org.ontoware.rdf2go.model.TriplePattern;
015: import org.ontoware.rdf2go.model.node.Node;
016: import org.ontoware.rdf2go.model.node.NodeOrVariable;
017: import org.ontoware.rdf2go.model.node.Resource;
018: import org.ontoware.rdf2go.model.node.ResourceOrVariable;
019: import org.ontoware.rdf2go.model.node.URI;
020: import org.ontoware.rdf2go.model.node.UriOrVariable;
021: import org.ontoware.rdf2go.model.node.Variable;
022: import org.ontoware.rdf2go.model.node.impl.PlainLiteralImpl;
023:
024: /**
025: * A statement with variables
026: *
027: * @author voelkel
028: *
029: */
030: public class TriplePatternImpl implements TriplePattern {
031:
032: public enum SPO {
033: SUBJECT, PREDICATE, OBJECT
034: }
035:
036: private ResourceOrVariable subject;
037:
038: private UriOrVariable predicate;
039:
040: private NodeOrVariable object;
041:
042: public SPO extract;
043:
044: public TriplePatternImpl(ResourceOrVariable subject,
045: UriOrVariable predicate, NodeOrVariable object) {
046: this .subject = subject;
047: this .predicate = predicate;
048: this .object = object;
049: }
050:
051: public TriplePatternImpl(ResourceOrVariable subject,
052: UriOrVariable predicate, NodeOrVariable object, SPO extract) {
053: this .subject = subject;
054: this .predicate = predicate;
055: this .object = object;
056: this .extract = extract;
057: }
058:
059: /**
060: * Convenience constructor
061: *
062: * @param subject
063: * @param predicate
064: * @param object
065: */
066: public TriplePatternImpl(ResourceOrVariable subject, URI predicate,
067: String object) {
068: this .subject = subject;
069: this .predicate = predicate;
070: this .object = new PlainLiteralImpl(object);
071: }
072:
073: public NodeOrVariable getObject() {
074: return this .object;
075: }
076:
077: public UriOrVariable getPredicate() {
078: return this .predicate;
079: }
080:
081: public ResourceOrVariable getSubject() {
082: return this .subject;
083: }
084:
085: /**
086: * @param statement
087: * an RDF2Go statement
088: * @return the part of the statement which should be extracted according to
089: * this pattern
090: */
091: public Node getExtract(Statement statement) {
092: switch (this .extract) {
093: case SUBJECT:
094: return statement.getSubject();
095: case PREDICATE:
096: return statement.getPredicate();
097: case OBJECT:
098: return statement.getObject();
099: default: {
100: assert false;
101: throw new RuntimeException();
102: }
103: }
104: }
105:
106: @Override
107: public boolean equals(Object o) {
108: return ((o instanceof Statement)
109: && (this .getSubject().equals(((Statement) o)
110: .getSubject()))
111: && (this .getPredicate().equals(((Statement) o)
112: .getPredicate())) && (this .getObject()
113: .equals(((Statement) o).getObject())));
114: }
115:
116: @Override
117: public int hashCode() {
118: return this .object.hashCode() + this .predicate.hashCode()
119: + this .object.hashCode();
120: }
121:
122: public static TriplePatternImpl createObjectPattern(
123: Resource resource, URI propertyURI) {
124: return new TriplePatternImpl(resource, propertyURI,
125: Variable.ANY, SPO.OBJECT);
126: }
127:
128: public static TriplePatternImpl createSubjectPattern(
129: URI propertyURI, Node objectNode) {
130: return new TriplePatternImpl(Variable.ANY, propertyURI,
131: objectNode, SPO.SUBJECT);
132: }
133:
134: public boolean matches(Statement statement) {
135: boolean matchesSubject = statement.getSubject().equals(
136: this .getSubject())
137: || this .getSubject() instanceof Variable;
138:
139: boolean matchesPredicate = statement.getPredicate().equals(
140: this .getPredicate())
141: || this .getPredicate() instanceof Variable;
142:
143: boolean matchesObject = statement.getObject().equals(
144: this .getObject())
145: || this .getObject() instanceof Variable;
146:
147: return matchesSubject && matchesPredicate && matchesObject;
148: }
149:
150: }
|