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. 7
      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 entryPoint: 0
isTest: 0 isTest: 0
00000003: 00000003:
type: {class: AddNode, ns: Samples.Nodes, asm: Assembly-CSharp} type: {class: CheckSizeNode, ns: Samples.Nodes, asm: Assembly-CSharp}
data: data:
positionInView: positionInView:
serializedVersion: 2 serializedVersion: 2
x: 1121 x: 917
y: 187 y: 228
width: 0 width: 0
height: 0 height: 0
id: 926f2eea-3403-4663-88bd-7ed16ce029fa id: f236a611-cc64-4fce-88d2-40baf7f4a490
nodeName: AddNode nodeName: CheckSizeNode
entryPoint: 0 entryPoint: 0
isTest: 0 isTest: 0
00000004: 00000004:
type: {class: CheckSizeNode, ns: Samples.Nodes, asm: Assembly-CSharp} type: {class: AddNode, ns: Samples.Nodes, asm: Assembly-CSharp}
data: data:
positionInView: positionInView:
serializedVersion: 2 serializedVersion: 2
x: 900 x: 1145.4003
y: 228 y: 188.50003
width: 0 width: 0
height: 0 height: 0
id: f236a611-cc64-4fce-88d2-40baf7f4a490 id: 926f2eea-3403-4663-88bd-7ed16ce029fa
nodeName: CheckSizeNode nodeName: AddNode
entryPoint: 0 entryPoint: 0
isTest: 0 isTest: 0
00000005: 00000005:
@ -137,7 +137,7 @@ MonoBehaviour:
HelloString: HelloString:
HelloGameObject: {fileID: 0} HelloGameObject: {fileID: 0}
Value: Value:
- 11 - 24.41
- 102.1 - 102.1
00000006: 00000006:
type: {class: GraphViewModel, ns: TNode.TNodeCore.Editor.Models, asm: Taoria.TNodeCore.Runtime} type: {class: GraphViewModel, ns: TNode.TNodeCore.Editor.Models, asm: Taoria.TNodeCore.Runtime}
@ -150,5 +150,5 @@ MonoBehaviour:
height: 0 height: 0
id: id:
persistScale: 0.8695652 persistScale: 0.8695652
persistOffset: {x: -76, y: 126.00001} persistOffset: {x: -125, y: 184}
isBlackboardOn: 1 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.Attributes.Ports;
using TNodeCore.Runtime.Models; using TNodeCore.Runtime.Models;
namespace Samples.Nodes{ namespace Samples.Nodes{
[GraphUsage(typeof(HelloGraph),"Math")] [GraphUsage(typeof(HelloGraph),"Math")]
public class AddNode:NodeData{ public class AddNode:NodeData{
[Input] public float A{ get; set; } = default;
[Input] [Input]
public float A{ get; set; } public float B{ get; set; }= default;
[Input]
public float B{ get; set; }
[Output] public float C => A + B; [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.Attributes.Ports;
using TNodeCore.Runtime.Models; using TNodeCore.Runtime.Models;
using UnityEngine;
namespace Samples.Nodes{ namespace Samples.Nodes{
[GraphUsage(typeof(HelloGraph),"Math")] [GraphUsage(typeof(HelloGraph),"Math")]
@ -25,5 +27,8 @@ namespace Samples.Nodes{
}; };
} }
public override void Process(){
this.Log($"{A}");
}
} }
} }

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

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

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

@ -297,7 +297,8 @@ namespace TNode.TNodeCore.Runtime.Tools{
//TODO looks like this string would be too long to make a cache //TODO looks like this string would be too long to make a cache
var cachedKey = $"{outNode.NodeData.id}-{nodeLink.inPort.portEntryName}"; 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){ if (_isCachingOutput){
OutputCached[cachedKey] = outValue; OutputCached[cachedKey] = outValue;
} }

Loading…
Cancel
Save