feature:add a reset method for cached model for port reset

main
taoria 3 years ago
parent 1cb16c082b
commit 4a1ada67db
  1. 24
      Samples/New HelloGraph.asset
  2. 11
      Samples/Nodes/AddNode.cs
  3. 9
      Samples/Nodes/CheckSizeNode.cs
  4. 2
      TNodeCore/Runtime/RuntimeCache/IModelPortAccessor.cs
  5. 16
      TNodeCore/Runtime/RuntimeCache/RuntimeCache.cs
  6. 7
      TNodeCore/Runtime/RuntimeModels/RuntimeNode.cs
  7. 3
      TNodeCore/Runtime/Tools/GraphTool.cs

@ -99,29 +99,29 @@ MonoBehaviour:
entryPoint: 0
isTest: 0
00000003:
type: {class: AddNode, ns: Samples.Nodes, asm: Assembly-CSharp}
type: {class: CheckSizeNode, ns: Samples.Nodes, asm: Assembly-CSharp}
data:
positionInView:
serializedVersion: 2
x: 1121
y: 187
x: 917
y: 228
width: 0
height: 0
id: 926f2eea-3403-4663-88bd-7ed16ce029fa
nodeName: AddNode
id: f236a611-cc64-4fce-88d2-40baf7f4a490
nodeName: CheckSizeNode
entryPoint: 0
isTest: 0
00000004:
type: {class: CheckSizeNode, ns: Samples.Nodes, asm: Assembly-CSharp}
type: {class: AddNode, ns: Samples.Nodes, asm: Assembly-CSharp}
data:
positionInView:
serializedVersion: 2
x: 900
y: 228
x: 1145.4003
y: 188.50003
width: 0
height: 0
id: f236a611-cc64-4fce-88d2-40baf7f4a490
nodeName: CheckSizeNode
id: 926f2eea-3403-4663-88bd-7ed16ce029fa
nodeName: AddNode
entryPoint: 0
isTest: 0
00000005:
@ -137,7 +137,7 @@ MonoBehaviour:
HelloString:
HelloGameObject: {fileID: 0}
Value:
- 11
- 24.41
- 102.1
00000006:
type: {class: GraphViewModel, ns: TNode.TNodeCore.Editor.Models, asm: Taoria.TNodeCore.Runtime}
@ -150,5 +150,5 @@ MonoBehaviour:
height: 0
id:
persistScale: 0.8695652
persistOffset: {x: -76, y: 126.00001}
persistOffset: {x: -125, y: 184}
isBlackboardOn: 1

@ -1,15 +1,18 @@
using TNodeCore.Runtime.Attributes;
using TNodeCore.Runtime;
using TNodeCore.Runtime.Attributes;
using TNodeCore.Runtime.Attributes.Ports;
using TNodeCore.Runtime.Models;
namespace Samples.Nodes{
[GraphUsage(typeof(HelloGraph),"Math")]
public class AddNode:NodeData{
[Input] public float A{ get; set; } = default;
[Input]
public float A{ get; set; }
[Input]
public float B{ get; set; }
public float B{ get; set; }= default;
[Output] public float C => A + B;
public override void Process(){
}
}
}

@ -1,6 +1,8 @@
using TNodeCore.Runtime.Attributes;
using TNodeCore.Runtime;
using TNodeCore.Runtime.Attributes;
using TNodeCore.Runtime.Attributes.Ports;
using TNodeCore.Runtime.Models;
using UnityEngine;
namespace Samples.Nodes{
[GraphUsage(typeof(HelloGraph),"Math")]
@ -24,6 +26,9 @@ namespace Samples.Nodes{
DataFunc = ()=>A
};
}
public override void Process(){
this.Log($"{A}");
}
}
}

@ -6,6 +6,8 @@ namespace TNodeCore.Runtime.RuntimeCache{
object GetValue(object model);
void SetValue(object model, object value);
void Reset(object model);
public Type Type{ get; set; }

@ -13,6 +13,8 @@ namespace TNodeCore.Runtime.RuntimeCache{
public class PortAccessor<T1, T2>:IModelPortAccessor{
public readonly Func<T1, T2> Get;
public readonly Action<T1, T2> Set;
private readonly Action<T1> _resetFunc;
private readonly T2 _defaultValue;
public PortAccessor(string name,bool property){
if (property){
Type t = typeof(T1);
@ -20,10 +22,18 @@ namespace TNodeCore.Runtime.RuntimeCache{
MethodInfo getter = t.GetMethod("get_" + name);
MethodInfo setter = t.GetMethod("set_" + name);
Type = getter?.ReturnType??setter?.GetParameters()[0].ParameterType;
if(getter!=null)
Get = (Func<T1, T2>)Delegate.CreateDelegate(typeof(Func<T1, T2>), null, getter);
if(setter!=null)
Set = (Action<T1, T2>)Delegate.CreateDelegate(typeof(Action<T1, T2>), null, setter);
if (Set != null){
var dummy = Activator.CreateInstance<T1>();
if (Get != null) _defaultValue = Get(dummy);
_resetFunc = (obj) => {
Set(obj, _defaultValue);
};
}
}
else{
Type t = typeof(T1);
@ -46,6 +56,12 @@ namespace TNodeCore.Runtime.RuntimeCache{
public void SetValue(object model, object value){
Set((T1)model,(T2)value);
}
public void Reset(object model){
//Get
_resetFunc((T1)model);
}
public Type Type{ get; set; }
}

@ -121,6 +121,12 @@ namespace TNodeCore.Runtime{
_portAccessors = RuntimeCache.RuntimeCache.Instance.CachedPortAccessors[_type];
}
public void ResetPortValue(){
foreach (var modelPortAccessor in _portAccessors){
modelPortAccessor.Value.Reset(this);
}
}
public List<string> GetInputNodesId(){
List<string> dependencies = new List<string>();
foreach (NodeLink link in InputLinks)
@ -129,7 +135,6 @@ namespace TNodeCore.Runtime{
}
return dependencies;
}
}
public enum Direction{
Input,

@ -297,7 +297,8 @@ namespace TNode.TNodeCore.Runtime.Tools{
//TODO looks like this string would be too long to make a cache
var cachedKey = $"{outNode.NodeData.id}-{nodeLink.inPort.portEntryName}";
var outValue = OutputCached.ContainsKey(cachedKey) ? OutputCached[cachedKey] : outNode.GetOutput(nodeLink.outPort.portEntryName);;
var outValue = OutputCached.ContainsKey(cachedKey) ? OutputCached[cachedKey] : outNode.GetOutput(nodeLink.outPort.portEntryName);
Debug.Log(outValue);
if (_isCachingOutput){
OutputCached[cachedKey] = outValue;
}

Loading…
Cancel
Save