All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.cambio.cds.util.ElementInstanceCollection Maven / Gradle / Ivy

There is a newer version: 1.3.26
Show newest version
package se.cambio.cds.util;

import org.openehr.rm.datatypes.basic.DataValue;
import org.openehr.rm.datatypes.text.DvCodedText;
import org.slf4j.LoggerFactory;
import se.cambio.cds.controller.guide.GuideManager;
import se.cambio.cds.gdl.model.Guide;
import se.cambio.cds.model.facade.execution.vo.*;
import se.cambio.cds.model.instance.ArchetypeReference;
import se.cambio.cds.model.instance.ElementInstance;

import java.util.*;

public class ElementInstanceCollection {

    public static final String EMPTY_CODE = "*EMPTY*";
    private Map>>> archetypeReferenceMap = null;
    private ElementInstanceCollectionManager elementInstanceCollectionManager;

    public ElementInstanceCollection(ElementInstanceCollectionManager elementInstanceCollectionManager) {
        this.elementInstanceCollectionManager = elementInstanceCollectionManager;
    }

    public void add(ElementInstance elementInstance) {
        add(elementInstance.getArchetypeReference(), null, null);
    }

    public void add(ArchetypeReference archetypeReferenceToAdd) {
        add(archetypeReferenceToAdd, null, null);
    }

    public void add(ArchetypeReference archetypeReferenceToAdd, GuideManager guideManager, Calendar date) {
        if (archetypeReferenceToAdd instanceof GeneratedArchetypeReference) {
            //Clone the AR
            ArchetypeReference arAux = archetypeReferenceToAdd.clone();
            for (String idElement : archetypeReferenceToAdd.getElementInstancesMap().keySet()) {
                ElementInstance originalEI = archetypeReferenceToAdd.getElementInstancesMap().get(idElement);
                if (originalEI instanceof PredicateGeneratedElementInstance) {
                    PredicateGeneratedElementInstance predicateOriginalEI =
                            (PredicateGeneratedElementInstance) originalEI;
                    //Its a predicate, so keep it as a generated element instance, but resolve the data value
                    DataValue dv = originalEI.getDataValue();
                    if (guideManager != null) {
                        Collection guides = getGuides(guideManager, predicateOriginalEI, dv);
                        dv = ElementInstanceCollectionManager.resolvePredicate(dv, predicateOriginalEI.getOperatorKind(), guides, date);
                        //Might be null i.e. max(date/time) or path!=null
                    }
                    PredicateGeneratedElementInstance pgei =
                            new PredicateGeneratedElementInstanceBuilder()
                                    .setId(predicateOriginalEI.getId())
                                    .setDataValue(dv)
                                    .setArchetypeReference(arAux)
                                    .setOperatorKind(predicateOriginalEI.getOperatorKind())
                                    .createPredicateGeneratedElementInstance();
                    pgei.setRuleReferences(predicateOriginalEI.getRuleReferences());
                    //Should not allow value changes for generated elements in the future
                } else {
                    //Clone a new instance
                    /*
                    GeneratedElementInstance gei = new GeneratedElementInstance(
                            originalEI.getId(),
                            null, arAux,
                            null, null);
                            */
                    ElementInstance ei = originalEI.clone();
                    ei.setArchetypeReference(arAux);
                    if (originalEI instanceof GeneratedElementInstance) {
                        GeneratedElementInstance generatedElementInstance = (GeneratedElementInstance) originalEI;
                        ((GeneratedElementInstance) ei).setRuleReferences(generatedElementInstance.getRuleReferences());
                    }
                }
            }
            archetypeReferenceToAdd = arAux;
        }


        Set archetypeReferencesInCollection = getArchetypeReferences(archetypeReferenceToAdd);
        archetypeReferencesInCollection.add(archetypeReferenceToAdd);
    }

    public void addAll(Collection elementInstances) {
        Set archetypeReferences = new HashSet<>();
        for (ElementInstance elementInstance : elementInstances) {
            archetypeReferences.add(elementInstance.getArchetypeReference());
        }
        addAll(archetypeReferences, null);
    }

    public void addAll(Collection archetypeReferences, GuideManager guideManager) {
        for (ArchetypeReference archetypeReferenceToAdd : archetypeReferences) {
            add(archetypeReferenceToAdd, guideManager, null);
        }
    }

    private Collection getGuides(GuideManager guideManager, GeneratedElementInstance predicateOriginalEI, DataValue dv) {
        Collection guides = new ArrayList<>();
        Set guideIds = getReferenceGuideIds(dv, predicateOriginalEI.getRuleReferences());
        for (String guideId : guideIds) {
            Guide guide = guideManager.getGuide(guideId);
            if (guide == null) {
                LoggerFactory.getLogger(ElementInstanceCollectionManager.class).warn("Guideline not found resolving rule reference '" + guideId + "'");
            } else {
                guides.add(guide);
            }
        }
        return guides;
    }

    private Set getReferenceGuideIds(DataValue dv, Collection ruleReferences) {
        Set guideIds = new HashSet<>();
        if (dv instanceof DvCodedText) {
            DvCodedText dvCT = (DvCodedText) dv;
            for (RuleReference ruleReference : ruleReferences) {
                if (ruleReference.getGtCode().equals(dvCT.getCode())) {
                    guideIds.add(ruleReference.getGuideId());
                }
            }
        } else {
            for (RuleReference ruleReference : ruleReferences) {
                guideIds.add(ruleReference.getGuideId());
            }
        }
        return guideIds;
    }

    public void remove(ArchetypeReference archetypeReference) {
        getArchetypeReferences(archetypeReference).remove(archetypeReference);
    }

    public boolean matches(GeneratedArchetypeReference generatedArchetypeReference, Map guideMap, Calendar date) {
        boolean matches = false;
        Iterator iterator = getArchetypeReferences(generatedArchetypeReference).iterator();
        while (iterator.hasNext() && !matches) {
            ArchetypeReference ar = iterator.next();
            matches = elementInstanceCollectionManager.matchAndFill(generatedArchetypeReference, ar, guideMap, date);
        }
        return matches;
    }

    public Set getElementIdsByIdDomain(String idDomain) {
        Set idElements = new HashSet<>();
        Set elementInstances = getAllElementInstancesByDomain(idDomain);
        for (ElementInstance elementInstance : elementInstances) {
            idElements.add(elementInstance.getId());
        }
        return idElements;
    }

    public Collection getAllElementInstances() {
        Collection elementInstances = new ArrayList<>();
        for (ArchetypeReference ar : getAllArchetypeReferences()) {
            elementInstances.addAll(ar.getElementInstancesMap().values());
        }
        return elementInstances;
    }

    public Set getAllElementInstancesByDomain(String idDomain) {
        Set elementInstances = new HashSet<>();
        for (ArchetypeReference archetypeReference : getAllArchetypeReferencesByDomain(idDomain)) {
            elementInstances.addAll(archetypeReference.getElementInstancesMap().values());
        }
        return elementInstances;
    }

    public void merge(ElementInstanceCollection eic) {
        for (ArchetypeReference archetypeReference : eic.getAllArchetypeReferences()) {
            add(archetypeReference);
        }
    }

    public Set getArchetypeReferences(ArchetypeReference archetypeReference) {
        String idDomain = archetypeReference.getIdDomain() != null ? archetypeReference.getIdDomain() : EMPTY_CODE;
        String idAux = null;
        if (Domains.CDS_ID.equals(idDomain)) {
            idAux = archetypeReference.getIdTemplate();
        }
        if (idAux == null) {
            idAux = EMPTY_CODE;
        }
        return getArchetypeReferences(archetypeReference.getIdArchetype(), idDomain, idAux);
    }

    private Set getArchetypeReferences(String idArchetype, String idDomain, String idAux) {
        if (idAux == null) {
            LoggerFactory.getLogger(ElementInstanceCollection.class).warn("Call to getArchetypeReferences with idAux=='null'");
        }
        return getArchetypeReferenceMap(idArchetype, idDomain).computeIfAbsent(idAux, k -> new HashSet<>());
    }

    private Set getAllArchetypeReferences(String idArchetype) {
        Set archetypeReferences = new HashSet<>();
        for (String idDomain : getArchetypeReferenceMap(idArchetype).keySet()) {
            archetypeReferences.addAll(getAllArchetypeReferences(idArchetype, idDomain));
        }
        return archetypeReferences;
    }

    private Set getAllArchetypeReferences(String idArchetype, String idDomain) {
        Set archetypeReferences = new HashSet<>();
        Map> archetypeReferencesMap = getArchetypeReferenceMap(idArchetype, idDomain);
        for (Set idAux : archetypeReferencesMap.values()) {
            archetypeReferences.addAll(idAux);
        }
        return archetypeReferences;
    }

    public Set getAllArchetypeReferences() {
        Set archetypeReferences = new HashSet<>();
        for (String idArchetype : getArchetypeReferenceMap().keySet()) {
            archetypeReferences.addAll(getAllArchetypeReferences(idArchetype));
        }
        return archetypeReferences;
    }

    private Map> getArchetypeReferenceMap(String idArchetype, String idDomain) {
        if (idDomain == null) {
            LoggerFactory.getLogger(ElementInstanceCollection.class).warn("Call to getArchetypeReferenceMap with idDomain=='null'");
        }
        return getArchetypeReferenceMap(idArchetype).computeIfAbsent(idDomain, k -> new HashMap<>());
    }

    private Map>> getArchetypeReferenceMap(String idArchetype) {
        return getArchetypeReferenceMap().computeIfAbsent(idArchetype, k -> new HashMap<>());
    }

    private Map>>> getArchetypeReferenceMap() {
        if (archetypeReferenceMap == null) {
            archetypeReferenceMap = new HashMap<>();
        }
        return archetypeReferenceMap;
    }

    public Collection getAllArchetypeReferencesByDomain(String domainId) {
        Collection ars = new ArrayList<>();
        for (String idArchetype : getArchetypeReferenceMap().keySet()) {
            ars.addAll(getAllArchetypeReferences(idArchetype, domainId));
        }
        return ars;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("-----\n");
        for (String idArchetype : getArchetypeReferenceMap().keySet()) {
            sb.append(idArchetype).append("\n");
            for (String idDomain : getArchetypeReferenceMap(idArchetype).keySet()) {
                sb.append("-Domain=").append(idDomain).append("\n");
                for (String idAux : getArchetypeReferenceMap(idArchetype, idDomain).keySet()) {
                    sb.append("--idAux=").append(idAux).append("\n");
                    int index = 1;
                    for (ArchetypeReference ar : getArchetypeReferences(idArchetype, idDomain, idAux)) {
                        sb.append("---[").append(index).append("]\n");
                        for (String idElement : ar.getElementInstancesMap().keySet()) {
                            sb.append("----").append(idElement).append("");
                            ElementInstance ei = ar.getElementInstancesMap().get(idElement);
                            if (ei.getDataValue() != null) {
                                sb.append(" (").append(ei.getDataValue().toString()).append(")");
                            }
                            sb.append("\n");
                        }
                    }
                }
            }
        }
        sb.append("-----\n");
        return sb.toString();
    }
}
/*
 *  ***** BEGIN LICENSE BLOCK *****
 *  Version: MPL 2.0/GPL 2.0/LGPL 2.1
 *
 *  The contents of this file are subject to the Mozilla Public 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.mozilla.org/MPL/
 *
 *  Software distributed under the License is distributed on an 'AS IS' basis,
 *  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 *  for the specific language governing rights and limitations under the
 *  License.
 *
 *
 *  The Initial Developers of the Original Code are Iago Corbal and Rong Chen.
 *  Portions created by the Initial Developer are Copyright (C) 2012-2013
 *  the Initial Developer. All Rights Reserved.
 *
 *  Contributor(s):
 *
 * Software distributed under the License is distributed on an 'AS IS' basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 *  ***** END LICENSE BLOCK *****
 */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy