From efa7a6b3854203dc3867617a9d0550f6b9838561 Mon Sep 17 00:00:00 2001 From: Jules Jacobsen Date: Wed, 8 Mar 2017 14:10:33 +0000 Subject: [PATCH] Updated BMKnowledgeBaseOWLAPIImpl to use java.util.Optional as now produced from CurieUtil. Removed useless right-hand class types from collection initialisation. e.g. = new ArrayList() -> new ArrayList<>(); --- .../owlsim/kb/impl/BMKnowledgeBaseOWLAPIImpl.java | 94 ++++++++++------------ 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/owlsim-core/src/main/java/org/monarchinitiative/owlsim/kb/impl/BMKnowledgeBaseOWLAPIImpl.java b/owlsim-core/src/main/java/org/monarchinitiative/owlsim/kb/impl/BMKnowledgeBaseOWLAPIImpl.java index 7c22da3..abb4337 100644 --- a/owlsim-core/src/main/java/org/monarchinitiative/owlsim/kb/impl/BMKnowledgeBaseOWLAPIImpl.java +++ b/owlsim-core/src/main/java/org/monarchinitiative/owlsim/kb/impl/BMKnowledgeBaseOWLAPIImpl.java @@ -1,13 +1,6 @@ package org.monarchinitiative.owlsim.kb.impl; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import org.apache.log4j.Logger; @@ -49,7 +42,6 @@ import org.semanticweb.owlapi.reasoner.OWLReasoner; import org.semanticweb.owlapi.reasoner.OWLReasonerFactory; -import com.google.common.base.Optional; import com.google.common.base.Preconditions; import com.googlecode.javaewah.EWAHCompressedBitmap; import com.hp.hpl.jena.query.Query; @@ -100,7 +92,7 @@ // private Set classesInSignature; private Set individualsInSignature; private Map>> propertyValueMapMap; - Map> opposingClassMap = new HashMap>(); + Map> opposingClassMap = new HashMap<>(); private int[] individualCountPerClassArray; @@ -187,7 +179,7 @@ private void populateLabelsFromOntology(LabelMapper labelMapper, OWLOntology ont } if (n == 0) { LOG.info("Setting labels from fragments"); - Set objs = new HashSet(); + Set objs = new HashSet<>(); objs.addAll(ontology.getClassesInSignature()); objs.addAll(ontology.getIndividualsInSignature()); for (OWLNamedObject obj : objs) { @@ -216,7 +208,7 @@ public LabelMapper getLabelMapper() { * @return set of all class identifiers */ public Set getClassIdsInSignature() { - Set ids = new HashSet(); + Set ids = new HashSet<>(); for (OWLClass i : getClassesInSignature()) { ids.add(getShortForm(i.getIRI())); } @@ -252,7 +244,7 @@ public int getNumClassNodes() { * @return ids */ public Set getIndividualIdsInSignature() { - Set ids = new HashSet(); + Set ids = new HashSet<>(); for (OWLNamedIndividual i : getIndividualsInSignature()) { ids.add(getShortForm(i.getIRI())); } @@ -284,8 +276,8 @@ private void translateFromDataOntology() { // Each OWLClass and OWLIndividual is mapped to an Integer index private void createMap() { LOG.info("Creating mapping from ontology objects to integers"); - classNodes = new HashSet>(); - individualNodes = new HashSet>(); + classNodes = new HashSet<>(); + individualNodes = new HashSet<>(); Set classesInSignature; classesInSignature = owlOntology.getClassesInSignature(true); LOG.info("|classes|=" + classesInSignature.size()); @@ -293,13 +285,13 @@ private void createMap() { classesInSignature.remove(getOWLNothing()); individualsInSignature = owlOntology.getIndividualsInSignature(true); LOG.info("|individuals|=" + individualsInSignature.size()); - classToNodeMap = new HashMap>(); - individualToNodeMap = new HashMap>(); - classNodeToIntegerMap = new HashMap, Integer>(); - individualNodeToIntegerMap = new HashMap, Integer>(); - propertyValueMapMap = new HashMap>>(); - final HashMap, Integer> classNodeToFrequencyMap = new HashMap, Integer>(); - final HashMap, Double> classNodeToFreqDepthMap = new HashMap, Double>(); + classToNodeMap = new HashMap<>(); + individualToNodeMap = new HashMap<>(); + classNodeToIntegerMap = new HashMap<>(); + individualNodeToIntegerMap = new HashMap<>(); + propertyValueMapMap = new HashMap<>(); + final HashMap, Integer> classNodeToFrequencyMap = new HashMap<>(); + final HashMap, Double> classNodeToFreqDepthMap = new HashMap<>(); for (OWLClass c : classesInSignature) { if (owlReasoner.getInstances(c, false).isEmpty()) { // TODO: deal with subclasses @@ -346,17 +338,15 @@ private void createMap() { // Content) // nodes are have LOWER indices // TODO: use depth as a tie breaker - List> classNodesSorted = new ArrayList>(classNodes); - Collections.sort(classNodesSorted, new Comparator>() { - public int compare(Node n1, Node n2) { - double f1 = classNodeToFreqDepthMap.get(n1); - double f2 = classNodeToFreqDepthMap.get(n2); - if (f1 < f2) - return -1; - if (f1 > f2) - return 1; - return 0; - } + List> classNodesSorted = new ArrayList<>(classNodes); + classNodesSorted.sort((n1, n2) -> { + double f1 = classNodeToFreqDepthMap.get(n1); + double f2 = classNodeToFreqDepthMap.get(n2); + if (f1 < f2) + return -1; + if (f1 > f2) + return 1; + return 0; }); int numClassNodes = classNodesSorted.size(); classNodeArray = classNodesSorted.toArray(new Node[numClassNodes]); @@ -378,7 +368,7 @@ public int compare(Node n1, Node n2) { private void setPropertyValues(OWLOntology ont, OWLNamedIndividual i) { Preconditions.checkNotNull(i); - Map> pvm = new HashMap>(); + Map> pvm = new HashMap<>(); String id = getShortForm(i.getIRI()); propertyValueMapMap.put(id, pvm); for (OWLIndividualAxiom ax : ont.getAxioms(i)) { @@ -418,7 +408,7 @@ private void setPropertyValues(OWLOntology ont, OWLNamedIndividual i) { private void addPropertyValue(Map> pvm, String pid, String v) { // LOG.debug("PV="+pid+"="+v); if (!pvm.containsKey(pid)) - pvm.put(pid, new HashSet()); + pvm.put(pid, new HashSet<>()); pvm.get(pid).add(v); } @@ -430,7 +420,7 @@ private void addOpposingClassPair(OWLClass c, OWLClassExpression dc) { private void addOpposingClassPairAsym(OWLClass c, OWLClassExpression d) { if (!opposingClassMap.containsKey(c)) - opposingClassMap.put(c, new HashSet()); + opposingClassMap.put(c, new HashSet<>()); opposingClassMap.get(c).add(d); } @@ -476,7 +466,7 @@ private void storeInferences() { // direct individuals are those asserted to be of type c or anything // equivalent to c - Set individualInts = new HashSet(); + Set individualInts = new HashSet<>(); for (OWLClass ec : owlReasoner.getEquivalentClasses(c).getEntities()) { for (OWLClassAssertionAxiom ax : owlOntology.getClassAssertionAxioms(ec)) { if (ax.getIndividual().isNamed()) { @@ -494,8 +484,8 @@ private void storeInferences() { ontoEWAHStore.setTypes(individualIndex, getIntegersForClassSet(owlReasoner.getTypes(i, false))); // Treat CLassAssertion( ComplementOf(c) i) as a negative assertion - Set ncs = new HashSet(); - Set ncsDirect = new HashSet(); + Set ncs = new HashSet<>(); + Set ncsDirect = new HashSet<>(); for (OWLClassAssertionAxiom cx : owlOntology.getClassAssertionAxioms(i)) { // TODO: investigate efficiency - number of items set may be // high @@ -595,7 +585,7 @@ private void storeIndividualToClassFrequencies() { } private Set getIntegersForClassSet(NodeSet nodeset) { - Set bits = new HashSet(); + Set bits = new HashSet<>(); for (Node n : nodeset.getNodes()) { if (n.contains(getOWLNothing())) continue; @@ -605,7 +595,7 @@ private void storeIndividualToClassFrequencies() { } private Set getIntegersForIndividualSet(NodeSet nodeset) { - Set bits = new HashSet(); + Set bits = new HashSet<>(); for (Node n : nodeset.getNodes()) { bits.add(getIndexForIndividualNode(n)); } @@ -702,7 +692,7 @@ public String getClassId(int index) { public Set getClassIds(int index) { Node n = getClassNode(index); - Set cids = new HashSet(); + Set cids = new HashSet<>(); for (OWLClass c : n.getEntities()) { cids.add(getShortForm(c.getIRI())); } @@ -710,7 +700,7 @@ public String getClassId(int index) { } public Set getClassIds(EWAHCompressedBitmap bm) { - Set cids = new HashSet(); + Set cids = new HashSet<>(); for (int x : bm) { Node n = getClassNode(x); for (OWLClass c : n.getEntities()) { @@ -799,7 +789,7 @@ protected EWAHCompressedBitmap getSuperClassesBM(OWLClass c, boolean isDirect) { * class */ protected EWAHCompressedBitmap getSuperClassesBMByOWLClassSet(Set clsSet) { - Set classIndices = new HashSet(); + Set classIndices = new HashSet<>(); for (OWLClass c : clsSet) { classIndices.add(getIndex(c)); } @@ -847,7 +837,7 @@ public EWAHCompressedBitmap getDirectSubClassesBM(int classIndex) { * @return union of all subClasses (direct and indirect) of any input class */ public EWAHCompressedBitmap getSubClassesBM(Set clsIds) { - Set classIndices = new HashSet(); + Set classIndices = new HashSet<>(); for (String id : clsIds) { classIndices.add(getClassIndex(id)); } @@ -859,7 +849,7 @@ public EWAHCompressedBitmap getSubClassesBM(Set clsIds) { * @return union of all direct subClasses of all input classes */ public EWAHCompressedBitmap getDirectSubClassesBM(Set clsIds) { - Set classIndices = new HashSet(); + Set classIndices = new HashSet<>(); for (String id : clsIds) { classIndices.add(getClassIndex(id)); } @@ -872,7 +862,7 @@ public EWAHCompressedBitmap getDirectSubClassesBM(Set clsIds) { * class */ public EWAHCompressedBitmap getSuperClassesBM(Set clsIds) { - Set classIndices = new HashSet(); + Set classIndices = new HashSet<>(); for (String id : clsIds) { classIndices.add(getClassIndex(id)); } @@ -1016,7 +1006,7 @@ protected OWLClass getOWLClass(String id) { if (curieUtil.getCurieMap().isEmpty()) { return getOWLClass(IRI.create(id)); } else { - return getOWLClass(IRI.create(curieUtil.getIri(id).or(id))); + return getOWLClass(IRI.create(curieUtil.getIri(id).orElse(id))); } } @@ -1046,7 +1036,7 @@ public OWLNamedIndividual getOWLNamedIndividual(String id) { if (curieUtil.getCurieMap().isEmpty()) { return getOWLNamedIndividual(IRI.create(id)); } else { - return getOWLNamedIndividual(IRI.create(curieUtil.getIri(id).or(id))); + return getOWLNamedIndividual(IRI.create(curieUtil.getIri(id).orElse(id))); } } @@ -1075,7 +1065,7 @@ public Entity getEntity(String id) { public Set getPropertyValues(String individualId, String property) { Map> m = getPropertyValueMap(individualId); if (m.containsKey(property)) - return new HashSet(m.get(property)); + return new HashSet<>(m.get(property)); else return Collections.emptySet(); } @@ -1099,7 +1089,7 @@ public String getIndividualId(int index) { @Override public EWAHCompressedBitmap getFilteredTypesBM(Set ids, String classId) { - Set classBits = new HashSet(); + Set classBits = new HashSet<>(); for (String id : ids) { classBits.add(this.getClassIndex(id)); } @@ -1110,7 +1100,7 @@ public EWAHCompressedBitmap getFilteredTypesBM(Set ids, String classId) public EWAHCompressedBitmap getFilteredDirectTypesBM(Set classIds, String classId) { - Set classBits = new HashSet(); + Set classBits = new HashSet<>(); for (String id : classIds) { classBits.add(this.getClassIndex(id)); }