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; 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) 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); 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 MinimumValue { get; } = new Guid("{bc90ffdf-9b6e-444a-a484-f9d06d7f3c31}");
public static Guid MaximumValue { get; } = new Guid("{b9353b1c-2597-4097-96eb-449a6fafcdab}"); 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 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 public static Guid GetReferencedInstanceSetMethod { get; } = new Guid("{bcfd0d64-3eba-4a97-9622-f3a960877d24}"); // 26
// BEM - Build Element Method - 29 // BEM - Build Element Method - 29
public static Guid BuildAttributeMethod { get; } = new Guid("{e5879955-0093-48c8-8042-813168578af2}"); // 30 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> /// <summary>
/// GSI - Get Specified Instances Method /// GSI - Get Specified Instances Method
/// Equivalent to YP: `static function $verb$name() : $returnType = $inst` /// 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 GreaterThanOrEqualTo { get; } = new Guid("{3a4eb7be-f96b-45ac-b97a-bb8fbfc35923}");
public static Guid LessThanOrEqualTo { get; } = new Guid("{528e5637-f995-4a6d-93c3-f5e1e62b4517}"); 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 class Languages
{ {
public static Guid English { get; } = new Guid ("{68BB6038-A4B5-4EE1-AAE9-326494942062}"); 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 class WorkSets
{ {
public static Guid TaskRelatedInstance { get; } = new Guid("{d3860252-491e-469e-939e-702d56d88063}"); 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

@ -77,7 +77,7 @@ namespace Mocha.Core
public static Guid Translation_Value__for__Translation { get; } = new Guid("{458c218a-9965-44ba-884b-deda5bf2931d}"); public static Guid Translation_Value__for__Translation { get; } = new Guid("{458c218a-9965-44ba-884b-deda5bf2931d}");
public static Guid Translation_Value__has__Language { get; } = new Guid("{3655AEC2-E2C9-4DDE-8D98-0C4D3CE1E569}"); public static Guid Translation_Value__has__Language { get; } = new Guid("{3655AEC2-E2C9-4DDE-8D98-0C4D3CE1E569}");
public static Guid Language__for__Translation_Value { get; } = new Guid("{bf885bb1-da84-447c-95dc-143ce9deac65}"); public static Guid Language__for__Translation_Value { get; } = new Guid("{bf885bb1-da84-447c-95dc-143ce9deac65}");
public static Guid Application__has_title__Translation { get; } = new Guid("{17e7437a-95bb-472f-a068-c433b715c3e4}"); public static Guid Application__has_title__Translation { get; } = new Guid("{17e7437a-95bb-472f-a068-c433b715c3e4}");
public static Guid Translation__title_for__Application { get; } = new Guid("{79aca30e-ddf9-4d3b-8be7-80b4aaf82c1f}"); public static Guid Translation__title_for__Application { get; } = new Guid("{79aca30e-ddf9-4d3b-8be7-80b4aaf82c1f}");
@ -161,7 +161,7 @@ namespace Mocha.Core
public static Guid System_Instance_Set_Routine__used_by__Get_Instance_Set_by_System_Routine_Method { get; } = new Guid("{6fb6534c-2a46-4d6d-b9df-fd581f19efed}"); public static Guid System_Instance_Set_Routine__used_by__Get_Instance_Set_by_System_Routine_Method { get; } = new Guid("{6fb6534c-2a46-4d6d-b9df-fd581f19efed}");
public static Guid Update_by_System_Routine_Method__uses__System_Update_Routine { get; } = new Guid("{9424a253-cb40-4c7d-bebd-0d0373458154}"); public static Guid Update_by_System_Routine_Method__uses__System_Update_Routine { get; } = new Guid("{9424a253-cb40-4c7d-bebd-0d0373458154}");
public static Guid Get_Referenced_Instance_Set_Method__returns__Work_Set { get; } = new Guid("{72057f5b-9b49-497d-852f-cd7e5e258d6c}"); public static Guid Get_Referenced_Instance_Set_Method__returns__Work_Set { get; } = new Guid("{72057f5b-9b49-497d-852f-cd7e5e258d6c}");
public static Guid Get_Referenced_Instance_Set_Method__uses_reference__Executable_returning_Instance_Set { get; } = new Guid("{2978238f-7cb0-4ba3-8c6f-473df782cfef}"); public static Guid Get_Referenced_Instance_Set_Method__uses_reference__Executable_returning_Instance_Set { get; } = new Guid("{2978238f-7cb0-4ba3-8c6f-473df782cfef}");
public static Guid Get_Referenced_Instance_Set_Method__uses_answer__Executable_returning_Instance_Set { get; } = new Guid("{6a65819e-c8cb-4575-9af8-ee221364049b}"); public static Guid Get_Referenced_Instance_Set_Method__uses_answer__Executable_returning_Instance_Set { get; } = new Guid("{6a65819e-c8cb-4575-9af8-ee221364049b}");
@ -183,8 +183,8 @@ namespace Mocha.Core
public static Guid Assign_Attribute_Method__uses__Executable_returning_Attribute { get; } = new Guid("{9313f96e-58af-416f-852e-ef83725057fc}"); public static Guid Assign_Attribute_Method__uses__Executable_returning_Attribute { get; } = new Guid("{9313f96e-58af-416f-852e-ef83725057fc}");
public static Guid Executable_returning_Attribute__used_by__Assign_Attribute_Method { get; } = new Guid("{cd8fd04c-dcdd-4dc8-9d5c-e8f83d080cb8}"); public static Guid Executable_returning_Attribute__used_by__Assign_Attribute_Method { get; } = new Guid("{cd8fd04c-dcdd-4dc8-9d5c-e8f83d080cb8}");
public static Guid Assign_Attribute_Method__assigns__Attribute { get; } = new Guid("{74061875-8a27-403b-9456-02e52cfd13b2}"); public static Guid Assign_Attribute_Method__assigns__Attribute { get; } = new Guid("{74061875-8a27-403b-9456-02e52cfd13b2}");
public static Guid Attribute__assigned_by__Assign_Attribute_Method { get; } = new Guid("{d3b540e8-0f52-4595-bf52-1968637da59a}"); public static Guid Attribute__assigned_by__Assign_Attribute_Method { get; } = new Guid("{d3b540e8-0f52-4595-bf52-1968637da59a}");
public static Guid Prompt_Value__has__Prompt { get; } = new Guid("{7CD62362-DDCE-4BFC-87B9-B5499B0BC141}"); public static Guid Prompt_Value__has__Prompt { get; } = new Guid("{7CD62362-DDCE-4BFC-87B9-B5499B0BC141}");
@ -223,10 +223,10 @@ namespace Mocha.Core
public static Guid Tenant__has_company_logo_image__File { get; } = new Guid("{3540c81c-b229-4eac-b9b5-9d4b4c6ad1eb}"); public static Guid Tenant__has_company_logo_image__File { get; } = new Guid("{3540c81c-b229-4eac-b9b5-9d4b4c6ad1eb}");
public static Guid Menu_Item__has_title__Translation { get; } = new Guid("{65E3C87E-A2F7-4A33-9FA7-781EFA801E02}"); public static Guid Menu_Item__has_title__Translation { get; } = new Guid("{65E3C87E-A2F7-4A33-9FA7-781EFA801E02}");
public static Guid Menu__has__Menu_Item { get; } = new Guid("{5b659d7c-58f9-453c-9826-dd3205c3c97f}"); public static Guid Menu__has__Menu_Item { get; } = new Guid("{5b659d7c-58f9-453c-9826-dd3205c3c97f}");
public static Guid Menu_Item__for__Menu { get; } = new Guid("{a22d949f-f8d1-4dcc-a3eb-d9f910228dfd}"); public static Guid Menu_Item__for__Menu { get; } = new Guid("{a22d949f-f8d1-4dcc-a3eb-d9f910228dfd}");
public static Guid Command_Menu_Item__has__Icon { get; } = new Guid("{8859DAEF-01F7-46FA-8F3E-7B2F28E0A520}"); public static Guid Command_Menu_Item__has__Icon { get; } = new Guid("{8859DAEF-01F7-46FA-8F3E-7B2F28E0A520}");
public static Guid Instance_Menu_Item__has_target__Instance { get; } = new Guid("{C599C20E-F01A-4B12-A919-5DC3B0F545C2}"); public static Guid Instance_Menu_Item__has_target__Instance { get; } = new Guid("{C599C20E-F01A-4B12-A919-5DC3B0F545C2}");
@ -272,7 +272,7 @@ namespace Mocha.Core
public static Guid Tenant__has_login_header__Translation { get; } = new Guid("{41D66ACB-AFDE-4B6F-892D-E66255F10DEB}"); public static Guid Tenant__has_login_header__Translation { get; } = new Guid("{41D66ACB-AFDE-4B6F-892D-E66255F10DEB}");
public static Guid Tenant__has_login_footer__Translation { get; } = new Guid("{A6203B6B-5BEB-4008-AE49-DB5E7DDBA45B}"); public static Guid Tenant__has_login_footer__Translation { get; } = new Guid("{A6203B6B-5BEB-4008-AE49-DB5E7DDBA45B}");
public static Guid Tenant__has_application_title__Translation { get; } = new Guid("{76683437-67ba-46d9-a5e7-2945be635345}"); public static Guid Tenant__has_application_title__Translation { get; } = new Guid("{76683437-67ba-46d9-a5e7-2945be635345}");
public static Guid Tenant__has_mega__Menu { get; } = new Guid("{cdd743cb-c74a-4671-9922-652c7db9f2d8}"); public static Guid Tenant__has_mega__Menu { get; } = new Guid("{cdd743cb-c74a-4671-9922-652c7db9f2d8}");
public static Guid Menu__mega_for__Tenant { get; } = new Guid("{788db047-ed7f-4e0c-a8b0-68478d486da7}"); public static Guid Menu__mega_for__Tenant { get; } = new Guid("{788db047-ed7f-4e0c-a8b0-68478d486da7}");
@ -288,7 +288,7 @@ namespace Mocha.Core
public static Guid Method__provides_content_for__Content_Page_Component { get; } = new Guid("{5E75000D-2421-4AD4-9E5F-B9FDD9CB4744}"); public static Guid Method__provides_content_for__Content_Page_Component { get; } = new Guid("{5E75000D-2421-4AD4-9E5F-B9FDD9CB4744}");
public static Guid Securable_Item__secured_by__Method { get; } = new Guid("{15199c49-9595-4288-846d-13b0ad5dcd4b}"); public static Guid Securable_Item__secured_by__Method { get; } = new Guid("{15199c49-9595-4288-846d-13b0ad5dcd4b}");
public static Guid Get_Relationship_Method__returns__Relationship { get; } = new Guid("{321581d6-60c1-4547-8344-9d5bda027adc}"); public static Guid Get_Relationship_Method__returns__Relationship { get; } = new Guid("{321581d6-60c1-4547-8344-9d5bda027adc}");
public static Guid Relationship__returned_by__Get_Relationship_Method { get; } = new Guid("{174242ec-7615-42d9-9639-c70ff6e84e0d}"); public static Guid Relationship__returned_by__Get_Relationship_Method { get; } = new Guid("{174242ec-7615-42d9-9639-c70ff6e84e0d}");
@ -349,7 +349,7 @@ namespace Mocha.Core
public static Guid Validation__for__Element_Content { get; } = new Guid("{3a4677e8-9c78-4149-80ad-46e5ac3b12f5}"); public static Guid Validation__for__Element_Content { get; } = new Guid("{3a4677e8-9c78-4149-80ad-46e5ac3b12f5}");
public static Guid Derived_Element_Content__update_with__Executable_returning_Work_Data { get; } = new Guid("{4151911a-485c-4488-8e19-1560f489d411}"); public static Guid Derived_Element_Content__update_with__Executable_returning_Work_Data { get; } = new Guid("{4151911a-485c-4488-8e19-1560f489d411}");
public static Guid Executable_returning_Work_Data__used_by__Derived_Element_Content { get; } = new Guid("{75fdacf8-163c-45ac-86eb-80677ff738ad}"); public static Guid Executable_returning_Work_Data__used_by__Derived_Element_Content { get; } = new Guid("{75fdacf8-163c-45ac-86eb-80677ff738ad}");
public static Guid Instance__has__Instance_Definition { get; } = new Guid("{329c54ee-17b8-4550-ae80-be5dee9ac53c}"); public static Guid Instance__has__Instance_Definition { get; } = new Guid("{329c54ee-17b8-4550-ae80-be5dee9ac53c}");
@ -438,7 +438,7 @@ namespace Mocha.Core
public static Guid Process_Updates_Method__has__PUM_Process { get; } = new Guid("{0ffaf08e-dbb5-49d0-8de9-d253ce6cbe7c}"); public static Guid Process_Updates_Method__has__PUM_Process { get; } = new Guid("{0ffaf08e-dbb5-49d0-8de9-d253ce6cbe7c}");
public static Guid PUM_Process__invokes__Execute_Update_Method_Binding { get; } = new Guid("{d3e83c17-fd38-46a0-a055-66281eabe9b0}"); public static Guid PUM_Process__invokes__Execute_Update_Method_Binding { get; } = new Guid("{d3e83c17-fd38-46a0-a055-66281eabe9b0}");
public static Guid BEM_Process__uses_loop__Executable_returning_Instance_Set { get; } = new Guid("{0fb2b538-eacb-418a-b7d8-43a584b85952}"); public static Guid BEM_Process__uses_loop__Executable_returning_Instance_Set { get; } = new Guid("{0fb2b538-eacb-418a-b7d8-43a584b85952}");
public static Guid Get_Instances_Method__returns__Work_Set { get; } = new Guid("{7d0f93b1-8c93-464e-a44d-d674f910b589}"); public static Guid Get_Instances_Method__returns__Work_Set { get; } = new Guid("{7d0f93b1-8c93-464e-a44d-d674f910b589}");
@ -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 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 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 !!! // !!! THIS IS PROBABLY NOT THREAD-SAFE. BE VERY VERY CAREFUL !!!
UpdateSyntacticSugar<AccessModifier>(typeof(KnownInstanceGuids.AccessModifiers)); UpdateSyntacticSugar<AccessModifier>(typeof(KnownInstanceGuids.AccessModifiers));
UpdateSyntacticSugar<RelationalOperator>(typeof(KnownInstanceGuids.RelationalOperators)); UpdateSyntacticSugar<RelationalOperator>(typeof(KnownInstanceGuids.RelationalOperators));
UpdateSyntacticSugar<ArithmeticOperator>(typeof(KnownInstanceGuids.ArithmeticOperators));
} }
} }

View File

@ -381,4 +381,14 @@ public class OmsMethodBuilder
InstanceHandle ihCase = CreateConditionalSelectAttributeCase(cas); InstanceHandle ihCase = CreateConditionalSelectAttributeCase(cas);
Oms.AssignRelationship(method.GetHandle(), Oms.GetInstance(KnownRelationshipGuids.Conditional_Select_Attribute_Method__has__Conditional_Select_Attribute_Case), ihCase); 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

@ -21,5 +21,15 @@ namespace Mocha.Core.Oop;
public class ArithmeticOperator : ConcreteInstanceWrapper public class ArithmeticOperator : ConcreteInstanceWrapper
{ {
public override Guid ClassId => KnownInstanceGuids.Classes.ArithmeticOperator; public override Guid ClassId => KnownInstanceGuids.Classes.ArithmeticOperator;
internal ArithmeticOperator(InstanceHandle handle) : base(handle) { } 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(); 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 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));
}
}
}