Newer
Older
using System;
using System.Collections.Generic;
using static CommunicationEvents;
Marco Zimmer
committed
using JsonSubTypes;
using System.Linq.Expressions;
using UnityEngine.ProBuilder;
Marco Zimmer
committed
public class ParsingDictionary
{
//TODO? get rid of this, use reflection? instead, if possible
//TODO: docu
//public static Dictionary<string, Func<Scroll.ScrollFact, Fact>> parseFactDictionary = new Dictionary<string, Func<Scroll.ScrollFact, Fact>>() {
public static Dictionary<string, Func<Scroll.ScrollFact, Fact>> parseFactDictionary = new() {
{JSONManager.MMTURIs.Point, PointFact.parseFact},
{JSONManager.MMTURIs.Metric, LineFact.parseFact},
{JSONManager.MMTURIs.Angle, AngleFact.parseFact},
{JSONManager.MMTURIs.LineType, RayFact.parseFact},
{JSONManager.MMTURIs.LineOf, RayFact.parseFact},
{JSONManager.MMTURIs.OnLine, OnLineFact.parseFact},
{JSONManager.MMTURIs.ParallelLine, ParallelLineFact.parseFact},
{JSONManager.MMTURIs.CircleType3d, CircleFact.parseFact},
{JSONManager.MMTURIs.OnCircle, OnCircleFact.parseFact },
{JSONManager.MMTURIs.AnglePlaneLine, AngleCircleLineFact.parseFact },
{JSONManager.MMTURIs.RadiusCircleMetric, RadiusFact.parseFact },
{JSONManager.MMTURIs.AreaCircle, AreaCircleFact.parseFact },
{JSONManager.MMTURIs.OrthoCircleLine, OrthogonalCircleLineFact.parseFact },
{JSONManager.MMTURIs.VolumeCone ,ConeVolumeFact.parseFact },
{JSONManager.MMTURIs.TruncatedVolumeCone ,TruncatedConeVolumeFact.parseFact },
{JSONManager.MMTURIs.RightAngle, RightAngleFact.parseFact },
{JSONManager.MMTURIs.CylinderVolume, CylinderVolumeFact.parseFact },
{JSONManager.MMTURIs.TestType, TestFact.parseFact },
{JSONManager.MMTURIs.EqualityCircles, EqualCirclesFact.parseFact },
{JSONManager.MMTURIs.UnEqualityCircles, UnEqualCirclesFact.parseFact }
// TODO: get rid of this
public static Dictionary<string, string> parseTermsToId = new();
/// <summary>
/// class to Read AddFact Responses.
/// </summary>
// TODO: docu
public class AddFactResponse
{
public string uri;
Marco Zimmer
committed
public static bool sendAdd(MMTDeclaration mmtDecl, out string uri)
{
string body = MMTSymbolDeclaration.ToJson(mmtDecl);
return sendAdd(CommunicationEvents.ServerAdress + "/fact/add", body, out uri);
}
public static bool sendAdd(string path, string body, out string uri)
{
if (!CommunicationEvents.ServerRunning)
{
Debug.LogWarning("Server not running");
Marco Zimmer
committed
uri = null;
return false;
Marco Zimmer
committed
Marco Zimmer
committed
Debug.Log("Sending to Server:\n" + body);
//Put constructor parses stringbody to byteArray internally (goofy workaround)
Marco Zimmer
committed
using UnityWebRequest www = UnityWebRequest.Put(path, body);
www.method = UnityWebRequest.kHttpVerbPOST;
www.SetRequestHeader("Content-Type", "application/json");
www.timeout = 1;
//TODO: implement real asynchronous communication ...
AsyncOperation op = www.SendWebRequest();
while (!op.isDone) ;
if (www.result == UnityWebRequest.Result.ConnectionError
|| www.result == UnityWebRequest.Result.ProtocolError)
{
Debug.LogWarning(www.error);
Marco Zimmer
committed
uri = null;
return false;
}
else
{
string answer = www.downloadHandler.text;
Marco Zimmer
committed
AddFactResponse res = JsonUtility.FromJson<AddFactResponse>(answer);
if (VerboseURI)
Debug.Log("Server added Fact:\n" + res.uri);
uri = res.uri;
return true;
}
}
}
/// <summary>
/// %Fact representation of Unity; mostly mirrors Facts of MMT.
/// </summary>
Marco Zimmer
committed
[JsonConverter(typeof(JsonSubtypes), "s_type")]
[JsonSubtypes.KnownSubType(typeof(PointFact), "PointFact")]
[JsonSubtypes.KnownSubType(typeof(LineFact), "LineFact")]
[JsonSubtypes.KnownSubType(typeof(RayFact), "RayFact")]
[JsonSubtypes.KnownSubType(typeof(OnLineFact), "OnLineFact")]
[JsonSubtypes.KnownSubType(typeof(AngleFact), "AngleFact")]
[JsonSubtypes.KnownSubType(typeof(CircleFact), "CircleFact")]
[JsonSubtypes.KnownSubType(typeof(ParallelLineFact), "ParallelLine")]
[JsonSubtypes.KnownSubType(typeof(OnCircleFact), "OnCircleFact")]
[JsonSubtypes.KnownSubType(typeof(AngleCircleLineFact), "AnglePlaneLineFact")]
[JsonSubtypes.KnownSubType(typeof(AreaCircleFact), "AreaCircle")]
[JsonSubtypes.KnownSubType(typeof(RadiusFact), "RadiusCircleMetric")]
[JsonSubtypes.KnownSubType(typeof(OrthogonalCircleLineFact), "OrthogonalCircleLineFact")]
[JsonSubtypes.KnownSubType(typeof(ConeVolumeFact), "ConeVolumeFact")]
[JsonSubtypes.KnownSubType(typeof(TruncatedConeVolumeFact), "TruncatedConeVolumeFact")]
[JsonSubtypes.KnownSubType(typeof(RightAngleFact), "RightAngleFact")]
[JsonSubtypes.KnownSubType(typeof(CylinderVolumeFact), "CylinderVolumeFact")]
[JsonSubtypes.KnownSubType(typeof(TestFact), "TestFact")]
[JsonSubtypes.KnownSubType(typeof(EqualCirclesFact), "EqualCirclesFact")]
[JsonSubtypes.KnownSubType(typeof(UnEqualCirclesFact), "UnEqualCirclesFact")]
//[JsonSubtypes.KnownSubType(typeof(FunctionFact<T0, TResult>), "FunctionFact<T0, TResult>")] //TODO: generics?
[JsonSubtypes.KnownSubType(typeof(FunctionFact<float, float>), "FunctionFact<float, float>")]
/// <summary>
/// Reference to <c>GameObject</c> that represents this Fact in the GameWorld.
/// </summary>
/// <seealso cref="FactObject"/>
public GameObject Representation;
Marco Zimmer
committed
/// <value>
/// [ClassName] for JSON de-/serialization.
/// Set in every non-abstract subclass of Fact.
/// Also add JsonSubtypes.KnownSubType decorator for deserialization to Fact!
Marco Zimmer
committed
/// </value>
Marco Zimmer
committed
protected static /*new*/ string s_type = "ERROR: set s_type in T:Fact"; // In the subtype! NOT here!
Marco Zimmer
committed
/// <value>
/// Unique Id. e.g.: MMT URI
/// </value>
set { _URI ??= value; } // needed for JSON
/// <summary>
/// MMT URI
/// </summary>
Marco Zimmer
committed
/// <value>
/// <c>get</c> initiates and subsequently updates a human readable name. <remarks>Should be called once a constructor call to be initiated.</remarks>
/// <c>set</c> calls <see cref="rename(string)"/>
/// </value>
public string Label
{
get
{ // in case of renamed dependables
return (_Facts.GetNumberOfFacts() == 0 && this is not PointFact) // JsonSerialization toggle && allow first (Point)Fact to be created
|| (hasCustomLabel && _CustomLabel != null)
Marco Zimmer
committed
? _CustomLabel
: generateLabel();
Marco Zimmer
committed
}
set { rename(value); }
}
/// <value>
/// Is true if Fact has a custom <see cref="Label"/> which is not <c>null</c> or <c>""</c>.
/// </value>
public bool hasCustomLabel => LabelId < 0;
/// <summary>
/// Stores custom <see cref="Label"/> if set.
/// </summary>
Marco Zimmer
committed
protected string _CustomLabel = null;
/// <summary>
/// Counter to organize auto generated <see cref="Label"/>.
/// Set to negative, if custom \ref Label is assigned.
/// </summary>
// property for JSON to set AFTER Label => declare AFTER Label
public int LabelId { get; set; }
/// <summary>
/// Reference to <see cref="FactOrganizer"/> in which this Fact and all its <see cref="getDependentFactIds">depending Facts</see> are beeing organized.
/// </summary>
protected FactOrganizer _Facts;
/// <summary>
/// Only being used by [JsonReader](https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonReader.htm) to initiate empty \ref Fact "Facts".
/// <seealso cref="JSONManager"/>
/// </summary>
protected Fact()
{
this._Facts = new FactOrganizer();
/// <summary>
/// Standard base-constructor.
/// </summary>
/// <param name="organizer"><see cref="_Facts"/></param>
protected Fact(FactOrganizer organizer)
{
this._Facts = organizer;
}
/// <summary>
/// Copies <paramref name="fact"/> by initiating new MMT %Fact.
/// </summary>
/// <param name="fact">Fact to be copied</param>
/// <param name="organizer"><see cref="_Facts"/></param>
protected Fact(Fact fact, FactOrganizer organizer)
this._Facts = organizer;
if (hasCustomLabel)
_CustomLabel = fact.Label;
}
/// <summary>
/// Assignes a custom <see cref="Label"/>, if <paramref name="newLabel"/> is not yet taken;
/// or clears custom <see cref="Label"/>.
/// </summary>
/// <param name="newLabel">To be new <see cref="Label"/>. To reset to auto-generation set to <c>null</c> or <c>""</c>.</param>
/// <returns></returns>
//TODO: notify about updated dependable Labelnames for UI
//TODO: check for colissions with not yet generated names
public bool rename(string newLabel)
// returns true if succeded
{
if (string.IsNullOrEmpty(newLabel))
// switch back to autogenerated
{
generateLabel();
_CustomLabel = null;
return true;
}
else
// set CustomLabel if available
{
if (_Facts.ContainsLabel(newLabel))
return false;
freeAutoLabel();
_CustomLabel = newLabel;
Marco Zimmer
committed
/// <returns><see langword="true"/> if Fact depends on other \ref Fact "Facts"; equivalent to <see cref="getDependentFactIds"/> returns non empty array</returns>
Marco Zimmer
committed
public abstract bool hasDependentFacts();
/// <returns> array of Fact <see cref="Id"> Ids </see> on which this Fact depends.</returns>
/// <example><see cref="AngleFact"/> needs 3 <see cref="PointFact"/>s to be defined.</example>
public abstract string[] getDependentFactIds();
/// <summary>
/// Initiates a <paramref name="prefab"/> at <paramref name="transform"/> e.g. by setting <see cref="Label"/>.
/// </summary>
/// <remarks>Does not set <see cref="Representation"/>.</remarks>
/// <param name="prefab"><c>GameObject</c> Prefab that will represent this Fact</param>
/// <param name="transform"><c>Transform</c> where to initiate <paramref name="prefab"/></param>
/// <returns></returns>
// TODO: set Representation here instead of ...
public abstract GameObject instantiateDisplay(GameObject prefab, Transform transform);
/// <summary>
/// Frees ressources e.g. <see cref="Label"/> and will eventually delete %Fact Server-Side in far-near future when feature is supported.
/// </summary>
/// <param name="keep_clean">when set to <c>true</c> will upkeep <see cref="Label"/> organization.</param>
// TODO? replace by ~Fact() { }
Marco Zimmer
committed
public virtual void delete(bool keep_clean = true)
Marco Zimmer
committed
if (keep_clean)
Marco Zimmer
committed
Marco Zimmer
committed
if (VerboseURI)
Marco Zimmer
committed
Debug.Log("Server removed Fact:\n" + this.Id);
/// <summary>
/// Compares \ref Fact "this" against <paramref name="f2"/>.
/// </summary>
/// <param name="f2">Fact to compare to</param>
/// <returns><c>true</c> if <paramref name="f2"/> is semantical very similar to \ref Fact "this"</returns>
Marco Zimmer
committed
public abstract bool Equivalent(Fact f2);
/// <summary>
/// Compares <paramref name="f1"/> against <paramref name="f2"/>.
/// </summary>
/// <param name="f1">Fact to compare to</param>
/// <param name="f2">Fact to compare to</param>
/// <returns><c>true</c> if <paramref name="f2"/> is semantical very similar to <paramref name="f1"/></returns>
Marco Zimmer
committed
public abstract bool Equivalent(Fact f1, Fact f2);
/// <summary>
/// canonical
/// </summary>
/// <returns>unique-ish Hash</returns>
public abstract override int GetHashCode();
/// <summary>
/// auto-generates <see cref="Label"/> using generation variable(s) e.g. <see cref="LabelId"/>;
/// if custom <see cref="Label"/> is set, tries to restore original generated <see cref="Label"/> **without** resetting <see cref="_CustomLabel"/>. If original <see cref="Label"/> is already taken, a new one will be generated.
/// </summary>
/// <returns>auto-generated <see cref="Label"/></returns>
Marco Zimmer
committed
protected virtual string generateLabel()
{
LabelId = _Facts.UnusedLabelIds.Remove(-LabelId) ? -LabelId : 0;
Marco Zimmer
committed
if (LabelId == 0)
if (_Facts.UnusedLabelIds.Count == 0)
LabelId = ++_Facts.MaxLabelId;
Marco Zimmer
committed
else
{
LabelId = _Facts.UnusedLabelIds.Min;
_Facts.UnusedLabelIds.Remove(LabelId);
Marco Zimmer
committed
}
return ((char)(64 + LabelId)).ToString();
}
/// <summary>
/// Parses <see cref="Scroll.ScrollFact"/> to actual Fact
/// </summary>
/// <param name="fact">instance to be parsed</param>
/// <returns>parsed Fact</returns>
public static Fact parseFact(Scroll.ScrollFact fact)
/// <summary>
/// Tells <see cref="_Facts"/> that \ref Fact "this" no longer uses auto-generated <see cref="Label"/>, but remembers current generation variable(s).
/// </summary>
// TODO? only get _Fact to freeLabel/
public /*protected internal*/ void freeAutoLabel()
{
if (LabelId > 0)
_Facts.UnusedLabelIds.Add(LabelId);
// store Label for name-persistance
Marco Zimmer
committed
LabelId = -LabelId;
}
/// <summary>
/// Implements CRTP for <see cref="Fact"/>; Escalates constructors;
/// </summary>
/// <typeparam name="T">class, which inherits from FactWrappedCRTP</typeparam>
public abstract class FactWrappedCRTP<T> : Fact where T : FactWrappedCRTP<T>
/// <summary>\copydoc Fact.Fact()</summary>
protected FactWrappedCRTP() : base() { }
/// <summary>\copydoc Fact.Fact(FactOrganizer)</summary>
protected FactWrappedCRTP(FactOrganizer organizer) : base(organizer) { }
/// <summary>\copydoc Fact.Fact(Fact, FactOrganizer)</summary>
protected FactWrappedCRTP(FactWrappedCRTP<T> fact, FactOrganizer organizer) : base(fact, organizer) { }
/// \copydoc Fact.Equivalent(Fact)
Marco Zimmer
committed
public override bool Equivalent(Fact f2)
Marco Zimmer
committed
/// \copydoc Fact.Equivalent(Fact, Fact)
Marco Zimmer
committed
public override bool Equivalent(Fact f1, Fact f2)
=> f1.GetType() == f2.GetType() && EquivalentWrapped((T)f1, (T)f2);
Marco Zimmer
committed
/// <summary>CRTP step of <see cref="Equivalent(Fact)"/> and <see cref="Equivalent(Fact, Fact)"/></summary>
Marco Zimmer
committed
protected abstract bool EquivalentWrapped(T f1, T f2);
}
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// TODO:
/// <summary>
/// Represents a function taking Parameters (<typeparam name="T0">) and Returning <typeparam name="TResult">
/// </summary>
public class FunctionFact<T0, TResult> : FactWrappedCRTP<FunctionFact<T0, TResult>>
{
/// \copydoc Fact.s_type
[JsonProperty]
protected static new string s_type = "FunctionFact<" + nameof(T0) + ", " + nameof(TResult) + ">";
//TODO: doc
public LambdaExpression function_expression { get; private set; }
public Func<T0, TResult> function { get; private set; }
//TODO
public Tuple<T0, T0> domain { get; private set; }
/// <summary> \copydoc Fact.Fact </summary>
public FunctionFact() : base()
{ //TODO: test
function_expression = (Expression<Func<T0, TResult>>)((T0 x) => default(TResult));
function = function_expression.Compile() as Func<T0, TResult>;
}
/// <summary>
/// Copies <paramref name="fact"/> by initiating new MMT %Fact.
/// </summary>
/// <param name="fact">Fact to be copied</param>
/// <param name="old_to_new"><c>Dictionary</c> mapping <paramref name="fact"/>.<see cref="getDependentFactIds"/> in <paramref name="fact"/>.<see cref="Fact._Facts"/> to corresponding <see cref="Fact.Id"/> in <paramref name="organizer"/> </param>
/// <param name="organizer">sets <see cref="_Facts"/></param>
public FunctionFact(FunctionFact<T0, TResult> fact, Dictionary<string, string> old_to_new, FactOrganizer organizer) : base(fact, organizer)
=> init(fact.function_expression);
/// <summary>
/// Standard Constructor
/// </summary>
/// <param name="function_expression">sets <see cref="function_expression"/> and contains the Abstract Syntax Tree</param>
/// <param name="organizer">sets <see cref="Fact._Facts"/></param>
public FunctionFact(LambdaExpression function_expression, FactOrganizer organizer) : base(organizer)
=> init(function_expression);
/// <summary>
/// Initiates <see cref="function_expression"/>, <see cref="function"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
/// </summary>
/// <param name="function_expression">sets <see cref="function_expression"/> and contains the Abstract Syntax Tree</param>
private void init(LambdaExpression function_expression)
{
this.function_expression = function_expression;
//TODO: catch
this.function = function_expression.Compile() as Func<T0, TResult>;
return;
////TODO: how!? Visitor?
//List<MMTTerm> arguments = new()
//{
// //new OMF(P.x),
// //new OMF(P.y),
// //new OMF(P.z)
//};
//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;
}
/// <summary>
/// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
/// </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(LambdaExpression function_expression, string uri, FactOrganizer organizer) : base(organizer)
{
this.function_expression = function_expression;
//TODO: catch
this.function = function_expression.Compile() as Func<T0, TResult>;
this._URI = uri;
_ = this.Label;
}
/// \copydoc Fact.parseFact(Scroll.ScrollFact)
public new static FunctionFact<T0, TResult> parseFact(Scroll.ScrollFact fact)
{
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 FunctionFact<T0, TResult>(df.GetLambdaExpression(), uri, StageStatic.stage.factState);
}
/// \copydoc Fact.hasDependentFacts
public override bool hasDependentFacts()
=> false;
/// \copydoc Fact.getDependentFactIds
public override string[] getDependentFactIds()
=> new string[] { };
/// \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<T0, TResult> f1, FunctionFact<T0, TResult> f2)
=> Neleus.LambdaCompare.Lambda.ExpressionsEqual(f1.function_expression, f2.function_expression);
}
/// <summary>
/// Base-class for 1D-Facts
/// </summary>
public abstract class AbstractLineFact : FactWrappedCRTP<AbstractLineFact>
Marco Zimmer
committed
{
/// @{ <summary>
/// One <see cref="Fact.Id">Id</see> of two <see cref="PointFact"/> defining <see cref="Dir"/>.
/// </summary>
/// @}
Marco Zimmer
committed
/// <summary>
/// Normalized Direction from <see cref="Pid1"/> to <see cref="Pid2"/>.
/// </summary>
public Vector3 Dir;
Marco Zimmer
committed
/// <summary>
/// \copydoc Fact.Fact()
/// </summary>
protected AbstractLineFact() : base()
{
Pid1 = null;
Pid2 = null;
Dir = Vector3.zero;
}
/// <summary>
/// Copies <paramref name="fact"/> by initiating new MMT %Fact.
/// </summary>
/// <param name="fact">Fact to be copied</param>
/// <param name="old_to_new"><c>Dictionary</c> mapping <paramref name="fact"/>.<see cref="getDependentFactIds"/> in <paramref name="fact"/>.<see cref="Fact._Facts"/> to corresponding <see cref="Fact.Id"/> in <paramref name="organizer"/> </param>
/// <param name="organizer">sets <see cref="_Facts"/></param>
protected AbstractLineFact(AbstractLineFact fact, Dictionary<string, string> old_to_new, FactOrganizer organizer) : base(fact, organizer)
{
set_public_members(old_to_new[fact.Pid1], old_to_new[fact.Pid2]);
}
/// <summary>
/// Standard Constructor
/// </summary>
/// <param name="pid1">sets <see cref="AbstractLineFact.Pid1"/></param>
/// <param name="pid2">sets <see cref="AbstractLineFact.Pid2"/></param>
/// <param name="organizer">sets <see cref="Fact._Facts"/></param>
protected AbstractLineFact(string pid1, string pid2, FactOrganizer organizer) : base(organizer)
{
set_public_members(pid1, pid2);
}
/// <summary>
/// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
/// </summary>
/// <param name="pid1">sets <see cref="Pid1"/></param>
/// <param name="pid2">sets <see cref="Pid2"/></param>
/// <param name="backendURI">MMT URI</param>
/// <param name="organizer">sets <see cref="Fact._Facts"/></param>
protected AbstractLineFact(string pid1, string pid2, string backendURI, FactOrganizer organizer) : base(organizer)
{
set_public_members(pid1, pid2);
this._URI = backendURI;
}
Marco Zimmer
committed
/// <summary>
/// Initiates <see cref="Pid1"/>, <see cref="Pid2"/>, <see cref="Dir"/>
/// </summary>
/// <param name="pid1">sets <see cref="Pid1"/></param>
/// <param name="pid2">sets <see cref="Pid2"/></param>
private void set_public_members(string pid1, string pid2)
Marco Zimmer
committed
{
this.Pid1 = pid1;
this.Pid2 = pid2;
PointFact pf1 = _Facts[pid1] as PointFact;
PointFact pf2 = _Facts[pid2] as PointFact;
Marco Zimmer
committed
this.Dir = (pf2.Point - pf1.Point).normalized;
}
/// \copydoc Fact.hasDependentFacts
Marco Zimmer
committed
public override bool hasDependentFacts()
{
return true;
}
/// \copydoc Fact.getDependentFactIds
public override string[] getDependentFactIds()
Marco Zimmer
committed
{
Marco Zimmer
committed
}
/// \copydoc Fact.GetHashCode
Marco Zimmer
committed
public override int GetHashCode()
{
return this.Pid1.GetHashCode() ^ this.Pid2.GetHashCode();
Marco Zimmer
committed
}
}
/// <summary>
/// Implements CRTP for <see cref="AbstractLineFact"/>; Escalates constructors;
/// </summary>
/// <typeparam name="T">class, which inherits from AbstractLineFactWrappedCRTP</typeparam>
public abstract class AbstractLineFactWrappedCRTP<T> : AbstractLineFact where T : AbstractLineFactWrappedCRTP<T>
Marco Zimmer
committed
{
/// <summary>\copydoc Fact.Fact</summary>
protected AbstractLineFactWrappedCRTP() : base() { }
Marco Zimmer
committed
/// <summary>\copydoc AbstractLineFact.AbstractLineFact(AbstractLineFact, Dictionary{string, string}, FactOrganizer)</summary>
protected AbstractLineFactWrappedCRTP(AbstractLineFactWrappedCRTP<T> fact, Dictionary<string, string> old_to_new, FactOrganizer organizer) : base(fact, old_to_new, organizer) { }
/// <summary>\copydoc AbstractLineFact.AbstractLineFact(string, string, FactOrganizer)</summary>
protected AbstractLineFactWrappedCRTP(string pid1, string pid2, FactOrganizer organizer) : base(pid1, pid2, organizer) { }
/// <summary>\copydoc AbstractLineFact.AbstractLineFact(string, string, string, FactOrganizer)</summary>
protected AbstractLineFactWrappedCRTP(string pid1, string pid2, string backendURI, FactOrganizer organizer) : base(pid1, pid2, backendURI, organizer) { }
Marco Zimmer
committed
/// \copydoc Fact.Equivalent(Fact, Fact)
Marco Zimmer
committed
protected override bool EquivalentWrapped(AbstractLineFact f1, AbstractLineFact f2)
=> EquivalentWrapped((T)f1, (T)f2);
Marco Zimmer
committed
/// <summary>CRTP step of <see cref="EquivalentWrapped(AbstractLineFact, AbstractLineFact)"/></summary>
Marco Zimmer
committed
protected abstract bool EquivalentWrapped(T f1, T f2);
/// <summary>
/// Point in 3D Space
/// </summary>
Marco Zimmer
committed
public class PointFact : FactWrappedCRTP<PointFact>
Marco Zimmer
committed
/// \copydoc Fact.s_type
Marco Zimmer
committed
protected static new string s_type = "PointFact";
Marco Zimmer
committed
/// <summary> Position </summary>
/// <summary> Orientation for <see cref="Fact.Representation"/> </summary>
public Vector3 Normal;
/// <summary> \copydoc Fact.Fact </summary>
public PointFact() : base()
{
this.Point = Vector3.zero;
this.Normal = Vector3.zero;
}
/// <summary>
/// Copies <paramref name="fact"/> by initiating new MMT %Fact.
/// </summary>
/// <param name="fact">Fact to be copied</param>
/// <param name="old_to_new"><c>Dictionary</c> mapping <paramref name="fact"/>.<see cref="getDependentFactIds"/> in <paramref name="fact"/>.<see cref="Fact._Facts"/> to corresponding <see cref="Fact.Id"/> in <paramref name="organizer"/> </param>
/// <param name="organizer">sets <see cref="_Facts"/></param>
public PointFact(PointFact fact, Dictionary<string, string> old_to_new, FactOrganizer organizer) : base(fact, organizer)
/// <summary>
/// Standard Constructor
/// </summary>
/// <param name="P">sets <see cref="Point"/></param>
/// <param name="N">sets <see cref="Normal"/></param>
/// <param name="organizer">sets <see cref="Fact._Facts"/></param>
public PointFact(Vector3 P, Vector3 N, FactOrganizer organizer) : base(organizer)
/// <summary>
/// Initiates <see cref="Point"/>, <see cref="Normal"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
/// </summary>
/// <param name="P">sets <see cref="Point"/></param>
/// <param name="N">sets <see cref="Normal"/></param>
private void init(Vector3 P, Vector3 N)
this.Point = P;
this.Normal = N;
MMTTerm tp = new OMS(MMTURIs.Point);
MMTTerm df = new OMA(
new OMS(MMTURIs.Tuple),
new List<MMTTerm> {
new OMF(P.x),
new OMF(P.y),
new OMF(P.z),
}
);
AddFactResponse.sendAdd(new MMTSymbolDeclaration(Label, tp, df), out _URI);
ParsingDictionary.parseTermsToId[df.ToString()] = _URI;
/// <summary>
/// Bypasses initialization of new MMT %Fact by using existend URI, _which is not checked for existence_.
/// <see cref="Normal"/> set to <c>Vector3.up</c>
/// </summary>
/// <param name="a">sets <c>x</c> coordinate of <see cref="Point"/></param>
/// <param name="b">sets <c>y</c> coordinate of <see cref="Point"/></param>
/// <param name="c">sets <c>z</c> coordinate of <see cref="Point"/></param>
/// <param name="uri">MMT URI</param>
/// <param name="organizer">sets <see cref="Fact._Facts"/></param>
public PointFact(float a, float b, float c, string uri, FactOrganizer organizer) : base(organizer)
this.Normal = Vector3.up;
/// \copydoc Fact.parseFact(Scroll.ScrollFact)
public new static PointFact parseFact(Scroll.ScrollFact fact)
{
string uri = fact.@ref.uri;
OMA df = (OMA)((Scroll.ScrollSymbolFact)fact).df;
string parse_id = df.ToString();
if (!ParsingDictionary.parseTermsToId.ContainsKey(parse_id))
ParsingDictionary.parseTermsToId[parse_id] = uri;
float a = ((OMF)df.arguments[0]).f;
float b = ((OMF)df.arguments[1]).f;
float c = ((OMF)df.arguments[2]).f;
return new PointFact(a, b, c, uri, StageStatic.stage.factState);
/// \copydoc Fact.hasDependentFacts
public override Boolean hasDependentFacts()
=> false;
/// \copydoc Fact.getDependentFactIds
public override string[] getDependentFactIds()
=> new string[] { };
/// \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.GetHashCode
Marco Zimmer
committed
public override int GetHashCode()
=> this.Point.GetHashCode() ^ this.Normal.GetHashCode();
/// \copydoc Fact.Equivalent(Fact, Fact)
Marco Zimmer
committed
protected override bool EquivalentWrapped(PointFact f1, PointFact f2)
=> Math3d.IsApproximatelyEqual(f1.Point, f2.Point);
/// <summary>
/// Line within 3D Space of finite length
/// </summary>
Marco Zimmer
committed
public class LineFact : AbstractLineFactWrappedCRTP<LineFact>
Marco Zimmer
committed
/// \copydoc Fact.s_type
Marco Zimmer
committed
protected static new string s_type = "LineFact";
Marco Zimmer
committed
/// <summary> Distance between <see cref="AbstractLineFact.Pid1"/> and <see cref="AbstractLineFact.Pid2"/></summary>
public float Distance;
/// <summary> \copydoc Fact.Fact </summary>
public LineFact() : base()
{
Distance = 0;
}
/// <summary> \copydoc AbstractLineFact.AbstractLineFact(AbstractLineFact, Dictionary<string, string>, FactOrganizer) </summary>
public LineFact(LineFact fact, Dictionary<string, string> old_to_new, FactOrganizer organizer) : base(fact, old_to_new, organizer)
=> init(old_to_new[fact.Pid1], old_to_new[fact.Pid2]);
/// <summary> \copydoc AbstractLineFact.AbstractLineFact(string, string, string, FactOrganizer) </summary>
public LineFact(string pid1, string pid2, string backendURI, FactOrganizer organizer) : base(pid1, pid2, backendURI, organizer)
{
SetDistance();
}
/// <summary> \copydoc AbstractLineFact.AbstractLineFact(string, string, FactOrganizer) </summary>
public LineFact(string pid1, string pid2, FactOrganizer organizer) : base(pid1, pid2, organizer)
/// <summary>
/// Initiates <see cref="AbstractLineFact.Pid1"/>, <see cref="AbstractLineFact.Pid2"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
/// </summary>
/// <param name="pid1">sets <see cref="AbstractLineFact.Pid1"/></param>
/// <param name="pid2">sets <see cref="AbstractLineFact.Pid2"/></param>
private void init(string pid1, string pid2)
SetDistance();
PointFact pf1 = _Facts[pid1] as PointFact;
PointFact pf2 = _Facts[pid2] as PointFact;
MMTTerm valueTp = new OMS(MMTURIs.RealLit);
John Schihada
committed
MMTTerm value = new OMF(v);
MMTValueDeclaration mmtDecl = new (this.Label, lhs, valueTp, value);
Marco Zimmer
committed
AddFactResponse.sendAdd(mmtDecl, out this._URI);
/// \copydoc Fact.parseFact(Scroll.ScrollFact)
public new static LineFact parseFact(Scroll.ScrollFact fact)
string uri = fact.@ref.uri;
string pointAUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[0]).uri;
string pointBUri = ((OMS)((OMA)((Scroll.ScrollValueFact)fact).lhs).arguments[1]).uri;
Marco Zimmer
committed
if (StageStatic.stage.factState.ContainsKey(pointAUri)
&& StageStatic.stage.factState.ContainsKey(pointBUri))
return new LineFact(pointAUri, pointBUri, uri, StageStatic.stage.factState);
//If dependent facts do not exist return null
/// \copydoc Fact.generateLabel
protected override string generateLabel()
=> "[" + _Facts[Pid1].Label + _Facts[Pid2].Label + "]";
/// \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 = _Facts[this.Pid1].Label;
obj.transform.GetChild(1).gameObject.GetComponent<TextMeshProUGUI>().text = _Facts[this.Pid2].Label;
obj.GetComponent<FactWrapper>().fact = this;
return obj;
}
/// \copydoc Fact.Equivalent(Fact, Fact)
Marco Zimmer
committed
protected override bool EquivalentWrapped(LineFact f1, LineFact f2)
Marco Zimmer
committed
if ((f1.Pid1 == f2.Pid1 && f1.Pid2 == f2.Pid2))// ||
//(f1.Pid1 == f2.Pid2 && f1.Pid2 == f2.Pid1))
return true;
PointFact p1f1 = (PointFact)_Facts[f1.Pid1];
PointFact p2f1 = (PointFact)_Facts[f1.Pid2];
PointFact p1f2 = (PointFact)_Facts[f2.Pid1];
PointFact p2f2 = (PointFact)_Facts[f2.Pid2];
Marco Zimmer
committed
return (p1f1.Equivalent(p1f2) && p2f1.Equivalent(p2f2))
;//|| (p1f1.Equivalent(p2f2) && p2f1.Equivalent(p1f2));
/// <summary> Calculates and sets <see cref="Distance"/>; <remarks> <see cref="AbstractLineFact.Pid1"/> and <see cref="AbstractLineFact.Pid2"/> needs to be set first.</remarks></summary>
private void SetDistance()
=> this.Distance = Vector3.Distance(((PointFact)_Facts[Pid1]).Point, ((PointFact)_Facts[Pid2]).Point);
/// <summary>
/// Ray within 3D Space of infinite length
/// </summary>
Marco Zimmer
committed
public class RayFact : AbstractLineFactWrappedCRTP<RayFact>
Marco Zimmer
committed
/// \copydoc Fact.s_type
Marco Zimmer
committed
protected static new string s_type = "RayFact";
Marco Zimmer
committed
/// <summary> \copydoc Fact.Fact </summary>
public RayFact() : base() { }
/// <summary> \copydoc AbstractLineFact.AbstractLineFact(AbstractLineFact, Dictionary<string, string>, FactOrganizer) </summary>
public RayFact(RayFact fact, Dictionary<string, string> old_to_new, FactOrganizer organizer) : base(fact, old_to_new, organizer)
=> init(old_to_new[fact.Pid1], old_to_new[fact.Pid2]);
/// <summary> \copydoc AbstractLineFact.AbstractLineFact(string, string, string, FactOrganizer) </summary>
public RayFact(string pid1, string pid2, string backendURI, FactOrganizer organizer) : base(pid1, pid2, backendURI, organizer)
/// <summary> \copydoc AbstractLineFact.AbstractLineFact(string, string, FactOrganizer) </summary>
public RayFact(string pid1, string pid2, FactOrganizer organizer) : base(pid1, pid2, organizer)
/// <summary>
/// Initiates <see cref="AbstractLineFact.Pid1"/>, <see cref="AbstractLineFact.Pid2"/>, <see cref="Fact._URI"/> and creates MMT %Fact Server-Side
/// </summary>
/// <param name="pid1">sets <see cref="AbstractLineFact.Pid1"/></param>
/// <param name="pid2">sets <see cref="AbstractLineFact.Pid2"/></param>
private void init(string pid1, string pid2)
MMTTerm tp = new OMS(MMTURIs.LineType);
MMTTerm df = new OMA(
new OMS(MMTURIs.LineOf),
new List<MMTTerm> {
new OMS(pid1),
new OMS(pid2)
});
AddFactResponse.sendAdd(new MMTSymbolDeclaration(this.Label, tp, df), out this._URI);
ParsingDictionary.parseTermsToId[df.ToString()] = this._URI;
/// \copydoc Fact.parseFact(Scroll.ScrollFact)
public new static RayFact parseFact(Scroll.ScrollFact fact)
{
if ((OMA)((Scroll.ScrollSymbolFact)fact).df == null)
return null;
string pointAUri = ((OMS)((OMA)((Scroll.ScrollSymbolFact)fact).df).arguments[0]).uri;
string pointBUri = ((OMS)((OMA)((Scroll.ScrollSymbolFact)fact).df).arguments[1]).uri;
if (StageStatic.stage.factState.ContainsKey(pointAUri)
Marco Zimmer
committed
&& StageStatic.stage.factState.ContainsKey(pointBUri))
return new RayFact(pointAUri, pointBUri, uri, StageStatic.stage.factState);
//If dependent facts do not exist return null
else return null;
/// \copydoc Fact.generateLabel
protected override string generateLabel()
{
// TODO this string is too large to properly depict on scrolls.
// return "]" + _Facts[Pid1].Label + _Facts[Pid2].Label + "[";
return _Facts[Pid1].Label + _Facts[Pid2].Label;
/// \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.Equivalent(Fact, Fact)
Marco Zimmer
committed
protected override bool EquivalentWrapped(RayFact f1, RayFact f2)
Marco Zimmer
committed
if (!Math3d.IsApproximatelyParallel(f1.Dir, f2.Dir))
PointFact p1f1 = (PointFact)_Facts[f1.Pid1];
PointFact p1f2 = (PointFact)_Facts[f2.Pid1];
PointFact p2f2 = (PointFact)_Facts[f2.Pid2];
Marco Zimmer
committed
return Math3d.IsPointApproximatelyOnLine(p1f1.Point, f1.Dir, p1f2.Point)
&& Math3d.IsPointApproximatelyOnLine(p1f1.Point, f1.Dir, p2f2.Point);
/// <summary>
/// A <see cref="PointFact"/> on a <see cref="AbstractLineFact"/>
/// </summary>
Marco Zimmer
committed
public class OnLineFact : FactWrappedCRTP<OnLineFact>
{
Marco Zimmer
committed
/// \copydoc Fact.s_type
Marco Zimmer
committed
protected static new string s_type = "OnLineFact";
Marco Zimmer
committed
public string
/// <summary> <see cref="PointFact"/>.<see cref="Fact.Id">Id</see> </summary>
Pid,
/// <summary> <see cref="AbstractLineFact"/>.<see cref="Fact.Id">Id</see> </summary>
Rid;
/// <summary> \copydoc Fact.Fact </summary>
public OnLineFact() : base()
{
this.Pid = null;
this.Rid = null;
}
/// <summary>