This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

WaterObjects questions

My task is to add demand data from the SQL server to WaterGEMS model using WaterObjects.NET.

I have the DataTable with 2 columns: Customer Label and Demand. I need to programmatically create a child scenario and add (or update) an alternative demand from DataTable to Customers in a model mapped by Label.
From the very laconic WaterObjects.NET programming guide and the examples it’s very difficult to understand how to perform this task.

Let’s start with creating the scenario: to add a Domain element:

IDomainElementManager domainElementManager = dataSet.DomainElementManager(domainElementType.Id);

but how to get domainElementType.Id for child alternative(scenario) ?

How to find DomainElement (Customer) by Field (Label)?

I would be very grateful for the help

  • Hi Otonas,

    Data for scenario's is stored in alternatives. A scenario is basically a fixed set of alternatives (active topology, physical, demand, etc). For your tooling to work, you would first need to copy the demands from your DataTable to a new demand alternative. The coding for a new demand alternative can be found in the Loadbuilder example included with the progreamming guide. It uses a LoadBuildNodeResult object which was produced earlier by the loadbuilder tool, you should change the references to this object to your datatable instead. Also, in the foreach loop you should retrieve the customer watergems id based on its label (I dont have the coding for that I'm afraid, I usually only use watergems id's, but I think there should be a function/easy way to do this).

    #code#

    String alternativeName = "ExampleAlternativeName";

    int newAlternativeId = 0;
    IAlternative newAlternative = null;

    newAlternativeId = (ActiveDomainDataSet.AlternativeManager((int)AlternativeType.DemandAlternative)).Add();
    newAlternative = (IAlternative)((ActiveDomainDataSet.AlternativeManager((int)AlternativeType.DemandAlternative)).Element(newAlternativeId));
    newAlternative.Label = alternativeName;

    IDomainElementManager demandNodeManager = ActiveDomainDataSet.DomainElementManager(StandardDomainElementTypeName.IdahoDemandNode);

    ICrossElementFieldListManager crossElementDemandManager = demandNodeManager.CrossElementFieldListManager
    (StandardFieldName.DemandCollection, StandardAlternativeName.Demand, newAlternativeId, new SortContextCollection(), new FilterContextCollection());

    IEditField baseFlowField = (IEditField)(crossElementDemandManager.Field(StandardFieldName.DemandCollection_BaseFlow));
    ((IUnitizedField)baseFlowField).WorkingUnitIndex = m_result.FlowUnit;

    foreach (LoadBuildNodeResult anoderesult in m_result.NodeResults)
    {
    int aintNodeId = anoderesult.NodeID;
    if (newAlternative.Manager.DomainDataSet.Exists(aintNodeId))
    {
    int demandIndex = -1;
    foreach (LoadBuildDemandResultPair aresultLoad in anoderesult.DemandResults)
    {
    if (aresultLoad != null)
    {
    crossElementDemandManager.ActiveElementID = anoderesult.NodeID;
    demandIndex = crossElementDemandManager.Add();
    baseFlowField.SetValue(demandIndex, aresultLoad.Demand);
    }
    }
    }
    }

    #endcode#

    I've got no experience creating new scenario's and change it's alternatives, but you might give this a try (I have not checked this in Visual Studio):

    #code#

    int newscenarioid = ActiveDomainDataSet.ScenarioManager.Add();
    IScenario childscenario = (IScenario)ActiveDomainDataSet.ScenarioManager.Element(newscenarioid);
    childscenario.ParentID = 2; #assign the correct parent id to the new scenario

    childscenario.Label = "new scenario label";

    childscenario.AlternativeID((int)AlternativeType.DemandAlternative, newAlternativeId); #change the demand alternative of the scenario to the previously created alternative 

    #endcode#

  • Hi Joeri, thank you very much for your help. I will try all this :)

  • Hi again, I'm trying the first step - create a scenario. Your code works like a charm, scenario is created.

    But I'm stuck in change the demand alternative of the scenario to the previously created alternative : newAlternativeId is equal to childscenario.Id (or newsscenarioid) ?

    It gives me an error: foreign key constraint failed

    #code

    childscenario.AlternativeID((int)AlternativeType.DemandAlternative, childscenario.Id);

    #endcode

  • Sorry, I'm beginning to understand, that once I've created a scenario I'll still need to create an alternative ...

  • Yeah, you can create the child scenario before you create the alternative but you'll need to create the new alternative before you can assign it to the child scenario (similar to the steps you would take in WaterGEMS itself visually)

    Also, in the code you replied, change "childscenario.Id" to "NewAlternativeId", you already specify of which scenario you want to change its demand alternative by starting the line of code with "childscenario.". The IDs required within the "AlternativeID()" brackets are the AlternativeTypeID (in this case the type is the typid of the demand alternative) and the actual new AlternativeID (the ID of the specific alternative which contains your new demands).