Search in sources :

Example 1 with Category

use of org.openlca.core.model.Category in project olca-modules by GreenDelta.

the class CategoryDao method getForPath.

public Category getForPath(ModelType type, String path) {
    if (type == null || path == null)
        return null;
    // first try via the refId
    var refId = KeyGen.get(path);
    var withRefId = getForRefId(refId);
    if (withRefId != null)
        return withRefId;
    // traverse the tree
    var parts = path.split("/");
    var next = getRootCategories(type);
    Category category = null;
    for (var p : parts) {
        var part = p.trim();
        if (part.isEmpty())
            continue;
        category = null;
        for (var c : next) {
            if (c.name == null)
                continue;
            if (c.name.trim().equalsIgnoreCase(part)) {
                category = c;
                next = c.childCategories;
                break;
            }
        }
        if (category == null)
            return null;
    }
    return category;
}
Also used : Category(org.openlca.core.model.Category)

Example 2 with Category

use of org.openlca.core.model.Category in project olca-modules by GreenDelta.

the class CategoryDao method sync.

/**
 * Creates the categories for the segments of the given path that do not yet
 * exist and returns the category of the last segment. If the given path is
 * empty or null, null is returned.
 */
public Category sync(ModelType type, String... path) {
    if (type == null || path == null || path.length == 0)
        return null;
    Category parent = null;
    List<Category> next = getRootCategories(type);
    for (var segment : path) {
        if (Strings.nullOrEmpty(segment))
            continue;
        segment = segment.trim();
        if (segment.isEmpty())
            continue;
        Category category = null;
        for (Category c : next) {
            if (segment.equalsIgnoreCase(c.name)) {
                category = c;
                break;
            }
        }
        if (category != null) {
            parent = category;
            next = category.childCategories;
            continue;
        }
        category = new Category();
        category.name = segment;
        category.lastChange = new Date().getTime();
        category.modelType = type;
        category.version = Version.valueOf(0, 0, 1);
        category.category = parent;
        category.refId = Categories.createRefId(category);
        if (parent == null) {
            category = insert(category);
        } else {
            parent.childCategories.add(category);
            parent = update(parent);
            // need to find the category now that is in sync with JPA
            for (Category child : parent.childCategories) {
                if (Objects.equals(child.refId, category.refId)) {
                    category = child;
                    break;
                }
            }
        }
        parent = category;
        next = category.childCategories;
    }
    return parent;
}
Also used : Category(org.openlca.core.model.Category) Date(java.util.Date)

Example 3 with Category

use of org.openlca.core.model.Category in project olca-modules by GreenDelta.

the class CategoryDao method mergeChildren.

private void mergeChildren(Category into, Category from) {
    for (Category child : from.childCategories) {
        if (contains(into.childCategories, child))
            continue;
        child.category = into;
        into.childCategories.add(child);
    }
}
Also used : Category(org.openlca.core.model.Category)

Example 4 with Category

use of org.openlca.core.model.Category in project olca-modules by GreenDelta.

the class ImpactMethodReferenceSearchTest method createModel.

@Override
protected ImpactMethod createModel() {
    ImpactMethod method = new ImpactMethod();
    method.category = insertAndAddExpected("category", new Category());
    String n1 = generateName();
    // String n2 = generateName();
    String n3 = generateName();
    String n4 = generateName();
    String n5 = generateName();
    // method.parameters.add(createParameter(n1, 3d, false));
    // method.parameters.add(createParameter(n2, n1 + "*2*" + n3, false));
    insertAndAddExpected(n3, createParameter(n3, "5*5", true));
    // formula with parameter to see if added as reference (unexpected)
    insertAndAddExpected(n4, createParameter(n4, "3*" + n5, true));
    Parameter globalUnreferenced = createParameter(n1, "3*3", true);
    Parameter globalUnreferenced2 = createParameter(n5, "3*3", true);
    // must be inserted manually
    globalUnreferenced = db.insert(globalUnreferenced);
    globalUnreferenced2 = db.insert(globalUnreferenced2);
    method.impactCategories.add(createImpactCategory(n4));
    method.impactCategories.add(createImpactCategory(n4));
    method = db.insert(method);
    for (ImpactCategory category : method.impactCategories) for (ImpactFactor f : category.impactFactors) {
        addExpected("flow", f.flow, "impactFactors", ImpactFactor.class, f.id);
        addExpected("flowPropertyFactor", f.flowPropertyFactor, "impactFactors", ImpactFactor.class, f.id);
        addExpected("flowProperty", f.flowPropertyFactor.flowProperty, "flowPropertyFactor", FlowPropertyFactor.class, f.flowPropertyFactor.id);
        addExpected("unit", f.unit, "impactFactors", ImpactFactor.class, f.id);
    }
    return method;
}
Also used : ImpactFactor(org.openlca.core.model.ImpactFactor) ImpactCategory(org.openlca.core.model.ImpactCategory) Category(org.openlca.core.model.Category) Parameter(org.openlca.core.model.Parameter) ImpactCategory(org.openlca.core.model.ImpactCategory) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) ImpactMethod(org.openlca.core.model.ImpactMethod)

Example 5 with Category

use of org.openlca.core.model.Category in project olca-modules by GreenDelta.

the class ProcessReferenceSearchTest method createModel.

@Override
protected Process createModel() {
    Process process = new Process();
    process.category = insertAndAddExpected("category", new Category());
    process.location = insertAndAddExpected("location", new Location());
    process.dqSystem = insertAndAddExpected("dqSystem", new DQSystem());
    process.exchangeDqSystem = insertAndAddExpected("exchangeDqSystem", new DQSystem());
    process.socialDqSystem = insertAndAddExpected("socialDqSystem", new DQSystem());
    String n1 = generateName();
    String n2 = generateName();
    String n3 = generateName();
    String n4 = generateName();
    String n5 = generateName();
    createExchange(process, 3d, true);
    createExchange(process, "2*" + n4, false);
    process.parameters.add(createParameter(n1, 3d, false));
    process.parameters.add(createParameter(n2, n1 + "*2*" + n3, false));
    process.socialAspects.add(createSocialAspect());
    process.socialAspects.add(createSocialAspect());
    process.documentation = createDocumentation();
    insertAndAddExpected(n3, createParameter(n3, "5*5", true));
    // formula with parameter to see if added as reference (unexpected)
    insertAndAddExpected(n4, createParameter(n4, "3*" + n5, true));
    Parameter globalUnreferenced = createParameter(n1, "3*3", true);
    Parameter globalUnreferenced2 = createParameter(n5, "3*3", true);
    // must be inserted manually
    globalUnreferenced = db.insert(globalUnreferenced);
    globalUnreferenced2 = db.insert(globalUnreferenced2);
    process = db.insert(process);
    for (Exchange e : process.exchanges) {
        addExpected("flow", e.flow, "exchanges", Exchange.class, e.id);
        addExpected("flowPropertyFactor", e.flowPropertyFactor, "exchanges", Exchange.class, e.id);
        addExpected("flowProperty", e.flowPropertyFactor.flowProperty, "flowPropertyFactor", FlowPropertyFactor.class, e.flowPropertyFactor.id);
        addExpected("unit", e.unit, "exchanges", Exchange.class, e.id);
        Process provider = processes.get(e.defaultProviderId);
        if (provider != null)
            addExpected("defaultProviderId", provider, "exchanges", Exchange.class, e.id);
    }
    for (SocialAspect a : process.socialAspects) {
        addExpected("indicator", a.indicator, "socialAspects", SocialAspect.class, a.id);
        addExpected("source", a.source, "socialAspects", SocialAspect.class, a.id);
    }
    ProcessDocumentation doc = process.documentation;
    addExpected("dataDocumentor", doc.dataDocumentor, "documentation", ProcessDocumentation.class, doc.id);
    addExpected("dataGenerator", doc.dataGenerator, "documentation", ProcessDocumentation.class, doc.id);
    addExpected("dataSetOwner", doc.dataSetOwner, "documentation", ProcessDocumentation.class, doc.id);
    addExpected("reviewer", doc.reviewer, "documentation", ProcessDocumentation.class, doc.id);
    addExpected("publication", doc.publication, "documentation", ProcessDocumentation.class, doc.id);
    for (Source s : process.documentation.sources) addExpected("sources", s, "documentation", ProcessDocumentation.class, doc.id);
    return process;
}
Also used : Exchange(org.openlca.core.model.Exchange) Category(org.openlca.core.model.Category) SocialAspect(org.openlca.core.model.SocialAspect) DQSystem(org.openlca.core.model.DQSystem) Parameter(org.openlca.core.model.Parameter) Process(org.openlca.core.model.Process) ProcessDocumentation(org.openlca.core.model.ProcessDocumentation) Source(org.openlca.core.model.Source) Location(org.openlca.core.model.Location)

Aggregations

Category (org.openlca.core.model.Category)77 Test (org.junit.Test)11 CategoryDao (org.openlca.core.database.CategoryDao)11 Flow (org.openlca.core.model.Flow)6 Parameter (org.openlca.core.model.Parameter)6 CategoryElement (org.openlca.app.navigation.elements.CategoryElement)4 ModelElement (org.openlca.app.navigation.elements.ModelElement)4 IDatabase (org.openlca.core.database.IDatabase)4 FlowProperty (org.openlca.core.model.FlowProperty)4 Location (org.openlca.core.model.Location)4 Process (org.openlca.core.model.Process)4 UnitGroup (org.openlca.core.model.UnitGroup)4 ArrayList (java.util.ArrayList)3 ImpactCategory (org.openlca.core.model.ImpactCategory)3 ImpactMethod (org.openlca.core.model.ImpactMethod)3 ModelType (org.openlca.core.model.ModelType)3 IsicNode (org.openlca.io.ecospold2.input.IsicTree.IsicNode)3 Actor (org.openlca.core.model.Actor)2 Currency (org.openlca.core.model.Currency)2 DQSystem (org.openlca.core.model.DQSystem)2