Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/* This file is part of the OWL API.
* The contents of this file are subject to the LGPL License, Version 3.0.
* Copyright 2014, The University of Manchester
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.
*
* Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */
package uk.ac.manchester.cs.owl.owlapi;
import static org.semanticweb.owlapi.util.OWLAPIPreconditions.checkNotNull;
import static org.semanticweb.owlapi.util.OWLAPIPreconditions.verifyNotNull;
import static org.semanticweb.owlapi.util.OWLAPIStreamUtils.empty;
import gnu.trove.map.hash.THashMap;
import gnu.trove.set.hash.THashSet;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.HasIRI;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLAxiomVisitorEx;
import org.semanticweb.owlapi.model.OWLEntity;
import org.semanticweb.owlapi.util.CollectionFactory;
import org.semanticweb.owlapi.util.OWLAxiomSearchFilter;
import org.semanticweb.owlapi.util.SmallSet;
import uk.ac.manchester.cs.owl.owlapi.InitVisitorFactory.InitCollectionVisitor;
import uk.ac.manchester.cs.owl.owlapi.InitVisitorFactory.InitVisitor;
/**
* * Objects that identify contained maps - so that getting the keys of a
* specific map does not require a specific method for each map nor does it
* require the map to be copied and returned.
*
* @param key
* @param value
* @author ignazio
*/
public class MapPointer {
private static final AtomicLong totalInUse = new AtomicLong(0);
private static final AtomicLong totalAllocated = new AtomicLong(0);
private static final float DEFAULT_LOAD_FACTOR = 0.75F;
private static final int DEFAULT_INITIAL_CAPACITY = 5;
protected final Internals i;
@Nullable
private final AxiomType type;
@Nullable
private final OWLAxiomVisitorEx visitor;
private final THashMap> map = new THashMap<>(17, 0.75F);
private boolean initialized;
@Nullable
private SoftReference> iris;
private int size = 0;
private boolean neverTrimmed = true;
/**
* @param t type of axioms contained
* @param v visitor
* @param initialized true if initialized
* @param i internals containing this pointer
*/
public MapPointer(@Nullable AxiomType t, @Nullable OWLAxiomVisitorEx v,
boolean initialized, Internals i) {
type = t;
visitor = v;
this.initialized = initialized;
this.i = checkNotNull(i, "i cannot be null");
}
static synchronized void resetCounts() {
totalAllocated.set(0);
totalInUse.set(0);
}
static synchronized long getTotalInUse() {
return totalInUse.get();
}
static synchronized long getTotalAllocated() {
return totalAllocated.get();
}
/**
* This method replicates the Map.forEach on all the key/value pairs
*
* @param consumer a consumer with two arguments
*/
public void forEach(BiConsumer consumer) {
keySet().forEach(k -> forEach(k, v -> consumer.accept(k, v)));
}
/**
* @param e entity
* @return true if an entity with the same iri as the input exists in the collection
*/
public synchronized boolean containsReference(OWLEntity e) {
return map.containsKey(e);
}
/**
* @param e IRI
* @return true if an entity with the same iri as the input exists in the collection
*/
public synchronized boolean containsReference(IRI e) {
Set set = null;
if (iris != null) {
set = iris.get();
}
if (set == null) {
set = initSet();
}
return set.contains(e);
}
private Set initSet() {
Set set = CollectionFactory.createSet();
for (K k : map.keySet()) {
if (k instanceof HasIRI) {
set.add(((HasIRI) k).getIRI());
} else if (k instanceof IRI) {
set.add((IRI) k);
}
}
iris = new SoftReference<>(set);
return set;
}
/**
* @return true if initialized
*/
public synchronized boolean isInitialized() {
return initialized;
}
/**
* init the map pointer
*
* @return the map pointer
*/
@SuppressWarnings({"unchecked"})
public synchronized MapPointer init() {
if (initialized) {
return this;
}
initialized = true;
if (visitor == null || type == null) {
return this;
}
if (visitor instanceof InitVisitor) {
i.getAxiomsByType().forEach(verifyNotNull(type),
ax -> putInternal(ax.accept((InitVisitor) verifyNotNull(
visitor)), (V) ax));
} else if (visitor instanceof InitCollectionVisitor) {
i.getAxiomsByType().forEach(verifyNotNull(type),
ax -> ax.accept((InitCollectionVisitor) verifyNotNull(
visitor)).forEach(key -> putInternal(key, (V) ax)));
}
return this;
}
@Override
public synchronized String toString() {
return initialized + map.toString();
}
/**
* @return keyset
*/
public synchronized Collection keySet() {
init();
Set keySet = map.keySet();
assert keySet != null;
return keySet;
}
/**
* @param key key to look up
* @return value
*/
public synchronized Stream getValues(K key) {
init();
Collection t = map.get(key);
if (t == null) {
return Stream.empty();
}
if (t.size() < 3) {
return t.stream();
}
return new ArrayList<>(t).stream();
}
/**
* @param key key to look up
* @param function consumer to apply
*/
public synchronized void forEach(K key, Consumer function) {
init();
get(key).forEach(function);
}
/**
* @param key key to look up
* @param function predicate to evaluate
* @return value
*/
public synchronized boolean matchOnValues(K key, Predicate function) {
init();
return get(key).anyMatch(function);
}
/**
* @param key key to look up
* @return value
*/
public synchronized Collection getValuesAsCollection(K key) {
init();
return getCollection(key);
}
private Collection getCollection(K k) {
Collection t = map.get(k);
if (t == null) {
return Collections.emptySet();
}
return new ArrayList<>(t);
}
/**
* @param key key to look up
* @return value
*/
public synchronized int countValues(K key) {
init();
return count(key);
}
private int count(K k) {
Collection t = map.get(k);
if (t == null) {
return 0;
}
return t.size();
}
/**
* @param key key to look up
* @param classType type of the returned values
* @return value
*/
@SuppressWarnings("unchecked")
public synchronized Stream values(K key,
@SuppressWarnings("unused") Class classType) {
init();
Collection t = map.get(key);
if (t == null) {
return empty();
}
return ((Collection) t).stream();
}
/**
* @param type of key
* @param filter filter to satisfy
* @param key key
* @return set of values
*/
public synchronized Collection filterAxioms(OWLAxiomSearchFilter filter, T key) {
init();
List toReturn = new ArrayList<>();
for (AxiomType at : filter.getAxiomTypes()) {
Collection collection = map.get(at);
if (collection != null) {
collection.stream().filter(x -> filter.pass(x, key)).forEach(toReturn::add);
}
}
return toReturn;
}
/**
* @param key key to look up
* @return true if there are values for key
*/
public synchronized boolean hasValues(K key) {
init();
return map.containsKey(key);
}
/**
* @param key key to add
* @param value value to add
* @return true if addition happens
*/
public synchronized boolean put(K key, V value) {
// lazy init: no elements added until a recall is made
if (!initialized) {
return false;
}
iris = null;
return putInternal(key, value);
}
/**
* @param key key to look up
* @param value value to remove
* @return true if removal happens
*/
public synchronized boolean remove(K key, V value) {
if (!initialized) {
return false;
}
iris = null;
return removeInternal(key, value);
}
/**
* @param key key to look up
* @return true if there are values for key
*/
public synchronized boolean containsKey(K key) {
init();
return map.containsKey(key);
}
/**
* @param key key to look up
* @param value value to look up
* @return true if key and value are contained
*/
public synchronized boolean contains(K key, V value) {
init();
return containsEntry(key, value);
}
/**
* @return all values contained
*/
public synchronized Stream getAllValues() {
init();
return values();
}
/**
* @return number of mapping contained
*/
public synchronized int size() {
init();
if (neverTrimmed) {
trimToSize();
}
return size;
}
/**
* @return true if empty
*/
public synchronized boolean isEmpty() {
init();
return size == 0;
}
private boolean putInternal(@Nullable K k, V v) {
if (k == null) {
return false;
}
Collection set = map.get(k);
if (set == null) {
set = Collections.singleton(v);
map.put(k, set);
size++;
return true;
}
if (set.size() == 1) {
if (set.contains(v)) {
return false;
} else {
set = new SmallSet<>(set);
map.put(k, set);
}
} else if (set.size() == 3) {
if (set.contains(v)) {
return false;
} else {
set = makeSet(set, v);
map.put(k, set);
size++;
return true;
}
}
boolean added = set.add(v);
if (added) {
size++;
}
return added;
}
private boolean containsEntry(K k, V v) {
Collection t = map.get(k);
if (t == null) {
return false;
}
return t.contains(v);
}
private boolean removeInternal(K k, V v) {
if (neverTrimmed) {
trimToSize();
}
Collection t = map.get(k);
if (t == null) {
return false;
}
if (t.size() == 1) {
if (t.contains(v)) {
map.remove(k);
size--;
return true;
} else {
return false;
}
}
boolean removed = t.remove(v);
if (removed) {
size--;
}
if (t.isEmpty()) {
map.remove(k);
}
return removed;
}
private Collection makeSet(Collection collection, V extra) {
if (neverTrimmed) {
List list = new ArrayList<>(collection);
list.add(extra);
return list;
}
return new THashSetForSet<>(collection, extra, DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR);
}
private Stream values() {
return map.values().stream().flatMap(Collection::stream);
}
private Stream get(K k) {
Collection t = map.get(k);
if (t == null) {
return Stream.empty();
}
return t.stream();
}
/**
* Trims the capacity of the map entries . An application can use this
* operation to minimize the storage of the map pointer instance.
*/
public synchronized void trimToSize() {
if (initialized) {
map.trimToSize();
neverTrimmed = false;
for (Map.Entry> entry : map.entrySet()) {
Collection set = entry.getValue();
if (set instanceof ArrayList) {
THashSet value = new THashSetForSet<>(DEFAULT_INITIAL_CAPACITY,
DEFAULT_LOAD_FACTOR);
value.addAll(set);
entry.setValue(value);
size = size - set.size() + value.size();
value.trimToSize();
} else if (set instanceof THashSet) {
THashSet vs = (THashSet) set;
vs.trimToSize();
totalInUse.addAndGet(set.size());
totalAllocated.addAndGet(vs.capacity());
} else if (set instanceof SmallSet) {
totalInUse.addAndGet(set.size());
totalAllocated.addAndGet(3);
} else {
totalInUse.addAndGet(1);
totalAllocated.addAndGet(1);
}
}
}
}
private static class THashSetForSet extends THashSet {
private boolean constructing = true;
public THashSetForSet(Collection set, E toAdd, int capacity, float load) {
super(capacity, load);
for (E e : set) {
add(e);
}
add(toAdd);
constructing = false;
}
public THashSetForSet(int capacity, float load) {
super(capacity, load);
constructing = false;
}
@Override
protected boolean equals(@Nullable Object notnull, @Nullable Object two) {
// shortcut: during construction from a set, no element is
// duplicate. The extra element is also guaranteed to be unique,
// given the use made in this class.
if (constructing) {
return notnull == two;
}
return super.equals(notnull, two);
}
@Override
public Stream stream() {
return new ArrayList<>(this).stream();
}
}
}