From bce1cf6437eb407ee463aff1806a0bfdad86093e Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Fri, 26 Sep 2025 23:06:44 -0400 Subject: [PATCH] implement CN - Calculate Numeric method with operands that are Executables returning Attribute --- .../src/lib/Mocha.Core/KnownInstanceGuids.cs | 2 ++ .../lib/Mocha.Core/KnownRelationshipGuids.cs | 3 ++ .../CalculateNumericMethodImplementation.cs | 26 ++++++++++++++-- .../src/lib/Mocha.Core/OmsMethodBuilder.cs | 10 ++++++ .../CalculateNumericMethodTests.cs | 31 +++++++++++++++++++ 5 files changed, 70 insertions(+), 2 deletions(-) diff --git a/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs b/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs index 6c3b7fd..40a8797 100755 --- a/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/KnownInstanceGuids.cs @@ -90,6 +90,8 @@ namespace Mocha.Core 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 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 WorkData { get; set; } = new Guid("{05e8f023-88cb-416b-913e-75299e665eb2}"); diff --git a/mocha-dotnet/src/lib/Mocha.Core/KnownRelationshipGuids.cs b/mocha-dotnet/src/lib/Mocha.Core/KnownRelationshipGuids.cs index f0b6d30..cf23e4d 100755 --- a/mocha-dotnet/src/lib/Mocha.Core/KnownRelationshipGuids.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/KnownRelationshipGuids.cs @@ -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 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}"); } } diff --git a/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/CalculateNumericMethodImplementation.cs b/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/CalculateNumericMethodImplementation.cs index 7e3edd4..4b4143c 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/CalculateNumericMethodImplementation.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/MethodImplementations/CalculateNumericMethodImplementation.cs @@ -39,8 +39,30 @@ public class CalculateNumericMethodImplementation : MethodImplementation throw new InvalidOperationException("no Arithmetic Operator specified for Arithmetic Calculation"); } - decimal primaryOperandValue = oms.GetAttributeValue(method, oms.GetInstance(KnownAttributeGuids.Numeric.PrimaryOperandValue)); - decimal secondaryOperandValue = oms.GetAttributeValue(method, oms.GetInstance(KnownAttributeGuids.Numeric.SecondaryOperandValue)); + decimal primaryOperandValue = 0.0M; + decimal secondaryOperandValue = 0.0M; + + if (oms.TryGetAttributeValue(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(hResult); + } + + if (oms.TryGetAttributeValue(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(hResult); + } decimal value = 0.0M; if (arithmeticOperator.Equals(ArithmeticOperator.Add)) diff --git a/mocha-dotnet/src/lib/Mocha.Core/OmsMethodBuilder.cs b/mocha-dotnet/src/lib/Mocha.Core/OmsMethodBuilder.cs index 2230bc4..194668d 100644 --- a/mocha-dotnet/src/lib/Mocha.Core/OmsMethodBuilder.cs +++ b/mocha-dotnet/src/lib/Mocha.Core/OmsMethodBuilder.cs @@ -391,4 +391,14 @@ public class OmsMethodBuilder Oms.AssignRelationship(method, Oms.GetInstance(KnownRelationshipGuids.Calculate_Numeric_Method__returns__Numeric_Attribute), returnsAttribute.GetHandle()); 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); + } } \ No newline at end of file diff --git a/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/CalculateNumericMethodTests.cs b/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/CalculateNumericMethodTests.cs index 40893b4..ae40e5a 100644 --- a/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/CalculateNumericMethodTests.cs +++ b/mocha-dotnet/tests/Mocha.Core.Tests/MethodTests/CalculateNumericMethodTests.cs @@ -161,6 +161,37 @@ namespace Mocha.Core.Tests.MethodTests 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)); + } } } \ No newline at end of file