From f14a616d6092abefeb948d01ed6feaa0e87d8c41 Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Sat, 17 Jun 2017 11:31:51 +0200 Subject: [PATCH 01/57] Add map that returns optional values --- .../lat/jcel/coreontology/common/OptMap.java | 195 ++++++++++++++++++ .../jcel/coreontology/common/OptMapImpl.java | 190 +++++++++++++++++ .../coreontology/common/package-info.java | 49 +++++ 3 files changed, 434 insertions(+) create mode 100644 jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMap.java create mode 100644 jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMapImpl.java create mode 100644 jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/package-info.java diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMap.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMap.java new file mode 100644 index 00000000..d1704490 --- /dev/null +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMap.java @@ -0,0 +1,195 @@ +/* + * + * Copyright (C) 2009-2017 Julian Mendez + * + * + * This file is part of jcel. + * + * + * The contents of this file are subject to the GNU Lesser General Public License + * version 3 + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * + * 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 de.tudresden.inf.lat.jcel.coreontology.common; + +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Optional; +import java.util.Set; + +/** + * An object implementing this interface wraps a map with some conditions: + * + * + * @author Julian Mendez + * @param + * type of keys in this map + * @param + * type of mapped values + */ +public interface OptMap { + + /** + * Returns the size of this map. + * + * @return the size of this map + */ + int size(); + + /** + * Returns true if and only if this map is empty. + * + * @return true if and only if this map is empty + */ + boolean isEmpty(); + + /** + * Returns true if and only if this map contains a mapping for + * the given key. This method replaces {@link Map#containsKey(Object)}. + * + * @param key + * key + * @return true if and only if this map contains a mapping for + * the given key + * @throws NullPointerException + * if a null value is given + */ + boolean containsKey(K key); + + /** + * Returns true if and only if this map associates one or more + * keys to the given value. This method replaces + * {@link Map#containsValue(Object)}. + * + * @param value + * value + * @return true if and only if this map associates one or more + * keys to the given value + * @throws NullPointerException + * if a null value is given + */ + boolean containsValue(V value); + + /** + * Returns an optional containing the value associated to the given key, is + * this association exists, or an empty optional otherwise. This method + * replaces {@link Map#get(Object)}. + * + * @param key + * key + * @return an optional containing the value associated to the given key, is + * this association exists, or an empty optional otherwise + */ + Optional get(K key); + + /** + * Associates the given value with the given key. This method replaces + * {@link Map#put put(K, V)}. + * + * @param key + * key + * @param value + * value + * @return an optional containing the previous associated value for the + * given key, or an empty optional if there was no mapping for that + * key + * @throws NullPointerException + * if a null value is given + */ + Optional put(K key, V value); + + /** + * Removes the mapping for the given key. This method replaces + * {@link Map#remove(Object)}. + * + * @param key + * key + * @return an optional containing the previous associated value for the + * given key, or an empty optional if there was no mapping for that + * key + * @throws NullPointerException + * if a null value is given + */ + public Optional remove(K key); + + /** + * Clears this map. + */ + void clear(); + + /** + * Adds all the associations given in the specified map. + * + * @param m + * map containing associations + */ + void putAll(Map m); + + /** + * Returns the set of keys. + * + * @return the set of keys + */ + Set keySet(); + + /** + * Returns the collection of values. + * + * @return the collection of values + */ + Collection values(); + + /** + * Returns a set of associations. + * + * @return a set of associations + */ + Set> entrySet(); + + /** + * Returns this as a {@link Map}. + * + * @return this as a Map + */ + Map asMap(); + +} diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMapImpl.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMapImpl.java new file mode 100644 index 00000000..7f34f9a0 --- /dev/null +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/OptMapImpl.java @@ -0,0 +1,190 @@ +/* + * + * Copyright (C) 2009-2017 Julian Mendez + * + * + * This file is part of jcel. + * + * + * The contents of this file are subject to the GNU Lesser General Public License + * version 3 + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * + * 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 de.tudresden.inf.lat.jcel.coreontology.common; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; + +/** + * This is the default implementation of {@link OptMap}. This implementation + * does not copy the map passed as an argument to create the instance, and it + * uses the map itself instead. This means that the internal representation can + * be modified externally, or it can be retrieved by using {@link #asMap()}. + * + * @author Julian Mendez + * + * @param + * type of keys in this map + * @param + * type of mapped values + */ +public class OptMapImpl implements OptMap { + + private final Map map; + + /** + * Constructs a new map. The default implementation structure is a + * {@link HashMap}. + */ + public OptMapImpl() { + this.map = new HashMap(); + } + + /** + * Constructs a new map using a specified {@link Map}. + * + * @param map + * map + */ + public OptMapImpl(Map map) { + Objects.requireNonNull(map); + this.map = map; + } + + /** + * Constructs a new map using another specified {@link OptMap}. + * + * @param map + * map + */ + public OptMapImpl(OptMap map) { + Objects.requireNonNull(map); + this.map = map.asMap(); + } + + @Override + public int size() { + return this.map.size(); + } + + @Override + public boolean isEmpty() { + return this.map.isEmpty(); + } + + @Override + public boolean containsKey(K key) { + Objects.requireNonNull(key); + return this.map.containsKey(key); + } + + @Override + public boolean containsValue(V value) { + Objects.requireNonNull(value); + return this.map.containsValue(value); + } + + @Override + public Optional get(K key) { + Objects.requireNonNull(key); + return Optional.ofNullable(this.map.get(key)); + } + + @Override + public Optional put(K key, V value) { + Objects.requireNonNull(key); + Objects.requireNonNull(value); + return Optional.ofNullable(this.map.put(key, value)); + } + + @Override + public Optional remove(K key) { + Objects.requireNonNull(key); + return Optional.ofNullable(this.map.remove(key)); + } + + @Override + public void putAll(Map m) { + Objects.requireNonNull(m); + this.map.putAll(m); + } + + @Override + public void clear() { + this.map.clear(); + } + + @Override + public Set keySet() { + return this.map.keySet(); + } + + @Override + public Collection values() { + return this.map.values(); + } + + @Override + public Set> entrySet() { + return this.map.entrySet(); + } + + @Override + public int hashCode() { + return this.map.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return this.map.equals(obj); + } + + @Override + public String toString() { + return this.map.toString(); + } + + @Override + public Map asMap() { + return this.map; + } + +} diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/package-info.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/package-info.java new file mode 100644 index 00000000..eb447cb9 --- /dev/null +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/common/package-info.java @@ -0,0 +1,49 @@ +/* + * + * Copyright (C) 2009-2017 Julian Mendez + * + * + * This file is part of jcel. + * + * + * The contents of this file are subject to the GNU Lesser General Public + * License version 3 + * + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser 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 Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + * + * 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. + * + */ + +/** + * This package contains interfaces and classes that can be used in any package. + */ +package de.tudresden.inf.lat.jcel.coreontology.common; From c24d65bcbaa73063ad725e1398cc403ca6506eb6 Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Sat, 17 Jun 2017 12:50:52 +0200 Subject: [PATCH 02/57] Use OptMap in CEL package --- .../algorithm/cel/CelExtendedOntology.java | 85 ++++++++++--------- .../jcel/core/algorithm/cel/CelProcessor.java | 42 +++++---- 2 files changed, 72 insertions(+), 55 deletions(-) diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelExtendedOntology.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelExtendedOntology.java index 24f7ab0d..25071638 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelExtendedOntology.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelExtendedOntology.java @@ -51,8 +51,8 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import de.tudresden.inf.lat.jcel.coreontology.axiom.FunctObjectPropAxiom; @@ -67,6 +67,8 @@ import de.tudresden.inf.lat.jcel.coreontology.axiom.RI2Axiom; import de.tudresden.inf.lat.jcel.coreontology.axiom.RI3Axiom; import de.tudresden.inf.lat.jcel.coreontology.axiom.RangeAxiom; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; /** * This class models an extended ontology. This is referred in the documentation @@ -76,10 +78,11 @@ */ public class CelExtendedOntology implements NormalizedIntegerAxiomVisitor { - private final Map> ohatOfClass = new HashMap<>(); - private final Map>> ohatOfExistential = new HashMap<>(); - private final Map> subPropertyAxiomSetByLeft = new HashMap<>(); - private final Map> subPropertyAxiomSetByRight = new HashMap<>(); + private final OptMap> ohatOfClass = new OptMapImpl<>(new HashMap<>()); + private final OptMap>> ohatOfExistential = new OptMapImpl<>( + new HashMap<>()); + private final OptMap> subPropertyAxiomSetByLeft = new OptMapImpl<>(new HashMap<>()); + private final OptMap> subPropertyAxiomSetByRight = new OptMapImpl<>(new HashMap<>()); /** * Constructs a new CEL extended ontology. @@ -87,24 +90,20 @@ public class CelExtendedOntology implements NormalizedIntegerAxiomVisitor()); } + this.ohatOfClass.get(classId).get().add(entry); } - private void addClassEntry(Integer classId, ExtensionEntry entry) { - addClass(classId); - this.ohatOfClass.get(classId).add(entry); - } - - private void addTo(Integer property, RI3Axiom axiom, Map> map) { - Set axiomSet = map.get(property); - if (Objects.isNull(axiomSet)) { - axiomSet = new HashSet<>(); - map.put(property, axiomSet); + private void addTo(Integer property, RI3Axiom axiom, OptMap> map) { + Optional> optAxiomSet = map.get(property); + if (!optAxiomSet.isPresent()) { + optAxiomSet = Optional.of(new HashSet<>()); + map.put(property, optAxiomSet.get()); } - axiomSet.add(axiom); + optAxiomSet.get().add(axiom); } /** @@ -126,11 +125,11 @@ public void clear() { */ public Set getClassEntries(Integer classId) { Objects.requireNonNull(classId); - Set ret = this.ohatOfClass.get(classId); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.ohatOfClass.get(classId); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } /** @@ -157,10 +156,12 @@ public Set getExistentialEntries(Integer propertyId, Integer cla Objects.requireNonNull(propertyId); Objects.requireNonNull(classId); Set ret = Collections.emptySet(); - Map> map = this.ohatOfExistential.get(propertyId); - if (Objects.nonNull(map)) { - ret = map.get(classId); - if (Objects.isNull(ret)) { + Optional>> optMap = this.ohatOfExistential.get(propertyId); + if (optMap.isPresent()) { + Optional> optSet = optMap.get().get(classId); + if (optSet.isPresent()) { + ret = optSet.get(); + } else { ret = Collections.emptySet(); } } @@ -179,11 +180,11 @@ public Set getExistentialEntries(Integer propertyId, Integer cla */ public Set getSubPropertyAxiomSetByLeft(Integer elem) { Objects.requireNonNull(elem); - Set ret = this.subPropertyAxiomSetByLeft.get(elem); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.subPropertyAxiomSetByLeft.get(elem); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } /** @@ -197,11 +198,11 @@ public Set getSubPropertyAxiomSetByLeft(Integer elem) { */ public Set getSubPropertyAxiomSetByRight(Integer elem) { Objects.requireNonNull(elem); - Set ret = this.subPropertyAxiomSetByRight.get(elem); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.subPropertyAxiomSetByRight.get(elem); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } /** @@ -278,13 +279,19 @@ public Boolean visit(GCI3Axiom axiom) { ExtensionEntry entry = new ImplicationEntry(new HashSet<>(), axiom.getSuperClass()); Integer propertyId = axiom.getPropertyInSubClass(); Integer classId = axiom.getClassInSubClass(); - Map> map = this.ohatOfExistential.get(propertyId); - if (Objects.isNull(map)) { - map = new HashMap<>(); + Optional>> optMap = this.ohatOfExistential.get(propertyId); + OptMap> map = null; + if (optMap.isPresent()) { + map = optMap.get(); + } else { + map = new OptMapImpl<>(new HashMap<>()); this.ohatOfExistential.put(propertyId, map); } - Set set = map.get(classId); - if (Objects.isNull(set)) { + Optional> optSet = map.get(classId); + Set set = null; + if (optSet.isPresent()) { + set = optSet.get(); + } else { set = new HashSet<>(); map.put(classId, set); } diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelProcessor.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelProcessor.java index 70811dc7..5a2cd80c 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelProcessor.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/cel/CelProcessor.java @@ -54,6 +54,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.logging.Logger; @@ -68,6 +69,8 @@ import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiom; import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiomFactory; import de.tudresden.inf.lat.jcel.coreontology.axiom.RI2Axiom; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityType; @@ -107,18 +110,18 @@ public class CelProcessor implements Processor { private IntegerSubsumerGraphImpl classGraph = null; private IntegerHierarchicalGraph classHierarchy = null; private IntegerHierarchicalGraph dataPropertyHierarchy = null; - private Map> directTypes = null; + private OptMap> directTypes = null; private final IntegerEntityManager entityManager; private CelExtendedOntology extendedOntology = null; private boolean isReady = false; private IntegerSubsumerGraphImpl objectPropertyGraph = null; private IntegerHierarchicalGraph objectPropertyHierarchy = null; - private Map> propertyUsedByClass = null; + private OptMap> propertyUsedByClass = null; private final Deque queueEntries = new ArrayDeque(); private final Deque queueKeys = new ArrayDeque(); private IntegerRelationMapImpl relationSet = null; - private Map> sameIndividualMap = null; - private Map> transitiveSubsumed = null; + private OptMap> sameIndividualMap = null; + private OptMap> transitiveSubsumed = null; /** * Constructs a new CEL processor. @@ -159,8 +162,8 @@ private void addToQueue(Integer className, Collection entrySet) * graph containing direct subsumers * @return a map with all the direct types for each individual. */ - private Map> computeDirectTypes(IntegerHierarchicalGraph hierarchicalGraph) { - Map> ret = new HashMap<>(); + private OptMap> computeDirectTypes(IntegerHierarchicalGraph hierarchicalGraph) { + OptMap> ret = new OptMapImpl<>(new HashMap<>()); Set individuals = getEntityManager().getEntities(IntegerEntityType.INDIVIDUAL, false); individuals.forEach(indiv -> { Set subsumers = hierarchicalGraph.getParents(getEntityManager().getAuxiliaryNominal(indiv).get()); @@ -174,8 +177,8 @@ private Map> computeDirectTypes(IntegerHierarchicalGraph h return ret; } - private Map> computeSameIndividualMap(IntegerHierarchicalGraph hierarchicalGraph) { - Map> ret = new HashMap<>(); + private OptMap> computeSameIndividualMap(IntegerHierarchicalGraph hierarchicalGraph) { + OptMap> ret = new OptMapImpl<>(new HashMap<>()); Set individuals = getEntityManager().getEntities(IntegerEntityType.INDIVIDUAL, false); individuals.forEach(indiv -> { Set equivalentClasses = hierarchicalGraph @@ -220,8 +223,8 @@ private IntegerSubsumerGraphImpl createObjectPropertyGraph(Set original return ret; } - private Map> createPropertyUseMap() { - Map> ret = new HashMap<>(); + private OptMap> createPropertyUseMap() { + OptMap> ret = new OptMapImpl<>(new HashMap<>()); getClassGraph().getElements().forEach(cA -> { Set propertySet = new HashSet<>(); getObjectPropertyGraph().getElements().forEach(r -> { @@ -240,8 +243,8 @@ private IntegerRelationMapImpl createRelationSet(Collection collection) return ret; } - private Map> createTransitiveSubsumed() { - Map> ret = new HashMap<>(); + private OptMap> createTransitiveSubsumed() { + OptMap> ret = new OptMapImpl<>(new HashMap<>()); getObjectPropertyGraph().getElements().forEach(r -> { Set related = new HashSet<>(); getObjectPropertyGraph().getElements().forEach(s -> { @@ -309,7 +312,7 @@ public Map> getDirectTypes() { if (!isReady()) { throw new UnclassifiedOntologyException(); } - return Collections.unmodifiableMap(this.directTypes); + return Collections.unmodifiableMap(this.directTypes.asMap()); } /** @@ -342,7 +345,12 @@ public NormalizedIntegerAxiomFactory getOntologyObjectFactory() { } private Set getPropertyUsedByClass(Integer cA) { - return this.propertyUsedByClass.get(cA); + Optional> optSet = this.propertyUsedByClass.get(cA); + if (optSet.isPresent()) { + return optSet.get(); + } else { + return Collections.emptySet(); + } } /** @@ -373,7 +381,7 @@ public Map> getSameIndividualMap() { if (!isReady()) { throw new UnclassifiedOntologyException(); } - return Collections.unmodifiableMap(this.sameIndividualMap); + return Collections.unmodifiableMap(this.sameIndividualMap.asMap()); } /** @@ -631,7 +639,9 @@ private void processImplication(Integer cA, ImplicationEntry eX) { } private void processNewEdge(Integer cA, Integer r, Integer cB) { - this.transitiveSubsumed.get(r).forEach(s -> { + Optional> optSet = this.transitiveSubsumed.get(r); + assert optSet.isPresent(); + optSet.get().forEach(s -> { this.relationSet.add(s, cA, cB); getPropertyUsedByClass(cB).add(s); From 198dbc0ed762094e849ad711fb958ed8bcdaaae6 Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Sat, 17 Jun 2017 20:18:02 +0200 Subject: [PATCH 03/57] Use OptMap in core ontology package --- .../axiom/ExtendedOntologyImpl.java | 176 +++++++++--------- .../datatype/IntegerEntityManagerImpl.java | 112 +++++------ 2 files changed, 146 insertions(+), 142 deletions(-) diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java index 9921a527..0da69cb4 100644 --- a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java @@ -49,10 +49,13 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; + /** * This class models an ontology with the property to look up by axiom type. * @@ -60,18 +63,18 @@ */ public class ExtendedOntologyImpl implements ExtendedOntology, NormalizedIntegerAxiomVisitor { - private final Map> mapOfGCI0 = new HashMap<>(); - private final Map> mapOfGCI1 = new HashMap<>(); - private final Map> mapOfGCI2 = new HashMap<>(); - private final Map> mapOfGCI3A = new HashMap<>(); - private final Map> mapOfGCI3r = new HashMap<>(); - private final Map>> mapOfGCI3rA = new HashMap<>(); - private final Map> mapOfNominalAxiom = new HashMap<>(); - private final Map> mapOfRangeAxiom = new HashMap<>(); - private final Map> mapOfRI2r = new HashMap<>(); - private final Map> mapOfRI2s = new HashMap<>(); - private final Map> mapOfRI3ByLeft = new HashMap<>(); - private final Map> mapOfRI3ByRight = new HashMap<>(); + private final OptMap> mapOfGCI0 = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfGCI1 = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfGCI2 = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfGCI3A = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfGCI3r = new OptMapImpl<>(new HashMap<>()); + private final OptMap>> mapOfGCI3rA = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfNominalAxiom = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfRangeAxiom = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfRI2r = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfRI2s = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfRI3ByLeft = new OptMapImpl<>(new HashMap<>()); + private final OptMap> mapOfRI3ByRight = new OptMapImpl<>(new HashMap<>()); private final Set setOfAllObjectProperties = new HashSet<>(); private final Set setOfClasses = new HashSet<>(); private final Set setOfFunctionalObjectProperties = new HashSet<>(); @@ -95,54 +98,53 @@ private void addEntities(NormalizedIntegerAxiom axiom) { } private void addGCI0Axiom(int classId, GCI0Axiom axiom) { - if (Objects.isNull(this.mapOfGCI0.get(classId))) { + if (!this.mapOfGCI0.get(classId).isPresent()) { this.mapOfGCI0.put(classId, new HashSet<>()); } - this.mapOfGCI0.get(classId).add(axiom); + this.mapOfGCI0.get(classId).get().add(axiom); } private void addGCI1Axiom(int classId, GCI1Axiom axiom) { - if (Objects.isNull(this.mapOfGCI1.get(classId))) { + if (!this.mapOfGCI1.get(classId).isPresent()) { this.mapOfGCI1.put(classId, new HashSet<>()); } - this.mapOfGCI1.get(classId).add(axiom); + this.mapOfGCI1.get(classId).get().add(axiom); } private void addGCI2Axiom(int classId, GCI2Axiom axiom) { - if (Objects.isNull(this.mapOfGCI2.get(classId))) { + if (!this.mapOfGCI2.get(classId).isPresent()) { this.mapOfGCI2.put(classId, new HashSet<>()); } - this.mapOfGCI2.get(classId).add(axiom); + this.mapOfGCI2.get(classId).get().add(axiom); } private void addGCI3Axiom(GCI3Axiom axiom, int objectPropertyId, int classId) { - - if (Objects.isNull(this.mapOfGCI3r.get(objectPropertyId))) { + if (!this.mapOfGCI3r.get(objectPropertyId).isPresent()) { this.mapOfGCI3r.put(objectPropertyId, new HashSet<>()); } - this.mapOfGCI3r.get(objectPropertyId).add(axiom); + this.mapOfGCI3r.get(objectPropertyId).get().add(axiom); if (Objects.isNull(this.mapOfGCI3A.get(classId))) { this.mapOfGCI3A.put(classId, new HashSet<>()); } - this.mapOfGCI3A.get(classId).add(axiom); + this.mapOfGCI3A.get(classId).get().add(axiom); - Map> map = this.mapOfGCI3rA.get(objectPropertyId); - if (Objects.isNull(map)) { - map = new HashMap<>(); - this.mapOfGCI3rA.put(objectPropertyId, map); + Optional>> optMap = this.mapOfGCI3rA.get(objectPropertyId); + if (!optMap.isPresent()) { + optMap = Optional.of(new OptMapImpl<>(new HashMap<>())); + this.mapOfGCI3rA.put(objectPropertyId, optMap.get()); } - if (Objects.isNull(map.get(classId))) { - map.put(classId, new HashSet<>()); + if (!optMap.get().get(classId).isPresent()) { + optMap.get().put(classId, new HashSet<>()); } - map.get(classId).add(axiom); + optMap.get().get(classId).get().add(axiom); } private void addNominalAxiom(int individualId, NominalAxiom axiom) { - if (Objects.isNull(this.mapOfNominalAxiom.get(individualId))) { + if (!this.mapOfNominalAxiom.get(individualId).isPresent()) { this.mapOfNominalAxiom.put(individualId, new HashSet<>()); } - this.mapOfNominalAxiom.get(individualId).add(axiom); + this.mapOfNominalAxiom.get(individualId).get().add(axiom); } @Override @@ -151,19 +153,19 @@ public void addObjectProperty(int objectProperty) { } private void addRangeAxiom(int propertyId, RangeAxiom axiom) { - if (Objects.isNull(this.mapOfRangeAxiom.get(propertyId))) { + if (!this.mapOfRangeAxiom.get(propertyId).isPresent()) { this.mapOfRangeAxiom.put(propertyId, new HashSet<>()); } - this.mapOfRangeAxiom.get(propertyId).add(axiom); + this.mapOfRangeAxiom.get(propertyId).get().add(axiom); } - private void addTo(int property, RI3Axiom axiom, Map> map) { - Set axiomSet = map.get(property); - if (Objects.isNull(axiomSet)) { - axiomSet = new HashSet<>(); - map.put(property, axiomSet); + private void addTo(int property, RI3Axiom axiom, OptMap> map) { + Optional> optAxiomSet = map.get(property); + if (!optAxiomSet.isPresent()) { + optAxiomSet = Optional.of(new HashSet<>()); + map.put(property, optAxiomSet.get()); } - axiomSet.add(axiom); + optAxiomSet.get().add(axiom); } @Override @@ -199,60 +201,60 @@ public Set getFunctionalObjectProperties() { @Override public Set getGCI0Axioms(int classId) { - Set ret = this.mapOfGCI0.get(classId); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfGCI0.get(classId); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getGCI1Axioms(int classId) { - Set ret = this.mapOfGCI1.get(classId); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfGCI1.get(classId); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getGCI2Axioms(int classId) { - Set ret = this.mapOfGCI2.get(classId); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfGCI2.get(classId); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getGCI3AAxioms(int classId) { - Set ret = this.mapOfGCI3A.get(classId); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfGCI3A.get(classId); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getGCI3rAAxioms(int objectPropertyId, int leftClassId) { - Set ret = null; - Map> map = this.mapOfGCI3rA.get(objectPropertyId); - if (Objects.nonNull(map)) { - ret = map.get(leftClassId); + Optional> optSet = null; + Optional>> optMap = this.mapOfGCI3rA.get(objectPropertyId); + if (optMap.isPresent()) { + optSet = optMap.get().get(leftClassId); } - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getGCI3rAxioms(int objectPropertyId) { - Set ret = this.mapOfGCI3r.get(objectPropertyId); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfGCI3r.get(objectPropertyId); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override @@ -267,38 +269,38 @@ public Set getReflexiveObjectProperties() { @Override public Set getRI2rAxioms(int elem) { - Set ret = this.mapOfRI2r.get(elem); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfRI2r.get(elem); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getRI2sAxioms(int elem) { - Set ret = this.mapOfRI2s.get(elem); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfRI2s.get(elem); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getRI3AxiomsByLeft(int elem) { - Set ret = this.mapOfRI3ByLeft.get(elem); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfRI3ByLeft.get(elem); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override public Set getRI3AxiomsByRight(int elem) { - Set ret = this.mapOfRI3ByRight.get(elem); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.mapOfRI3ByRight.get(elem); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } - return Collections.unmodifiableSet(ret); + return Collections.unmodifiableSet(optSet.get()); } @Override @@ -392,16 +394,16 @@ public Boolean visit(RI1Axiom axiom) { public Boolean visit(RI2Axiom axiom) { Objects.requireNonNull(axiom); Integer subProperty = axiom.getSubProperty(); - if (Objects.isNull(this.mapOfRI2r.get(subProperty))) { + if (!this.mapOfRI2r.get(subProperty).isPresent()) { this.mapOfRI2r.put(subProperty, new HashSet<>()); } - this.mapOfRI2r.get(subProperty).add(axiom); + this.mapOfRI2r.get(subProperty).get().add(axiom); Integer superProperty = axiom.getSuperProperty(); - if (Objects.isNull(this.mapOfRI2s.get(superProperty))) { + if (!this.mapOfRI2s.get(superProperty).isPresent()) { this.mapOfRI2s.put(superProperty, new HashSet<>()); } - this.mapOfRI2s.get(superProperty).add(axiom); + this.mapOfRI2s.get(superProperty).get().add(axiom); return true; } diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java index 770067b1..2b45f8bd 100644 --- a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java @@ -49,12 +49,14 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.TreeSet; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; + /** * An object of this class generates new identification numbers for object * properties and classes. @@ -66,16 +68,16 @@ public class IntegerEntityManagerImpl implements IntegerEntityManager { public static final String anonymousEntity = "AnonymousEntity"; public static final String auxiliaryEntity = "AuxiliaryEntity"; - private final Map> auxEntityMap = new HashMap<>(); + private final OptMap> auxEntityMap = new OptMapImpl<>(new HashMap<>()); private final Set auxEntitySet = new HashSet<>(); private final Set auxInverseObjectPropertySet = new HashSet<>(); - private final Map auxNominalInvMap = new HashMap<>(); - private final Map auxNominalMap = new HashMap<>(); + private final OptMap auxNominalInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap auxNominalMap = new OptMapImpl<>(new HashMap<>()); private int entityCounter = firstUsableIdentifier; - private final Map entityTypeMap = new HashMap<>(); - private final Map inverseObjectPropertyMap = new HashMap<>(); - private final Map nameMap = new HashMap<>(); - private final Map> nonAuxEntityMap = new HashMap<>(); + private final OptMap entityTypeMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap inverseObjectPropertyMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap nameMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap> nonAuxEntityMap = new OptMapImpl<>(new HashMap<>()); /** * Constructs a new identifier generator. @@ -111,26 +113,26 @@ public Integer createNamedEntity(IntegerEntityType type, String name, boolean au @Override public Integer createOrGetClassIdForIndividual(Integer individual) { Objects.requireNonNull(individual); - Integer ret = this.auxNominalMap.get(individual); - if (Objects.isNull(ret)) { - ret = createAnonymousEntity(IntegerEntityType.CLASS, true); - this.auxNominalInvMap.put(ret, individual); - this.auxNominalMap.put(individual, ret); + Optional optId = this.auxNominalMap.get(individual); + if (!optId.isPresent()) { + optId = Optional.of(createAnonymousEntity(IntegerEntityType.CLASS, true)); + this.auxNominalInvMap.put(optId.get(), individual); + this.auxNominalMap.put(individual, optId.get()); } - return ret; + return optId.get(); } @Override public Integer createOrGetInverseObjectPropertyOf(Integer propertyId) throws IndexOutOfBoundsException { Objects.requireNonNull(propertyId); - Integer ret = this.inverseObjectPropertyMap.get(propertyId); - if (Objects.isNull(ret)) { - ret = createAnonymousEntity(IntegerEntityType.OBJECT_PROPERTY, true); - this.auxInverseObjectPropertySet.add(ret); - this.inverseObjectPropertyMap.put(propertyId, ret); - this.inverseObjectPropertyMap.put(ret, propertyId); + Optional optId = this.inverseObjectPropertyMap.get(propertyId); + if (!optId.isPresent()) { + optId = Optional.of(createAnonymousEntity(IntegerEntityType.OBJECT_PROPERTY, true)); + this.auxInverseObjectPropertySet.add(optId.get()); + this.inverseObjectPropertyMap.put(propertyId, optId.get()); + this.inverseObjectPropertyMap.put(optId.get(), propertyId); } - return ret; + return optId.get(); } @Override @@ -158,7 +160,7 @@ public Set getAuxiliaryInverseObjectProperties() { @Override public Optional getAuxiliaryNominal(Integer individual) { Objects.requireNonNull(individual); - return Optional.ofNullable(this.auxNominalMap.get(individual)); + return this.auxNominalMap.get(individual); } @Override @@ -170,11 +172,11 @@ public Set getAuxiliaryNominals() { public Set getEntities(IntegerEntityType type) { Objects.requireNonNull(type); Set ret = new TreeSet<>(); - if (Objects.nonNull(this.nonAuxEntityMap.get(type))) { - ret.addAll(this.nonAuxEntityMap.get(type)); + if (this.nonAuxEntityMap.get(type).isPresent()) { + ret.addAll(this.nonAuxEntityMap.get(type).get()); } - if (Objects.nonNull(this.auxEntityMap.get(type))) { - ret.addAll(this.auxEntityMap.get(type)); + if (this.auxEntityMap.get(type).isPresent()) { + ret.addAll(this.auxEntityMap.get(type).get()); } ret = Collections.unmodifiableSet(ret); return ret; @@ -183,24 +185,24 @@ public Set getEntities(IntegerEntityType type) { @Override public Set getEntities(IntegerEntityType type, boolean auxiliary) { Objects.requireNonNull(type); - Set ret; + Optional> optSet; if (auxiliary) { - ret = this.auxEntityMap.get(type); + optSet = this.auxEntityMap.get(type); } else { - ret = this.nonAuxEntityMap.get(type); + optSet = this.nonAuxEntityMap.get(type); } - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } else { - ret = Collections.unmodifiableSet(ret); + optSet = Optional.of(Collections.unmodifiableSet(optSet.get())); } - return ret; + return optSet.get(); } @Override public Optional getIndividual(Integer auxNominal) { Objects.requireNonNull(auxNominal); - return Optional.ofNullable(this.auxNominalInvMap.get(auxNominal)); + return this.auxNominalInvMap.get(auxNominal); } @Override @@ -215,25 +217,25 @@ public String getName(Integer identifier) { throw new IndexOutOfBoundsException("Invalid identifier : " + identifier); } - String ret = this.nameMap.get(identifier); - if (Objects.isNull(ret)) { + Optional optName = this.nameMap.get(identifier); + if (Objects.isNull(optName)) { if (this.auxEntitySet.contains(identifier)) { - ret = auxiliaryEntity + identifier; + optName = Optional.of(auxiliaryEntity + identifier); } else { - ret = anonymousEntity + identifier; + optName = Optional.of(anonymousEntity + identifier); } } - return ret; + return optName.get(); } @Override public IntegerEntityType getType(Integer identifier) { Objects.requireNonNull(identifier); - IntegerEntityType ret = this.entityTypeMap.get(identifier); - if (Objects.isNull(ret)) { + Optional optType = this.entityTypeMap.get(identifier); + if (!optType.isPresent()) { throw new IndexOutOfBoundsException("Invalid identifier : " + identifier); } - return ret; + return optType.get(); } @Override @@ -261,9 +263,9 @@ public boolean proposeInverseObjectPropertyOf(Integer firstProperty, Integer sec Objects.requireNonNull(firstProperty); Objects.requireNonNull(secondProperty); boolean ret = false; - Integer invFirstProperty = this.inverseObjectPropertyMap.get(firstProperty); - Integer invSecondProperty = this.inverseObjectPropertyMap.get(secondProperty); - if ((Objects.isNull(invFirstProperty)) && (Objects.isNull(invSecondProperty))) { + Optional optInvFirstProperty = this.inverseObjectPropertyMap.get(firstProperty); + Optional optInvSecondProperty = this.inverseObjectPropertyMap.get(secondProperty); + if (!optInvFirstProperty.isPresent() && !optInvSecondProperty.isPresent()) { this.inverseObjectPropertyMap.put(firstProperty, secondProperty); this.inverseObjectPropertyMap.put(secondProperty, firstProperty); ret = true; @@ -274,19 +276,19 @@ public boolean proposeInverseObjectPropertyOf(Integer firstProperty, Integer sec private void registerProperty(Integer identifier, IntegerEntityType type, boolean auxiliary) { if (auxiliary) { this.auxEntitySet.add(identifier); - Set set = this.auxEntityMap.get(type); - if (Objects.isNull(set)) { - set = new HashSet<>(); - this.auxEntityMap.put(type, set); + Optional> optSet = this.auxEntityMap.get(type); + if (!optSet.isPresent()) { + optSet = Optional.of(new HashSet<>()); + this.auxEntityMap.put(type, optSet.get()); } - set.add(identifier); + optSet.get().add(identifier); } else { - Set set = this.nonAuxEntityMap.get(type); - if (Objects.isNull(set)) { - set = new HashSet<>(); - this.nonAuxEntityMap.put(type, set); + Optional> optSet = this.nonAuxEntityMap.get(type); + if (!optSet.isPresent()) { + optSet = Optional.of(new HashSet<>()); + this.nonAuxEntityMap.put(type, optSet.get()); } - set.add(identifier); + optSet.get().add(identifier); } this.entityTypeMap.put(identifier, type); } From c8ecdea3b42b8183dc397d5fa92f8011dc74907a Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Sat, 17 Jun 2017 21:24:00 +0200 Subject: [PATCH 04/57] Fix comparison to verify optional value in map --- .../inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java | 2 +- .../jcel/coreontology/datatype/IntegerEntityManagerImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java index 0da69cb4..d2cbf964 100644 --- a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java @@ -124,7 +124,7 @@ private void addGCI3Axiom(GCI3Axiom axiom, int objectPropertyId, int classId) { } this.mapOfGCI3r.get(objectPropertyId).get().add(axiom); - if (Objects.isNull(this.mapOfGCI3A.get(classId))) { + if (!this.mapOfGCI3A.get(classId).isPresent()) { this.mapOfGCI3A.put(classId, new HashSet<>()); } this.mapOfGCI3A.get(classId).get().add(axiom); diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java index 2b45f8bd..419d445f 100644 --- a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/datatype/IntegerEntityManagerImpl.java @@ -218,7 +218,7 @@ public String getName(Integer identifier) { } Optional optName = this.nameMap.get(identifier); - if (Objects.isNull(optName)) { + if (!optName.isPresent()) { if (this.auxEntitySet.contains(identifier)) { optName = Optional.of(auxiliaryEntity + identifier); } else { From 32e767998ed086877f593f737bafbdc5b1428690 Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Sat, 17 Jun 2017 21:30:58 +0200 Subject: [PATCH 05/57] Fix default value of optional --- .../inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java index d2cbf964..7885de1c 100644 --- a/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java +++ b/jcel-coreontology/src/main/java/de/tudresden/inf/lat/jcel/coreontology/axiom/ExtendedOntologyImpl.java @@ -237,7 +237,7 @@ public Set getGCI3AAxioms(int classId) { @Override public Set getGCI3rAAxioms(int objectPropertyId, int leftClassId) { - Optional> optSet = null; + Optional> optSet = Optional.empty(); Optional>> optMap = this.mapOfGCI3rA.get(objectPropertyId); if (optMap.isPresent()) { optSet = optMap.get().get(leftClassId); From 97c01dabe5db6d314db86cf7c20ce7854dee2f7f Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Mon, 19 Jun 2017 22:44:05 +0200 Subject: [PATCH 06/57] Use OptMap in owlapi package --- .../translator/TranslationRepository.java | 144 +++++++++--------- 1 file changed, 73 insertions(+), 71 deletions(-) diff --git a/jcel-owlapi/src/main/java/de/tudresden/inf/lat/jcel/owlapi/translator/TranslationRepository.java b/jcel-owlapi/src/main/java/de/tudresden/inf/lat/jcel/owlapi/translator/TranslationRepository.java index bec89560..5b100dd2 100644 --- a/jcel-owlapi/src/main/java/de/tudresden/inf/lat/jcel/owlapi/translator/TranslationRepository.java +++ b/jcel-owlapi/src/main/java/de/tudresden/inf/lat/jcel/owlapi/translator/TranslationRepository.java @@ -47,9 +47,11 @@ package de.tudresden.inf.lat.jcel.owlapi.translator; import java.util.HashMap; -import java.util.Map; -import java.util.Objects; import java.util.Optional; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; + +import java.util.Objects; import org.semanticweb.owlapi.model.OWLAnnotationProperty; import org.semanticweb.owlapi.model.OWLAnnotationValue; @@ -77,21 +79,21 @@ public class TranslationRepository { private final OWLClass bottomClass; private final OWLDataProperty bottomDataProperty; private final OWLObjectProperty bottomObjectProperty; - private final Map classInvMap = new HashMap<>(); - private final Map classMap = new HashMap<>(); - private final Map dataPropertyInvMap = new HashMap<>(); - private final Map dataPropertyMap = new HashMap<>(); + private final OptMap classInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap classMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap dataPropertyInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap dataPropertyMap = new OptMapImpl<>(new HashMap<>()); private final IntegerEntityManager entityManager; - private final Map individualInvMap = new HashMap<>(); - private final Map individualMap = new HashMap<>(); - private final Map literalInvMap = new HashMap<>(); - private final Map literalMap = new HashMap<>(); - private final Map objectPropertyInvMap = new HashMap<>(); - private final Map objectPropertyMap = new HashMap<>(); - private final Map annotationPropertyInvMap = new HashMap<>(); - private final Map annotationPropertyMap = new HashMap<>(); - private final Map annotationValueInvMap = new HashMap<>(); - private final Map annotationValueMap = new HashMap<>(); + private final OptMap individualInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap individualMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap literalInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap literalMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap objectPropertyInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap objectPropertyMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap annotationPropertyInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap annotationPropertyMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap annotationValueInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap annotationValueMap = new OptMapImpl<>(new HashMap<>()); private final OWLClass topClass; private final OWLDataProperty topDataProperty; private final OWLObjectProperty topObjectProperty; @@ -286,62 +288,62 @@ public boolean addAnnotationValue(OWLAnnotationValue annValue) { public Integer getId(OWLClass owlClass) { Objects.requireNonNull(owlClass); - Integer ret = this.classInvMap.get(owlClass); - if (Objects.isNull(ret)) { + Optional ret = this.classInvMap.get(owlClass); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(owlClass.toStringID()); } - return ret; + return ret.get(); } public Integer getId(OWLDataProperty owlDataProperty) { Objects.requireNonNull(owlDataProperty); - Integer ret = this.dataPropertyInvMap.get(owlDataProperty); - if (Objects.isNull(ret)) { + Optional ret = this.dataPropertyInvMap.get(owlDataProperty); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(owlDataProperty.toStringID()); } - return ret; + return ret.get(); } public Integer getId(OWLIndividual individual) { Objects.requireNonNull(individual); - Integer ret = this.individualInvMap.get(individual); - if (Objects.isNull(ret)) { + Optional ret = this.individualInvMap.get(individual.asOWLNamedIndividual()); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(individual.toStringID()); } - return ret; + return ret.get(); } public Integer getId(OWLLiteral owlLiteral) { Objects.requireNonNull(owlLiteral); - Integer ret = this.literalInvMap.get(owlLiteral); - if (Objects.isNull(ret)) { + Optional ret = this.literalInvMap.get(owlLiteral); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(owlLiteral.getLiteral()); } - return ret; + return ret.get(); } public Integer getId(OWLObjectProperty owlObjectProperty) { Objects.requireNonNull(owlObjectProperty); - Integer ret = this.objectPropertyInvMap.get(owlObjectProperty); - if (Objects.isNull(ret)) { + Optional ret = this.objectPropertyInvMap.get(owlObjectProperty); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(owlObjectProperty.toStringID()); } - return ret; + return ret.get(); } public Integer getId(OWLAnnotationProperty owlAnnotationProperty) { Objects.requireNonNull(owlAnnotationProperty); - Integer ret = this.annotationPropertyInvMap.get(owlAnnotationProperty); - if (Objects.isNull(ret)) { + Optional ret = this.annotationPropertyInvMap.get(owlAnnotationProperty); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(owlAnnotationProperty.toStringID()); } - return ret; + return ret.get(); } public Integer getId(OWLAnnotationValue owlAnnotationValue) { Objects.requireNonNull(owlAnnotationValue); - Integer ret = this.annotationValueInvMap.get(owlAnnotationValue); - if (Objects.isNull(ret)) { + Optional ret = this.annotationValueInvMap.get(owlAnnotationValue); + if (!ret.isPresent()) { String msg = null; if (owlAnnotationValue.asLiteral().isPresent()) { msg = owlAnnotationValue.asLiteral().get().getLiteral(); @@ -350,135 +352,135 @@ public Integer getId(OWLAnnotationValue owlAnnotationValue) { } throw TranslationException.newIncompleteMapException(msg); } - return ret; + return ret.get(); } public OWLClass getOWLClass(Integer index) { Objects.requireNonNull(index); - OWLClass ret = this.classMap.get(index); - if (Objects.isNull(ret)) { + Optional ret = this.classMap.get(index); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(index.toString()); } - return ret; + return ret.get(); } public OWLDataProperty getOWLDataProperty(Integer index) { Objects.requireNonNull(index); - OWLDataProperty ret = this.dataPropertyMap.get(index); - if (Objects.isNull(ret)) { + Optional ret = this.dataPropertyMap.get(index); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(index.toString()); } - return ret; + return ret.get(); } public OWLNamedIndividual getOWLNamedIndividual(Integer index) { Objects.requireNonNull(index); - OWLNamedIndividual ret = this.individualMap.get(index); - if (Objects.isNull(ret)) { + Optional ret = this.individualMap.get(index); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(index.toString()); } - return ret; + return ret.get(); } public OWLObjectProperty getOWLObjectProperty(Integer index) { Objects.requireNonNull(index); - OWLObjectProperty ret = this.objectPropertyMap.get(index); - if (Objects.isNull(ret)) { + Optional ret = this.objectPropertyMap.get(index); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(index.toString()); } - return ret; + return ret.get(); } public OWLAnnotationProperty getOWLAnnotationProperty(Integer index) { Objects.requireNonNull(index); - OWLAnnotationProperty ret = this.annotationPropertyMap.get(index); - if (Objects.isNull(ret)) { + Optional ret = this.annotationPropertyMap.get(index); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(index.toString()); } - return ret; + return ret.get(); } public OWLAnnotationValue getOWLAnnotationValue(Integer index) { Objects.requireNonNull(index); - OWLAnnotationValue ret = this.annotationValueMap.get(index); - if (Objects.isNull(ret)) { + Optional ret = this.annotationValueMap.get(index); + if (!ret.isPresent()) { throw TranslationException.newIncompleteMapException(index.toString()); } - return ret; + return ret.get(); } public Optional getOptId(OWLClass owlClass) { Objects.requireNonNull(owlClass); - return Optional.ofNullable(this.classInvMap.get(owlClass)); + return this.classInvMap.get(owlClass); } public Optional getOptId(OWLDataProperty owlDataProperty) { Objects.requireNonNull(owlDataProperty); - return Optional.ofNullable(this.dataPropertyInvMap.get(owlDataProperty)); + return this.dataPropertyInvMap.get(owlDataProperty); } public Optional getOptId(OWLIndividual individual) { Objects.requireNonNull(individual); - return Optional.ofNullable(this.individualInvMap.get(individual)); + return this.individualInvMap.get(individual.asOWLNamedIndividual()); } public Optional getOptId(OWLLiteral owlLiteral) { Objects.requireNonNull(owlLiteral); - return Optional.ofNullable(this.literalInvMap.get(owlLiteral)); + return this.literalInvMap.get(owlLiteral); } public Optional getOptId(OWLObjectProperty owlObjectProperty) { Objects.requireNonNull(owlObjectProperty); - return Optional.ofNullable(this.objectPropertyInvMap.get(owlObjectProperty)); + return this.objectPropertyInvMap.get(owlObjectProperty); } public Optional getOptId(OWLAnnotationProperty owlAnnotationProperty) { Objects.requireNonNull(owlAnnotationProperty); - return Optional.ofNullable(this.annotationPropertyInvMap.get(owlAnnotationProperty)); + return this.annotationPropertyInvMap.get(owlAnnotationProperty); } public Optional getOptId(OWLAnnotationValue owlAnnotationValue) { Objects.requireNonNull(owlAnnotationValue); - Integer ret = this.annotationValueInvMap.get(owlAnnotationValue); - if (Objects.isNull(ret)) { - String msg = null; + Optional ret = this.annotationValueInvMap.get(owlAnnotationValue); + if (!ret.isPresent()) { + String msg = null; // FIXME this variable is never used if (owlAnnotationValue.asLiteral().isPresent()) { msg = owlAnnotationValue.asLiteral().get().getLiteral(); } else { msg = owlAnnotationValue.toString(); } } - return Optional.ofNullable(ret); + return ret; } public Optional getOptOWLClass(Integer index) { Objects.requireNonNull(index); - return Optional.ofNullable(this.classMap.get(index)); + return this.classMap.get(index); } public Optional getOptOWLDataProperty(Integer index) { Objects.requireNonNull(index); - return Optional.ofNullable(this.dataPropertyMap.get(index)); + return this.dataPropertyMap.get(index); } public Optional getOptOWLNamedIndividual(Integer index) { Objects.requireNonNull(index); - return Optional.ofNullable(this.individualMap.get(index)); + return this.individualMap.get(index); } public Optional getOptOWLObjectProperty(Integer index) { Objects.requireNonNull(index); - return Optional.ofNullable(this.objectPropertyMap.get(index)); + return this.objectPropertyMap.get(index); } public Optional getOptOWLAnnotationProperty(Integer index) { Objects.requireNonNull(index); - return Optional.ofNullable(this.annotationPropertyMap.get(index)); + return this.annotationPropertyMap.get(index); } public Optional getOptOWLAnnotationValue(Integer index) { Objects.requireNonNull(index); - return Optional.ofNullable(this.annotationValueMap.get(index)); + return this.annotationValueMap.get(index); } private void initializeMaps() { From 83d56c379513386db12e15070601952353cbba10 Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Mon, 19 Jun 2017 23:16:42 +0200 Subject: [PATCH 07/57] Use OptMap in reasoner package --- .../jcel/reasoner/main/RuleBasedReasoner.java | 27 +++++----- .../reasoner/module/ClassModuleProcessor.java | 51 ++++++++++--------- .../jcel/reasoner/module/ModuleProcessor.java | 18 ++++--- 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/main/RuleBasedReasoner.java b/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/main/RuleBasedReasoner.java index d62af262..78188ff4 100644 --- a/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/main/RuleBasedReasoner.java +++ b/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/main/RuleBasedReasoner.java @@ -49,7 +49,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Map; +import java.util.Optional; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; + import java.util.Objects; import java.util.Set; import java.util.logging.Logger; @@ -83,8 +86,8 @@ public class RuleBasedReasoner implements IntegerReasoner { private static final Logger logger = Logger.getLogger(RuleBasedReasoner.class.getName()); - private final Map auxClassInvMap = new HashMap<>(); - private final Map auxClassMap = new HashMap<>(); + private final OptMap auxClassInvMap = new OptMapImpl<>(new HashMap<>()); + private final OptMap auxClassMap = new OptMapImpl<>(new HashMap<>()); private boolean classified = false; private final OntologyEntailmentChecker entailmentChecker = new OntologyEntailmentChecker(this); private final IntegerOntologyObjectFactory factory; @@ -157,8 +160,8 @@ protected IntegerClass flattenClassExpression(IntegerClassExpression ce) { if (ce instanceof IntegerClass) { ret = (IntegerClass) ce; } else { - Integer classIndex = this.auxClassInvMap.get(ce); - if (Objects.isNull(classIndex)) { + Optional optClassIndex = this.auxClassInvMap.get(ce); + if (!optClassIndex.isPresent()) { Integer auxClassId = this.factory.getEntityManager().createAnonymousEntity(IntegerEntityType.CLASS, false); ret = getDataTypeFactory().createClass(auxClassId); @@ -181,7 +184,7 @@ protected IntegerClass flattenClassExpression(IntegerClassExpression ce) { this.classified = false; } else { - ret = getDataTypeFactory().createClass(classIndex); + ret = getDataTypeFactory().createClass(optClassIndex.get()); } } @@ -504,17 +507,17 @@ public Set> getTypes(IntegerNamedIndividual ind, boolean direc Objects.requireNonNull(ind); classify(); IntegerHierarchicalGraph graph = getProcessor().getClassHierarchy(); - Map> map = getProcessor().getDirectTypes(); - Set directElemSet = map.get(ind.getId()); - if (Objects.isNull(directElemSet)) { - directElemSet = Collections.emptySet(); + OptMap> map = new OptMapImpl<>(getProcessor().getDirectTypes()); + Optional> optDirectElemSet = map.get(ind.getId()); + if (!optDirectElemSet.isPresent()) { + optDirectElemSet = Optional.of(Collections.emptySet()); } Set set = null; if (direct) { - set = directElemSet; + set = optDirectElemSet.get(); } else { set = new HashSet<>(); - for (Integer current : directElemSet) { + for (Integer current : optDirectElemSet.get()) { set.addAll(graph.getAncestors(current)); } } diff --git a/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ClassModuleProcessor.java b/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ClassModuleProcessor.java index b60103e3..c29848b9 100644 --- a/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ClassModuleProcessor.java +++ b/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ClassModuleProcessor.java @@ -53,6 +53,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.TreeSet; import java.util.logging.Logger; @@ -64,6 +65,8 @@ import de.tudresden.inf.lat.jcel.core.graph.IntegerHierarchicalGraphImpl; import de.tudresden.inf.lat.jcel.core.graph.IntegerSubsumerGraphImpl; import de.tudresden.inf.lat.jcel.coreontology.axiom.IntegerAnnotation; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager; import de.tudresden.inf.lat.jcel.ontology.axiom.complex.ComplexIntegerAxiom; import de.tudresden.inf.lat.jcel.ontology.axiom.complex.ComplexIntegerAxiomFactory; @@ -93,10 +96,10 @@ public class ClassModuleProcessor implements Processor { private Set accumulatedAxiomSet = null; private final IntegerOntologyObjectFactory axiomFactory; private IntegerHierarchicalGraph classHierarchy = null; - private final Map> classToAxiom = new HashMap<>(); - private final Map> classToClass = new HashMap<>(); + private final OptMap> classToAxiom = new OptMapImpl<>(new HashMap<>()); + private final OptMap> classToClass = new OptMapImpl<>(new HashMap<>()); private IntegerHierarchicalGraphImpl dataPropertyHierarchy = null; - private Map> directTypes = null; + private OptMap> directTypes = null; private boolean finalClassification = false; private boolean isReady = false; private Integer moduleIndex = 0; @@ -104,7 +107,7 @@ public class ClassModuleProcessor implements Processor { private IntegerHierarchicalGraph objectPropertyHierarchy = null; private Processor processor = null; private final ModuleProcessorFactory processorFactory; - private Map> sameIndividualMap = null; + private OptMap> sameIndividualMap = null; private final Set sharedAxioms = new HashSet<>(); /** @@ -188,19 +191,19 @@ private void createMaps(Set axiomSet) { } else { classSet.forEach(classId -> { - Set complexAxioms = this.classToAxiom.get(classId); - if (Objects.isNull(complexAxioms)) { - complexAxioms = new HashSet<>(); - this.classToAxiom.put(classId, complexAxioms); + Optional> optComplexAxioms = this.classToAxiom.get(classId); + if (!optComplexAxioms.isPresent()) { + optComplexAxioms = Optional.of(new HashSet<>()); + this.classToAxiom.put(classId, optComplexAxioms.get()); } - complexAxioms.add(axiom); + optComplexAxioms.get().add(axiom); - Set otherClasses = this.classToClass.get(classId); - if (Objects.isNull(otherClasses)) { - otherClasses = new HashSet<>(); - this.classToClass.put(classId, otherClasses); + Optional> optOtherClasses = this.classToClass.get(classId); + if (!optOtherClasses.isPresent()) { + optOtherClasses = Optional.of(new HashSet<>()); + this.classToClass.put(classId, optOtherClasses.get()); } - otherClasses.addAll(classSet); + optOtherClasses.get().addAll(classSet); }); } @@ -217,9 +220,9 @@ private List> findModules(Set axio clustersOfClasses.forEach(classSet -> { Set currentModule = new HashSet<>(); classSet.forEach(classId -> { - Set reachable = this.classToAxiom.get(classId); - if (Objects.nonNull(reachable)) { - currentModule.addAll(reachable); + Optional> optReachable = this.classToAxiom.get(classId); + if (optReachable.isPresent()) { + currentModule.addAll(optReachable.get()); } }); currentModule.addAll(this.sharedAxioms); @@ -278,7 +281,7 @@ public Map> getDirectTypes() { if (!isReady()) { throw new UnclassifiedOntologyException(); } - return this.directTypes; + return this.directTypes.asMap(); } @Override @@ -296,9 +299,9 @@ private Set getReachableClasses(Integer firstClassId, Set reachable = this.classToClass.get(classId); - if (Objects.nonNull(reachable)) { - toVisit.addAll(reachable); + Optional> reachable = this.classToClass.get(classId); + if (reachable.isPresent()) { + toVisit.addAll(reachable.get()); } toVisit.removeAll(ret); } @@ -310,7 +313,7 @@ public Map> getSameIndividualMap() { if (!isReady()) { throw new UnclassifiedOntologyException(); } - return this.sameIndividualMap; + return this.sameIndividualMap.asMap(); } @Override @@ -336,8 +339,8 @@ private void preProcess(Set axioms) { this.moduleIndex = 0; this.accumulatedAxiomSet = new HashSet<>(); - this.directTypes = new HashMap<>(); - this.sameIndividualMap = new HashMap<>(); + this.directTypes = new OptMapImpl<>(new HashMap<>()); + this.sameIndividualMap = new OptMapImpl<>(new HashMap<>()); logger.fine(""); logger.fine(""); diff --git a/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ModuleProcessor.java b/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ModuleProcessor.java index c985339f..b7e3b352 100644 --- a/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ModuleProcessor.java +++ b/jcel-reasoner/src/main/java/de/tudresden/inf/lat/jcel/reasoner/module/ModuleProcessor.java @@ -61,6 +61,8 @@ import de.tudresden.inf.lat.jcel.core.graph.IntegerHierarchicalGraph; import de.tudresden.inf.lat.jcel.core.graph.IntegerHierarchicalGraphImpl; import de.tudresden.inf.lat.jcel.core.graph.IntegerSubsumerGraphImpl; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager; import de.tudresden.inf.lat.jcel.ontology.axiom.complex.ComplexIntegerAxiom; @@ -78,14 +80,14 @@ public class ModuleProcessor implements Processor { private IntegerHierarchicalGraphImpl classHierarchy = null; private IntegerHierarchicalGraphImpl dataPropertyHierarchy = null; - private Map> directTypes = null; + private OptMap> directTypes = null; private boolean isReady = false; private Integer moduleIndex = 0; private List> moduleList = null; private IntegerHierarchicalGraphImpl objectPropertyHierarchy = null; private Processor processor = null; private final ModuleProcessorFactory processorFactory; - private Map> sameIndividualMap = null; + private OptMap> sameIndividualMap = null; /** * Constructs a new module processor. It uses an auxiliary processor to @@ -183,9 +185,9 @@ public IntegerHierarchicalGraph getDataPropertyHierarchy() throws UnclassifiedOn @Override public Map> getDirectTypes() { - Map> ret = null; + Map> ret = new HashMap<>(); if (isReady()) { - ret = this.directTypes; + ret = this.directTypes.asMap(); } return ret; } @@ -201,9 +203,9 @@ public IntegerHierarchicalGraph getObjectPropertyHierarchy() { @Override public Map> getSameIndividualMap() { - Map> ret = null; + Map> ret = new HashMap<>(); if (isReady()) { - ret = this.sameIndividualMap; + ret = this.sameIndividualMap.asMap(); } return ret; } @@ -233,8 +235,8 @@ private void preProcess(Set originalAxiomSet) { IntegerEntityManager.topClassId); this.objectPropertyHierarchy = new IntegerHierarchicalGraphImpl(IntegerEntityManager.bottomObjectPropertyId, IntegerEntityManager.topObjectPropertyId); - this.directTypes = new HashMap<>(); - this.sameIndividualMap = new HashMap<>(); + this.directTypes = new OptMapImpl<>(new HashMap<>()); + this.sameIndividualMap = new OptMapImpl<>(new HashMap<>()); logger.fine(""); logger.fine(""); From f8fd4a53ebe645bed98e546193f401ddd62b5ed3 Mon Sep 17 00:00:00 2001 From: Julian Mendez Date: Wed, 21 Jun 2017 00:37:39 +0200 Subject: [PATCH 08/57] Use OptMap in some subpackages of core package --- .../module/DefaultModuleExtractor.java | 30 +++++---- .../rulebased/ClassifierStatusImpl.java | 64 +++++++++++-------- .../rulebased/RuleBasedProcessor.java | 35 +++++----- .../algorithm/rulebased/TurtleRenderer.java | 17 +++-- .../completion/common/ClassifierStatus.java | 3 +- .../jcel/core/completion/ext/CR6RExtRule.java | 10 ++- .../jcel/core/completion/ext/CR6SExtRule.java | 10 ++- .../jcel/core/completion/ext/CR7RExtRule.java | 17 +++-- .../core/completion/ext/CR9RExtOptRule.java | 20 +++++- .../jcel/core/completion/ext/CR9RExtRule.java | 19 ++++-- .../inf/lat/jcel/core/saturation/SR4Rule.java | 10 +-- .../core/saturation/SaturationRuleHelper.java | 44 +++++++------ 12 files changed, 172 insertions(+), 107 deletions(-) diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/module/DefaultModuleExtractor.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/module/DefaultModuleExtractor.java index da36c1a8..40d7bdc6 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/module/DefaultModuleExtractor.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/module/DefaultModuleExtractor.java @@ -49,12 +49,13 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; -import java.util.Map; -import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.TreeSet; import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiom; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerAxiom; /** @@ -80,17 +81,18 @@ public DefaultModuleExtractor() { * @return a map that relates a class with the set of axioms where this * class occurs on the left side of the axiom */ - Map> buildMapOfAxioms(Set normalizedAxioms) { - Map> map = new HashMap<>(); + OptMap> buildMapOfAxioms( + Set normalizedAxioms) { + OptMap> map = new OptMapImpl<>(new HashMap<>()); normalizedAxioms.forEach(axiom -> { Set classesOnTheLeft = axiom.getClassesOnTheLeft(); classesOnTheLeft.forEach(classId -> { - Set value = map.get(classId); - if (Objects.isNull(value)) { - value = new HashSet<>(); - map.put(classId, value); + Optional> optValue = map.get(classId); + if (!optValue.isPresent()) { + optValue = Optional.of(new HashSet<>()); + map.put(classId, optValue.get()); } - value.add(axiom); + optValue.get().add(axiom); }); }); return map; @@ -107,12 +109,12 @@ Set getAxiomsWithoutEntitiesOnTheLeft(Set getAxiomsWithClassesOnTheLeft(Set classesToVisit, - Map> map) { + OptMap> map) { Set ret = new HashSet<>(); classesToVisit.forEach(classId -> { - Set newAxioms = map.get(classId); - if (Objects.nonNull(newAxioms)) { - ret.addAll(newAxioms); + Optional> optNewAxioms = map.get(classId); + if (optNewAxioms.isPresent()) { + ret.addAll(optNewAxioms.get()); } }); return ret; @@ -146,7 +148,7 @@ public Module extractModule(Collection setOfAxioms, Set< newAxioms.addAll(getAxiomsWithoutEntitiesOnTheLeft(axioms)); - Map> map = buildMapOfAxioms(axioms); + OptMap> map = buildMapOfAxioms(axioms); Set visitedClasses = new TreeSet<>(); Set classesToVisit = new TreeSet<>(); diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/ClassifierStatusImpl.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/ClassifierStatusImpl.java index 6edba3f0..0f45eb3a 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/ClassifierStatusImpl.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/ClassifierStatusImpl.java @@ -53,9 +53,9 @@ import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.TreeSet; @@ -69,6 +69,8 @@ import de.tudresden.inf.lat.jcel.core.graph.VNodeImpl; import de.tudresden.inf.lat.jcel.coreontology.axiom.ExtendedOntology; import de.tudresden.inf.lat.jcel.coreontology.axiom.RI2Axiom; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityType; @@ -87,15 +89,15 @@ public class ClassifierStatusImpl implements ClassifierStatus { private static final int topObjectPropertyId = IntegerEntityManager.topObjectPropertyId; private IntegerSubsumerGraphImpl classGraph = null; - private final Map> cognateFunctPropMap = new HashMap<>(); + private final OptMap> cognateFunctPropMap = new OptMapImpl<>(new HashMap<>()); private final ExtendedOntology extendedOntology; private IntegerEntityManager entityManager = null; - private final Map invNodeSet = new HashMap<>(); + private final OptMap invNodeSet = new OptMapImpl<>(new HashMap<>()); private final Object monitorClassGraph = new Object(); private final Object monitorRelationSet = new Object(); private final Object monitorSetQsubR = new Object(); private final Object monitorSetQsubS = new Object(); - private final Map nodeSet = new HashMap<>(); + private final OptMap nodeSet = new OptMapImpl<>(new HashMap<>()); private IntegerSubsumerBidirectionalGraphImpl objectPropertyGraph = null; private IntegerRelationMapImpl relationSet = null; private final Set setQsubR = new TreeSet<>(); @@ -181,7 +183,11 @@ public boolean addToS(int subClass, int superClass) { @Override public boolean contains(VNode node) { Objects.requireNonNull(node); - return Objects.nonNull(this.invNodeSet.get(node)); + boolean ret = false; + if (node instanceof VNodeImpl) { + ret = this.invNodeSet.get((VNodeImpl) node).isPresent(); + } + return ret; } private void createClassGraph() { @@ -201,12 +207,12 @@ private void createMapOfObjectPropertiesWithFunctionalAncestor() { this.extendedOntology.getFunctionalObjectProperties().forEach(s -> { Collection cognates = getSubObjectProperties(s); cognates.forEach(r -> { - Set currentSet = this.cognateFunctPropMap.get(r); - if (Objects.isNull(currentSet)) { - currentSet = new HashSet<>(); - this.cognateFunctPropMap.put(r, currentSet); + Optional> optCurrentSet = this.cognateFunctPropMap.get(r); + if (!optCurrentSet.isPresent()) { + optCurrentSet = Optional.of(new HashSet<>()); + this.cognateFunctPropMap.put(r, optCurrentSet.get()); } - currentSet.addAll(cognates); + optCurrentSet.get().addAll(cognates); }); }); } @@ -233,18 +239,21 @@ private void createObjectPropertyGraph() { @Override public int createOrGetNodeId(VNode node) { Objects.requireNonNull(node); - Integer ret = this.invNodeSet.get(node); - if (Objects.isNull(ret)) { - ret = node.getClassId(); + Optional optNodeId = Optional.empty(); + if (node instanceof VNodeImpl) { + optNodeId = this.invNodeSet.get((VNodeImpl) node); + } + if (!optNodeId.isPresent()) { + optNodeId = Optional.of(node.getClassId()); if (!node.isEmpty()) { - ret = getIdGenerator().createAnonymousEntity(IntegerEntityType.CLASS, true); + optNodeId = Optional.of(getIdGenerator().createAnonymousEntity(IntegerEntityType.CLASS, true)); VNodeImpl newNode = new VNodeImpl(node.getClassId()); newNode.addExistentialsOf(node); - this.nodeSet.put(ret, newNode); - this.invNodeSet.put(newNode, ret); + this.nodeSet.put(optNodeId.get(), newNode); + this.invNodeSet.put(newNode, optNodeId.get()); } } - return ret; + return optNodeId.get(); } private void createRelationSet() { @@ -321,7 +330,7 @@ public long getDeepSizeOfS() { * @return the number of elements in the node set */ public long getDeepSizeOfV() { - return this.nodeSet.keySet().stream().map(nodeId -> this.nodeSet.get(nodeId).getDeepSize()).reduce(0L, + return this.nodeSet.keySet().stream().map(nodeId -> this.nodeSet.get(nodeId).get().getDeepSize()).reduce(0L, (accum, elem) -> (accum + elem)); } @@ -354,8 +363,13 @@ public int getInverseObjectPropertyOf(int propertyId) { } @Override - public VNode getNode(int nodeId) { - return this.nodeSet.get(nodeId); + public Optional getNode(int nodeId) { + Optional node = this.nodeSet.get(nodeId); + Optional ret = Optional.empty(); + if (node.isPresent()) { + ret = Optional.of(node.get()); + } + return ret; } /** @@ -405,13 +419,13 @@ public Collection getObjectPropertiesBySecond(int cA) { @Override public Set getObjectPropertiesWithFunctionalAncestor(int objectProperty) { - Set ret = this.cognateFunctPropMap.get(objectProperty); - if (Objects.isNull(ret)) { - ret = Collections.emptySet(); + Optional> optSet = this.cognateFunctPropMap.get(objectProperty); + if (!optSet.isPresent()) { + optSet = Optional.of(Collections.emptySet()); } else { - ret = Collections.unmodifiableSet(ret); + optSet = Optional.of(Collections.unmodifiableSet(optSet.get())); } - return ret; + return optSet.get(); } /** diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/RuleBasedProcessor.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/RuleBasedProcessor.java index b557180e..f0d2d36a 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/RuleBasedProcessor.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/RuleBasedProcessor.java @@ -58,6 +58,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.logging.Logger; @@ -75,6 +76,8 @@ import de.tudresden.inf.lat.jcel.coreontology.axiom.ExtendedOntologyImpl; import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiom; import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiomFactory; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityType; import de.tudresden.inf.lat.jcel.coreontology.datatype.OntologyExpressivity; @@ -128,7 +131,7 @@ public void run() { private SChain chainS = null; private IntegerHierarchicalGraph classHierarchy = null; private IntegerHierarchicalGraph dataPropertyHierarchy = null; - private Map> directTypes = null; + private OptMap> directTypes = null; private final IntegerEntityManager entityManager; private final NormalizedIntegerAxiomFactory factory; private boolean isReady = false; @@ -136,7 +139,7 @@ public void run() { private long loggingCount = loggingFrequency; private final boolean multiThreadedMode = false; private IntegerHierarchicalGraph objectPropertyHierarchy = null; - private Map> sameIndividualMap = null; + private OptMap> sameIndividualMap = null; private ClassifierStatusImpl status = null; private WorkerThreadR threadR1 = null; private WorkerThreadR threadR2 = null; @@ -192,8 +195,8 @@ public void addAxioms(Set normalizedAxiomSet) { * graph containing direct subsumers * @return a map with all the direct types for each individual. */ - private Map> computeDirectTypes(IntegerHierarchicalGraph hierarchicalGraph) { - Map> ret = new HashMap<>(); + private OptMap> computeDirectTypes(IntegerHierarchicalGraph hierarchicalGraph) { + OptMap> ret = new OptMapImpl<>(new HashMap<>()); Set individuals = getEntityManager().getIndividuals(); individuals.forEach(indiv -> { Set subsumers = hierarchicalGraph.getParents(getEntityManager().getAuxiliaryNominal(indiv).get()); @@ -235,17 +238,17 @@ private Set computeReachability(Integer c) { return ret; } - private Set computeReachability(Integer c, Map> reachableNodeCache) { - Set reachableNodes = reachableNodeCache.get(c); - if (Objects.isNull(reachableNodes)) { - reachableNodes = computeReachability(c); - reachableNodeCache.put(c, reachableNodes); + private Set computeReachability(Integer c, OptMap> reachableNodeCache) { + Optional> optReachableNodes = reachableNodeCache.get(c); + if (!optReachableNodes.isPresent()) { + optReachableNodes = Optional.of(computeReachability(c)); + reachableNodeCache.put(c, optReachableNodes.get()); } - return reachableNodes; + return optReachableNodes.get(); } - private Map> computeSameIndividualMap(IntegerHierarchicalGraph hierarchicalGraph) { - Map> ret = new HashMap<>(); + private OptMap> computeSameIndividualMap(IntegerHierarchicalGraph hierarchicalGraph) { + OptMap> ret = new OptMapImpl<>(new HashMap<>()); Set individuals = getEntityManager().getIndividuals(); individuals.forEach(indiv -> { Set equivalentClasses = hierarchicalGraph @@ -269,7 +272,7 @@ private Map> computeSameIndividualMap(IntegerHierarchicalG * key * @param value * value - * @return a map entry created using the paramenters + * @return a map entry created using the parameters */ private Map.Entry createEntry(String key, String value) { return new AbstractMap.SimpleEntry(key, value); @@ -365,7 +368,7 @@ public Map> getDirectTypes() { if (!isReady()) { throw new UnclassifiedOntologyException(); } - return Collections.unmodifiableMap(this.directTypes); + return Collections.unmodifiableMap(this.directTypes.asMap()); } protected IntegerEntityManager getEntityManager() { @@ -421,7 +424,7 @@ public Map> getSameIndividualMap() { if (!isReady()) { throw new UnclassifiedOntologyException(); } - return Collections.unmodifiableMap(this.sameIndividualMap); + return Collections.unmodifiableMap(this.sameIndividualMap.asMap()); } /** @@ -594,7 +597,7 @@ private boolean processMultiThreaded() { * the hierarchical graph */ private void processNominals(IntegerHierarchicalGraph hierarchicalGraph) { - Map> reachabilityCache = new HashMap<>(); + OptMap> reachabilityCache = new OptMapImpl<>(new HashMap<>()); Set nominals = getEntityManager().getAuxiliaryNominals(); nominals.forEach(indiv -> { Set descendants = getDescendants(hierarchicalGraph, indiv); diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/TurtleRenderer.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/TurtleRenderer.java index cd6a8845..cce38939 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/TurtleRenderer.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/algorithm/rulebased/TurtleRenderer.java @@ -52,10 +52,13 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Collection; -import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.TreeMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; + /** * An object of this class creates a stream in a particular case of Turtle * (Terse RDF Triple Language). @@ -75,7 +78,7 @@ public class TurtleRenderer { private static final String space = " "; private static final String uriDelimiterLeft = "<"; private static final String uriDelimiterRight = ">"; - private final Map mapOfPrefixes = new TreeMap<>(); + private final OptMap mapOfPrefixes = new OptMapImpl<>(new TreeMap<>()); private final BufferedWriter output; @@ -124,9 +127,9 @@ private String getTurtleEntity(String name) { URI uri = new URI(name); String prefix = getPrefix(uri); if (prefix.length() > 0) { - String prefixId = this.mapOfPrefixes.get(prefix); - if (Objects.nonNull(prefixId)) { - ret = prefixId + prefixSeparator + getName(uri); + Optional optPrefixId = this.mapOfPrefixes.get(prefix); + if (optPrefixId.isPresent()) { + ret = optPrefixId.get() + prefixSeparator + getName(uri); } else { ret = uriDelimiterLeft + name + uriDelimiterRight; } @@ -168,10 +171,10 @@ public boolean loadPrefixes(Collection identifiers) { */ public void renderPrefixes() throws IOException { for (String prefix : this.mapOfPrefixes.keySet()) { - String prefixId = this.mapOfPrefixes.get(prefix); + Optional optPrefixId = this.mapOfPrefixes.get(prefix); this.output.write(prefixKeyword); this.output.write(space); - this.output.write(prefixId); + this.output.write(optPrefixId.get()); this.output.write(prefixSeparator); this.output.write(space); this.output.write(uriDelimiterLeft); diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/common/ClassifierStatus.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/common/ClassifierStatus.java index 9414b260..209ecdff 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/common/ClassifierStatus.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/common/ClassifierStatus.java @@ -47,6 +47,7 @@ package de.tudresden.inf.lat.jcel.core.completion.common; import java.util.Collection; +import java.util.Optional; import java.util.Set; import de.tudresden.inf.lat.jcel.core.graph.VNode; @@ -145,7 +146,7 @@ public interface ClassifierStatus { * node identifier * @return the node for the given node identifier */ - VNode getNode(int nodeId); + Optional getNode(int nodeId); /** * Returns the set of object properties related with a certain class as a diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6RExtRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6RExtRule.java index a74c7895..be4a5334 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6RExtRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6RExtRule.java @@ -47,6 +47,7 @@ package de.tudresden.inf.lat.jcel.core.completion.ext; import java.util.Objects; +import java.util.Optional; import de.tudresden.inf.lat.jcel.core.completion.common.ClassifierStatus; import de.tudresden.inf.lat.jcel.core.completion.common.CompletionRuleMonitor; @@ -94,9 +95,12 @@ private boolean applyRule(ClassifierStatus status, int r, int x, int y) { status.getExtendedOntology().getGCI3rAAxioms(sMinus, a).forEach(axiom -> { int b = axiom.getSuperClass(); if (!status.getSubsumers(y).contains(b)) { - VNode psiNode = status.getNode(y); - VNodeImpl newNode = new VNodeImpl(psiNode.getClassId()); - newNode.addExistentialsOf(psiNode); + Optional optPsiNode = status.getNode(y); + if (!optPsiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + y + "'."); + } + VNodeImpl newNode = new VNodeImpl(optPsiNode.get().getClassId()); + newNode.addExistentialsOf(optPsiNode.get()); newNode.addExistential(rMinus, a); boolean inV = status.contains(newNode); int v = status.createOrGetNodeId(newNode); diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6SExtRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6SExtRule.java index 9cbd9ccc..d20a08cf 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6SExtRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR6SExtRule.java @@ -47,6 +47,7 @@ package de.tudresden.inf.lat.jcel.core.completion.ext; import java.util.Objects; +import java.util.Optional; import de.tudresden.inf.lat.jcel.core.completion.common.ClassifierStatus; import de.tudresden.inf.lat.jcel.core.completion.common.CompletionRuleMonitor; @@ -94,9 +95,12 @@ private boolean applyRule(ClassifierStatus status, int x, int a) { int b = axiom.getSuperClass(); status.getSecondByFirst(r, x).forEach(y -> { if (!status.getSubsumers(y).contains(b)) { - VNode psiNode = status.getNode(y); - VNodeImpl newNode = new VNodeImpl(psiNode.getClassId()); - newNode.addExistentialsOf(psiNode); + Optional optPsiNode = status.getNode(y); + if (!optPsiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + y + "'."); + } + VNodeImpl newNode = new VNodeImpl(optPsiNode.get().getClassId()); + newNode.addExistentialsOf(optPsiNode.get()); newNode.addExistential(rMinus, a); boolean inV = status.contains(newNode); int v = status.createOrGetNodeId(newNode); diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR7RExtRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR7RExtRule.java index cecc6ad6..98022979 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR7RExtRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR7RExtRule.java @@ -47,6 +47,7 @@ package de.tudresden.inf.lat.jcel.core.completion.ext; import java.util.Objects; +import java.util.Optional; import de.tudresden.inf.lat.jcel.core.completion.common.ClassifierStatus; import de.tudresden.inf.lat.jcel.core.completion.common.CompletionRuleMonitor; @@ -89,8 +90,14 @@ public boolean apply(ClassifierStatus status, int property, int leftClass, int r private boolean applyRule(ClassifierStatus status, int r2, int x, int y) { CompletionRuleMonitor ret = new CompletionRuleMonitor(); - VNode phiNode = status.getNode(x); - VNode psiNode = status.getNode(y); + Optional optPhiNode = status.getNode(x); + if (!optPhiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + x + "'."); + } + Optional optPsiNode = status.getNode(y); + if (!optPsiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + y + "'."); + } status.getSuperObjectProperties(r2).forEach(r -> { if (status.getExtendedOntology().getTransitiveObjectProperties().contains(r)) { int rMinus = status.getInverseObjectPropertyOf(r); @@ -101,9 +108,9 @@ private boolean applyRule(ClassifierStatus status, int r2, int x, int y) { int b = axiom.getSuperClass(); status.getSubObjectProperties(r).forEach(r1 -> { int r1Minus = status.getInverseObjectPropertyOf(r1); - if (phiNode.containsExistential(r1Minus, a)) { - VNodeImpl newNode = new VNodeImpl(psiNode.getClassId()); - newNode.addExistentialsOf(psiNode); + if (optPhiNode.get().containsExistential(r1Minus, a)) { + VNodeImpl newNode = new VNodeImpl(optPsiNode.get().getClassId()); + newNode.addExistentialsOf(optPsiNode.get()); newNode.addExistential(rMinus, a); boolean inV = status.contains(newNode); int v = status.createOrGetNodeId(newNode); diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtOptRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtOptRule.java index f6d97b43..642476d8 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtOptRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtOptRule.java @@ -48,11 +48,13 @@ import java.util.HashSet; import java.util.Objects; +import java.util.Optional; import java.util.Set; import de.tudresden.inf.lat.jcel.core.completion.common.ClassifierStatus; import de.tudresden.inf.lat.jcel.core.completion.common.CompletionRuleMonitor; import de.tudresden.inf.lat.jcel.core.completion.common.RObserverRule; +import de.tudresden.inf.lat.jcel.core.graph.VNode; import de.tudresden.inf.lat.jcel.core.graph.VNodeImpl; import de.tudresden.inf.lat.jcel.coreontology.datatype.IntegerEntityManager; @@ -94,13 +96,21 @@ public boolean apply(ClassifierStatus status, int property, int leftClass, int r private boolean applyRule(ClassifierStatus status, int r1, int x, int y) { CompletionRuleMonitor ret = new CompletionRuleMonitor(); - if (status.getNode(y).getClassId() == IntegerEntityManager.topClassId) { + Optional optPsiNode = status.getNode(y); + if (!optPsiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + y + "'."); + } + if (optPsiNode.get().getClassId() == IntegerEntityManager.topClassId) { Set valid = new HashSet<>(); valid.add(y); status.getObjectPropertiesWithFunctionalAncestor(r1).forEach(r2 -> { status.getSecondByFirst(r2, x).forEach(yi -> { - if (status.getNode(yi).getClassId() == IntegerEntityManager.topClassId) { + Optional optPhiNode = status.getNode(yi); + if (!optPhiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + yi + "'."); + } + if (optPhiNode.get().getClassId() == IntegerEntityManager.topClassId) { valid.add(yi); } }); @@ -109,7 +119,11 @@ private boolean applyRule(ClassifierStatus status, int r1, int x, int y) { if (valid.size() > 1) { VNodeImpl newNode = new VNodeImpl(IntegerEntityManager.topClassId); valid.forEach(yi -> { - newNode.addExistentialsOf(status.getNode(yi)); + Optional optPhiNode = status.getNode(yi); + if (!optPsiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + yi + "'."); + } + newNode.addExistentialsOf(optPhiNode.get()); }); int v = status.createOrGetNodeId(newNode); valid.forEach(yi -> { diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtRule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtRule.java index 7a67cd85..54b48b15 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtRule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/completion/ext/CR9RExtRule.java @@ -47,6 +47,7 @@ package de.tudresden.inf.lat.jcel.core.completion.ext; import java.util.Objects; +import java.util.Optional; import de.tudresden.inf.lat.jcel.core.completion.common.ClassifierStatus; import de.tudresden.inf.lat.jcel.core.completion.common.CompletionRuleMonitor; @@ -89,16 +90,22 @@ public boolean apply(ClassifierStatus status, int property, int leftClass, int r private boolean applyRule(ClassifierStatus status, int r1, int x, int y) { CompletionRuleMonitor ret = new CompletionRuleMonitor(); - VNode psiNode = status.getNode(y); - if (psiNode.getClassId() == IntegerEntityManager.topClassId) { + Optional optPsiNode = status.getNode(y); + if (!optPsiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + y + "'."); + } + if (optPsiNode.get().getClassId() == IntegerEntityManager.topClassId) { status.getObjectPropertiesWithFunctionalAncestor(r1).forEach(r2 -> { status.getSecondByFirst(r2, x).forEach(z -> { - VNode phiNode = status.getNode(z); - if (phiNode.getClassId() == IntegerEntityManager.topClassId) { + Optional optPhiNode = status.getNode(z); + if (!optPsiNode.isPresent()) { + throw new IllegalStateException("Node not found in internal structure '" + z + "'."); + } + if (optPhiNode.get().getClassId() == IntegerEntityManager.topClassId) { if (y != z) { VNodeImpl newNode = new VNodeImpl(IntegerEntityManager.topClassId); - newNode.addExistentialsOf(psiNode); - newNode.addExistentialsOf(phiNode); + newNode.addExistentialsOf(optPsiNode.get()); + newNode.addExistentialsOf(optPhiNode.get()); int v = status.createOrGetNodeId(newNode); status.getSubsumers(y).forEach(p -> { diff --git a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/saturation/SR4Rule.java b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/saturation/SR4Rule.java index 69869920..6937728c 100644 --- a/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/saturation/SR4Rule.java +++ b/jcel-core/src/main/java/de/tudresden/inf/lat/jcel/core/saturation/SR4Rule.java @@ -48,13 +48,14 @@ import java.util.Collections; import java.util.HashSet; -import java.util.Map; import java.util.Objects; import java.util.Set; import de.tudresden.inf.lat.jcel.coreontology.axiom.FunctObjectPropAxiom; import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiom; import de.tudresden.inf.lat.jcel.coreontology.axiom.NormalizedIntegerAxiomFactory; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMap; +import de.tudresden.inf.lat.jcel.coreontology.common.OptMapImpl; /** *