Search in sources :

Example 1 with ConceptTreeChild

use of com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild in project conquery by bakdata.

the class FrontEndConceptBuilder method createCTRoot.

private static FENode createCTRoot(Concept<?> c, StructureNode[] structureNodes) {
    MatchingStats matchingStats = c.getMatchingStats();
    StructureNodeId structureParent = Arrays.stream(structureNodes).filter(sn -> sn.getContainedRoots().contains(c.getId())).findAny().map(StructureNode::getId).orElse(null);
    FENode n = FENode.builder().active(true).description(c.getDescription()).label(c.getLabel()).additionalInfos(c.getAdditionalInfos()).matchingEntries(matchingStats.countEvents()).matchingEntities(matchingStats.countEntities()).dateRange(matchingStats.spanEvents() != null ? matchingStats.spanEvents().toSimpleRange() : null).detailsAvailable(Boolean.TRUE).codeListResolvable(c.countElements() > 1).parent(structureParent).selects(c.getSelects().stream().map(FrontEndConceptBuilder::createSelect).collect(Collectors.toList())).tables(c.getConnectors().stream().map(FrontEndConceptBuilder::createTable).collect(Collectors.toList())).build();
    if (c instanceof ConceptTreeNode) {
        ConceptTreeNode<?> tree = (ConceptTreeNode<?>) c;
        if (tree.getChildren() != null) {
            n.setChildren(tree.getChildren().stream().map(ConceptTreeChild::getId).toArray(ConceptTreeChildId[]::new));
        }
    }
    return n;
}
Also used : ConceptTreeChild(com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild) Arrays(java.util.Arrays) IdentifiableImpl(com.bakdata.conquery.models.identifiable.IdentifiableImpl) Subject(com.bakdata.conquery.models.auth.entities.Subject) ArrayUtils(org.apache.commons.lang3.ArrayUtils) ConceptTreeNode(com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeNode) IId(com.bakdata.conquery.models.identifiable.ids.IId) ArrayList(java.util.ArrayList) FEValidityDate(com.bakdata.conquery.apiv1.frontend.FEValidityDate) NamespaceStorage(com.bakdata.conquery.io.storage.NamespaceStorage) Select(com.bakdata.conquery.models.datasets.concepts.select.Select) FERoot(com.bakdata.conquery.apiv1.frontend.FERoot) StructureNodeId(com.bakdata.conquery.models.identifiable.ids.specific.StructureNodeId) Map(java.util.Map) ConceptId(com.bakdata.conquery.models.identifiable.ids.specific.ConceptId) Nullable(javax.annotation.Nullable) Identifiable(com.bakdata.conquery.models.identifiable.Identifiable) ConceptConfigurationException(com.bakdata.conquery.models.exceptions.ConceptConfigurationException) FEValue(com.bakdata.conquery.apiv1.frontend.FEValue) FESecondaryId(com.bakdata.conquery.apiv1.frontend.FESecondaryId) Collectors(java.util.stream.Collectors) FEList(com.bakdata.conquery.apiv1.frontend.FEList) Objects(java.util.Objects) List(java.util.List) Filter(com.bakdata.conquery.models.datasets.concepts.filters.Filter) Slf4j(lombok.extern.slf4j.Slf4j) FETable(com.bakdata.conquery.apiv1.frontend.FETable) FENode(com.bakdata.conquery.apiv1.frontend.FENode) Ability(com.bakdata.conquery.models.auth.permissions.Ability) FEFilter(com.bakdata.conquery.apiv1.frontend.FEFilter) ConceptTreeChildId(com.bakdata.conquery.models.identifiable.ids.specific.ConceptTreeChildId) AllArgsConstructor(lombok.AllArgsConstructor) FESelect(com.bakdata.conquery.apiv1.frontend.FESelect) Column(com.bakdata.conquery.models.datasets.Column) ConceptTreeNode(com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeNode) FENode(com.bakdata.conquery.apiv1.frontend.FENode) ConceptTreeChild(com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild) ConceptTreeChildId(com.bakdata.conquery.models.identifiable.ids.specific.ConceptTreeChildId) StructureNodeId(com.bakdata.conquery.models.identifiable.ids.specific.StructureNodeId)

Example 2 with ConceptTreeChild

use of com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild in project conquery by bakdata.

the class ConceptsProcessor method resolveConceptElements.

public ResolvedConceptsResult resolveConceptElements(TreeConcept concept, List<String> conceptCodes) {
    List<ConceptElementId<?>> resolvedCodes = new ArrayList<>();
    List<String> unknownCodes = new ArrayList<>();
    if (concept == null) {
        return new ResolvedConceptsResult(null, null, conceptCodes);
    }
    for (String conceptCode : conceptCodes) {
        ConceptTreeChild child;
        try {
            child = concept.findMostSpecificChild(conceptCode, new CalculatedValue<>(Collections::emptyMap));
            if (child != null) {
                resolvedCodes.add(child.getId());
            } else {
                unknownCodes.add(conceptCode);
            }
        } catch (ConceptConfigurationException e) {
            log.error("Error while trying to resolve " + conceptCode, e);
        }
    }
    return new ResolvedConceptsResult(resolvedCodes, null, unknownCodes);
}
Also used : ConceptElementId(com.bakdata.conquery.models.identifiable.ids.specific.ConceptElementId) ArrayList(java.util.ArrayList) ConceptTreeChild(com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild) CalculatedValue(com.bakdata.conquery.util.CalculatedValue) ToString(lombok.ToString) Collections(java.util.Collections) ConceptConfigurationException(com.bakdata.conquery.models.exceptions.ConceptConfigurationException)

Example 3 with ConceptTreeChild

use of com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild in project conquery by bakdata.

the class CBlock method calculateSpecificChildrenPaths.

/**
 * Calculates the path for each event from the root of the {@link TreeConcept} to the most specific {@link ConceptTreeChild}
 * denoted by the individual {@link ConceptTreeChild#getPrefix()}.
 */
private static int[][] calculateSpecificChildrenPaths(Bucket bucket, ConceptTreeConnector connector) {
    final Column column = connector.getColumn();
    final TreeConcept treeConcept = connector.getConcept();
    final StringStore stringStore;
    // If we have a column and it is of string-type, we create indices and caches.
    if (column != null && bucket.getStores()[column.getPosition()] instanceof StringStore) {
        stringStore = (StringStore) bucket.getStores()[column.getPosition()];
        // Create index and insert into Tree.
        TreeChildPrefixIndex.putIndexInto(treeConcept);
        treeConcept.initializeIdCache(stringStore, bucket.getImp());
    } else // No column only possible if we have just one tree element!
    if (treeConcept.countElements() == 1) {
        stringStore = null;
    } else {
        throw new IllegalStateException(String.format("Cannot build tree over Connector[%s] without Column", connector.getId()));
    }
    final int[][] mostSpecificChildren = new int[bucket.getNumberOfEvents()][];
    Arrays.fill(mostSpecificChildren, ConceptTreeConnector.NOT_CONTAINED);
    final ConceptTreeCache cache = treeConcept.getCache(bucket.getImp());
    final int[] root = treeConcept.getPrefix();
    for (int event = 0; event < bucket.getNumberOfEvents(); event++) {
        try {
            // Events can also be filtered, allowing a single table to be used by multiple connectors.
            if (column != null && !bucket.has(event, column)) {
                mostSpecificChildren[event] = Connector.NOT_CONTAINED;
                continue;
            }
            String stringValue = "";
            int valueIndex = -1;
            if (stringStore != null) {
                valueIndex = bucket.getString(event, column);
                stringValue = stringStore.getElement(valueIndex);
            }
            // Lazy evaluation of map to avoid allocations if possible.
            // Copy event for closure.
            final int _event = event;
            final CalculatedValue<Map<String, Object>> rowMap = new CalculatedValue<>(() -> bucket.calculateMap(_event));
            if ((connector.getCondition() != null && !connector.getCondition().matches(stringValue, rowMap))) {
                mostSpecificChildren[event] = Connector.NOT_CONTAINED;
                continue;
            }
            ConceptTreeChild child = cache == null ? treeConcept.findMostSpecificChild(stringValue, rowMap) : cache.findMostSpecificChild(valueIndex, stringValue, rowMap);
            // All unresolved elements resolve to the root.
            if (child == null) {
                mostSpecificChildren[event] = root;
                continue;
            }
            // put path into event
            mostSpecificChildren[event] = child.getPrefix();
        } catch (ConceptConfigurationException ex) {
            log.error("Failed to resolve event {}-{} against concept {}", bucket, event, treeConcept, ex);
        }
    }
    if (cache != null) {
        log.trace("Hits: {}, Misses: {}, Hits/Misses: {}, %Hits: {} (Up to now)", cache.getHits(), cache.getMisses(), (double) cache.getHits() / cache.getMisses(), (double) cache.getHits() / (cache.getHits() + cache.getMisses()));
    }
    return mostSpecificChildren;
}
Also used : ConceptTreeCache(com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeCache) ConceptTreeChild(com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild) StringStore(com.bakdata.conquery.models.events.stores.root.StringStore) ConceptConfigurationException(com.bakdata.conquery.models.exceptions.ConceptConfigurationException) Column(com.bakdata.conquery.models.datasets.Column) TreeConcept(com.bakdata.conquery.models.datasets.concepts.tree.TreeConcept) CalculatedValue(com.bakdata.conquery.util.CalculatedValue) Map(java.util.Map)

Aggregations

ConceptTreeChild (com.bakdata.conquery.models.datasets.concepts.tree.ConceptTreeChild)3 ConceptConfigurationException (com.bakdata.conquery.models.exceptions.ConceptConfigurationException)3 Column (com.bakdata.conquery.models.datasets.Column)2 CalculatedValue (com.bakdata.conquery.util.CalculatedValue)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 FEFilter (com.bakdata.conquery.apiv1.frontend.FEFilter)1 FEList (com.bakdata.conquery.apiv1.frontend.FEList)1 FENode (com.bakdata.conquery.apiv1.frontend.FENode)1 FERoot (com.bakdata.conquery.apiv1.frontend.FERoot)1 FESecondaryId (com.bakdata.conquery.apiv1.frontend.FESecondaryId)1 FESelect (com.bakdata.conquery.apiv1.frontend.FESelect)1 FETable (com.bakdata.conquery.apiv1.frontend.FETable)1 FEValidityDate (com.bakdata.conquery.apiv1.frontend.FEValidityDate)1 FEValue (com.bakdata.conquery.apiv1.frontend.FEValue)1 NamespaceStorage (com.bakdata.conquery.io.storage.NamespaceStorage)1 Subject (com.bakdata.conquery.models.auth.entities.Subject)1 Ability (com.bakdata.conquery.models.auth.permissions.Ability)1 Filter (com.bakdata.conquery.models.datasets.concepts.filters.Filter)1 Select (com.bakdata.conquery.models.datasets.concepts.select.Select)1