Skip to content

Commit

Permalink
#103 Rename variable index -> value reference
Browse files Browse the repository at this point in the history
  • Loading branch information
eidekrist committed Aug 27, 2019
1 parent d6680bb commit 0d55305
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 50 deletions.
66 changes: 33 additions & 33 deletions cse/cse.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,51 +146,51 @@ func executionSetCustomRealTimeFactor(execution *C.cse_execution, status *struct
return true, "Custom real time factor successfully set"
}

func setReal(manipulator *C.cse_manipulator, slaveIndex int, variableIndex int, value float64) (bool, string) {
vi := make([]C.cse_variable_index, 1)
vi[0] = C.cse_variable_index(variableIndex)
func setReal(manipulator *C.cse_manipulator, slaveIndex int, valueRef int, value float64) (bool, string) {
vr := make([]C.cse_value_reference, 1)
vr[0] = C.cse_value_reference(valueRef)
v := make([]C.double, 1)
v[0] = C.double(value)
success := C.cse_manipulator_slave_set_real(manipulator, C.cse_slave_index(slaveIndex), &vi[0], C.size_t(1), &v[0])
success := C.cse_manipulator_slave_set_real(manipulator, C.cse_slave_index(slaveIndex), &vr[0], C.size_t(1), &v[0])
if int(success) < 0 {
return false, strCat("Unable to set real variable value: ", lastErrorMessage())
} else {
return true, "Successfully set real variable value"
}
}

func setInteger(manipulator *C.cse_manipulator, slaveIndex int, variableIndex int, value int) (bool, string) {
vi := make([]C.cse_variable_index, 1)
vi[0] = C.cse_variable_index(variableIndex)
func setInteger(manipulator *C.cse_manipulator, slaveIndex int, valueRef int, value int) (bool, string) {
vr := make([]C.cse_value_reference, 1)
vr[0] = C.cse_value_reference(valueRef)
v := make([]C.int, 1)
v[0] = C.int(value)
success := C.cse_manipulator_slave_set_integer(manipulator, C.cse_slave_index(slaveIndex), &vi[0], C.size_t(1), &v[0])
success := C.cse_manipulator_slave_set_integer(manipulator, C.cse_slave_index(slaveIndex), &vr[0], C.size_t(1), &v[0])
if int(success) < 0 {
return false, strCat("Unable to set integer variable value: ", lastErrorMessage())
} else {
return true, "Successfully set integer variable value"
}
}

func setBoolean(manipulator *C.cse_manipulator, slaveIndex int, variableIndex int, value bool) (bool, string) {
vi := make([]C.cse_variable_index, 1)
vi[0] = C.cse_variable_index(variableIndex)
func setBoolean(manipulator *C.cse_manipulator, slaveIndex int, valueRef int, value bool) (bool, string) {
vr := make([]C.cse_value_reference, 1)
vr[0] = C.cse_value_reference(valueRef)
v := make([]C.bool, 1)
v[0] = C.bool(value)
success := C.cse_manipulator_slave_set_boolean(manipulator, C.cse_slave_index(slaveIndex), &vi[0], C.size_t(1), &v[0])
success := C.cse_manipulator_slave_set_boolean(manipulator, C.cse_slave_index(slaveIndex), &vr[0], C.size_t(1), &v[0])
if int(success) < 0 {
return false, strCat("Unable to set boolean variable value: ", lastErrorMessage())
} else {
return true, "Successfully set boolean variable value"
}
}

func setString(manipulator *C.cse_manipulator, slaveIndex int, variableIndex int, value string) (bool, string) {
vi := make([]C.cse_variable_index, 1)
vi[0] = C.cse_variable_index(variableIndex)
func setString(manipulator *C.cse_manipulator, slaveIndex int, valueRef int, value string) (bool, string) {
vr := make([]C.cse_value_reference, 1)
vr[0] = C.cse_value_reference(valueRef)
v := make([]*C.char, 1)
v[0] = C.CString(value)
success := C.cse_manipulator_slave_set_string(manipulator, C.cse_slave_index(slaveIndex), &vi[0], C.size_t(1), &v[0])
success := C.cse_manipulator_slave_set_string(manipulator, C.cse_slave_index(slaveIndex), &vr[0], C.size_t(1), &v[0])
if int(success) < 0 {
return false, strCat("Unable to set boolean variable value", lastErrorMessage())
} else {
Expand All @@ -203,7 +203,7 @@ func setVariableValue(sim *Simulation, slaveIndex string, valueType string, valu
if err != nil {
return false, strCat("Can't parse slave index as integer: ", slaveIndex)
}
varIndex, err := strconv.Atoi(valueReference)
valueRef, err := strconv.Atoi(valueReference)
if err != nil {
return false, strCat("Can't parse value reference as integer: ", valueReference)
}
Expand All @@ -214,24 +214,24 @@ func setVariableValue(sim *Simulation, slaveIndex string, valueType string, valu
log.Println(err)
return false, strCat("Can't parse value as double: ", value, ", error: ", err.Error())
} else {
return setReal(sim.OverrideManipulator, index, varIndex, val)
return setReal(sim.OverrideManipulator, index, valueRef, val)
}
case "Integer":
val, err := strconv.Atoi(value)
if err != nil {
log.Println(err)
return false, strCat("Can't parse value as integer: ", value, ", error: ", err.Error())
} else {
return setInteger(sim.OverrideManipulator, index, varIndex, val)
return setInteger(sim.OverrideManipulator, index, valueRef, val)
}
case "Boolean":
var val = false
if "true" == value {
val = true
}
return setBoolean(sim.OverrideManipulator, index, varIndex, val)
return setBoolean(sim.OverrideManipulator, index, valueRef, val)
case "String":
return setString(sim.OverrideManipulator, index, varIndex, value)
return setString(sim.OverrideManipulator, index, valueRef, value)

default:
message := strCat("Can't set this value: ", value)
Expand All @@ -240,10 +240,10 @@ func setVariableValue(sim *Simulation, slaveIndex string, valueType string, valu
}
}

func resetVariable(manipulator *C.cse_manipulator, slaveIndex int, variableType C.cse_variable_type, variableIndex int) (bool, string) {
vi := make([]C.cse_variable_index, 1)
vi[0] = C.cse_variable_index(variableIndex)
success := C.cse_manipulator_slave_reset(manipulator, C.cse_slave_index(slaveIndex), variableType, &vi[0], C.size_t(1))
func resetVariable(manipulator *C.cse_manipulator, slaveIndex int, variableType C.cse_variable_type, valueRef int) (bool, string) {
vr := make([]C.cse_value_reference, 1)
vr[0] = C.cse_value_reference(valueRef)
success := C.cse_manipulator_slave_reset(manipulator, C.cse_slave_index(slaveIndex), variableType, &vr[0], C.size_t(1))
if int(success) < 0 {
return false, strCat("Unable to reset variable value: ", lastErrorMessage())
} else {
Expand All @@ -256,19 +256,19 @@ func resetVariableValue(sim *Simulation, slaveIndex string, valueType string, va
if err != nil {
return false, strCat("Can't parse slave index as integer: ", slaveIndex)
}
varIndex, err := strconv.Atoi(valueReference)
valueRef, err := strconv.Atoi(valueReference)
if err != nil {
return false, strCat("Can't parse value reference as integer: ", valueReference)
}
switch valueType {
case "Real":
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_REAL, varIndex)
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_REAL, valueRef)
case "Integer":
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_INTEGER, varIndex)
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_INTEGER, valueRef)
case "Boolean":
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_BOOLEAN, varIndex)
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_BOOLEAN, valueRef)
case "String":
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_STRING, varIndex)
return resetVariable(sim.OverrideManipulator, index, C.CSE_VARIABLE_TYPE_STRING, valueRef)
default:
message := strCat("Can't reset variable with type ", valueType, " and value reference ", valueReference, " for slave with index ", slaveIndex)
log.Println(message)
Expand Down Expand Up @@ -340,7 +340,7 @@ func addVariableMetadata(execution *C.cse_execution, fmu *structs.FMU) error {
}
for _, variable := range variables[0:int(nVariablesRead)] {
name := C.GoString(&variable.name[0])
index := int(variable.index)
ref := int(variable.reference)
causality, err := parseCausality(variable.causality)
if err != nil {
return errors.New(strCat("Problem parsing causality for slave ", fmu.Name, ", variable ", name))
Expand All @@ -355,7 +355,7 @@ func addVariableMetadata(execution *C.cse_execution, fmu *structs.FMU) error {
}
fmu.Variables = append(fmu.Variables, structs.Variable{
name,
index,
ref,
causality,
variability,
valueType,
Expand All @@ -381,7 +381,7 @@ func fetchManipulatedVariables(execution *C.cse_execution) []structs.Manipulated
var varStructs = make([]structs.ManipulatedVariable, numVars)
for n, variable := range variables[0:numVars] {
slaveIndex := int(variable.slave_index)
valueReference := int(variable.variable_index)
valueReference := int(variable.value_reference)
variableType, err := parseType(variable._type)

if err != nil {
Expand Down
34 changes: 17 additions & 17 deletions cse/observers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func observerDestroy(observer *C.cse_observer) {
}

func observerGetReals(observer *C.cse_observer, variables []structs.Variable, slaveIndex int) (realSignals []structs.Signal) {
var realValueRefs []C.cse_variable_index
var realValueRefs []C.cse_value_reference
var realVariables []structs.Variable
var numReals int
for _, variable := range variables {
if variable.Type == "Real" {
ref := C.cse_variable_index(variable.ValueReference)
ref := C.cse_value_reference(variable.ValueReference)
realValueRefs = append(realValueRefs, ref)
realVariables = append(realVariables, variable)
numReals++
Expand All @@ -70,12 +70,12 @@ func observerGetReals(observer *C.cse_observer, variables []structs.Variable, sl
}

func observerGetIntegers(observer *C.cse_observer, variables []structs.Variable, slaveIndex int) (intSignals []structs.Signal) {
var intValueRefs []C.cse_variable_index
var intValueRefs []C.cse_value_reference
var intVariables []structs.Variable
var numIntegers int
for _, variable := range variables {
if variable.Type == "Integer" {
ref := C.cse_variable_index(variable.ValueReference)
ref := C.cse_value_reference(variable.ValueReference)
intValueRefs = append(intValueRefs, ref)
intVariables = append(intVariables, variable)
numIntegers++
Expand All @@ -100,12 +100,12 @@ func observerGetIntegers(observer *C.cse_observer, variables []structs.Variable,
}

func observerGetBooleans(observer *C.cse_observer, variables []structs.Variable, slaveIndex int) (boolSignals []structs.Signal) {
var boolValueRefs []C.cse_variable_index
var boolValueRefs []C.cse_value_reference
var boolVariables []structs.Variable
var numBooleans int
for _, variable := range variables {
if variable.Type == "Boolean" {
ref := C.cse_variable_index(variable.ValueReference)
ref := C.cse_value_reference(variable.ValueReference)
boolValueRefs = append(boolValueRefs, ref)
boolVariables = append(boolVariables, variable)
numBooleans++
Expand All @@ -130,12 +130,12 @@ func observerGetBooleans(observer *C.cse_observer, variables []structs.Variable,
}

func observerGetStrings(observer *C.cse_observer, variables []structs.Variable, slaveIndex int) (stringSignals []structs.Signal) {
var stringValueRefs []C.cse_variable_index
var stringValueRefs []C.cse_value_reference
var stringVariables []structs.Variable
var numStrings int
for _, variable := range variables {
if variable.Type == "String" {
ref := C.cse_variable_index(variable.ValueReference)
ref := C.cse_value_reference(variable.ValueReference)
stringValueRefs = append(stringValueRefs, ref)
stringVariables = append(stringVariables, variable)
numStrings++
Expand All @@ -161,7 +161,7 @@ func observerGetStrings(observer *C.cse_observer, variables []structs.Variable,

func observerGetRealSamples(observer *C.cse_observer, signal *structs.TrendSignal, spec structs.TrendSpec) {
slaveIndex := C.cse_slave_index(signal.SlaveIndex)
variableIndex := C.cse_variable_index(signal.ValueReference)
valueRef := C.cse_value_reference(signal.ValueReference)

stepNumbers := make([]C.cse_step_number, 2)
var success C.int
Expand All @@ -184,7 +184,7 @@ func observerGetRealSamples(observer *C.cse_observer, signal *structs.TrendSigna
realOutVal := make([]C.double, numSamples)
timeVal := make([]C.cse_time_point, numSamples)
timeStamps := make([]C.cse_step_number, numSamples)
actualNumSamples := C.cse_observer_slave_get_real_samples(observer, slaveIndex, variableIndex, first, cnSamples, &realOutVal[0], &timeStamps[0], &timeVal[0])
actualNumSamples := C.cse_observer_slave_get_real_samples(observer, slaveIndex, valueRef, first, cnSamples, &realOutVal[0], &timeStamps[0], &timeVal[0])
ns := int(actualNumSamples)
if ns <= 0 {
return
Expand All @@ -201,10 +201,10 @@ func observerGetRealSamples(observer *C.cse_observer, signal *structs.TrendSigna

func observerGetRealSynchronizedSamples(observer *C.cse_observer, signal1 *structs.TrendSignal, signal2 *structs.TrendSignal, spec structs.TrendSpec) {
slaveIndex1 := C.cse_slave_index(signal1.SlaveIndex)
variableIndex1 := C.cse_variable_index(signal1.ValueReference)
valueRef1 := C.cse_value_reference(signal1.ValueReference)

slaveIndex2 := C.cse_slave_index(signal2.SlaveIndex)
variableIndex2 := C.cse_variable_index(signal2.ValueReference)
valueRef2 := C.cse_value_reference(signal2.ValueReference)

stepNumbers := make([]C.cse_step_number, 2)
var success C.int
Expand All @@ -226,7 +226,7 @@ func observerGetRealSynchronizedSamples(observer *C.cse_observer, signal1 *struc
cnSamples := C.size_t(numSamples)
realOutVal1 := make([]C.double, numSamples)
realOutVal2 := make([]C.double, numSamples)
actualNumSamples := C.cse_observer_slave_get_real_synchronized_series(observer, slaveIndex1, variableIndex1, slaveIndex2, variableIndex2, first, cnSamples, &realOutVal1[0], &realOutVal2[0])
actualNumSamples := C.cse_observer_slave_get_real_synchronized_series(observer, slaveIndex1, valueRef1, slaveIndex2, valueRef2, first, cnSamples, &realOutVal1[0], &realOutVal2[0])
ns := int(actualNumSamples)
if ns <= 0 {
return
Expand Down Expand Up @@ -255,20 +255,20 @@ func toVariableType(valueType string) (C.cse_variable_type, error) {
return C.CSE_VARIABLE_TYPE_REAL, errors.New(strCat("Unknown variable type:", valueType))
}

func observerStartObserving(observer *C.cse_observer, slaveIndex int, valueType string, varIndex int) error {
func observerStartObserving(observer *C.cse_observer, slaveIndex int, valueType string, valueRef int) error {
variableType, err := toVariableType(valueType)
if err != nil {
return err
}
C.cse_observer_start_observing(observer, C.cse_slave_index(slaveIndex), variableType, C.cse_variable_index(varIndex))
C.cse_observer_start_observing(observer, C.cse_slave_index(slaveIndex), variableType, C.cse_value_reference(valueRef))
return nil
}

func observerStopObserving(observer *C.cse_observer, slaveIndex int, valueType string, varIndex int) error {
func observerStopObserving(observer *C.cse_observer, slaveIndex int, valueType string, valueRef int) error {
variableType, err := toVariableType(valueType)
if err != nil {
return err
}
C.cse_observer_stop_observing(observer, C.cse_slave_index(slaveIndex), variableType, C.cse_variable_index(varIndex))
C.cse_observer_stop_observing(observer, C.cse_slave_index(slaveIndex), variableType, C.cse_value_reference(valueRef))
return nil
}

0 comments on commit 0d55305

Please sign in to comment.