Skip to content

Method: addAssertion(Assertion)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.descriptor;
16:
17: import cz.cvut.kbss.ontodriver.model.Assertion;
18: import cz.cvut.kbss.ontodriver.model.NamedResource;
19:
20: import java.net.URI;
21: import java.util.*;
22:
23: /**
24: * This descriptor specifies subject and properties of axioms to search for in the ontology. </p>
25: * <p>
26: * Additionally, it can specify context URIs for both the descriptor and individual properties so that the underlying
27: * driver knows where to look for the corresponding axioms.
28: *
29: * @author ledvima1
30: */
31: public class AxiomDescriptor {
32:
33: private final NamedResource subject;
34: private final Set<Assertion> assertions;
35:
36: private URI subjectContext;
37: private final Map<Assertion, URI> assertionContexts;
38:
39: public AxiomDescriptor(NamedResource subject) {
40: this.subject = Objects.requireNonNull(subject);
41: this.assertions = new HashSet<>();
42: this.assertionContexts = new HashMap<>();
43: }
44:
45: /**
46: * Sets subject context.
47: *
48: * @param context The context to use for subject
49: */
50: public void setSubjectContext(URI context) {
51: this.subjectContext = context;
52: }
53:
54: /**
55: * Adds the specified assertion to this descriptor.
56: *
57: * @param assertion The assertion to add
58: * @throws NullPointerException
59: */
60: public void addAssertion(Assertion assertion) {
61: Objects.requireNonNull(assertion);
62: assertions.add(assertion);
63: }
64:
65: /**
66: * Sets context for the specified assertion. </p>
67: * <p>
68: * Note that the assertion has to be already present in this descriptor.
69: *
70: * @param assertion The property to set context for
71: * @param context Context URI
72: * @throws IllegalArgumentException If there is no such assertion in this descriptor
73: * @throws NullPointerException
74: */
75: public void setAssertionContext(Assertion assertion, URI context) {
76: Objects.requireNonNull(assertion);
77: if (!assertions.contains(assertion)) {
78: throw new IllegalArgumentException("Assertion " + assertion
79: + " is not present in this loading descriptor.");
80: }
81: assertionContexts.put(assertion, context);
82: }
83:
84: public NamedResource getSubject() {
85: return subject;
86: }
87:
88: public URI getSubjectContext() {
89: return subjectContext;
90: }
91:
92: /**
93: * Gets context of the specified assertion. </p>
94: * <p>
95: * If the context was not explicitly set, the same context as the subject's is returned.
96: *
97: * @return Assertion context
98: */
99: public URI getAssertionContext(Assertion assertion) {
100: Objects.requireNonNull(assertion);
101: if (!assertionContexts.containsKey(assertion)) {
102: return subjectContext;
103: }
104: return assertionContexts.get(assertion);
105: }
106:
107: /**
108: * Gets unmodifiable view of the properties in this descriptor.
109: */
110: public Set<Assertion> getAssertions() {
111: return Collections.unmodifiableSet(assertions);
112: }
113:
114: /**
115: * Checks whether this descriptor contains the specified assertion.
116: *
117: * @param assertion The assertion to check
118: * @return True if the assertion is already present in this descriptor, false otherwise
119: */
120: public boolean containsAssertion(Assertion assertion) {
121: return assertions.contains(assertion);
122: }
123:
124: @Override
125: public int hashCode() {
126: final int prime = 31;
127: int result = 1;
128: result = prime * result + subject.hashCode();
129: result = prime * result + ((subjectContext == null) ? 0 : subjectContext.hashCode());
130: result = prime * result + assertions.hashCode();
131: result = prime * result + assertionContexts.hashCode();
132: return result;
133: }
134:
135: @Override
136: public boolean equals(Object obj) {
137: if (this == obj)
138: return true;
139: if (obj == null)
140: return false;
141: if (getClass() != obj.getClass())
142: return false;
143: AxiomDescriptor other = (AxiomDescriptor) obj;
144: if (!subject.equals(other.subject))
145: return false;
146: if (subjectContext == null) {
147: if (other.subjectContext != null)
148: return false;
149: } else if (!subjectContext.equals(other.subjectContext))
150: return false;
151: if (!assertions.equals(other.assertions))
152: return false;
153: if (!assertionContexts.equals(other.assertionContexts))
154: return false;
155: return true;
156: }
157:
158: @Override
159: public String toString() {
160: final StringBuilder sb = new StringBuilder();
161: sb.append("[").append(subject);
162: if (subjectContext != null) {
163: sb.append(" - ").append(subjectContext);
164: }
165: if (!assertionContexts.isEmpty()) {
166: sb.append(", properties: ").append(assertionContexts);
167: } else {
168: sb.append(", properties: ").append(assertions);
169: }
170: sb.append("]");
171: return sb.toString();
172: }
173: }