Skip to content

Method: getAssertionList(Assertion)

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.ontodriver.descriptor;
19:
20: import cz.cvut.kbss.ontodriver.model.Assertion;
21: import cz.cvut.kbss.ontodriver.model.NamedResource;
22: import cz.cvut.kbss.ontodriver.model.Value;
23:
24: import java.net.URI;
25: import java.util.*;
26:
27: public class AxiomValueDescriptor extends AbstractAxiomDescriptor {
28:
29: private URI subjectContext;
30:
31: private final Map<Assertion, AssertionData> assertionData;
32:
33: public AxiomValueDescriptor(NamedResource subject) {
34: super(subject);
35: this.assertionData = new HashMap<>();
36: }
37:
38: /**
39: * Sets subject context.
40: *
41: * @param context The context to use for subject
42: */
43: public void setSubjectContext(URI context) {
44: this.subjectContext = context;
45: }
46:
47: public URI getSubjectContext() {
48: return subjectContext;
49: }
50:
51: /**
52: * Adds the specified assertion to this descriptor.
53: *
54: * @param assertion The assertion to add
55: * @throws NullPointerException When {@code assertion} is {@code null}
56: */
57: public void addAssertion(Assertion assertion) {
58: Objects.requireNonNull(assertion);
59: assertionData.put(assertion, new AssertionData());
60: }
61:
62: /**
63: * Gets unmodifiable view of the properties in this descriptor.
64: *
65: * @return Set of assertions in this descriptor
66: */
67: public Set<Assertion> getAssertions() {
68: return Collections.unmodifiableSet(assertionData.keySet());
69: }
70:
71: /**
72: * Checks whether this descriptor contains the specified assertion.
73: *
74: * @param assertion The assertion to check
75: * @return True if the assertion is already present in this descriptor, false otherwise
76: */
77: public boolean containsAssertion(Assertion assertion) {
78: return assertionData.containsKey(assertion);
79: }
80:
81: @Override
82: public Set<URI> getSubjectContexts() {
83: return subjectContext != null ? Collections.singleton(getSubjectContext()) : Collections.emptySet();
84: }
85:
86: @Override
87: public Set<URI> getAssertionContexts(Assertion assertion) {
88: final URI ctx = getAssertionContext(assertion);
89: return ctx != null ? Collections.singleton(ctx) : Collections.emptySet();
90: }
91:
92: /**
93: * Gets context of the specified assertion.
94: * <p>
95: * If the context was not explicitly set, the same context as the subject's is returned.
96: *
97: * @param assertion Assertion for which context should be resolved
98: * @return Assertion context
99: */
100: public URI getAssertionContext(Assertion assertion) {
101: Objects.requireNonNull(assertion);
102: if (!assertionData.containsKey(assertion) || !assertionData.get(assertion).hasContext) {
103: return subjectContext;
104: }
105: return assertionData.get(assertion).context;
106: }
107:
108: /**
109: * Sets context for the specified assertion.
110: * <p>
111: * Note that the assertion has to be already present in this descriptor.
112: *
113: * @param assertion The property to set context for
114: * @param context Context URI
115: * @throws IllegalArgumentException If there is no such assertion in this descriptor
116: * @throws NullPointerException When {@code assertion} is {@code null}
117: */
118: public void setAssertionContext(Assertion assertion, URI context) {
119: Objects.requireNonNull(assertion);
120: if (!assertionData.containsKey(assertion)) {
121: throw new IllegalArgumentException("Assertion " + assertion + " is not present in this descriptor.");
122: }
123: assertionData.get(assertion).setContext(context);
124: }
125:
126: /**
127: * Adds a new value for the specified assertion.
128: *
129: * @param assertion The assertion
130: * @param value The value to add
131: * @return This descriptor
132: * @throws NullPointerException if either of the arguments is {@code null}
133: */
134: public AxiomValueDescriptor addAssertionValue(Assertion assertion, Value<?> value) {
135: Objects.requireNonNull(assertion);
136: Objects.requireNonNull(value);
137:
138: final List<Value<?>> assertionValues = getAssertionList(assertion);
139: assertionValues.add(value);
140: return this;
141: }
142:
143: /**
144: * Gets values of the specified assertion held by this descriptor.
145: *
146: * @param assertion The assertion
147: * @return Unmodifiable list of values or an empty list, if there are none
148: * @throws NullPointerException If the argument is {@code null}
149: */
150: public List<Value<?>> getAssertionValues(Assertion assertion) {
151: Objects.requireNonNull(assertion);
152: if (!assertionData.containsKey(assertion)) {
153: return Collections.emptyList();
154: }
155: return Collections.unmodifiableList(assertionData.get(assertion).values);
156: }
157:
158: private List<Value<?>> getAssertionList(Assertion assertion) {
159:• assert assertion != null;
160:• if (!assertionData.containsKey(assertion)) {
161: addAssertion(assertion);
162: }
163: return assertionData.get(assertion).values;
164: }
165:
166: @Override
167: public boolean equals(Object o) {
168: if (this == o) {
169: return true;
170: }
171: if (!(o instanceof AxiomValueDescriptor)) {
172: return false;
173: }
174: if (!super.equals(o)) {
175: return false;
176: }
177: AxiomValueDescriptor that = (AxiomValueDescriptor) o;
178: return Objects.equals(subjectContext, that.subjectContext) &&
179: assertionData.equals(that.assertionData);
180: }
181:
182: @Override
183: public int hashCode() {
184: return Objects.hash(super.hashCode(), subjectContext, assertionData);
185: }
186:
187: @Override
188: public String toString() {
189: return "AxiomValueDescriptor{<" + getSubject() + "> - " + assertionData + '}';
190: }
191:
192: private static class AssertionData {
193: private final List<Value<?>> values = new ArrayList<>();
194: private URI context;
195: private boolean hasContext;
196:
197: private void setContext(URI context) {
198: this.context = context;
199: this.hasContext = true;
200: }
201:
202: @Override
203: public String toString() {
204: return "AssertionData{" +
205: "values=" + values +
206: '}';
207: }
208: }
209: }