Newer
Older
MaZiFAU
committed
using System;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using static SOMDocManager;
using System.Linq.Expressions;
using System.Linq;
MaZiFAU
committed
public class FunctionCallFact : FactWrappedCRTP<FunctionCallFact>
MaZiFAU
committed
{
MaZiFAU
committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
public string arg_func_id;
public (float t_0, float t_n) Domain;
[JsonIgnore]
public FunctionFact Function_in => (FunctionFact)FactOrganizer.AllFacts[func_id];
[JsonIgnore]
public FunctionFact Function_args => (FunctionFact)FactOrganizer.AllFacts[arg_func_id];
public FunctionCallFact() : base() { }
public FunctionCallFact(string func_id, string arg_func_id, (float t_0, float t_n) Domain, FactOrganizer organizer) : base(organizer)
{
this.func_id = func_id;
this.arg_func_id = arg_func_id;
this.Domain = Domain;
_URI = func_id.ToString() + arg_func_id.ToString() + Domain.ToString();
}
public object[] Call(float t)
{
if (t < Domain.t_0 || t > Domain.t_n)
return null;
return Function_in.Function(Function_args.Function(new object[] { t }));
}
public override bool HasDependentFacts
=> true;
protected override string[] GetGetDependentFactIds()
=> new[] { func_id, arg_func_id };
public override GameObject instantiateDisplay(GameObject prefab, Transform transform)
{
var obj = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity, transform);
//obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = this.Label;
//obj.GetComponent<FactWrapper>().fact = this;
return obj;
}
protected override bool EquivalentWrapped(FunctionCallFact f1, FunctionCallFact f2)
=> f1.Domain.t_0.IsApproximatelyEqual(f2.Domain.t_0)
&& f1.Domain.t_n.IsApproximatelyEqual(f2.Domain.t_n)
&& (DependentFactsEquivalent(f1, f2)
|| (f1.Function_in.Equivalent(f2.Function_in)
&& f1.Function_args.Equivalent(f2.Function_args)
));
protected override MMTDeclaration MakeMMTDeclaration()
{
throw new NotImplementedException();
}
protected override void RecalculateTransform()
{
Position = Function_in.Position;
Rotation = Function_in.Rotation;
LocalScale = Function_in.LocalScale;
}
protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactOrganizer organizer)
=> new FunctionCallFact(old_to_new[this.func_id], old_to_new[this.arg_func_id], this.Domain, organizer);
}
public class FunctionFact : FactWrappedCRTP<FunctionFact>
{
MaZiFAU
committed
public SOMDoc Function_SOMDoc;
//TODO: doc
[JsonIgnore]
public LambdaExpression Function_expression;
[JsonIgnore]
MaZiFAU
committed
[JsonIgnore]
public Func<object[], object[]> Function;
MaZiFAU
committed
/// <summary> \copydoc Fact.Fact </summary>
public FunctionFact() : base() { }
/// <summary>
/// Standard Constructor:
/// Initiates members and creates MMT %Fact Server-Side
MaZiFAU
committed
/// </summary>
/// <param name="Function_SOMDoc">sets <see cref="Function_SOMDoc"/> and contains the Abstract Syntax Tree</param>
MaZiFAU
committed
/// <param name="organizer">sets <see cref="Fact._Facts"/></param>
public FunctionFact(SOMDoc Function_SOMDoc, FactOrganizer organizer) : base(organizer)
MaZiFAU
committed
{
this.Function_SOMDoc = Function_SOMDoc;
this.Function_expression = this.Function_SOMDoc.PartialInvokeCastingLambdaExpression(out Signature);
////TODO: catch
//string debug_tostring = Function_expression.ToString();
//Delegate debug_function = Function_expression.Compile();
this.Function = this.Function_expression.Compile() as Func<object[], object[]>;
MaZiFAU
committed
////TODO: Function_SOMDoc+domain
//MMTTerm tp = new OMS(JSONManager.MMTURIs.Point);
//MMTTerm df = new OMA(new OMS(JSONManager.MMTURIs.Tuple), arguments);
//AddFactResponse.sendAdd(new MMTSymbolDeclaration(Label, tp, df), out _URI);
//ParsingDictionary.parseTermsToId[df.ToString()] = _URI;
MaZiFAU
committed
}
/// <summary>
/// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
MaZiFAU
committed
/// </summary>
/// <param name="function_expression">sets <see cref="Function_expression"/> and contains the Abstract Syntax Tree</param>
/// <param name="uri">MMT URI</param>
/// <param name="organizer">sets <see cref="Fact._Facts"/></param>
public FunctionFact(SOMDoc Function_SOMDoc, string uri, FactOrganizer organizer) : base(organizer)
MaZiFAU
committed
{
this.Function_SOMDoc = Function_SOMDoc;
this.Function_expression = Function_SOMDoc.PartialInvokeCastingLambdaExpression(out Signature);
MaZiFAU
committed
////TODO: catch
//string debug_tostring = Function_expression.ToString();
//dynamic debug_function = Function_expression.Compile();
this.Function = this.Function_expression.Compile() as Func<object[], object[]>;
MaZiFAU
committed
this._URI = uri;
_ = this.Label;
MaZiFAU
committed
}
MaZiFAU
committed
/// \copydoc Fact.parseFact(Scroll.ScrollFact)
public new static FunctionFact parseFact(Scroll.ScrollFact fact)
MaZiFAU
committed
{// TODO Correctness
string uri = fact.@ref.uri;
MaZiFAU
committed
OMA df = (OMA)((Scroll.ScrollSymbolFact)fact).df;
if (df == null)
return null;
string parse_id = df.ToString();
if (!ParsingDictionary.parseTermsToId.ContainsKey(parse_id))
ParsingDictionary.parseTermsToId[parse_id] = uri;
return new FunctionFact(df, uri, StageStatic.stage.factState);
MaZiFAU
committed
}
/// \copydoc Fact.hasDependentFacts
public override bool HasDependentFacts
=> false;
MaZiFAU
committed
/// \copydoc Fact.getDependentFactIds
MaZiFAU
committed
protected override string[] GetGetDependentFactIds()
MaZiFAU
committed
/// \copydoc Fact.GetHashCode
public override int GetHashCode()
=> Function_expression.GetHashCode();
/// \copydoc Fact.instantiateDisplay(GameObject, Transform)
public override GameObject instantiateDisplay(GameObject prefab, Transform transform)
{
var obj = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity, transform);
//obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = this.Label;
//obj.GetComponent<FactWrapper>().fact = this;
return obj;
}
/// \copydoc Fact.EquivalentWrapped
protected override bool EquivalentWrapped(FunctionFact f1, FunctionFact f2)
=> f1.Function_SOMDoc.Equivalent(f2.Function_SOMDoc);
protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactOrganizer organizer)
=> new FunctionFact(this.Function_SOMDoc.MapURIs(old_to_new), organizer);
protected override MMTDeclaration MakeMMTDeclaration()
{
throw new NotImplementedException();
}
MaZiFAU
committed
}
public class AttachedPositionFunction : FactWrappedCRTP<AttachedPositionFunction>
{
public string fid;
[JsonIgnore]
[JsonIgnore]
public FunctionCallFact[] FunctionFacts
{
get => func_call_ids.Select(f => FactOrganizer.AllFacts[f] as FunctionCallFact).ToArray();
}
/// <summary>\copydoc Fact.Fact()</summary>
public AttachedPositionFunction() : base() { }
/// <summary>\copydoc Fact.Fact(FactOrganizer)</summary>
public AttachedPositionFunction(string fid, string[] funcids, FactOrganizer organizer) : base(organizer)
{
init(fid, funcids);
//TODO: call MMT, set URI
_URI = Fact.Id + "{" + string.Join(", ", FunctionFacts.Select(f => f.Id)) + "}";
}
private void init(string fid, string[] funcids)
{
this.fid = fid;
this.func_call_ids = new string[funcids.Length];
funcids.CopyTo(this.func_call_ids, 0);
}
protected AttachedPositionFunction(string fid, string[] funcids, string uri, FactOrganizer organizer) : base(organizer)
{
_URI = uri;
}
public new static AttachedPositionFunction parseFact(Scroll.ScrollFact fact)
{// TODO Correctness
string uri = fact.@ref.uri;
OMA df = (OMA)((Scroll.ScrollSymbolFact)fact).df;
if (df == null)
return null;
string parse_id = df.ToString();
if (!ParsingDictionary.parseTermsToId.ContainsKey(parse_id))
ParsingDictionary.parseTermsToId[parse_id] = uri;
return new AttachedPositionFunction(default, default, uri, StageStatic.stage.factState);
}
public override bool HasDependentFacts
=> true;
protected override string[] GetGetDependentFactIds()
=> new string[] { fid }.ShallowCloneAppend(func_call_ids);
public override int GetHashCode()
=> Fact.GetHashCode() ^ FunctionFacts.GetHashCode();
public override GameObject instantiateDisplay(GameObject prefab, Transform transform)
{
var obj = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity, transform);
//obj.transform.GetChild(0).gameObject.GetComponent<TextMeshProUGUI>().text = this.Label;
//obj.GetComponent<FactWrapper>().fact = this;
return obj;
}
protected override bool EquivalentWrapped(AttachedPositionFunction f1, AttachedPositionFunction f2)
{
Position = Fact.Position;
Rotation = Fact.Rotation;
LocalScale = Fact.LocalScale;
}
protected override Fact _ReInitializeMe(Dictionary<string, string> old_to_new, FactOrganizer organizer)
=> new AttachedPositionFunction(old_to_new[this.fid], this.func_call_ids.Select(id => old_to_new[id]).ToArray(), organizer);
protected override MMTDeclaration MakeMMTDeclaration()
{
throw new NotImplementedException();
}