improvements to dynamic code generation and interface modeling
This commit is contained in:
parent
9cda89829c
commit
2b4b8b8408
@ -22,9 +22,12 @@ public class CreateInstance : IMethodMember
|
|||||||
public ObjectReference ObjectReference { get; set; }
|
public ObjectReference ObjectReference { get; set; }
|
||||||
public List<object> ParameterValues { get; } = new List<object>();
|
public List<object> ParameterValues { get; } = new List<object>();
|
||||||
|
|
||||||
public CreateInstance(IEnumerable<string> objectNames, IEnumerable<object> parameterValues)
|
public CreateInstance(ObjectReference createWhat, IEnumerable<object>? parameterValues = null)
|
||||||
|
{
|
||||||
|
ObjectReference = createWhat;
|
||||||
|
if (parameterValues != null)
|
||||||
{
|
{
|
||||||
ObjectReference = new ObjectReference(objectNames);
|
|
||||||
ParameterValues.AddRange(parameterValues);
|
ParameterValues.AddRange(parameterValues);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@ -32,8 +32,7 @@ public class MethodCall : IMethodMember
|
|||||||
{
|
{
|
||||||
foreach (object parmValue in parmValues)
|
foreach (object parmValue in parmValues)
|
||||||
{
|
{
|
||||||
MethodParameter parm = new MethodParameter();
|
MethodParameter parm = new MethodParameter(parmValue);
|
||||||
parm.Value = parmValue;
|
|
||||||
Parameters.Add(parm);
|
Parameters.Add(parm);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,13 +19,17 @@ namespace Mocha.Modeling.CodeGeneration;
|
|||||||
|
|
||||||
public class MethodParameter
|
public class MethodParameter
|
||||||
{
|
{
|
||||||
public MethodParameter(string? name = null, ObjectReference? dataType = null)
|
public MethodParameter(object value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
public MethodParameter(string name, ObjectReference dataType)
|
||||||
{
|
{
|
||||||
Name = name;
|
Name = name;
|
||||||
DataType = dataType;
|
DataType = dataType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ObjectReference? DataType { get; set; } = null;
|
public ObjectReference DataType { get; set; } = null;
|
||||||
public string? Name { get; set; } = null;
|
public string Name { get; set; } = null;
|
||||||
public object? Value { get; set; } = null;
|
public object? Value { get; set; } = null;
|
||||||
}
|
}
|
||||||
@ -15,14 +15,31 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace Mocha.Modeling.CodeGeneration;
|
namespace Mocha.Modeling.CodeGeneration;
|
||||||
|
|
||||||
public class ObjectReference
|
public class ObjectReference
|
||||||
{
|
{
|
||||||
public IEnumerable<string> ObjectNames { get; set; } = null;
|
public IEnumerable<string> ObjectNames { get; set; } = null;
|
||||||
|
public IEnumerable<ObjectReference>? GenericArguments { get; set; } = null;
|
||||||
|
|
||||||
public ObjectReference(IEnumerable<string> objectNames)
|
public ObjectReference(IEnumerable<string> objectNames, IEnumerable<ObjectReference>? genericArguments = null)
|
||||||
{
|
{
|
||||||
ObjectNames = objectNames;
|
ObjectNames = objectNames;
|
||||||
|
GenericArguments = genericArguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.Append(String.Join('.', ObjectNames));
|
||||||
|
if (GenericArguments != null && GenericArguments.Count() > 0)
|
||||||
|
{
|
||||||
|
sb.Append('<');
|
||||||
|
sb.Append(String.Join(',', GenericArguments));
|
||||||
|
sb.Append('>');
|
||||||
|
}
|
||||||
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
34
mocha-dotnet/src/lib/Mocha.Modeling/CodeGeneration/Field.cs
Normal file
34
mocha-dotnet/src/lib/Mocha.Modeling/CodeGeneration/Field.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// Copyright (C) 2025 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/>.
|
||||||
|
|
||||||
|
namespace Mocha.Modeling.CodeGeneration;
|
||||||
|
|
||||||
|
public class Field : IClassMember
|
||||||
|
{
|
||||||
|
public Field(string name, ObjectReference returnType, object? value = null)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
ReturnType = returnType;
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AccessModifier AccessModifier { get; set; } = AccessModifier.None;
|
||||||
|
public string Name { get; set; } = "";
|
||||||
|
public ObjectReference ReturnType { get; set; }
|
||||||
|
public object? Value { get; set; }
|
||||||
|
public bool ReadOnly { get; set; } = false;
|
||||||
|
}
|
||||||
@ -47,7 +47,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
if (item is Namespace ns)
|
if (item is Namespace ns)
|
||||||
{
|
{
|
||||||
sb.Append("namespace ");
|
sb.Append("namespace ");
|
||||||
sb.Append(BuildObjectReference(ns.Names));
|
sb.Append(BuildObjectReference(new ObjectReference(ns.Names)));
|
||||||
sb.Append(" { ");
|
sb.Append(" { ");
|
||||||
foreach (ICodeItem subItem in ns.Items)
|
foreach (ICodeItem subItem in ns.Items)
|
||||||
{
|
{
|
||||||
@ -63,7 +63,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
if (c.InheritsClass != null)
|
if (c.InheritsClass != null)
|
||||||
{
|
{
|
||||||
sb.Append(" : ");
|
sb.Append(" : ");
|
||||||
sb.Append(BuildObjectReference(c.InheritsClass.ObjectNames));
|
sb.Append(BuildObjectReference(c.InheritsClass));
|
||||||
}
|
}
|
||||||
sb.Append(" { ");
|
sb.Append(" { ");
|
||||||
foreach (IClassMember subItem in c.Items)
|
foreach (IClassMember subItem in c.Items)
|
||||||
@ -115,10 +115,35 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
if (p.Value != null || (p.UseAutoProperty && p.ReadOnly))
|
if (p.Value != null || (p.UseAutoProperty && p.ReadOnly))
|
||||||
{
|
{
|
||||||
sb.Append("= ");
|
sb.Append("= ");
|
||||||
|
if (p.Value != null)
|
||||||
|
{
|
||||||
sb.Append(ExpressionOrLiteral(p.Value));
|
sb.Append(ExpressionOrLiteral(p.Value));
|
||||||
|
}
|
||||||
|
else if (p.UseAutoProperty && p.GetMethod != null && p.GetMethod.Items.Count == 1 && p.GetMethod.Items[0] is Return r)
|
||||||
|
{
|
||||||
|
sb.Append(ExpressionOrLiteral(r.Value));
|
||||||
|
}
|
||||||
sb.Append(";");
|
sb.Append(";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (item is Field f)
|
||||||
|
{
|
||||||
|
sb.Append(GenerateCode(f.AccessModifier));
|
||||||
|
sb.Append(' ');
|
||||||
|
if (f.ReadOnly)
|
||||||
|
{
|
||||||
|
sb.Append("readonly ");
|
||||||
|
}
|
||||||
|
sb.Append(GenerateCode(f.ReturnType));
|
||||||
|
sb.Append(' ');
|
||||||
|
sb.Append(f.Name);
|
||||||
|
if (f.Value != null)
|
||||||
|
{
|
||||||
|
sb.Append(" = ");
|
||||||
|
sb.Append(ExpressionOrLiteral(f.Value));
|
||||||
|
}
|
||||||
|
sb.Append(";");
|
||||||
|
}
|
||||||
else if (item is Method m)
|
else if (item is Method m)
|
||||||
{
|
{
|
||||||
sb.Append(GenerateCode(m.AccessModifier));
|
sb.Append(GenerateCode(m.AccessModifier));
|
||||||
@ -144,7 +169,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
for (int i = 0; i < m.Parameters.Count; i++)
|
for (int i = 0; i < m.Parameters.Count; i++)
|
||||||
{
|
{
|
||||||
MethodParameter parm = m.Parameters[i];
|
MethodParameter parm = m.Parameters[i];
|
||||||
sb.Append(BuildObjectReference(parm.DataType.ObjectNames));
|
sb.Append(BuildObjectReference(parm.DataType));
|
||||||
sb.Append(' ');
|
sb.Append(' ');
|
||||||
sb.Append(parm.Name);
|
sb.Append(parm.Name);
|
||||||
if (i < m.Parameters.Count - 1)
|
if (i < m.Parameters.Count - 1)
|
||||||
@ -165,7 +190,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
else if (item is CreateInstance ci)
|
else if (item is CreateInstance ci)
|
||||||
{
|
{
|
||||||
sb.Append("new ");
|
sb.Append("new ");
|
||||||
sb.Append(BuildObjectReference(ci.ObjectReference.ObjectNames));
|
sb.Append(BuildObjectReference(ci.ObjectReference));
|
||||||
sb.Append("(");
|
sb.Append("(");
|
||||||
sb.Append(BuildParameterList(ci.ParameterValues));
|
sb.Append(BuildParameterList(ci.ParameterValues));
|
||||||
sb.Append(")");
|
sb.Append(")");
|
||||||
@ -177,7 +202,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
}
|
}
|
||||||
else if (item is MethodCall mc)
|
else if (item is MethodCall mc)
|
||||||
{
|
{
|
||||||
sb.Append(BuildObjectReference(mc.ObjectName.ObjectNames));
|
sb.Append(BuildObjectReference(mc.ObjectName));
|
||||||
sb.Append('.');
|
sb.Append('.');
|
||||||
sb.Append(mc.MethodName);
|
sb.Append(mc.MethodName);
|
||||||
if (mc.GenericParameters != null)
|
if (mc.GenericParameters != null)
|
||||||
@ -192,16 +217,9 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
}
|
}
|
||||||
else if (item is PropertyAssignment pa)
|
else if (item is PropertyAssignment pa)
|
||||||
{
|
{
|
||||||
sb.Append(BuildObjectReference(pa.ObjectReference.ObjectNames));
|
sb.Append(BuildObjectReference(pa.ObjectReference));
|
||||||
sb.Append(" = ");
|
sb.Append(" = ");
|
||||||
if (pa.Value != null)
|
sb.Append(ExpressionOrLiteral(pa.Value));
|
||||||
{
|
|
||||||
sb.Append(pa.Value);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sb.Append("null");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
@ -242,7 +260,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
}
|
}
|
||||||
else if (value is ObjectReference orr)
|
else if (value is ObjectReference orr)
|
||||||
{
|
{
|
||||||
return BuildObjectReference(orr.ObjectNames);
|
return BuildObjectReference(orr);
|
||||||
}
|
}
|
||||||
return value.ToString() ?? "";
|
return value.ToString() ?? "";
|
||||||
}
|
}
|
||||||
@ -251,20 +269,49 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
|
|
||||||
protected ObjectReference ParseObjectReference(string value)
|
protected ObjectReference ParseObjectReference(string value)
|
||||||
{
|
{
|
||||||
string[] parts = value.Split(new char[] { '.' });
|
string[]? genericArgs = null;
|
||||||
return new ObjectReference(parts);
|
string typePart = value;
|
||||||
|
List<ObjectReference> genericArgList = new List<ObjectReference>();
|
||||||
|
if (value.Contains('<') && value.Contains('>'))
|
||||||
|
{
|
||||||
|
string genericPart = value.Substring(value.IndexOf('<') + 1, value.IndexOf('>') - value.IndexOf('<') - 1);
|
||||||
|
genericArgs = genericPart.Split(new char[] { ',' });
|
||||||
|
foreach (string genericArg in genericArgs)
|
||||||
|
{
|
||||||
|
genericArgList.Add(ParseObjectReference(genericArg));
|
||||||
|
}
|
||||||
|
typePart = value.Substring(0, value.IndexOf('<'));
|
||||||
|
}
|
||||||
|
string[] parts = typePart.Split(new char[] { '.' });
|
||||||
|
return new ObjectReference(parts, genericArgList);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Property GenerateProperty(PropertyInfo pi)
|
protected Property GenerateProperty(CodeGeneration.Class parentClass, PropertyInfo pi)
|
||||||
{
|
{
|
||||||
Property pp = new Property(pi.Name, ParseObjectReference(SafeTypeName(pi.PropertyType)));
|
Property pp = new Property(pi.Name, ParseObjectReference(SafeTypeName(pi.PropertyType)));
|
||||||
pp.AccessModifier = AccessModifier.Public;
|
pp.AccessModifier = AccessModifier.Public;
|
||||||
if (pi.PropertyType.IsSubclassOfGeneric(typeof(IList<>)))
|
if (pi.PropertyType.IsSubclassOfGeneric(typeof(IList<>)))
|
||||||
|
{
|
||||||
|
Type[] genericArgs = pi.PropertyType.GetGenericArguments();
|
||||||
|
if (genericArgs.Length == 1 && genericArgs[0].GetInterfaces().Contains(typeof(IOmsClass)))
|
||||||
|
{
|
||||||
|
Field backingField = new Field("__backing_" + pi.Name, ParseObjectReference(SafeTypeName(pi.PropertyType)));
|
||||||
|
parentClass.Items.Add(backingField);
|
||||||
|
|
||||||
|
pp.GetMethod = new Method(new IMethodMember[]
|
||||||
|
{
|
||||||
|
new Return(new ObjectReference(new string[] { "__backing_" + pi.Name }))
|
||||||
|
});
|
||||||
|
pp.UseAutoProperty = false;
|
||||||
|
pp.ReadOnly = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
pp.GetMethod = new Method(new IMethodMember[]
|
pp.GetMethod = new Method(new IMethodMember[]
|
||||||
{
|
{
|
||||||
new Return(null)
|
new Return(null)
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (pi.GetSetMethod() != null)
|
if (pi.GetSetMethod() != null)
|
||||||
{
|
{
|
||||||
@ -281,7 +328,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
||||||
{
|
{
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new string[] { "System", "Guid" }, new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new ObjectReference(new string[] { "System", "Guid" }), new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
||||||
}) { GenericParameters = [ new ObjectReference(["string"]) ]})
|
}) { GenericParameters = [ new ObjectReference(["string"]) ]})
|
||||||
});
|
});
|
||||||
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
||||||
@ -298,7 +345,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
||||||
{
|
{
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new string[] { "System", "Guid" }, new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new ObjectReference(new string[] { "System", "Guid" }), new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
||||||
}) { GenericParameters = [ new ObjectReference(["int"]) ]})
|
}) { GenericParameters = [ new ObjectReference(["int"]) ]})
|
||||||
});
|
});
|
||||||
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
||||||
@ -315,7 +362,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
||||||
{
|
{
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new string[] { "System", "Guid" }, new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new ObjectReference(new string[] { "System", "Guid" }), new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
||||||
}) { GenericParameters = [ new ObjectReference(["decimal"]) ]})
|
}) { GenericParameters = [ new ObjectReference(["decimal"]) ]})
|
||||||
});
|
});
|
||||||
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
||||||
@ -332,7 +379,7 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
|
|||||||
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
|
||||||
{
|
{
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
|
||||||
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new string[] { "System", "Guid" }, new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new ObjectReference(new string[] { "System", "Guid" }), new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
|
||||||
}) { GenericParameters = [ new ObjectReference(["System.DateTime"]) ]})
|
}) { GenericParameters = [ new ObjectReference(["System.DateTime"]) ]})
|
||||||
});
|
});
|
||||||
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
|
||||||
|
|||||||
@ -28,7 +28,7 @@ public abstract class DotNetCodeGenerator : CodeGenerator
|
|||||||
{
|
{
|
||||||
public string GenerateCode(ObjectReference objectReference)
|
public string GenerateCode(ObjectReference objectReference)
|
||||||
{
|
{
|
||||||
return BuildObjectReference(objectReference.ObjectNames);
|
return BuildObjectReference(objectReference);
|
||||||
}
|
}
|
||||||
protected string? SafeTypeName(Type type)
|
protected string? SafeTypeName(Type type)
|
||||||
{
|
{
|
||||||
@ -92,22 +92,20 @@ public abstract class DotNetCodeGenerator : CodeGenerator
|
|||||||
|
|
||||||
protected abstract string ExpressionOrLiteral(object? value);
|
protected abstract string ExpressionOrLiteral(object? value);
|
||||||
|
|
||||||
protected virtual string BuildObjectReference(IEnumerable<string> objectNames)
|
protected virtual string BuildObjectReference(ObjectReference objectReference)
|
||||||
{
|
{
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
int i = 0;
|
sb.Append(String.Join('.', objectReference.ObjectNames));
|
||||||
foreach (string objectName in objectNames)
|
if (objectReference.GenericArguments != null && objectReference.GenericArguments.Count() > 0)
|
||||||
{
|
{
|
||||||
sb.Append(objectName);
|
sb.Append('<');
|
||||||
if (i < objectNames.Count() - 1)
|
sb.Append(String.Join(',', objectReference.GenericArguments));
|
||||||
{
|
sb.Append('>');
|
||||||
sb.Append('.');
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected Assembly CompileScript(SyntaxTree syntaxTree, IEnumerable<string>? additionalDLLs = null)
|
protected Assembly CompileScript(SyntaxTree syntaxTree, IEnumerable<string>? additionalDLLs = null)
|
||||||
{
|
{
|
||||||
// thanks https://stackoverflow.com/questions/66672329
|
// thanks https://stackoverflow.com/questions/66672329
|
||||||
|
|||||||
@ -41,6 +41,17 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
|
|||||||
{
|
{
|
||||||
CreateType(oms, t2);
|
CreateType(oms, t2);
|
||||||
}
|
}
|
||||||
|
foreach (Type t2 in t.GetNestedTypes())
|
||||||
|
{
|
||||||
|
PropertyInfo[] pis = t2.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||||
|
foreach (PropertyInfo pi in pis)
|
||||||
|
{
|
||||||
|
if (pi.PropertyType.IsSubclassOfGeneric(typeof(IList<>)) || pi.PropertyType.GetInterfaces().Contains(typeof(IOmsClass)))
|
||||||
|
{
|
||||||
|
CreateRelationship(oms, t2, pi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Type?[] tis;
|
Type?[] tis;
|
||||||
try
|
try
|
||||||
@ -60,6 +71,58 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
|
|||||||
}
|
}
|
||||||
throw new TypeInitializationException(typeof(TOmsClass).Name, null);
|
throw new TypeInitializationException(typeof(TOmsClass).Name, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void CreateRelationship(Oms oms, Type parentClassType, PropertyInfo pi)
|
||||||
|
{
|
||||||
|
OmsRelationshipTypeAttribute? attrRelationshipType = pi.GetCustomAttribute<OmsRelationshipTypeAttribute>();
|
||||||
|
OmsGlobalIdentifierAttribute? attrGlobalIdentifier = pi.GetCustomAttribute<OmsGlobalIdentifierAttribute>();
|
||||||
|
|
||||||
|
if (attrGlobalIdentifier != null)
|
||||||
|
{
|
||||||
|
if (!oms.TryGetInstance(attrGlobalIdentifier.GlobalIdentifier, out InstanceHandle ih))
|
||||||
|
{
|
||||||
|
InstanceHandle ihRelationship = oms.CreateInstanceOf(oms.GetInstance(KnownInstanceGuids.Classes.Relationship), attrGlobalIdentifier.GlobalIdentifier);
|
||||||
|
|
||||||
|
InstanceHandle ihSourceClass = InstanceHandle.Empty;
|
||||||
|
InstanceHandle ihDestinationClass = InstanceHandle.Empty;
|
||||||
|
|
||||||
|
OmsGlobalIdentifierAttribute? attrParentClassGlobalIdentifier = parentClassType.GetCustomAttribute<OmsGlobalIdentifierAttribute>();
|
||||||
|
if (attrParentClassGlobalIdentifier != null)
|
||||||
|
{
|
||||||
|
ihSourceClass = oms.GetInstance(attrParentClassGlobalIdentifier.GlobalIdentifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool singular = false;
|
||||||
|
if (pi.PropertyType.IsSubclassOfGeneric(typeof(IList<>)))
|
||||||
|
{
|
||||||
|
OmsGlobalIdentifierAttribute? attrTargetClassGlobalIdentifier = pi.PropertyType.GetGenericArguments()[0].GetCustomAttribute<OmsGlobalIdentifierAttribute>();
|
||||||
|
if (attrTargetClassGlobalIdentifier != null)
|
||||||
|
{
|
||||||
|
ihDestinationClass = oms.GetInstance(attrTargetClassGlobalIdentifier.GlobalIdentifier);
|
||||||
|
}
|
||||||
|
singular = false;
|
||||||
|
}
|
||||||
|
else if (pi.PropertyType.GetInterfaces().Contains(typeof(IOmsClass)))
|
||||||
|
{
|
||||||
|
OmsGlobalIdentifierAttribute? attrTargetClassGlobalIdentifier = pi.PropertyType.GetCustomAttribute<OmsGlobalIdentifierAttribute>();
|
||||||
|
if (attrTargetClassGlobalIdentifier != null)
|
||||||
|
{
|
||||||
|
ihDestinationClass = oms.GetInstance(attrTargetClassGlobalIdentifier.GlobalIdentifier);
|
||||||
|
}
|
||||||
|
singular = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
oms.AssignRelationship(ihRelationship, oms.GetInstance(KnownRelationshipGuids.Relationship__has_source__Class), ihSourceClass);
|
||||||
|
oms.AssignRelationship(ihRelationship, oms.GetInstance(KnownRelationshipGuids.Relationship__has_destination__Class), ihDestinationClass);
|
||||||
|
if (attrRelationshipType != null)
|
||||||
|
{
|
||||||
|
oms.SetAttributeValue(ihRelationship, oms.GetInstance(KnownAttributeGuids.Text.RelationshipType), attrRelationshipType.Value);
|
||||||
|
}
|
||||||
|
oms.SetAttributeValue(ihRelationship, oms.GetInstance(KnownAttributeGuids.Boolean.Singular), singular);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public TOmsClass CreateInstance(Oms oms, Guid globalIdentifier)
|
public TOmsClass CreateInstance(Oms oms, Guid globalIdentifier)
|
||||||
{
|
{
|
||||||
string script = BuildScript(globalIdentifier);
|
string script = BuildScript(globalIdentifier);
|
||||||
@ -132,14 +195,29 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
|
|||||||
|
|
||||||
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
||||||
{
|
{
|
||||||
cl.Items.Add(GenerateProperty(pi));
|
cl.Items.Add(GenerateProperty(cl, pi));
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.Items.Add(new Property("Oms", new ObjectReference(new string[] { "Mocha", "Core", "Oms" }), null) { AccessModifier = CodeGeneration.AccessModifier.Public, UseAutoProperty = true, ReadOnly = false });
|
cl.Items.Add(new Property("Oms", new ObjectReference(new string[] { "Mocha", "Core", "Oms" }), null) { AccessModifier = CodeGeneration.AccessModifier.Public, UseAutoProperty = true, ReadOnly = false });
|
||||||
cl.Items.Add(new CodeGeneration.Method("Initialize", new MethodParameter[] { new MethodParameter("oms", new ObjectReference(["Mocha", "Core", "Oms"]))}, new IMethodMember[]
|
|
||||||
|
List<IMethodMember> list = new List<IMethodMember>();
|
||||||
|
list.Add(new PropertyAssignment(new ObjectReference(["this", "Oms"]), "oms"));
|
||||||
|
foreach (IClassMember item in cl.Items)
|
||||||
{
|
{
|
||||||
new PropertyAssignment(new ObjectReference(["this", "Oms"]), "oms")
|
if (item is Field f)
|
||||||
}) { AccessModifier = CodeGeneration.AccessModifier.Public});
|
{
|
||||||
|
if (f.Name.StartsWith("__backing_"))
|
||||||
|
{
|
||||||
|
ObjectReference returnType = f.ReturnType;
|
||||||
|
if (f.ReturnType.ToString().StartsWith("System.Collections.Generic.IList<"))
|
||||||
|
{
|
||||||
|
returnType = new ObjectReference(["Mocha", "Core", "Modeling", "PropertyImplementations", "OmsNonsingularRelationshipProperty"], f.ReturnType.GenericArguments);
|
||||||
|
}
|
||||||
|
list.Add(new PropertyAssignment(new ObjectReference(["this", f.Name]), new CreateInstance(returnType, new object[] { new ObjectReference(["this", "Oms"]) })));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cl.Items.Add(new CodeGeneration.Method("Initialize", new MethodParameter[] { new MethodParameter("oms", new ObjectReference(["Mocha", "Core", "Oms"])) }, list) { AccessModifier = CodeGeneration.AccessModifier.Public });
|
||||||
|
|
||||||
return cl;
|
return cl;
|
||||||
}
|
}
|
||||||
@ -155,12 +233,12 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
|
|||||||
globalIdentifier = OmsDatabase.GetGlobalIdentifierForClass(t);
|
globalIdentifier = OmsDatabase.GetGlobalIdentifierForClass(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
cl.Items.Add(new Property("GlobalIdentifier", new ObjectReference(new string[] { "System", "Guid" }), new CreateInstance(["System", "Guid"], [globalIdentifier.ToString()])) { AccessModifier = CodeGeneration.AccessModifier.Public });
|
cl.Items.Add(new Property("GlobalIdentifier", new ObjectReference(new string[] { "System", "Guid" }), new CreateInstance(new ObjectReference(["System", "Guid"]), [globalIdentifier.ToString()])) { AccessModifier = CodeGeneration.AccessModifier.Public });
|
||||||
cl.Items.Add(new Property("Oms", new ObjectReference(new string[] { "Mocha", "Core", "Oms" }), null) { AccessModifier = CodeGeneration.AccessModifier.Public, UseAutoProperty = true, ReadOnly = false });
|
cl.Items.Add(new Property("Oms", new ObjectReference(new string[] { "Mocha", "Core", "Oms" }), null) { AccessModifier = CodeGeneration.AccessModifier.Public, UseAutoProperty = true, ReadOnly = false });
|
||||||
|
|
||||||
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
||||||
{
|
{
|
||||||
cl.Items.Add(GenerateProperty(pi));
|
cl.Items.Add(GenerateProperty(cl, pi));
|
||||||
}
|
}
|
||||||
return cl;
|
return cl;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,26 @@
|
|||||||
|
// Copyright (C) 2025 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/>.
|
||||||
|
|
||||||
|
namespace Mocha.Core.Modeling;
|
||||||
|
|
||||||
|
public abstract class OmsPropertyImplementation
|
||||||
|
{
|
||||||
|
internal OmsPropertyImplementation(Oms oms)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -15,8 +15,71 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
using System.Collections;
|
||||||
|
using Mocha.Modeling;
|
||||||
|
|
||||||
namespace Mocha.Core.Modeling.PropertyImplementations;
|
namespace Mocha.Core.Modeling.PropertyImplementations;
|
||||||
|
|
||||||
public class OmsNonsingularRelationshipProperty : OmsRelationshipProperty
|
public class OmsNonsingularRelationshipProperty<T> : OmsPropertyImplementation, IList<T> where T : IOmsClass
|
||||||
{
|
{
|
||||||
|
internal OmsNonsingularRelationshipProperty(Oms oms) : base(oms)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public T this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
||||||
|
|
||||||
|
public int Count => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public bool IsReadOnly => throw new NotImplementedException();
|
||||||
|
|
||||||
|
public void Add(T item)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Clear()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Contains(T item)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void CopyTo(T[] array, int arrayIndex)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IEnumerator<T> GetEnumerator()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int IndexOf(T item)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Insert(int index, T item)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Remove(T item)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveAt(int index)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
IEnumerator IEnumerable.GetEnumerator()
|
||||||
|
{
|
||||||
|
return GetEnumerator();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
// Copyright (C) 2024 Michael Becker <alcexhim@gmail.com>
|
// Copyright (C) 2025 Michael Becker <alcexhim@gmail.com>
|
||||||
//
|
//
|
||||||
// This file is part of Mocha.NET.
|
// This file is part of Mocha.NET.
|
||||||
//
|
//
|
||||||
@ -15,9 +15,9 @@
|
|||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
// along with Mocha.NET. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
namespace Mocha.Core.Modeling.PropertyImplementations;
|
namespace Mocha.Core.Modeling.PropertyImplementations;
|
||||||
|
|
||||||
public abstract class OmsRelationshipProperty
|
public class OmsSingularRelationshipProperty
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user