Skip to content

Method: addAssertion(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:
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.
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: public class AxiomDescriptor extends AbstractAxiomDescriptor {
30:
31: private final Set<URI> subjectContexts = new HashSet<>(4);
32: private final Map<Assertion, Set<URI>> assertionContexts;
33:
34: public AxiomDescriptor(NamedResource subject) {
35: super(subject);
36: this.assertionContexts = new HashMap<>();
37: }
38:
39: /**
40: * Adds subject context.
41: * <p>
42: * The context can be {@code null}, indicating the default context.
43: *
44: * @param context The context to use for subject
45: * @return This instance
46: */
47: public AxiomDescriptor addSubjectContext(URI context) {
48: if (context == null) {
49: subjectContexts.clear();
50: } else {
51: subjectContexts.add(context);
52: }
53: return this;
54: }
55:
56: /**
57: * Adds the specified assertion to this descriptor.
58: *
59: * @param assertion The assertion to add
60: * @throws NullPointerException When {@code assertion} is {@code null}
61: */
62: public void addAssertion(Assertion assertion) {
63: Objects.requireNonNull(assertion);
64: assertionContexts.put(assertion, null);
65: }
66:
67: /**
68: * Sets context for the specified assertion.
69: * <p>
70: * Note that the assertion has to be already present in this descriptor.
71: *
72: * @param assertion The property to set context for
73: * @param context Context URI. Use {@code null} to indicate the default context
74: * @return This instance
75: * @throws IllegalArgumentException If there is no such assertion in this descriptor
76: * @throws NullPointerException When {@code assertion} is {@code null}
77: */
78: public AxiomDescriptor addAssertionContext(Assertion assertion, URI context) {
79: Objects.requireNonNull(assertion);
80: if (!assertionContexts.containsKey(assertion)) {
81: throw new IllegalArgumentException("Assertion " + assertion + " is not present in this descriptor.");
82: }
83: final Set<URI> contexts = assertionContexts.computeIfAbsent(assertion, a -> new HashSet<>());
84: if (context == null) {
85: contexts.clear();
86: } else {
87: contexts.add(context);
88: }
89: return this;
90: }
91:
92: /**
93: * Gets unmodifiable view of the properties in this descriptor.
94: *
95: * @return Set of assertions in this descriptor
96: */
97: public Set<Assertion> getAssertions() {
98: return Collections.unmodifiableSet(assertionContexts.keySet());
99: }
100:
101: /**
102: * Checks whether this descriptor contains the specified assertion.
103: *
104: * @param assertion The assertion to check
105: * @return True if the assertion is already present in this descriptor, false otherwise
106: */
107: public boolean containsAssertion(Assertion assertion) {
108: return assertionContexts.containsKey(assertion);
109: }
110:
111: @Override
112: public Set<URI> getSubjectContexts() {
113: return Collections.unmodifiableSet(subjectContexts);
114: }
115:
116: @Override
117: public Set<URI> getAssertionContexts(Assertion assertion) {
118: Objects.requireNonNull(assertion);
119: if (!assertionContexts.containsKey(assertion) || assertionContexts.get(assertion) == null) {
120: return getSubjectContexts();
121: }
122: return Collections.unmodifiableSet(assertionContexts.get(assertion));
123: }
124:
125: @Override
126: public boolean equals(Object o) {
127: if (this == o) {
128: return true;
129: }
130: if (!(o instanceof AxiomDescriptor)) {
131: return false;
132: }
133: if (!super.equals(o)) {
134: return false;
135: }
136: AxiomDescriptor that = (AxiomDescriptor) o;
137: return subjectContexts.equals(that.subjectContexts) && assertionContexts.equals(that.assertionContexts);
138: }
139:
140: @Override
141: public int hashCode() {
142: return Objects.hash(super.hashCode(), subjectContexts, assertionContexts);
143: }
144:
145: @Override
146: public String toString() {
147: final StringBuilder sb = new StringBuilder();
148: sb.append("[").append(getSubject());
149: sb.append(" - ").append(subjectContexts);
150: if (!assertionContexts.isEmpty()) {
151: sb.append(", properties: ").append(assertionContexts);
152: }
153: sb.append("]");
154: return sb.toString();
155: }
156: }