Skip to content

Method: getAssertionValues(Assertion)

1: /**
2: * Copyright (C) 2023 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: import cz.cvut.kbss.ontodriver.model.Value;
20:
21: import java.net.URI;
22: import java.util.*;
23:
24: public class AxiomValueDescriptor extends AbstractAxiomDescriptor {
25:
26: private URI subjectContext;
27:
28: private final Map<Assertion, AssertionData> assertionData;
29:
30: public AxiomValueDescriptor(NamedResource subject) {
31: super(subject);
32: this.assertionData = new HashMap<>();
33: }
34:
35: /**
36: * Sets subject context.
37: *
38: * @param context The context to use for subject
39: */
40: public void setSubjectContext(URI context) {
41: this.subjectContext = context;
42: }
43:
44: public URI getSubjectContext() {
45: return subjectContext;
46: }
47:
48: /**
49: * Adds the specified assertion to this descriptor.
50: *
51: * @param assertion The assertion to add
52: * @throws NullPointerException When {@code assertion} is {@code null}
53: */
54: public void addAssertion(Assertion assertion) {
55: Objects.requireNonNull(assertion);
56: assertionData.put(assertion, new AssertionData());
57: }
58:
59: /**
60: * Gets unmodifiable view of the properties in this descriptor.
61: *
62: * @return Set of assertions in this descriptor
63: */
64: public Set<Assertion> getAssertions() {
65: return Collections.unmodifiableSet(assertionData.keySet());
66: }
67:
68: /**
69: * Checks whether this descriptor contains the specified assertion.
70: *
71: * @param assertion The assertion to check
72: * @return True if the assertion is already present in this descriptor, false otherwise
73: */
74: public boolean containsAssertion(Assertion assertion) {
75: return assertionData.containsKey(assertion);
76: }
77:
78: @Override
79: public Set<URI> getSubjectContexts() {
80: return subjectContext != null ? Collections.singleton(getSubjectContext()) : Collections.emptySet();
81: }
82:
83: @Override
84: public Set<URI> getAssertionContexts(Assertion assertion) {
85: final URI ctx = getAssertionContext(assertion);
86: return ctx != null ? Collections.singleton(ctx) : Collections.emptySet();
87: }
88:
89: /**
90: * Gets context of the specified assertion.
91: * <p>
92: * If the context was not explicitly set, the same context as the subject's is returned.
93: *
94: * @param assertion Assertion for which context should be resolved
95: * @return Assertion context
96: */
97: public URI getAssertionContext(Assertion assertion) {
98: Objects.requireNonNull(assertion);
99: if (!assertionData.containsKey(assertion) || !assertionData.get(assertion).hasContext) {
100: return subjectContext;
101: }
102: return assertionData.get(assertion).context;
103: }
104:
105: /**
106: * Sets context for the specified assertion.
107: * <p>
108: * Note that the assertion has to be already present in this descriptor.
109: *
110: * @param assertion The property to set context for
111: * @param context Context URI
112: * @throws IllegalArgumentException If there is no such assertion in this descriptor
113: * @throws NullPointerException When {@code assertion} is {@code null}
114: */
115: public void setAssertionContext(Assertion assertion, URI context) {
116: Objects.requireNonNull(assertion);
117: if (!assertionData.containsKey(assertion)) {
118: throw new IllegalArgumentException("Assertion " + assertion + " is not present in this descriptor.");
119: }
120: assertionData.get(assertion).setContext(context);
121: }
122:
123: /**
124: * Adds a new value for the specified assertion.
125: *
126: * @param assertion The assertion
127: * @param value The value to add
128: * @return This descriptor
129: * @throws NullPointerException if either of the arguments is {@code null}
130: */
131: public AxiomValueDescriptor addAssertionValue(Assertion assertion, Value<?> value) {
132: Objects.requireNonNull(assertion);
133: Objects.requireNonNull(value);
134:
135: final List<Value<?>> assertionValues = getAssertionList(assertion);
136: assertionValues.add(value);
137: return this;
138: }
139:
140: /**
141: * Gets values of the specified assertion held by this descriptor.
142: *
143: * @param assertion The assertion
144: * @return Unmodifiable list of values or an empty list, if there are none
145: * @throws NullPointerException If the argument is {@code null}
146: */
147: public List<Value<?>> getAssertionValues(Assertion assertion) {
148: Objects.requireNonNull(assertion);
149:• if (!assertionData.containsKey(assertion)) {
150: return Collections.emptyList();
151: }
152: return Collections.unmodifiableList(assertionData.get(assertion).values);
153: }
154:
155: private List<Value<?>> getAssertionList(Assertion assertion) {
156: assert assertion != null;
157: if (!assertionData.containsKey(assertion)) {
158: addAssertion(assertion);
159: }
160: return assertionData.get(assertion).values;
161: }
162:
163: @Override
164: public boolean equals(Object o) {
165: if (this == o) {
166: return true;
167: }
168: if (!(o instanceof AxiomValueDescriptor)) {
169: return false;
170: }
171: if (!super.equals(o)) {
172: return false;
173: }
174: AxiomValueDescriptor that = (AxiomValueDescriptor) o;
175: return Objects.equals(subjectContext, that.subjectContext) &&
176: assertionData.equals(that.assertionData);
177: }
178:
179: @Override
180: public int hashCode() {
181: return Objects.hash(super.hashCode(), subjectContext, assertionData);
182: }
183:
184: @Override
185: public String toString() {
186: return "AxiomValueDescriptor{<" + getSubject() + "> - " + assertionData + '}';
187: }
188:
189: private static class AssertionData {
190: private final List<Value<?>> values = new ArrayList<>();
191: private URI context;
192: private boolean hasContext;
193:
194: private void setContext(URI context) {
195: this.context = context;
196: this.hasContext = true;
197: }
198: }
199: }