org.protempa.CollectSubtreeGetterSlowStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of protempa-framework Show documentation
Show all versions of protempa-framework Show documentation
Protempa Framework is the core of Protempa.
package org.protempa;
/*
* #%L
* Protempa Framework
* %%
* Copyright (C) 2012 - 2015 Emory University
* %%
* 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.
* #L%
*/
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.arp.javautil.arrays.Arrays;
/**
*
* @author Andrew Post
*/
class CollectSubtreeGetterSlowStrategy {
private final Map propositionDefinitionMap;
private final PropositionDefinitionWalker propDefWalker;
private final PropIdWalker propIdWalker;
private final boolean narrower;
CollectSubtreeGetterSlowStrategy(Map propositionDefinitionMap, boolean narrower) {
assert propositionDefinitionMap != null : "propositionDefinitionMap cannot be null";
this.propositionDefinitionMap = propositionDefinitionMap;
this.narrower = narrower;
this.propDefWalker = new PropositionDefinitionWalker();
this.propIdWalker = new PropIdWalker();
}
class InDataSourceResult {
private final Set result;
private final Set missing;
private InDataSourceResult(Set result, Set missing) {
this.result = result;
this.missing = missing;
}
public Set getResult() {
return result;
}
public Set getMissing() {
return missing;
}
}
InDataSourceResult collectPropIds(boolean inDataSourceOnly, Set propIds)
throws KnowledgeSourceReadException {
if (propIds.contains(null)) {
throw new IllegalArgumentException(
"propIds cannot contain a null element");
}
return this.propIdWalker.walkNarrowerPropDefs(inDataSourceOnly, propIds);
}
InDataSourceResult collectPropDefs(boolean inDataSourceOnly, Set propIds)
throws KnowledgeSourceReadException {
if (propIds.contains(null)) {
throw new IllegalArgumentException(
"propIds cannot contain a null element");
}
return this.propDefWalker.walkNarrowerPropDefs(inDataSourceOnly, propIds);
}
private abstract class Walker {
InDataSourceResult walkNarrowerPropDefs(boolean inDataSource, Set propIds) {
Set found = new HashSet<>();
Set missing = new HashSet<>();
Queue queue = new LinkedList<>(propIds);
while (!queue.isEmpty()) {
String propId = queue.poll();
PropositionDefinition pd = propositionDefinitionMap.get(propId);
if (pd != null) {
if (!inDataSource || pd.getInDataSource()) {
addToFound(propId, pd, found);
}
if (narrower) {
Arrays.addAll(queue, pd.getChildren());
} else {
Arrays.addAll(queue, pd.getInverseIsA());
}
} else {
missing.add(propId);
}
}
InDataSourceResult inDataSourceResult = new InDataSourceResult<>(found, missing);
return inDataSourceResult;
}
protected abstract void addToFound(String propId, PropositionDefinition propDef, Set found);
}
private final class PropositionDefinitionWalker extends Walker {
@Override
protected void addToFound(String propId, PropositionDefinition propDef, Set found) {
found.add(propDef);
}
}
private final class PropIdWalker extends Walker {
@Override
protected void addToFound(String propId, PropositionDefinition propDef, Set found) {
found.add(propId);
}
}
}