use of com.b2international.snowowl.snomed.datastore.StatementFragment in project snow-owl by b2ihealthcare.
the class NormalFormGenerator method computeChanges.
@Override
public final void computeChanges(final IProgressMonitor monitor, final OntologyChangeProcessor<StatementFragment> statementProcessor, final OntologyChangeProcessor<ConcreteDomainFragment> concreteDomainProcessor) {
final Stopwatch stopwatch = Stopwatch.createStarted();
LOGGER.info(">>> Distribution normal form generation");
final LongList entries = reasonerTaxonomy.getIterationOrder();
final SubMonitor subMonitor = SubMonitor.convert(monitor, "Generating distribution normal form...", entries.size() * 2);
try {
LongSet previousLayer = null;
LongSet currentLayer = PrimitiveSets.newLongOpenHashSet();
final Set<Long> graphTypeIds = reasonerTaxonomy.getPropertyChains().stream().map(PropertyChain::getDestinationType).collect(Collectors.toSet());
// The first round can be skipped entirely, if no type IDs participate in a property chain
final boolean propertyChainsPresent = !graphTypeIds.isEmpty();
if (propertyChainsPresent) {
// Initialize node graphs for properties we need to traverse
LOGGER.info("--- Initializing node graphs for types: {}", graphTypeIds);
graphTypeIds.forEach(id -> transitiveNodeGraphs.put(id, new NodeGraph()));
// Round 1: build alternative hierarchies
for (final LongIterator itr = entries.iterator(); itr.hasNext(); ) /* empty */
{
final long conceptId = itr.next();
if (conceptId == ReasonerTaxonomyInferrer.DEPTH_CHANGE) {
if (previousLayer != null) {
invalidate(previousLayer);
}
previousLayer = currentLayer;
currentLayer = PrimitiveSets.newLongOpenHashSet();
continue;
}
precomputeProperties(conceptId, false);
final Collection<StatementFragment> inferredNonIsAFragments = statementCache.get(conceptId);
inferredNonIsAFragments.stream().filter(r -> transitiveNodeGraphs.keySet().contains(r.getTypeId())).filter(StatementFragmentWithDestination.class::isInstance).map(StatementFragmentWithDestination.class::cast).forEachOrdered(r -> transitiveNodeGraphs.get(r.getTypeId()).addParent(conceptId, r.getDestinationId()));
}
// Clear the last layer of concepts
previousLayer = null;
currentLayer = PrimitiveSets.newLongOpenHashSet();
statementCache.clear();
concreteDomainCache.clear();
} else {
LOGGER.info("--- Node graphs computation skipped, no types used for property chaining");
}
LOGGER.info("--- Use node graphs for hierarchy computation");
// Round 2: record changes using the hierarchies
for (final LongIterator itr = entries.iterator(); itr.hasNext(); ) /* empty */
{
final long conceptId = itr.next();
if (conceptId == ReasonerTaxonomyInferrer.DEPTH_CHANGE) {
if (previousLayer != null) {
invalidate(previousLayer);
}
previousLayer = currentLayer;
currentLayer = PrimitiveSets.newLongOpenHashSet();
continue;
}
// Run costly comparison of property chain hierarchies only if there are any
precomputeProperties(conceptId, propertyChainsPresent);
final Collection<StatementFragment> existingStatements = reasonerTaxonomy.getExistingInferredRelationships().get(conceptId);
final Collection<StatementFragment> targetStatements = getTargetRelationships(conceptId);
statementProcessor.apply(conceptId, existingStatements, targetStatements, StatementFragmentOrdering.INSTANCE, subMonitor.newChild(1));
final Collection<ConcreteDomainFragment> existingMembers = reasonerTaxonomy.getInferredConcreteDomainMembers().get(Long.toString(conceptId));
final Collection<ConcreteDomainFragment> targetMembers = getTargetMembers(conceptId);
concreteDomainProcessor.apply(conceptId, existingMembers, targetMembers, ConcreteDomainChangeOrdering.INSTANCE, subMonitor.newChild(1));
}
} finally {
subMonitor.done();
LOGGER.info("<<< Distribution normal form generation [{}]", stopwatch.toString());
}
}
use of com.b2international.snowowl.snomed.datastore.StatementFragment in project snow-owl by b2ihealthcare.
the class NormalFormGenerator method toZeroUnionGroups.
private Iterable<NormalFormUnionGroup> toZeroUnionGroups(final Collection<StatementFragment> unionGroupRelationships, final Collection<ConcreteDomainFragment> unionGroupMembers, final boolean useNodeGraphs) {
final ImmutableList.Builder<NormalFormUnionGroup> zeroUnionGroups = ImmutableList.builder();
for (final StatementFragment unionGroupRelationship : unionGroupRelationships) {
final NormalFormProperty normalFormProperty = toProperty(unionGroupRelationship, useNodeGraphs);
zeroUnionGroups.add(new NormalFormUnionGroup(normalFormProperty));
}
for (final ConcreteDomainFragment unionGroupMember : unionGroupMembers) {
final NormalFormValue normalFormValue = new NormalFormValue(unionGroupMember, reasonerTaxonomy);
zeroUnionGroups.add(new NormalFormUnionGroup(normalFormValue));
}
return zeroUnionGroups.build();
}
use of com.b2international.snowowl.snomed.datastore.StatementFragment in project snow-owl by b2ihealthcare.
the class NormalFormGenerator method precomputeProperties.
private void precomputeProperties(final long conceptId, final boolean useNodeGraphs) {
final LongSet parentIds = reasonerTaxonomy.getInferredAncestors().getDestinations(conceptId, true);
/*
* Non IS-A relationships are fetched from ancestors; redundancy must be removed. Since we are working through the list
* of concepts in breadth-first order, we only need to look at cached results from the direct parents, and "distill"
* a non-redundant set of components out of them.
*/
final LongKeyMap<Collection<StatementFragment>> candidateNonIsARelationships = PrimitiveMaps.newLongKeyOpenHashMap();
for (final LongIterator itr = parentIds.iterator(); itr.hasNext(); ) /* empty */
{
final long parentId = itr.next();
candidateNonIsARelationships.put(parentId, statementCache.get(parentId));
}
// Stated axiom fragments are non-IS A, but any stated relationships need to be filtered (if they are still present)
final Collection<StatementFragment> ownStatedRelationships = reasonerTaxonomy.getStatedRelationships().get(conceptId);
final Collection<StatementFragment> ownStatedNonIsaRelationships = ownStatedRelationships.stream().filter(r -> r.getTypeId() != IS_A).collect(Collectors.toList());
candidateNonIsARelationships.put(conceptId, ImmutableList.<StatementFragment>builder().addAll(ownStatedNonIsaRelationships).addAll(reasonerTaxonomy.getAxiomNonIsARelationships().get(conceptId)).addAll(reasonerTaxonomy.getAdditionalGroupedRelationships().get(conceptId)).build());
// Collect existing inferred relationships for cross-referencing group numbers
final Collection<StatementFragment> ownInferredRelationships = reasonerTaxonomy.getExistingInferredRelationships().get(conceptId);
final Collection<StatementFragment> ownInferredNonIsaRelationships = ownInferredRelationships.stream().filter(r -> r.getTypeId() != IS_A).collect(Collectors.toList());
/*
* Do the same as the above, but for CD members
*/
final LongKeyMap<Collection<ConcreteDomainFragment>> candidateMembers = PrimitiveMaps.newLongKeyOpenHashMap();
for (final LongIterator itr = parentIds.iterator(); itr.hasNext(); ) /* empty */
{
final long parentId = itr.next();
candidateMembers.put(parentId, concreteDomainCache.get(parentId));
}
final String referencedComponentId = Long.toString(conceptId);
final Collection<ConcreteDomainFragment> ownStatedMembers = reasonerTaxonomy.getStatedConcreteDomainMembers().get(referencedComponentId);
final Collection<ConcreteDomainFragment> ownAdditionalGroupedMembers = reasonerTaxonomy.getAdditionalGroupedConcreteDomainMembers().get(referencedComponentId);
candidateMembers.put(conceptId, ImmutableList.<ConcreteDomainFragment>builder().addAll(ownStatedMembers).addAll(ownAdditionalGroupedMembers).build());
final Collection<ConcreteDomainFragment> ownInferredMembers = reasonerTaxonomy.getInferredConcreteDomainMembers().get(referencedComponentId);
// Remove redundancy
final NormalFormGroupSet targetGroupSet = getTargetGroupSet(conceptId, parentIds, ownInferredNonIsaRelationships, ownInferredMembers, candidateNonIsARelationships, candidateMembers, useNodeGraphs);
// Extract results; place them in the cache, so following concepts can re-use it
statementCache.put(conceptId, ImmutableList.copyOf(relationshipsFromGroupSet(targetGroupSet)));
concreteDomainCache.put(conceptId, ImmutableList.copyOf(membersFromGroupSet(targetGroupSet)));
}
Aggregations