Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

13장 calculation과 constraint

12장의 expression은 쓰는 자리에 직접 적는 식이었습니다. 체공 시간을 구하는 식이 드론의 사양, 임무 분석, 요구사항 검증에 모두 필요하다면 같은 식을 세 번 적게 되고, 식을 고칠 때 한 곳을 빠뜨릴 수 있습니다. 식에 이름을 붙여 한 번만 정의하면 이 문제가 없어집니다. calculation은 값을 내는 식을, constraint는 참이나 거짓을 내는 조건을 재사용할 수 있는 단위로 정의합니다. constraint는 14장의 requirement를 이루는 재료이기도 합니다.

calculation definition

calculation definition은 입력을 받아 결과 하나를 내는 계산을 정의합니다. 키워드는 calc def입니다.

calc def FlightTime {
    in capacity : EnergyValue;
    in averagePower : PowerValue;
    return : DurationValue;

    capacity / averagePower
}

본문은 세 부분으로 이루어집니다.

부분표기설명
입력 parameterin capacity : EnergyValue;direction이 in인 usage입니다.
result parameterreturn : DurationValue;결과의 type을 정합니다. 이름은 생략할 수 있습니다.
result expressioncapacity / averagePower결과를 내는 식입니다. 본문의 마지막에 세미콜론 없이 적습니다.

result expression을 따로 적지 않고 result parameter에 값을 묶어도 됩니다. 두 선언은 뜻이 같습니다.

calc def FlightTime {
    in capacity : EnergyValue;
    in averagePower : PowerValue;
    return : DurationValue = capacity / averagePower;
}

계산이 길면 본문에 usage를 선언해 중간 결과를 담습니다. 다음 Range는 체공 시간을 먼저 구하고 거기에 순항 속력을 곱합니다.

calc def Range {
    in capacity : EnergyValue;
    in averagePower : PowerValue;
    in cruiseSpeed : SpeedValue;
    return : LengthValue;

    attribute flightTime : DurationValue = FlightTime(capacity, averagePower);
    flightTime * cruiseSpeed
}

FlightTime(capacity, averagePower)는 12장에서 본 function 호출입니다. calculation definition은 function이므로 Kernel Function Library의 function과 같은 방식으로 호출합니다.

수학 함수처럼 쓰는 calculation은 같은 입력에 언제나 같은 결과를 내야 하고, 계산 바깥의 무엇도 바꾸지 않아야 합니다. 그러려면 입력을 attribute로 받거나, part나 item을 받더라도 호출할 때마다 달라질 수 있는 feature에 기대지 않아야 합니다.

calculation을 쓰는 두 가지 방법

첫째는 expression 안에서 호출하는 것입니다.

part def Drone {
    attribute hoverPower : PowerValue;
    attribute cruiseSpeed : SpeedValue;
    part battery : Battery;

    attribute endurance : DurationValue = FlightTime(battery.capacity, hoverPower);
    attribute range : LengthValue =
        Range(capacity = battery.capacity, averagePower = hoverPower, cruiseSpeed = cruiseSpeed);
}

배터리 용량이 600 Wh이고 hoverPower가 1200 W이면 endurance는 0.5 h, 곧 30분입니다. 순항 속력이 15 m/s이면 range는 27 km입니다.

둘째는 calculation usage를 선언하는 것입니다. 키워드는 calc이고, 본문에서 입력 parameter에 값을 묶습니다.

part def Drone {
    attribute hoverPower : PowerValue;
    part battery : Battery;

    calc flightTime : FlightTime {
        in capacity = battery.capacity;
        in averagePower = hoverPower;
    }

    attribute endurance : DurationValue = flightTime.result;
}

flightTime은 Drone의 feature가 된 계산입니다. 결과는 flightTime.result로 가리킵니다. result parameter에 이름을 주지 않으면 그 이름은 library가 정한 result입니다.

방법맞는 경우
expression 안에서 호출결과 값만 필요할 때
calculation usage계산 자체에 이름을 붙여 다른 곳에서 가리키거나 재정의해야 할 때

Drone을 특수화한 definition이 flightTime을 재정의해 다른 계산식으로 바꿀 수 있고, 20장의 analysis case가 이 계산을 가리킬 수 있습니다. calculation usage는 action usage의 한 가지이기도 해서, 16장에서 볼 action의 단계로 쓸 수 있습니다.

constraint

constraint definition은 참이나 거짓으로 평가되는 조건을 정의합니다. 선언하는 방법은 calculation definition과 같지만 result parameter를 적지 않습니다. 결과가 언제나 Boolean이기 때문입니다.

constraint def MassLimit {
    in mass : MassValue;
    in limit : MassValue;

    mass <= limit
}

constraint usage는 parameter에 값을 묶어, 조건을 특정한 대상에 적용합니다.

part def Drone {
    attribute emptyMass : MassValue;
    attribute maxPayload : MassValue;
    attribute maxTakeoffMass : MassValue = emptyMass + maxPayload;

    constraint takeoffMassLimit : MassLimit {
        in mass = maxTakeoffMass;
        in limit = 9 [kg];
    }
}

한 곳에서만 쓰는 조건은 definition 없이 적습니다. 본문의 식은 constraint usage가 놓인 자리의 feature를 바로 가리킬 수 있습니다.

constraint enoughEndurance { endurance >= 20 [min] }

constraint usage는 조건을 적어 둔 것일 뿐, 그 조건이 성립한다고 주장하지는 않습니다. 식이 참으로 평가되면 constraint가 충족되었다고 하고, 그렇지 않으면 위반되었다고 합니다. 같은 constraint가 어떤 시점에는 충족되고 다른 시점에는 위반될 수 있습니다. 위 takeoffMassLimit은 emptyMass가 5.2 kg이고 maxPayload가 2.5 kg인 드론에서 7.7 kg과 9 kg을 견주어 충족됩니다.

assert: 조건이 성립한다고 주장하기

조건이 언제나 성립해야 한다고 모델에 적으려면 assert constraint를 씁니다.

part def Battery {
    attribute chargeLevel : Real;

    assert constraint chargeRange { chargeLevel >= 0.0 and chargeLevel <= 1.0 }
}

assert constraint usage가 위반되면 모델이 스스로 모순된 것입니다. 잔량이 1.2인 배터리는 이 모델이 허용하지 않는 대상이며, 조건을 평가하는 도구는 이런 위반을 찾아냅니다.

반대로 조건이 언제나 거짓이어야 한다고 주장하려면 assert not constraint를 씁니다. 이미 선언한 constraint usage를 주장할 때에는 assert 뒤에 그 이름을 적습니다.

part def Drone {
    attribute maxPayload : MassValue;
    attribute maxTakeoffMass : MassValue;

    constraint takeoffMassLimit : MassLimit {
        in mass = maxTakeoffMass;
        in limit = 9 [kg];
    }

    assert takeoffMassLimit;
    assert not constraint { maxPayload > maxTakeoffMass }
}

조건을 모델에 적는 방법은 그 조건의 성격에 따라 고릅니다.

조건의 성격적는 방법
평가해서 결과를 보려는 조건constraint
물리 법칙이나 정의처럼 어길 수 없는 조건, 모델 전체가 전제로 삼는 조건assert constraint
설계가 달성해야 하는 조건requirement의 require constraint (14장)

10장의 interface definition에서 양쪽 port의 전압이 같다고 한 것과, 11장에서 variant의 조합을 제한한 것이 assert constraint의 예입니다. 최대 이륙 질량이 9 kg 이하여야 한다는 조건은 설계가 달성해야 하는 목표이므로, 예제 모델에서는 14장의 requirement로 적습니다.

KerML에서 보면

calculation과 constraint는 12장에서 본 function과 expression에 SysML의 이름을 붙인 것입니다.

SysMLKerML기준 library element
calculation definition (CalculationDefinition)FunctionCalculations::Calculation
calculation usage (CalculationUsage)ExpressionCalculations::calculations
constraint definition (ConstraintDefinition)PredicateConstraints::ConstraintCheck
constraint usage (ConstraintUsage)BooleanExpressionConstraints::constraintChecks
assert constraint usage (AssertConstraintUsage)InvariantConstraints::assertedConstraintChecks, 부정이면 Constraints::negatedConstraintChecks

predicate는 결과가 Boolean 하나인 function이고, boolean expression은 predicate를 type으로 갖는 expression입니다. invariant는 언제나 참이라고, 또는 언제나 거짓이라고 주장하는 boolean expression입니다. KerML 표기법에서는 function과 expression을 function과 expr로, predicate와 boolean expression을 predicate와 bool로, invariant를 inv로 선언합니다.

predicate MassLimit {
    in mass : Real;
    in limit : Real;

    mass <= limit
}

struct Battery {
    feature chargeLevel : Real;

    inv chargeRange { chargeLevel >= 0.0 and chargeLevel <= 1.0 }
}

assert not constraint에 해당하는 KerML 표기는 inv false { … }입니다.

SysML의 두 definition은 metaclass 계층에서 놓인 자리가 다릅니다. CalculationDefinition은 ActionDefinition과 Function을 함께 특수화하므로 calculation은 action입니다. ConstraintDefinition은 OccurrenceDefinition과 Predicate를 특수화하며 action definition은 아닙니다. 그래서 calculation usage는 action 안에서 순서를 주어 수행할 수 있고, constraint usage는 그렇게 쓸 수 없습니다. 둘 다 KerML의 function이므로 expression 안에서 호출하는 것은 양쪽 모두 가능합니다. MassLimit(maxTakeoffMass, 9 [kg])은 Boolean 값을 내는 expression입니다.

구문 요약

구문뜻
calc def C { in x : T; return : R; 식 }calculation definition입니다.
return : R = 식;result parameter에 값을 묶습니다.
C(a, b), C(x = a, y = b)calculation을 expression 안에서 호출합니다.
calc c : C { in x = a; }calculation usage입니다. 결과는 c.result입니다.
constraint def K { in x : T; 조건 }constraint definition입니다.
constraint k : K { in x = a; }constraint usage입니다.
constraint { 조건 }definition과 이름이 없는 constraint usage입니다.
assert constraint { 조건 }조건이 언제나 참이라고 주장합니다.
assert not constraint { 조건 }조건이 언제나 거짓이라고 주장합니다.
assert k;이미 선언한 constraint usage k를 주장합니다.

명세에서 찾아보기

주제조항
calculation definition과 usageSysML 7.19, 8.3.19, 8.4.15
constraint definition과 usageSysML 7.20.1, 7.20.2, 8.3.20, 8.4.16
assert constraint usageSysML 7.20.3
Calculations, Constraints librarySysML 9.2.12, 9.2.13
function, predicate, invariantKerML 7.4.8