Skip to content
Snippets Groups Projects
Fact.cs 82.9 KiB
Newer Older
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using Newtonsoft.Json;
MaZiFAU's avatar
MaZiFAU committed
using System.Linq;
using REST_JSON_API;
MaZiFAU's avatar
MaZiFAU committed
using UnityEngine.ProBuilder;
using Unity.Mathematics;
using System.Runtime.ExceptionServices;
public static class ParsingDictionary
    //TODO? get rid of this, use reflection? instead, if possible
    //TODO: docu
ki7077's avatar
ki7077 committed

    public static Dictionary<string, Func<MMTDeclaration, Fact>> parseFactDictionary = new() {
        { MMT_OMS_URI.Point,
            PointFact.parseFact },
        { MMT_OMS_URI.Metric,
            LineFact.parseFact },
        { MMT_OMS_URI.Angle,
            AngleFact.parseFact },
        { MMT_OMS_URI.Eq,
            AngleFact.parseFact },
        { MMT_OMS_URI.RightAngle,
            RightAngleFact.parseFact },
        { MMT_OMS_URI.LineType,
            RayFact.parseFact },
        { MMT_OMS_URI.LineOf,
            RayFact.parseFact },
        { MMT_OMS_URI.OnLine,
            OnLineFact.parseFact },
        { MMT_OMS_URI.ParallelLine,
            ParallelLineFact.parseFact },
        { MMT_OMS_URI.CircleType3d,
            CircleFact.parseFact },
        { MMT_OMS_URI.OnCircle,
            OnCircleFact.parseFact },
        { MMT_OMS_URI.AnglePlaneLine,
            AngleCircleLineFact.parseFact },
        { MMT_OMS_URI.RadiusCircleMetric,
            RadiusFact.parseFact },
        { MMT_OMS_URI.AreaCircle,
            AreaCircleFact.parseFact },
        { MMT_OMS_URI.OrthoCircleLine,
            OrthogonalCircleLineFact.parseFact },
        { MMT_OMS_URI.VolumeCone,
            ConeVolumeFact.parseFact },
        { MMT_OMS_URI.TruncatedVolumeCone,
            TruncatedConeVolumeFact.parseFact },
        { MMT_OMS_URI.CylinderVolume,
            CylinderVolumeFact.parseFact },
        { MMT_OMS_URI.TestType,
            TestFact.parseFact },
        { MMT_OMS_URI.EqualityCircles,
            EqualCirclesFact.parseFact },
        { MMT_OMS_URI.UnEqualityCircles,
MaZiFAU's avatar
MaZiFAU committed
            UnEqualCirclesFact.parseFact },
MaZiFAU's avatar
MaZiFAU committed
        { MMT_OMS_URI.ListLiteral,
            ListFact.parseFact },
    // TODO: get rid of this
    public static Dictionary<string, string> parseTermsToId = new();
/// <summary>
/// %Fact representation of Unity; mostly mirrors Facts of MMT.
/// </summary>
[JsonConverter(typeof(JsonSubtypes), "s_type")]
[JsonSubtypes.KnownSubType(typeof(PointFact), nameof(PointFact))]
[JsonSubtypes.KnownSubType(typeof(LineFact), nameof(LineFact))]
[JsonSubtypes.KnownSubType(typeof(RayFact), nameof(RayFact))]
[JsonSubtypes.KnownSubType(typeof(OnLineFact), nameof(OnLineFact))]
[JsonSubtypes.KnownSubType(typeof(AngleFact), nameof(AngleFact))]
[JsonSubtypes.KnownSubType(typeof(CircleFact), nameof(CircleFact))]
[JsonSubtypes.KnownSubType(typeof(ParallelLineFact), nameof(ParallelLineFact))]
[JsonSubtypes.KnownSubType(typeof(OnCircleFact), nameof(OnCircleFact))]
[JsonSubtypes.KnownSubType(typeof(AngleCircleLineFact), nameof(AngleCircleLineFact))]
MaZiFAU's avatar
MaZiFAU committed
[JsonSubtypes.KnownSubType(typeof(OrthogonalCircleLineFact), nameof(OrthogonalCircleLineFact))]
[JsonSubtypes.KnownSubType(typeof(AreaCircleFact), nameof(AreaCircleFact))]
[JsonSubtypes.KnownSubType(typeof(RadiusFact), nameof(RadiusFact))]
[JsonSubtypes.KnownSubType(typeof(ConeVolumeFact), nameof(ConeVolumeFact))]
[JsonSubtypes.KnownSubType(typeof(TruncatedConeVolumeFact), nameof(TruncatedConeVolumeFact))]
[JsonSubtypes.KnownSubType(typeof(RightAngleFact), nameof(RightAngleFact))]
[JsonSubtypes.KnownSubType(typeof(CylinderVolumeFact), nameof(CylinderVolumeFact))]
[JsonSubtypes.KnownSubType(typeof(TestFact), nameof(TestFact))]
[JsonSubtypes.KnownSubType(typeof(EqualCirclesFact), nameof(EqualCirclesFact))]
[JsonSubtypes.KnownSubType(typeof(UnEqualCirclesFact), nameof(UnEqualCirclesFact))]
[JsonSubtypes.KnownSubType(typeof(AttachedPositionFunction), nameof(AttachedPositionFunction))]
MaZiFAU's avatar
MaZiFAU committed
[JsonSubtypes.KnownSubType(typeof(FunctionFact), nameof(FunctionFact))]
[JsonSubtypes.KnownSubType(typeof(FunctionCallFact), nameof(FunctionCallFact))]
MaZiFAU's avatar
MaZiFAU committed
[JsonSubtypes.KnownSubType(typeof(ListFact), nameof(ListFact))]
MaZiFAU's avatar
MaZiFAU committed
[JsonSubtypes.KnownSubType(typeof(TupelFact), nameof(TupelFact))]
//[JsonSubtypes.KnownSubType(typeof(FunctionFact<T0, TResult>), "FunctionFact<T0, TResult>")] //TODO: generics? => nameof does not work (generic agnostic)
MaZiFAU's avatar
MaZiFAU committed
//[JsonSubtypes.KnownSubType(typeof(FunctionFactFloat<Vector3>), "FunctionFact<System.Single, UnityEngine.Vector3>")]
public abstract class Fact
    /// <summary>
    /// Reference to <c>GameObject</c> that represents this Fact in the GameWorld.
    /// </summary>
MaZiFAU's avatar
MaZiFAU committed
    /// <seealso cref="FactObject3D"/>
    public FactWrapper WorldRepresentation;
    /// <summary>
    /// Collection of <c>Type</c>s of *all* available <see cref="Fact"/>s to choose from.
    /// </summary>
    [JsonIgnore]
MaZiFAU's avatar
MaZiFAU committed
    public static readonly Type[] Types = TypeExtensions<Fact>.UAssemblyInheritenceTypes;
    /// [ClassName] for JSON de-/serialization.
    /// Automatically set in <see cref="Fact()"/> for <b>NON-Generiy-Types</b>!
    /// Also add JsonSubtypes.KnownSubType decorator for deserialization to Fact!
    [JsonProperty]
    /// <returns><see langword="true"/> if Fact depends on other \ref Fact "Facts"; equivalent to <see cref="getDependentFactIds"/> returns non empty array</returns>
    [JsonIgnore]
MaZiFAU's avatar
MaZiFAU committed
    public virtual bool HasDependentFacts => DependentFactIds.Length > 0;

    /// <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 string[] DependentFactIds => _DependentFactIds ??= GetGetDependentFactIds();
    private string[] _DependentFactIds;

    /// <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>
    protected abstract string[] GetGetDependentFactIds();

    /// <value>
    /// Unique Id. e.g.: MMT URI
    /// </value>
MaZiFAU's avatar
MaZiFAU committed
    [JsonIgnore]
    public string Id
    {
MaZiFAU's avatar
MaZiFAU committed
        get
        {
            //while (FetchURICoroutine.MoveNext()) ; //active wait for server
MaZiFAU's avatar
MaZiFAU committed
            return _URI;
        }
    //private IEnumerator FetchURICoroutine = IEnumeratorExtensions.yield_break;
MaZiFAU's avatar
MaZiFAU committed
    [JsonProperty]
    protected string _URI;
    /// <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
MaZiFAU's avatar
MaZiFAU committed
            return _Facts == null // JsonSerialization toggle (_Facts.GetNumberOfFacts() == 0 && this is not PointFact) // JsonSerialization toggle && allow first (Point)Fact to be created
                || (hasCustomLabel && _CustomLabel != null)
        set
        {
            if (_Facts == null) // JsonSerialization toggle)
MaZiFAU's avatar
MaZiFAU committed
            {
                _CustomLabel = value;
                LabelId = -LabelId;
                return;
            }
            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>
    /// <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; }
Loading
Loading full blame...