Skip to content

Method: removeProperties(Map, URI)

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.jopa.oom;
19:
20: import cz.cvut.kbss.jopa.exceptions.StorageAccessException;
21: import cz.cvut.kbss.ontodriver.Connection;
22: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
23: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
24: import cz.cvut.kbss.ontodriver.descriptor.SimpleListValueDescriptor;
25: import cz.cvut.kbss.ontodriver.exception.OntoDriverException;
26: import cz.cvut.kbss.ontodriver.model.Assertion;
27: import cz.cvut.kbss.ontodriver.model.NamedResource;
28: import cz.cvut.kbss.ontodriver.model.Value;
29:
30: import java.net.URI;
31: import java.util.*;
32:
33: /**
34: * Gathers axiom values in descriptor for persist or update.
35: */
36: class AxiomValueGatherer {
37:
38: private final AxiomValueDescriptor axiomDescriptor;
39: private final Collection<SimpleListValueDescriptor> simpleListDescriptors = new ArrayList<>();
40: private final Collection<ReferencedListValueDescriptor> referencedListDescriptors = new ArrayList<>();
41: private final Set<URI> typesToAdd = new HashSet<>();
42: private final Set<URI> typesToRemove = new HashSet<>();
43: private URI typesContext;
44: private Map<Assertion, Set<Value<?>>> propertiesToAdd;
45: private Map<Assertion, Set<Value<?>>> propertiesToRemove;
46: private URI propertiesContext;
47:
48: AxiomValueGatherer(NamedResource subject, URI subjectContext) {
49: this.axiomDescriptor = new AxiomValueDescriptor(subject);
50: axiomDescriptor.setSubjectContext(subjectContext);
51: }
52:
53: NamedResource getSubjectIdentifier() {
54: return axiomDescriptor.getSubject();
55: }
56:
57: void addValue(Assertion assertion, Value<?> value, URI context) {
58: addValues(assertion, Collections.singleton(value), context);
59: }
60:
61: void addValues(Assertion assertion, Collection<Value<?>> values, URI context) {
62: axiomDescriptor.addAssertion(assertion);
63: for (Value<?> v : values) {
64: axiomDescriptor.addAssertionValue(assertion, v);
65: }
66: if (!Objects.equals(axiomDescriptor.getSubjectContext(), context)) {
67: axiomDescriptor.setAssertionContext(assertion, context);
68: }
69: }
70:
71: void addSimpleListValues(SimpleListValueDescriptor listDescriptor) {
72: simpleListDescriptors.add(listDescriptor);
73: }
74:
75: void addReferencedListValues(ReferencedListValueDescriptor listDescriptor) {
76: referencedListDescriptors.add(listDescriptor);
77: }
78:
79: void addTypes(Set<URI> types, URI context) {
80: appendTypes(typesToAdd, types, context);
81: }
82:
83: private void appendTypes(Set<URI> target, Set<URI> types, URI context) {
84: target.addAll(types);
85: this.typesContext = context;
86: }
87:
88: void removeTypes(Set<URI> types, URI context) {
89: appendTypes(typesToRemove, types, context);
90: }
91:
92: void addProperties(Map<Assertion, Set<Value<?>>> properties, URI context) {
93: if (propertiesToAdd == null) {
94: this.propertiesToAdd = new HashMap<>(properties.size());
95: }
96: appendProperties(propertiesToAdd, properties, context);
97: }
98:
99: private void appendProperties(Map<Assertion, Set<Value<?>>> target, Map<Assertion, Set<Value<?>>> properties,
100: URI context) {
101: for (Map.Entry<Assertion, Set<Value<?>>> e : properties.entrySet()) {
102: if (!target.containsKey(e.getKey())) {
103: target.put(e.getKey(), e.getValue());
104: } else {
105: target.get(e.getKey()).addAll(e.getValue());
106: }
107: }
108: this.propertiesContext = context;
109: }
110:
111: void removeProperties(Map<Assertion, Set<Value<?>>> properties, URI context) {
112:• if (propertiesToRemove == null) {
113: this.propertiesToRemove = new HashMap<>(properties.size());
114: }
115: appendProperties(propertiesToRemove, properties, context);
116: }
117:
118: void persist(Connection connection) {
119: try {
120: connection.persist(axiomDescriptor);
121: if (!typesToAdd.isEmpty()) {
122: connection.types().addTypes(axiomDescriptor.getSubject(), typesContext, typesToAdd);
123: }
124: if (propertiesToAdd != null) {
125: connection.properties().addProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToAdd);
126: }
127: for (SimpleListValueDescriptor d : simpleListDescriptors) {
128: connection.lists().persistSimpleList(d);
129: }
130: for (ReferencedListValueDescriptor d : referencedListDescriptors) {
131: connection.lists().persistReferencedList(d);
132: }
133: } catch (OntoDriverException e) {
134: throw new StorageAccessException(e);
135: }
136: }
137:
138: void update(Connection connection) {
139: try {
140: connection.update(axiomDescriptor);
141: if (!typesToAdd.isEmpty()) {
142: connection.types().addTypes(axiomDescriptor.getSubject(), typesContext, typesToAdd);
143: }
144: if (!typesToRemove.isEmpty()) {
145: connection.types().removeTypes(axiomDescriptor.getSubject(), typesContext, typesToRemove);
146: }
147: if (propertiesToAdd != null) {
148: connection.properties().addProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToAdd);
149: }
150: if (propertiesToRemove != null) {
151: connection.properties()
152: .removeProperties(axiomDescriptor.getSubject(), propertiesContext, propertiesToRemove);
153: }
154: for (SimpleListValueDescriptor d : simpleListDescriptors) {
155: connection.lists().updateSimpleList(d);
156: }
157: for (ReferencedListValueDescriptor d : referencedListDescriptors) {
158: connection.lists().updateReferencedList(d);
159: }
160: } catch (OntoDriverException e) {
161: throw new StorageAccessException(e);
162: }
163: }
164: }