implement CN - Calculate Numeric Method

This commit is contained in:
Michael Becker 2025-09-26 00:11:22 -04:00
parent 87ad532073
commit 70f5c03435
12 changed files with 360 additions and 15 deletions

@ -1 +1 @@
Subproject commit a99dd737a920919dcff856ec8319e72ad461e815
Subproject commit ce10750b3eafc2430bb52234592bf9937a0a7bfd

View File

@ -77,11 +77,18 @@ namespace Mocha.Core
return _ID == other._ID && !IsEmpty;
}
public bool Equals(IInstanceReference other)
{
if (IsEmpty && other.GetHandle().IsEmpty)
return true;
return _ID == other.GetHandle()._ID && !IsEmpty;
}
public override bool Equals([NotNullWhen(true)] object? obj)
{
if (obj is InstanceHandle)
if (obj is IInstanceReference)
{
return this.Equals(((InstanceHandle)obj));
return this.Equals(((IInstanceReference)obj));
}
return base.Equals(obj);
}

View File

@ -78,6 +78,11 @@ namespace Mocha.Core
public static Guid MinimumValue { get; } = new Guid("{bc90ffdf-9b6e-444a-a484-f9d06d7f3c31}");
public static Guid MaximumValue { get; } = new Guid("{b9353b1c-2597-4097-96eb-449a6fafcdab}");
public static Guid RandomNumber { get; } = new Guid("{1e18b839-5a53-495e-a240-8ec03b6b9c0d}");
public static Guid Value { get; } = new Guid("{4d60b06a-0aa3-4530-af67-805feeeaced6}");
public static Guid PrimaryOperandValue { get; } = new Guid("{2e6b3528-f68d-40f8-80f3-4e0c463c00a5}"); // {fe327566-7657-4227-94aa-66bbc1d1afce}
public static Guid SecondaryOperandValue { get; } = new Guid("{253a04bb-feb7-474d-b608-43219a753ef8}"); // {e666aa75-6b53-47ea-9afa-369378e17b5d}
}
}
}

View File

@ -240,7 +240,11 @@ namespace Mocha.Core
public static Guid GetReferencedInstanceSetMethod { get; } = new Guid("{bcfd0d64-3eba-4a97-9622-f3a960877d24}"); // 26
// BEM - Build Element Method - 29
public static Guid BuildAttributeMethod { get; } = new Guid("{e5879955-0093-48c8-8042-813168578af2}"); // 30
// CN - Calculate Numeric Method - 35
/// <summary>
/// CN - Calculate Numeric Method
/// </summary>
/// <returns></returns>
public static Guid CalculateNumericMethod { get; } = new Guid("{0f1ab8e8-c718-4842-a26c-989472141d61}"); // 35
/// <summary>
/// GSI - Get Specified Instances Method
/// Equivalent to YP: `static function $verb$name() : $returnType = $inst`
@ -468,6 +472,20 @@ namespace Mocha.Core
public static Guid GreaterThanOrEqualTo { get; } = new Guid("{3a4eb7be-f96b-45ac-b97a-bb8fbfc35923}");
public static Guid LessThanOrEqualTo { get; } = new Guid("{528e5637-f995-4a6d-93c3-f5e1e62b4517}");
}
public static class ArithmeticOperators
{
// {7124b817-188d-4ee7-8e81-af595ae4055e}
// {79565ea0-e181-445a-8d34-c8edc45786d4}
public static Guid Add { get; internal set; } = new Guid("{04a1cf5e-a38f-46ef-9e34-c619a41866fa}");
public static Guid Subtract { get; internal set; } = new Guid("{64ea1785-3a4d-4e20-b0f6-d90d189f9861}");
public static Guid Multiply { get; internal set; } = new Guid("{246dfaf3-4caf-409e-8f74-99cd730f6ae3}");
public static Guid Divide { get; internal set; } = new Guid("{abf2ce5e-c6c9-4789-8ebe-dab7d4973c55}");
public static Guid Power { get; internal set; } = new Guid("{ed47da30-c066-4230-8aff-2efd2e0176df}");
public static Guid Root { get; internal set; } = new Guid("{1773e5ff-29e5-445a-88cc-1ad9e56b4e29}");
public static Guid LShift { get; internal set; } = new Guid("{1f3e720a-53a9-4251-a412-484f6441e57b}");
public static Guid RShift { get; internal set; } = new Guid("{df716fc2-9d84-41fb-a853-7ac6e6f9d548}");
public static Guid Logarithm { get; internal set; } = new Guid("{f8c9ef0a-c7ce-4442-8b83-ee6bd545992e}");
}
public static class Languages
{
public static Guid English { get; } = new Guid ("{68BB6038-A4B5-4EE1-AAE9-326494942062}");
@ -500,6 +518,9 @@ namespace Mocha.Core
public static class WorkSets
{
public static Guid TaskRelatedInstance { get; } = new Guid("{d3860252-491e-469e-939e-702d56d88063}");
public static Guid AccessModifierNonsingular { get; } = new Guid("{686a3322-03a5-4da6-ab00-33548cc03b98}");
public static Guid ParentClass { get; } = new Guid("{98da8ac1-1fea-4cb4-a278-0c5460e625fd}");
public static Guid DestinationClassForRelationship { get; } = new Guid("{cc6bf015-ea36-4cc5-8855-5047095affee}");
}
}
}

View File

@ -446,5 +446,10 @@ namespace Mocha.Core
public static Guid Method_Binding__uses_super__Return_Instance_Set_Method_Binding { get; } = new Guid("{444279f1-3bf9-4d1f-848e-e7bf33fa0fd7}");
public static Guid Metadata_With_Access_Modifier__has__Access_Modifier { get; } = new Guid("{c8551fb1-6dc5-47cb-8b8f-204d49e6007e}");
public static Guid Calculate_Numeric_Method__returns__Numeric_Attribute { get; } = new Guid("{9f9a9341-f2d6-4152-8879-e91f574ca309}");
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}");
}
}

View File

@ -0,0 +1,85 @@
// Copyright (C) 2024 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
using System.Diagnostics;
using Microsoft.VisualBasic;
using Mocha.Core.Oop;
namespace Mocha.Core.MethodImplementations;
public class CalculateNumericMethodImplementation : MethodImplementation
{
public override Guid MethodClassGuid => KnownInstanceGuids.MethodClasses.CalculateNumericMethod;
protected override InstanceHandle ExecuteInternal(Oms oms, OmsContext context, InstanceHandle method)
{
InstanceHandle irForClass = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Method__for__Class));
InstanceHandle returnsAttribute = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Calculate_Numeric_Method__returns__Numeric_Attribute));
InstanceHandle arithmeticOperator = oms.GetRelatedInstance(method, oms.GetInstance(KnownRelationshipGuids.Arithmetic_Calculation__has__Arithmetic_Operator));
if (returnsAttribute == InstanceHandle.Empty)
{
throw new InvalidOperationException("no return Attribute specified for method");
}
if (arithmeticOperator == InstanceHandle.Empty)
{
throw new InvalidOperationException("no Arithmetic Operator specified for Arithmetic Calculation");
}
decimal primaryOperandValue = oms.GetAttributeValue<decimal>(method, oms.GetInstance(KnownAttributeGuids.Numeric.PrimaryOperandValue));
decimal secondaryOperandValue = oms.GetAttributeValue<decimal>(method, oms.GetInstance(KnownAttributeGuids.Numeric.SecondaryOperandValue));
decimal value = 0.0M;
if (arithmeticOperator.Equals(ArithmeticOperator.Add))
{
value = primaryOperandValue + secondaryOperandValue;
}
else if (arithmeticOperator.Equals(ArithmeticOperator.Subtract))
{
value = primaryOperandValue - secondaryOperandValue;
}
else if (arithmeticOperator.Equals(ArithmeticOperator.Multiply))
{
value = primaryOperandValue * secondaryOperandValue;
}
else if (arithmeticOperator.Equals(ArithmeticOperator.Divide))
{
value = primaryOperandValue / secondaryOperandValue;
}
else if (arithmeticOperator.Equals(ArithmeticOperator.Power))
{
value = (decimal)Math.Pow((double)primaryOperandValue, (double)secondaryOperandValue);
}
else if (arithmeticOperator.Equals(ArithmeticOperator.Root))
{
value = (decimal)Math.Pow((double)primaryOperandValue, (double)(1 / secondaryOperandValue));
}
else if (arithmeticOperator.Equals(ArithmeticOperator.LShift))
{
value = (int)primaryOperandValue << (int)secondaryOperandValue;
}
else if (arithmeticOperator.Equals(ArithmeticOperator.RShift))
{
value = (int)primaryOperandValue >> (int)secondaryOperandValue;
}
else if (arithmeticOperator.Equals(ArithmeticOperator.Logarithm))
{
value = (decimal) Math.Log((double)primaryOperandValue, (double)secondaryOperandValue);
}
context.SetWorkData(returnsAttribute, value);
return returnsAttribute;
}
}

View File

@ -265,6 +265,7 @@ public abstract class Oms
// !!! THIS IS PROBABLY NOT THREAD-SAFE. BE VERY VERY CAREFUL !!!
UpdateSyntacticSugar<AccessModifier>(typeof(KnownInstanceGuids.AccessModifiers));
UpdateSyntacticSugar<RelationalOperator>(typeof(KnownInstanceGuids.RelationalOperators));
UpdateSyntacticSugar<ArithmeticOperator>(typeof(KnownInstanceGuids.ArithmeticOperators));
}
}

View File

@ -381,4 +381,14 @@ public class OmsMethodBuilder
InstanceHandle ihCase = CreateConditionalSelectAttributeCase(cas);
Oms.AssignRelationship(method.GetHandle(), Oms.GetInstance(KnownRelationshipGuids.Conditional_Select_Attribute_Method__has__Conditional_Select_Attribute_Case), ihCase);
}
public CalculateNumericMethod CreateCalculateNumericMethod(InstanceHandle clasz, string verb, string name, AccessModifier accessModifier, bool isStatic, IInstanceReference returnsAttribute, decimal primaryOperand, ArithmeticOperator oper, decimal secondaryOperand)
{
InstanceHandle method = CreateMethodBase(Oms.GetInstance(KnownInstanceGuids.MethodClasses.CalculateNumericMethod), clasz, verb, name, accessModifier, isStatic);
Oms.SetAttributeValue(method, Oms.GetInstance(KnownAttributeGuids.Numeric.PrimaryOperandValue), 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

@ -22,4 +22,14 @@ public class ArithmeticOperator : ConcreteInstanceWrapper
{
public override Guid ClassId => KnownInstanceGuids.Classes.ArithmeticOperator;
internal ArithmeticOperator(InstanceHandle handle) : base(handle) { }
public static ArithmeticOperator Add { get; internal set; } = null;
public static ArithmeticOperator Subtract { get; internal set; } = null;
public static ArithmeticOperator Multiply { get; internal set; } = null;
public static ArithmeticOperator Divide { get; internal set; } = null;
public static ArithmeticOperator Power { get; internal set; } = null;
public static ArithmeticOperator Root { get; internal set; } = null;
public static ArithmeticOperator LShift { get; internal set; } = null;
public static ArithmeticOperator RShift { get; internal set; } = null;
public static ArithmeticOperator Logarithm { get; internal set; } = null;
}

View File

@ -26,6 +26,33 @@ public abstract class InstanceWrapper : IInstanceReference
{
return GetHandleInternal();
}
public static bool operator ==(InstanceWrapper left, IInstanceReference right)
{
if ((object)left == null && (object)right == null)
{
return true;
}
else if ((object)left == null || (object)right == null)
{
return false;
}
return left.GetHandle().Equals(right.GetHandle());
}
public static bool operator !=(InstanceWrapper left, IInstanceReference right)
{
if ((object)left == null && (object)right == null)
{
return false;
}
else if ((object)left == null || (object)right == null)
{
return true;
}
return !left.GetHandle().Equals(right.GetHandle());
}
}
public static class InstanceWrapperArrayExtensions
{

View File

@ -0,0 +1,8 @@
namespace Mocha.Core.Oop.Methods;
public class CalculateNumericMethod : Method
{
public override Guid ClassId => KnownInstanceGuids.MethodClasses.CalculateNumericMethod;
internal CalculateNumericMethod(InstanceHandle handle) : base(handle) { }
}

View File

@ -0,0 +1,166 @@
// Copyright (C) 2024 Michael Becker <alcexhim@gmail.com>
//
// This file is part of Mocha.NET.
//
// Mocha.NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Mocha.NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
using Mocha.Core.Oop;
using Mocha.Core.Oop.Methods;
namespace Mocha.Core.Tests.MethodTests
{
public class CalculateNumericMethodTests : MethodTestsBase
{
[Test]
public void Add_Two_Literal_Numbers_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_value = 1.0M;
decimal test_addition = 3.0M;
decimal test_result = test_value + test_addition;
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_value, ArithmeticOperator.Add, test_addition);
OmsContext context = Oms.CreateContext();
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
[Test]
public void Subtract_Two_Literal_Numbers_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_1 = 6.0M;
decimal test_2 = 4.0M;
decimal test_result = test_2 - test_1;
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_2, ArithmeticOperator.Subtract, test_1);
OmsContext context = Oms.CreateContext();
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
[Test]
public void Multiply_Two_Literal_Numbers_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_1 = 3.0M;
decimal test_2 = 9.0M;
decimal test_result = test_1 * test_2;
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Multiply, test_2);
OmsContext context = Oms.CreateContext();
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
[Test]
public void Divide_Two_Literal_Numbers_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_1 = 28.0M;
decimal test_2 = 7.0M;
decimal test_result = test_1 / test_2;
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Divide, test_2);
OmsContext context = Oms.CreateContext();
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
[Test]
public void Power_Two_Literal_Numbers_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_1 = 4.0M;
decimal test_2 = 3.0M;
decimal test_result = (decimal)Math.Pow((double)test_1, (double)test_2);
decimal test_result2 = (test_1 * test_1 * test_1);
Assert.That(test_result, Is.EqualTo(test_result2));
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Power, test_2);
OmsContext context = Oms.CreateContext();
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
[Test]
public void Root_Two_Literal_Numbers_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_1 = 4.0M;
decimal test_2 = 2.0M;
decimal test_result = (decimal)Math.Sqrt((double)test_1);
decimal test_result2 = 2;
Assert.That(test_result, Is.EqualTo(test_result2));
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Root, test_2);
OmsContext context = Oms.CreateContext();
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
[Test]
public void Logarithm_Two_Literal_Numbers_Test()
{
InstanceHandle irTestClass = Oms.GetInstance(TEST_CLASS_GUID);
InstanceHandle irTestClassInstance = Oms.CreateInstanceOf(irTestClass);
decimal test_1 = 100.0M;
decimal test_2 = 10.0M;
decimal test_result = 2;
Assert.That(test_result, Is.EqualTo(test_result));
OmsMethodBuilder builder = new OmsMethodBuilder(Oms);
CalculateNumericMethod ih_CN = builder.CreateCalculateNumericMethod(irTestClass, "build", "Test Attribute", AccessModifier.Public, true, Oms.GetInstance(KnownAttributeGuids.Numeric.Index), test_1, ArithmeticOperator.Logarithm, test_2);
OmsContext context = Oms.CreateContext();
InstanceHandle ih_test = Oms.Execute(context, ih_CN);
object value = context.GetWorkData(ih_test);
Assert.That(value, Is.EqualTo(test_result));
}
}
}