Search in sources :

Example 1 with AbstractEntity

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

the class BaseReferenceSearchTest method testAllReferencesSingleModel.

@Test
public void testAllReferencesSingleModel() {
    AbstractEntity fullModel = createModel();
    List<Reference> references = findReferences(Collections.singleton(fullModel.id));
    Assert.assertNotNull(references);
    for (Reference ref : expectedReferences) {
        Reference reference = find(ref, references, fullModel.id);
        String text = ref.getType().getName() + " " + ref.id + " not found";
        Assert.assertNotNull(text, reference);
        references.remove(reference);
    }
    for (Reference r : references) {
        if (r.optional && r.id == 0)
            continue;
        Assert.fail("Unexpected: " + r.getType().getName() + " " + r.id);
    }
}
Also used : AbstractEntity(org.openlca.core.model.AbstractEntity) Test(org.junit.Test)

Example 2 with AbstractEntity

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

the class BaseParametrizedReferenceSearch method toReferences.

private List<Reference> toReferences(List<ParameterDescriptor> descriptors, boolean optional, Map<Long, Set<String>> names, Map<Long, Long> ownerIds, Class<? extends AbstractEntity> nestedType, String nestedProperty) {
    Map<Long, Set<Long>> descriptorToOwnerIds = new HashMap<>();
    for (ParameterDescriptor d : descriptors) {
        for (long ownerId : names.keySet()) {
            if (!names.get(ownerId).contains(d.name.toLowerCase()))
                continue;
            put(d.id, ownerId, descriptorToOwnerIds);
        }
    }
    List<Reference> references = new ArrayList<>();
    for (ParameterDescriptor descriptor : descriptors) {
        Set<Long> set = descriptorToOwnerIds.get(descriptor.id);
        if (set == null)
            continue;
        for (long ownerId : set) {
            Class<? extends AbstractEntity> type = (Class<? extends AbstractEntity>) descriptor.type.getModelClass();
            long id = descriptor.id;
            long nestedOwnerId = 0;
            if (ownerIds != null) {
                nestedOwnerId = ownerId;
                ownerId = ownerIds.get(ownerId);
            }
            Reference reference = new Reference(descriptor.name, type, id, this.type, ownerId, nestedProperty, nestedType, nestedOwnerId, optional);
            references.add(reference);
        }
    }
    return references;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ParameterDescriptor(org.openlca.core.model.descriptors.ParameterDescriptor) ArrayList(java.util.ArrayList) AbstractEntity(org.openlca.core.model.AbstractEntity)

Example 3 with AbstractEntity

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

the class BaseReferenceSearchTest method testAllReferencesMultipleModel.

@Test
public void testAllReferencesMultipleModel() {
    Set<Long> ids = new HashSet<>();
    for (int i = 0; i < 3; i++) {
        AbstractEntity fullModel = createModel();
        referencesByOwner.put(fullModel.id, expectedReferences);
        expectedReferences = new ArrayList<>();
        ids.add(fullModel.id);
    }
    List<Reference> references = findReferences(ids);
    Assert.assertNotNull(references);
    for (long id : ids) {
        List<Reference> refs = new ArrayList<>();
        for (Reference reference : references) if (!isNestedSearchTest() && reference.ownerId == id)
            refs.add(reference);
        else if (isNestedSearchTest() && reference.nestedOwnerId == id)
            refs.add(reference);
        for (Reference ref : referencesByOwner.get(id)) {
            Reference reference = find(ref, refs, id);
            String text = ref.getType().getName() + " " + ref.id + " not found";
            Assert.assertNotNull(text, reference);
            refs.remove(reference);
        }
        for (Reference r : refs) {
            if (r.optional && r.id == 0)
                continue;
            Assert.fail("Unexpected: " + r.getType().getName() + " " + r.id);
        }
    }
}
Also used : AbstractEntity(org.openlca.core.model.AbstractEntity) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AbstractEntity (org.openlca.core.model.AbstractEntity)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Set (java.util.Set)1 ParameterDescriptor (org.openlca.core.model.descriptors.ParameterDescriptor)1