implement CN - Calculate Numeric method with operands that are Executables returning Attribute

This commit is contained in:
Michael Becker 2025-09-26 23:06:44 -04:00
parent 1682dcd542
commit bce1cf6437
5 changed files with 70 additions and 2 deletions

View File

@ -90,6 +90,8 @@ namespace Mocha.Core
public static Guid ExecutableReturningElement { get; } = new Guid("{a15a4f52-1f1a-4ef3-80a7-033d45cc0548}"); public static Guid ExecutableReturningElement { get; } = new Guid("{a15a4f52-1f1a-4ef3-80a7-033d45cc0548}");
public static Guid ExecutableReturningWorkData { get; } = new Guid("{a0365b76-ad1f-462e-84da-d6a1d5b9c88c}"); public static Guid ExecutableReturningWorkData { get; } = new Guid("{a0365b76-ad1f-462e-84da-d6a1d5b9c88c}");
public static Guid Calculation { get; } = new Guid("{ab4fdf54-b8b7-4b07-bdaf-2548bdb4c290}");
public static Guid Event { get; set; } = new Guid("{ca727ecd-8536-4aeb-9e75-352dbb958767}"); public static Guid Event { get; set; } = new Guid("{ca727ecd-8536-4aeb-9e75-352dbb958767}");
public static Guid WorkData { get; set; } = new Guid("{05e8f023-88cb-416b-913e-75299e665eb2}"); public static Guid WorkData { get; set; } = new Guid("{05e8f023-88cb-416b-913e-75299e665eb2}");

View File

@ -451,5 +451,8 @@ namespace Mocha.Core
public static Guid Numeric_Attribute__returned_by__Calculate_Numeric_Method { get; } = new Guid("{03275ef5-c9cf-4816-a4f5-4f3aaf0f9479}"); public static Guid Numeric_Attribute__returned_by__Calculate_Numeric_Method { get; } = new Guid("{03275ef5-c9cf-4816-a4f5-4f3aaf0f9479}");
public static Guid Arithmetic_Calculation__has__Arithmetic_Operator { get; } = new Guid("{f286edc6-0875-41bf-8fe9-127b8c0664ae}"); public static Guid Arithmetic_Calculation__has__Arithmetic_Operator { get; } = new Guid("{f286edc6-0875-41bf-8fe9-127b8c0664ae}");
public static Guid Arithmetic_Calculation__has_first_operand__Calculation { get; } = new Guid("{4f002495-dee4-4eca-8a15-beb8f928648d}");
public static Guid Arithmetic_Calculation__has_second_operand__Calculation { get; } = new Guid("{0d74918d-5e7b-4492-9fa4-e35558f40ef3}");
} }
} }

View File

@ -39,8 +39,30 @@ public class CalculateNumericMethodImplementation : MethodImplementation
throw new InvalidOperationException("no Arithmetic Operator specified for Arithmetic Calculation"); throw new InvalidOperationException("no Arithmetic Operator specified for Arithmetic Calculation");
} }
decimal primaryOperandValue = oms.GetAttributeValue<decimal>(method, oms.GetInstance(KnownAttributeGuids.Numeric.PrimaryOperandValue)); decimal primaryOperandValue = 0.0M;
decimal secondaryOperandValue = oms.GetAttributeValue<decimal>(method, oms.GetInstance(KnownAttributeGuids.Numeric.SecondaryOperandValue)); decimal secondaryOperandValue = 0.0M;
if (oms.TryGetAttributeValue<decimal>(method, oms.GetInstance(KnownAttributeGuids.Numeric.PrimaryOperandValue), out decimal v))
{
primaryOperandValue = v;
}
else
{
InstanceHandle hFirstOperand = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Arithmetic_Calculation__has_first_operand__Calculation));
InstanceHandle hResult = oms.Execute(context, hFirstOperand);
primaryOperandValue = context.GetWorkData<decimal>(hResult);
}
if (oms.TryGetAttributeValue<decimal>(method, oms.GetInstance(KnownAttributeGuids.Numeric.SecondaryOperandValue), out decimal v2))
{
secondaryOperandValue = v2;
}
else
{
InstanceHandle hSecondOperand = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Arithmetic_Calculation__has_second_operand__Calculation));
InstanceHandle hResult = oms.Execute(context, hSecondOperand);
secondaryOperandValue = context.GetWorkData<decimal>(hResult);
}
decimal value = 0.0M; decimal value = 0.0M;
if (arithmeticOperator.Equals(ArithmeticOperator.Add)) if (arithmeticOperator.Equals(ArithmeticOperator.Add))

View File

@ -391,4 +391,14 @@ public class OmsMethodBuilder
Oms.AssignRelationship(method, Oms.GetInstance(KnownRelationshipGuids.Calculate_Numeric_Method__returns__Numeric_Attribute), returnsAttribute.GetHandle()); Oms.AssignRelationship(method, Oms.GetInstance(KnownRelationshipGuids.Calculate_Numeric_Method__returns__Numeric_Attribute), returnsAttribute.GetHandle());
return new CalculateNumericMethod(method); return new CalculateNumericMethod(method);
} }
public CalculateNumericMethod CreateCalculateNumericMethod(InstanceHandle clasz, string verb, string name, AccessModifier accessModifier, bool isStatic, IInstanceReference returnsAttribute, IInstanceReference primaryOperand, ArithmeticOperator oper, decimal secondaryOperand)
{
InstanceHandle method = CreateMethodBase(Oms.GetInstance(KnownInstanceGuids.MethodClasses.CalculateNumericMethod), clasz, verb, name, accessModifier, isStatic);
Oms.AssignRelationship(method, Oms.GetInstance(KnownRelationshipGuids.Arithmetic_Calculation__has_first_operand__Calculation), primaryOperand);
Oms.AssignRelationship(method, Oms.GetInstance(KnownRelationshipGuids.Arithmetic_Calculation__has__Arithmetic_Operator), oper.GetHandle());
Oms.SetAttributeValue(method, Oms.GetInstance(KnownAttributeGuids.Numeric.SecondaryOperandValue), secondaryOperand);
Oms.AssignRelationship(method, Oms.GetInstance(KnownRelationshipGuids.Calculate_Numeric_Method__returns__Numeric_Attribute), returnsAttribute.GetHandle());
return new CalculateNumericMethod(method);
}
} }

View File

@ -161,6 +161,37 @@ namespace Mocha.Core.Tests.MethodTests
Assert.That(value, Is.EqualTo(test_result)); Assert.That(value, Is.EqualTo(test_result));
} }
[Test]
public void Add_GA_Plus_Literal_Number_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_value = 20.0M;
InstanceHandle irAttribute = Oms.GetInstance(KnownAttributeGuids.Numeric.MaximumLength);
Oms.SetAttributeValue(irTestClassInstance, irAttribute, test_value);
// `Test Class Instance`.`Maximum Length` is now 20.0M
decimal test_addition = 5.0M;
decimal test_result = test_value + test_addition;
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
GetAttributeMethod ih_GA = builder.CreateGetAttributeMethod(irTestClass, "get", "Maximum Length", irAttribute);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), ih_GA, ArithmeticOperator.Add, test_addition);
OmsContext context = Oms.CreateContext();
// below is REQUIRED, else we get "cannot call non-static method without instance reference"
context.SetWorkData(irTestClass, irTestClassInstance);
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
} }
} }