Search in sources :

Example 1 with DownstreamLink

use of org.openlca.ilcd.models.DownstreamLink in project olca-modules by GreenDelta.

the class Graph method buildLinks.

private static void buildLinks(Graph g, Model model) {
    // also support such things here...
    for (ProcessInstance pi : model.info.technology.processes) {
        Node refNode = g.getNode(pi.id);
        if (refNode == null)
            continue;
        for (Connection con : pi.connections) {
            Exchange output = refNode.findOutput(con.outputFlow);
            Exchange input = null;
            if (output == null) {
                input = refNode.findInput(con.outputFlow);
                if (input == null)
                    continue;
            }
            for (DownstreamLink dlink : con.downstreamLinks) {
                Node provider = null;
                Node recipient = null;
                if (output != null) {
                    provider = refNode;
                    recipient = g.getNode(dlink.process);
                    if (recipient == null)
                        continue;
                    input = recipient.findInput(dlink.inputFlow);
                } else if (input != null) {
                    recipient = refNode;
                    provider = g.getNode(dlink.process);
                    if (provider == null)
                        continue;
                    output = provider.findOutput(dlink.inputFlow);
                }
                if (input == null || output == null)
                    continue;
                Link link = new Link();
                link.provider = provider;
                link.output = output;
                link.recipient = recipient;
                link.input = input;
                g.putLink(link);
            }
        }
    }
}
Also used : Exchange(org.openlca.core.model.Exchange) Connection(org.openlca.ilcd.models.Connection) ProcessInstance(org.openlca.ilcd.models.ProcessInstance) DownstreamLink(org.openlca.ilcd.models.DownstreamLink) DownstreamLink(org.openlca.ilcd.models.DownstreamLink)

Example 2 with DownstreamLink

use of org.openlca.ilcd.models.DownstreamLink in project olca-modules by GreenDelta.

the class ModelImport method collectFlows.

/**
 * Collect the flows that are used in the process links. This function must
 * be called after all processes are imported.
 */
private Map<String, Flow> collectFlows(Technology tech) {
    Set<String> usedFlows = new HashSet<>();
    for (ProcessInstance pi : tech.processes) {
        for (Connection con : pi.connections) {
            usedFlows.add(con.outputFlow);
            for (DownstreamLink link : con.downstreamLinks) {
                usedFlows.add(link.inputFlow);
            }
        }
    }
    FlowDao dao = new FlowDao(config.db());
    Map<String, Flow> m = new HashMap<>();
    for (Flow f : dao.getForRefIds(usedFlows)) {
        m.put(f.refId, f);
    }
    return m;
}
Also used : HashMap(java.util.HashMap) FlowDao(org.openlca.core.database.FlowDao) Connection(org.openlca.ilcd.models.Connection) ProcessInstance(org.openlca.ilcd.models.ProcessInstance) DownstreamLink(org.openlca.ilcd.models.DownstreamLink) HashSet(java.util.HashSet) Flow(org.openlca.core.model.Flow)

Example 3 with DownstreamLink

use of org.openlca.ilcd.models.DownstreamLink in project olca-modules by GreenDelta.

the class ModelIOTest method testSimpleModel.

@Test
public void testSimpleModel() throws Exception {
    Model model = new Model();
    Models.forceDataSetInfo(model).uuid = UUID.randomUUID().toString();
    Models.forceModelName(model).name.add(LangString.of("Example model", "en"));
    Models.forcePublication(model).version = "01.00.000";
    Classification classification = new Classification();
    Category category = new Category();
    category.level = 0;
    category.value = "Life cycle models";
    classification.categories.add(category);
    Models.forceClassifications(model).add(classification);
    Models.forceQuantitativeReference(model).refProcess = 42;
    Technology tech = Models.forceTechnology(model);
    Group group = new Group();
    group.id = 42;
    group.name.add(LangString.of("Use phase", "en"));
    tech.groups.add(group);
    ProcessInstance pi = new ProcessInstance();
    tech.processes.add(pi);
    GroupRef groupRef = new GroupRef();
    groupRef.groupID = 42;
    pi.groupRefs.add(groupRef);
    Parameter param = new Parameter();
    param.name = "distance";
    param.value = 42.42;
    pi.parameters.add(param);
    Connection con = new Connection();
    con.outputFlow = UUID.randomUUID().toString();
    DownstreamLink link = new DownstreamLink();
    link.inputFlow = UUID.randomUUID().toString();
    link.process = 42;
    con.downstreamLinks.add(link);
    pi.connections.add(con);
    StringWriter writer = new StringWriter();
    XmlBinder binder = new XmlBinder();
    binder.toWriter(model, writer);
    StringReader reader = new StringReader(writer.toString());
    model = JAXB.unmarshal(reader, Model.class);
    assertEquals(1, model.info.technology.processes.size());
}
Also used : Group(org.openlca.ilcd.models.Group) Category(org.openlca.ilcd.commons.Category) Connection(org.openlca.ilcd.models.Connection) DownstreamLink(org.openlca.ilcd.models.DownstreamLink) StringWriter(java.io.StringWriter) Technology(org.openlca.ilcd.models.Technology) Classification(org.openlca.ilcd.commons.Classification) Model(org.openlca.ilcd.models.Model) StringReader(java.io.StringReader) Parameter(org.openlca.ilcd.models.Parameter) ProcessInstance(org.openlca.ilcd.models.ProcessInstance) GroupRef(org.openlca.ilcd.models.GroupRef) Test(org.junit.Test)

Example 4 with DownstreamLink

use of org.openlca.ilcd.models.DownstreamLink in project olca-modules by GreenDelta.

the class SystemExport method addLink.

private void addLink(ProcessInstance pi, ProcessLink link, FlowDescriptor flow) {
    if (pi == null || link == null || flow == null)
        return;
    Connection con = null;
    for (Connection c : pi.connections) {
        if (Objects.equals(c.outputFlow, flow.refId)) {
            con = c;
            break;
        }
    }
    if (con == null) {
        con = new Connection();
        con.outputFlow = flow.refId;
        pi.connections.add(con);
    }
    DownstreamLink dl = new DownstreamLink();
    dl.inputFlow = flow.refId;
    dl.linkedExchange = exchangeIDs.get(link.exchangeId);
    long linkProcess = 0L;
    if (flow.flowType == FlowType.PRODUCT_FLOW) {
        linkProcess = link.processId;
    } else if (flow.flowType == FlowType.WASTE_FLOW) {
        linkProcess = link.providerId;
    }
    dl.process = processIDs.getOrDefault(linkProcess, -1);
    if (dl.process != -1) {
        con.downstreamLinks.add(dl);
    }
}
Also used : Connection(org.openlca.ilcd.models.Connection) DownstreamLink(org.openlca.ilcd.models.DownstreamLink)

Example 5 with DownstreamLink

use of org.openlca.ilcd.models.DownstreamLink in project olca-modules by GreenDelta.

the class ModelImport method mapModel.

private void mapModel(Model m) {
    Technology tech = Models.getTechnology(m);
    if (tech == null)
        return;
    Map<Integer, Process> processes = insertProcesses(m, tech);
    Map<String, Flow> flows = collectFlows(tech);
    for (ProcessInstance pi : tech.processes) {
        Process out = processes.get(pi.id);
        if (out == null)
            continue;
        for (Connection con : pi.connections) {
            Flow outFlow = flows.get(con.outputFlow);
            if (outFlow == null)
                continue;
            for (DownstreamLink link : con.downstreamLinks) {
                Flow inFlow = flows.get(link.inputFlow);
                Process in = processes.get(link.process);
                if (inFlow == null || in == null)
                    continue;
                if (Objects.equals(inFlow, outFlow)) {
                    addLink(out, in, inFlow, link.linkedExchange);
                } else {
                    Process connector = connector(inFlow, outFlow);
                    addLink(out, connector, outFlow, null);
                    addLink(connector, in, inFlow, null);
                }
            }
        }
    }
}
Also used : Technology(org.openlca.ilcd.models.Technology) Connection(org.openlca.ilcd.models.Connection) Process(org.openlca.core.model.Process) ProcessInstance(org.openlca.ilcd.models.ProcessInstance) DownstreamLink(org.openlca.ilcd.models.DownstreamLink) Flow(org.openlca.core.model.Flow)

Aggregations

Connection (org.openlca.ilcd.models.Connection)5 DownstreamLink (org.openlca.ilcd.models.DownstreamLink)5 ProcessInstance (org.openlca.ilcd.models.ProcessInstance)4 Flow (org.openlca.core.model.Flow)2 Technology (org.openlca.ilcd.models.Technology)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 FlowDao (org.openlca.core.database.FlowDao)1 Exchange (org.openlca.core.model.Exchange)1 Process (org.openlca.core.model.Process)1 Category (org.openlca.ilcd.commons.Category)1 Classification (org.openlca.ilcd.commons.Classification)1 Group (org.openlca.ilcd.models.Group)1 GroupRef (org.openlca.ilcd.models.GroupRef)1 Model (org.openlca.ilcd.models.Model)1 Parameter (org.openlca.ilcd.models.Parameter)1