mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-05 19:12:08 -10:00
802 lines
29 KiB
Rust
802 lines
29 KiB
Rust
mod migration;
|
|
pub mod value;
|
|
|
|
pub use migration::{NodeExportVersions, NodeOutput, OldDocumentNode, OldDocumentNodeImplementation, OldDocumentNodeMetadata, OldNodeNetwork, OldPreviewing, OldRootNode};
|
|
|
|
use crate::document::value::TaggedValue;
|
|
use core_types::memo::MemoHashGuard;
|
|
pub use core_types::uuid::NodeId;
|
|
pub use core_types::uuid::generate_uuid;
|
|
use core_types::{Context, Cow, MemoHash, NodeCompileMetadata, NodeParameter, ProtoNodeIdentifier, Type};
|
|
use dyn_any::DynAny;
|
|
use glam::IVec2;
|
|
use log::Metadata;
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
|
use std::collections::HashMap;
|
|
use std::hash::Hash;
|
|
|
|
/// Utility function for providing a default boolean value to serde.
|
|
#[inline(always)]
|
|
fn return_true() -> bool {
|
|
true
|
|
}
|
|
|
|
/// An instance of a [`DocumentNodeDefinition`] that has been instantiated in a [`NodeNetwork`].
|
|
/// Currently, when an instance is made, it lives all on its own without any lasting connection to the definition.
|
|
/// But we will want to change it in the future so it merely references its definition.
|
|
#[derive(Clone, Debug, PartialEq, DynAny, serde::Serialize, serde::Deserialize)]
|
|
pub struct DocumentNode {
|
|
/// The inputs to a node, which are either:
|
|
/// - From other nodes within this graph [`NodeInput::Node`],
|
|
/// - A constant value [`NodeInput::Value`],
|
|
/// - A [`NodeInput::Import`] which specifies that this input comes from outside the graph, which is resolved in the graph flattening step in the case of nested networks.
|
|
///
|
|
/// In the root network, it is resolved when evaluating the borrow tree.
|
|
/// Ensure the click target in the encapsulating network is updated when the inputs cause the node shape to change (currently only when exposing/hiding an input)
|
|
/// by using network.update_click_target(node_id).
|
|
#[cfg_attr(target_family = "wasm", serde(alias = "outputs"))]
|
|
pub inputs: Vec<NodeInput>,
|
|
/// Type of the argument which this node can be evaluated with.
|
|
#[serde(default, alias = "manual_composition", deserialize_with = "migration::migrate_call_argument")]
|
|
pub call_argument: Type,
|
|
// A nested document network or a proto-node identifier.
|
|
pub implementation: DocumentNodeImplementation,
|
|
/// Represents the eye icon for hiding/showing the node in the graph UI. When hidden, a node gets replaced with a passthrough node during the graph flattening step.
|
|
#[serde(default = "return_true")]
|
|
pub visible: bool,
|
|
#[serde(skip)]
|
|
pub compile_metadata: Option<NodeCompileMetadata>,
|
|
/// The path to this node and its inputs and outputs as of when [`NodeNetwork::generate_node_paths`] was called.
|
|
#[serde(skip)]
|
|
pub original_location: OriginalLocation,
|
|
}
|
|
|
|
/// Represents the original location of a node input/output when [`NodeNetwork::generate_node_paths`] was called, allowing the types and errors to be derived.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, DynAny, serde::Serialize, serde::Deserialize)]
|
|
pub struct Source {
|
|
pub node: Vec<NodeId>,
|
|
pub index: usize,
|
|
}
|
|
|
|
/// The path to this node and its inputs and outputs as of when [`NodeNetwork::generate_node_paths`] was called.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, DynAny, Default, serde::Serialize, serde::Deserialize)]
|
|
#[non_exhaustive]
|
|
pub struct OriginalLocation {
|
|
/// The original location to the document node - e.g. [grandparent_id, parent_id, node_id].
|
|
pub path: Option<Vec<NodeId>>,
|
|
/// List of nodes which depend on this node
|
|
pub dependants: Vec<Vec<NodeId>>,
|
|
/// For automatically inserted Convert and Into nodes, if there is an error, display it on the node it is connect to.
|
|
pub auto_convert_index: Option<usize>,
|
|
}
|
|
|
|
impl Default for DocumentNode {
|
|
fn default() -> Self {
|
|
Self {
|
|
inputs: Default::default(),
|
|
call_argument: concrete!(Context),
|
|
implementation: Default::default(),
|
|
visible: true,
|
|
original_location: OriginalLocation::default(),
|
|
compile_metadata: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl DocumentNode {
|
|
/// The input slot named by the given parameter symbol, e.g. `node.input(stroke::WeightInput)`.
|
|
pub fn input<P: NodeParameter>(&self, _parameter: P) -> Option<&NodeInput> {
|
|
self.inputs.get(P::INDEX)
|
|
}
|
|
|
|
/// Mutable access to the input slot named by the given parameter symbol.
|
|
pub fn input_mut<P: NodeParameter>(&mut self, _parameter: P) -> Option<&mut NodeInput> {
|
|
self.inputs.get_mut(P::INDEX)
|
|
}
|
|
|
|
/// The stored value of the given parameter, if that input currently holds a value rather than a wire.
|
|
pub fn input_value<P: NodeParameter>(&self, parameter: P) -> Option<&TaggedValue> {
|
|
self.input(parameter)?.as_value()
|
|
}
|
|
|
|
/// Normalizes this node's stored types (call argument, `Import` input types, `TypeDefault` value payloads, and any nested network) to their structural form.
|
|
/// Applied once at ingestion (document migration and clipboard paste) so no name-encoded ranked type enters a live document.
|
|
pub fn normalize_stored_types(&mut self) {
|
|
self.call_argument = self.call_argument.clone().normalize_rank();
|
|
for input in &mut self.inputs {
|
|
normalize_input_stored_type(input);
|
|
}
|
|
if let Some(network) = self.implementation.get_network_mut() {
|
|
network.normalize_stored_types();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Represents the possible inputs to a node.
|
|
#[derive(Debug, Clone, PartialEq, core_types::CacheHash, DynAny, serde::Serialize, serde::Deserialize)]
|
|
pub enum NodeInput {
|
|
/// A reference to another node in the same network from which this node can receive its input.
|
|
Node { node_id: NodeId, output_index: usize },
|
|
|
|
/// A hardcoded value that can't change after the graph is compiled. Gets converted into a value node during graph compilation.
|
|
Value {
|
|
#[cfg_attr(feature = "loading", serde(deserialize_with = "crate::document::value::deserialize_tagged_value_with_legacy_migration"))]
|
|
tagged_value: MemoHash<TaggedValue>,
|
|
exposed: bool,
|
|
},
|
|
|
|
// TODO: Remove import_type and get type from parent node input
|
|
/// Input that is provided by the import from the parent network to this document node network.
|
|
#[serde(alias = "Network")]
|
|
Import { import_type: Type, import_index: usize },
|
|
|
|
/// Input that is extracted from the parent scopes the node resides in. The string argument is the key.
|
|
Scope(Cow<'static, str>),
|
|
|
|
/// Input that is replaced at graph compilation with introspective metadata about this node's location.
|
|
Reflection(DocumentNodeMetadata),
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Hash, core_types::CacheHash, DynAny, serde::Serialize, serde::Deserialize)]
|
|
pub enum DocumentNodeMetadata {
|
|
DocumentNodePath,
|
|
SourceId,
|
|
}
|
|
|
|
impl DocumentNodeMetadata {
|
|
pub fn ty(&self) -> Type {
|
|
match self {
|
|
DocumentNodeMetadata::DocumentNodePath => item!(core_types::list::NodeIdPath),
|
|
DocumentNodeMetadata::SourceId => item!(core_types::SourceId),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NodeInput {
|
|
pub const fn node(node_id: NodeId, output_index: usize) -> Self {
|
|
Self::Node { node_id, output_index }
|
|
}
|
|
|
|
pub fn value(tagged_value: TaggedValue, exposed: bool) -> Self {
|
|
let tagged_value = tagged_value.into();
|
|
Self::Value { tagged_value, exposed }
|
|
}
|
|
|
|
/// Constructs a `NodeInput::Value` whose tagged value is `TaggedValue::TypeDefault(ty)`, recording only the
|
|
/// type so the runtime materializes its default rather than baking a placeholder value into the saved document.
|
|
pub fn type_default(ty: Type, exposed: bool) -> Self {
|
|
Self::value(TaggedValue::TypeDefault(ty.normalize_rank()), exposed)
|
|
}
|
|
|
|
pub const fn import(import_type: Type, import_index: usize) -> Self {
|
|
Self::Import { import_type, import_index }
|
|
}
|
|
|
|
pub fn scope(key: impl Into<Cow<'static, str>>) -> Self {
|
|
Self::Scope(key.into())
|
|
}
|
|
|
|
fn map_ids(&mut self, f: impl Fn(NodeId) -> NodeId) {
|
|
if let &mut NodeInput::Node { node_id, output_index } = self {
|
|
*self = NodeInput::Node { node_id: f(node_id), output_index }
|
|
}
|
|
}
|
|
|
|
pub fn is_exposed(&self) -> bool {
|
|
match self {
|
|
NodeInput::Node { .. } => true,
|
|
NodeInput::Value { exposed, .. } => *exposed,
|
|
NodeInput::Import { .. } => true,
|
|
NodeInput::Scope(_) => false,
|
|
NodeInput::Reflection(_) => false,
|
|
}
|
|
}
|
|
|
|
pub fn ty(&self) -> Type {
|
|
match self {
|
|
NodeInput::Node { .. } => unreachable!("ty() called on NodeInput::Node"),
|
|
NodeInput::Value { tagged_value, .. } => tagged_value.ty(),
|
|
// Stored import types are normalized to their structural form once at document migration
|
|
NodeInput::Import { import_type, .. } => import_type.clone(),
|
|
NodeInput::Scope(_) => panic!("ty() called on NodeInput::Scope"),
|
|
NodeInput::Reflection(_) => concrete!(Metadata),
|
|
}
|
|
}
|
|
|
|
pub fn as_value(&self) -> Option<&TaggedValue> {
|
|
if let NodeInput::Value { tagged_value, .. } = self { Some(tagged_value) } else { None }
|
|
}
|
|
pub fn as_value_mut(&mut self) -> Option<MemoHashGuard<'_, TaggedValue>> {
|
|
if let NodeInput::Value { tagged_value, .. } = self { Some(tagged_value.inner_mut()) } else { None }
|
|
}
|
|
pub fn as_non_exposed_value(&self) -> Option<&TaggedValue> {
|
|
if let NodeInput::Value { tagged_value, exposed: false } = self { Some(tagged_value) } else { None }
|
|
}
|
|
|
|
pub fn as_node(&self) -> Option<NodeId> {
|
|
if let NodeInput::Node { node_id, .. } = self { Some(*node_id) } else { None }
|
|
}
|
|
}
|
|
|
|
#[repr(transparent)]
|
|
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, core_types::CacheHash, PartialOrd, Ord, DynAny, serde::Serialize, serde::Deserialize)]
|
|
pub struct NetworkId(pub u64);
|
|
|
|
impl NetworkId {
|
|
pub fn new() -> Self {
|
|
Self(generate_uuid())
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, core_types::CacheHash, DynAny, serde::Serialize, serde::Deserialize)]
|
|
/// Represents the implementation of a node, which can be a nested [`NodeNetwork`], a reference to a definitions table entry, or a proto [`ProtoNodeIdentifier`].
|
|
pub enum DocumentNodeImplementation {
|
|
/// This describes a (document) node built out of a subgraph of other (document) nodes.
|
|
///
|
|
/// A nested [`NodeNetwork`] that is flattened by the [`NodeNetwork::flatten`] function.
|
|
Network(NodeNetwork),
|
|
Definition(NetworkId),
|
|
/// This describes a (document) node implemented as a proto node.
|
|
///
|
|
/// A proto node identifier which can be found in `node_registry.rs`.
|
|
#[serde(alias = "Unresolved")] // TODO: Eventually remove this alias document upgrade code
|
|
ProtoNode(ProtoNodeIdentifier),
|
|
}
|
|
|
|
impl Default for DocumentNodeImplementation {
|
|
fn default() -> Self {
|
|
Self::ProtoNode(graphene_core::ops::passthrough::IDENTIFIER)
|
|
}
|
|
}
|
|
|
|
impl DocumentNodeImplementation {
|
|
pub fn get_network(&self) -> Option<&NodeNetwork> {
|
|
match self {
|
|
DocumentNodeImplementation::Network(n) => Some(n),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn get_network_mut(&mut self) -> Option<&mut NodeNetwork> {
|
|
match self {
|
|
DocumentNodeImplementation::Network(n) => Some(n),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn get_proto_node(&self) -> Option<&ProtoNodeIdentifier> {
|
|
match self {
|
|
DocumentNodeImplementation::ProtoNode(p) => Some(p),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn output_count(&self) -> usize {
|
|
match self {
|
|
DocumentNodeImplementation::Network(network) => network.exports.len(),
|
|
DocumentNodeImplementation::Definition(id) => {
|
|
log::error!("The output count of definition {id:?} was requested without access to the definitions table");
|
|
1
|
|
}
|
|
_ => 1,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Default, Debug, DynAny, serde::Serialize, serde::Deserialize)]
|
|
/// A network (subgraph) of nodes containing each [`DocumentNode`] and its ID, as well as list mapping each export to its connected node, or a value if disconnected
|
|
pub struct NodeNetwork {
|
|
/// The list of data outputs that are exported from this network to the parent network.
|
|
/// Each export is a reference to a node within this network, paired with its output index, that is the source of the network's exported data.
|
|
// TODO: Eventually remove this alias document upgrade code
|
|
#[cfg_attr(target_family = "wasm", serde(alias = "outputs", deserialize_with = "migration::deserialize_exports"))]
|
|
pub exports: Vec<NodeInput>,
|
|
// TODO: Instead of storing import types in each NodeInput::Import connection, the types are stored here. This is similar to how types need to be defined for parameters when creating a function in Rust.
|
|
// pub import_types: Vec<Type>,
|
|
/// The list of all nodes in this network.
|
|
#[serde(
|
|
serialize_with = "graphic_types::vector_types::vector::serialize_hashmap",
|
|
deserialize_with = "graphic_types::vector_types::vector::deserialize_hashmap"
|
|
)]
|
|
pub nodes: FxHashMap<NodeId, DocumentNode>,
|
|
/// A network may expose nodes as constants which can by used by other nodes using a `NodeInput::Scope(key)`.
|
|
#[serde(default)]
|
|
#[serde(
|
|
serialize_with = "graphic_types::vector_types::vector::serialize_hashmap",
|
|
deserialize_with = "graphic_types::vector_types::vector::deserialize_hashmap"
|
|
)]
|
|
pub scope_injections: FxHashMap<String, (NodeId, Type)>,
|
|
#[serde(default, skip_serializing_if = "FxHashMap::is_empty")]
|
|
pub definitions: FxHashMap<NetworkId, NodeNetwork>,
|
|
#[serde(skip)]
|
|
pub generated: bool,
|
|
}
|
|
|
|
/// Where a network of the document lives: a definitions table entry, or `None` for the document network itself.
|
|
pub type NetworkLocation = Option<NetworkId>;
|
|
|
|
/// The output count of `implementation`, or `None` for a table entry that `definitions` lacks.
|
|
pub(crate) fn output_count_in(implementation: &DocumentNodeImplementation, definitions: &FxHashMap<NetworkId, NodeNetwork>) -> Option<usize> {
|
|
match implementation {
|
|
DocumentNodeImplementation::Definition(id) => definitions.get(id).map(|definition| definition.exports.len()),
|
|
_ => Some(implementation.output_count()),
|
|
}
|
|
}
|
|
|
|
/// One inline network that [`NodeNetwork::lift_nested_networks`] moved into the definitions table.
|
|
/// The node `node_id` in table entry `containing` (`None` for the document network) now names it as `id`.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub struct LiftedNetwork {
|
|
pub containing: Option<NetworkId>,
|
|
pub node_id: NodeId,
|
|
pub id: NetworkId,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub enum DefinitionError {
|
|
Missing(NetworkId),
|
|
Cycle(Vec<NetworkId>),
|
|
NestedTable,
|
|
}
|
|
|
|
impl std::fmt::Display for DefinitionError {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Self::Missing(id) => write!(f, "Definition {id:?} is referenced but not defined"),
|
|
Self::Cycle(cycle) => write!(f, "Definitions reference each other in a cycle: {cycle:?}"),
|
|
Self::NestedTable => write!(f, "Only the document network may hold definitions"),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::error::Error for DefinitionError {}
|
|
|
|
fn validate_named_definitions(
|
|
network: &NodeNetwork,
|
|
definitions: &FxHashMap<NetworkId, NodeNetwork>,
|
|
visiting: &mut Vec<NetworkId>,
|
|
validated: &mut FxHashSet<NetworkId>,
|
|
) -> Result<(), DefinitionError> {
|
|
for node in network.nodes.values() {
|
|
match &node.implementation {
|
|
DocumentNodeImplementation::Definition(id) => {
|
|
if let Some(start) = visiting.iter().position(|visiting| visiting == id) {
|
|
return Err(DefinitionError::Cycle(visiting[start..].to_vec()));
|
|
}
|
|
if validated.contains(id) {
|
|
continue;
|
|
}
|
|
|
|
let definition = definitions.get(id).ok_or(DefinitionError::Missing(*id))?;
|
|
if !definition.definitions.is_empty() {
|
|
return Err(DefinitionError::NestedTable);
|
|
}
|
|
visiting.push(*id);
|
|
validate_named_definitions(definition, definitions, visiting, validated)?;
|
|
visiting.pop();
|
|
validated.insert(*id);
|
|
}
|
|
DocumentNodeImplementation::Network(nested) => {
|
|
if !nested.definitions.is_empty() {
|
|
return Err(DefinitionError::NestedTable);
|
|
}
|
|
validate_named_definitions(nested, definitions, visiting, validated)?;
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
struct DefinitionInliner {
|
|
definitions: FxHashMap<NetworkId, NodeNetwork>,
|
|
inlined: FxHashMap<NetworkId, NodeNetwork>,
|
|
visiting: Vec<NetworkId>,
|
|
}
|
|
|
|
impl DefinitionInliner {
|
|
fn inline_network(&mut self, network: &mut NodeNetwork) -> Result<(), DefinitionError> {
|
|
if !network.definitions.is_empty() {
|
|
return Err(DefinitionError::NestedTable);
|
|
}
|
|
|
|
for node in network.nodes.values_mut() {
|
|
if let DocumentNodeImplementation::Definition(id) = node.implementation {
|
|
node.implementation = DocumentNodeImplementation::Network(self.inlined_definition(id)?.clone());
|
|
} else if let DocumentNodeImplementation::Network(nested) = &mut node.implementation {
|
|
self.inline_network(nested)?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn inlined_definition(&mut self, id: NetworkId) -> Result<&NodeNetwork, DefinitionError> {
|
|
if !self.inlined.contains_key(&id) {
|
|
if let Some(start) = self.visiting.iter().position(|&visiting| visiting == id) {
|
|
return Err(DefinitionError::Cycle(self.visiting[start..].to_vec()));
|
|
}
|
|
|
|
let mut network = self.definitions.get(&id).ok_or(DefinitionError::Missing(id))?.clone();
|
|
self.visiting.push(id);
|
|
self.inline_network(&mut network)?;
|
|
self.visiting.pop();
|
|
self.inlined.insert(id, network);
|
|
}
|
|
|
|
Ok(&self.inlined[&id])
|
|
}
|
|
}
|
|
|
|
impl core_types::CacheHash for NodeNetwork {
|
|
fn cache_hash<H: ::core::hash::Hasher>(&self, state: &mut H) {
|
|
self.exports.cache_hash(state);
|
|
|
|
let mut nodes: Vec<_> = self.nodes.iter().collect();
|
|
nodes.sort_by_key(|(id, _)| *id);
|
|
for (id, node) in nodes {
|
|
id.cache_hash(state);
|
|
node.cache_hash(state);
|
|
}
|
|
|
|
let mut scope_injections: Vec<_> = self.scope_injections.iter().collect();
|
|
scope_injections.sort_by_key(|(key, _)| key.as_str());
|
|
for (key, (node_id, ty)) in scope_injections {
|
|
key.cache_hash(state);
|
|
node_id.cache_hash(state);
|
|
ty.cache_hash(state);
|
|
}
|
|
|
|
let mut definitions: Vec<_> = self.definitions.iter().collect();
|
|
definitions.sort_by_key(|(id, _)| *id);
|
|
for (id, definition) in definitions {
|
|
id.cache_hash(state);
|
|
definition.cache_hash(state);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PartialEq for NodeNetwork {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.exports == other.exports && self.nodes == other.nodes && self.scope_injections == other.scope_injections && self.definitions == other.definitions
|
|
}
|
|
}
|
|
|
|
/// Graph modification functions
|
|
impl NodeNetwork {
|
|
pub fn current_hash(&self) -> u64 {
|
|
use core_types::graphene_hash::CacheHash;
|
|
use rustc_hash::FxHasher;
|
|
use std::hash::Hasher;
|
|
let mut hasher = FxHasher::default();
|
|
self.cache_hash(&mut hasher);
|
|
hasher.finish()
|
|
}
|
|
|
|
pub fn value_network(node: DocumentNode) -> Self {
|
|
Self {
|
|
exports: vec![NodeInput::node(NodeId(0), 0)],
|
|
nodes: [(NodeId(0), node)].into_iter().collect(),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
/// Get the nested network given by the path of node ids
|
|
pub fn nested_network(&self, nested_path: &[NodeId]) -> Option<&Self> {
|
|
let mut network = self;
|
|
|
|
for segment in nested_path {
|
|
network = self.implementation_network(&network.nodes.get(segment)?.implementation)?;
|
|
}
|
|
Some(network)
|
|
}
|
|
|
|
/// Get the mutable nested network given by the path of node ids
|
|
pub fn nested_network_mut(&mut self, nested_path: &[NodeId]) -> Option<&mut Self> {
|
|
let location = self.locate(nested_path)?;
|
|
self.located_network_mut(location)
|
|
}
|
|
|
|
/// The location of the network at `nested_path`, or `None` if the path doesn't end at a table entry.
|
|
pub fn locate(&self, nested_path: &[NodeId]) -> Option<NetworkLocation> {
|
|
let Some((node_id, parent_path)) = nested_path.split_last() else { return Some(None) };
|
|
match self.nested_network(parent_path)?.nodes.get(node_id)?.implementation {
|
|
DocumentNodeImplementation::Definition(id) if self.definitions.contains_key(&id) => Some(Some(id)),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn located_network(&self, location: NetworkLocation) -> Option<&Self> {
|
|
match location {
|
|
Some(id) => self.definitions.get(&id),
|
|
None => Some(self),
|
|
}
|
|
}
|
|
|
|
pub fn located_network_mut(&mut self, location: NetworkLocation) -> Option<&mut Self> {
|
|
match location {
|
|
Some(id) => self.definitions.get_mut(&id),
|
|
None => Some(self),
|
|
}
|
|
}
|
|
|
|
pub fn implementation_network<'a>(&'a self, implementation: &'a DocumentNodeImplementation) -> Option<&'a Self> {
|
|
match implementation {
|
|
DocumentNodeImplementation::Network(network) => Some(network),
|
|
DocumentNodeImplementation::Definition(id) => self.definitions.get(id),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
pub fn implementation_output_count(&self, implementation: &DocumentNodeImplementation) -> usize {
|
|
output_count_in(implementation, &self.definitions).unwrap_or(0)
|
|
}
|
|
|
|
/// The path of the node producing this output once compiled, following network exports and the primary inputs of hidden and Passthrough nodes.
|
|
pub fn output_source(&self, node_path: &[NodeId], output_index: usize) -> Option<Vec<NodeId>> {
|
|
let (node_id, network_path) = node_path.split_last()?;
|
|
let node = self.nested_network(network_path)?.nodes.get(node_id)?;
|
|
|
|
let passthrough = DocumentNodeImplementation::ProtoNode(graphene_core::ops::passthrough::IDENTIFIER);
|
|
if !node.visible || node.implementation == passthrough {
|
|
return self.input_source(network_path, node.inputs.first()?);
|
|
}
|
|
|
|
match self.implementation_network(&node.implementation) {
|
|
Some(network) => self.input_source(node_path, network.exports.get(output_index)?),
|
|
None => Some(node_path.to_vec()),
|
|
}
|
|
}
|
|
|
|
/// The path of the node producing what this input in the network at the path receives, following imports out to the network node's inputs.
|
|
pub fn input_source(&self, network_path: &[NodeId], input: &NodeInput) -> Option<Vec<NodeId>> {
|
|
match input {
|
|
NodeInput::Node { node_id, output_index } => self.output_source(&[network_path, &[*node_id]].concat(), *output_index),
|
|
NodeInput::Import { import_index, .. } => {
|
|
let (network_node_id, parent_network_path) = network_path.split_last()?;
|
|
let network_node = self.nested_network(parent_network_path)?.nodes.get(network_node_id)?;
|
|
self.input_source(parent_network_path, network_node.inputs.get(*import_index)?)
|
|
}
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Moves every inline network at every nesting level into the definitions table under a fresh ID.
|
|
/// Returns the moves so that each network is listed before the networks lifted out of it.
|
|
pub fn lift_nested_networks(&mut self) -> Vec<LiftedNetwork> {
|
|
let mut lifted = Vec::new();
|
|
let mut pending: Vec<Option<NetworkId>> = std::iter::once(None).chain(self.definitions.keys().copied().map(Some)).collect();
|
|
|
|
while let Some(containing) = pending.pop() {
|
|
let network = match containing {
|
|
None => &mut *self,
|
|
Some(id) => match self.definitions.get_mut(&id) {
|
|
Some(definition) => definition,
|
|
None => continue,
|
|
},
|
|
};
|
|
|
|
let mut node_ids: Vec<NodeId> = network
|
|
.nodes
|
|
.iter()
|
|
.filter(|(_, node)| matches!(node.implementation, DocumentNodeImplementation::Network(_)))
|
|
.map(|(node_id, _)| *node_id)
|
|
.collect();
|
|
node_ids.sort();
|
|
|
|
let mut bodies = Vec::with_capacity(node_ids.len());
|
|
for node_id in node_ids {
|
|
let Some(node) = network.nodes.get_mut(&node_id) else { continue };
|
|
let id = NetworkId::new();
|
|
if let DocumentNodeImplementation::Network(body) = std::mem::replace(&mut node.implementation, DocumentNodeImplementation::Definition(id)) {
|
|
bodies.push((id, body));
|
|
lifted.push(LiftedNetwork { containing, node_id, id });
|
|
}
|
|
}
|
|
|
|
for (id, body) in bodies {
|
|
self.definitions.insert(id, body);
|
|
pending.push(Some(id));
|
|
}
|
|
}
|
|
|
|
lifted
|
|
}
|
|
|
|
/// The table entries the document network reaches through its nodes, directly or through other entries.
|
|
pub fn reachable_definitions(&self) -> FxHashSet<NetworkId> {
|
|
fn visit(network: &NodeNetwork, definitions: &FxHashMap<NetworkId, NodeNetwork>, reached: &mut FxHashSet<NetworkId>) {
|
|
for node in network.nodes.values() {
|
|
match &node.implementation {
|
|
DocumentNodeImplementation::Definition(id) => {
|
|
if reached.insert(*id)
|
|
&& let Some(definition) = definitions.get(id)
|
|
{
|
|
visit(definition, definitions, reached);
|
|
}
|
|
}
|
|
DocumentNodeImplementation::Network(nested) => visit(nested, definitions, reached),
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut reached = FxHashSet::default();
|
|
visit(self, &self.definitions, &mut reached);
|
|
reached
|
|
}
|
|
|
|
/// Drops every table entry the document network does not reach, and returns their IDs in sorted order.
|
|
pub fn retain_reachable_definitions(&mut self) -> Vec<NetworkId> {
|
|
let reached = self.reachable_definitions();
|
|
let mut dropped: Vec<NetworkId> = self.definitions.keys().filter(|id| !reached.contains(id)).copied().collect();
|
|
dropped.sort();
|
|
for id in &dropped {
|
|
self.definitions.remove(id);
|
|
}
|
|
dropped
|
|
}
|
|
|
|
/// Checks that every table entry a node names exists, that no entries name each other in a cycle, and that only this network holds a table.
|
|
pub fn validate_definitions(&self) -> Result<(), DefinitionError> {
|
|
validate_named_definitions(self, &self.definitions, &mut Vec::new(), &mut FxHashSet::default())
|
|
}
|
|
|
|
pub fn inline_definitions(&mut self) -> Result<(), DefinitionError> {
|
|
let mut inliner = DefinitionInliner {
|
|
definitions: std::mem::take(&mut self.definitions),
|
|
inlined: FxHashMap::default(),
|
|
visiting: Vec::new(),
|
|
};
|
|
inliner.inline_network(self)
|
|
}
|
|
|
|
/// Is the node being used directly as an output?
|
|
pub fn outputs_contain(&self, node_id_to_check: NodeId) -> bool {
|
|
self.exports
|
|
.iter()
|
|
.any(|output| if let NodeInput::Node { node_id, .. } = output { *node_id == node_id_to_check } else { false })
|
|
}
|
|
|
|
/// Check there are no cycles in the graph (this should never happen).
|
|
pub fn is_acyclic(&self) -> bool {
|
|
self.is_acyclic_with(None)
|
|
}
|
|
|
|
/// Whether the network is acyclic, optionally answering for one input replaced by a proposed value
|
|
/// rather than for the inputs as they stand, so a write can be tested without being performed.
|
|
///
|
|
/// `replaced` names the node, the input index, and what would be written there.
|
|
pub fn is_acyclic_with(&self, replaced: Option<(NodeId, usize, &NodeInput)>) -> bool {
|
|
let dependency = |input: &NodeInput| if let NodeInput::Node { node_id, .. } = input { Some(*node_id) } else { None };
|
|
|
|
let mut dependencies: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
|
|
for (node_id, node) in &self.nodes {
|
|
dependencies.insert(
|
|
*node_id,
|
|
node.inputs
|
|
.iter()
|
|
.enumerate()
|
|
.filter_map(|(index, input)| match replaced {
|
|
Some((replaced_node, replaced_index, proposed)) if replaced_node == *node_id && replaced_index == index => dependency(proposed),
|
|
_ => dependency(input),
|
|
})
|
|
.collect(),
|
|
);
|
|
}
|
|
while !dependencies.is_empty() {
|
|
let Some((&disconnected, _)) = dependencies.iter().find(|(_, l)| l.is_empty()) else {
|
|
error!("Dependencies {dependencies:?}");
|
|
return false;
|
|
};
|
|
dependencies.remove(&disconnected);
|
|
for connections in dependencies.values_mut() {
|
|
connections.retain(|&id| id != disconnected);
|
|
}
|
|
}
|
|
true
|
|
}
|
|
}
|
|
|
|
/// Functions for compiling the network
|
|
/// Normalizes the ranked types an input can store: an `Import`'s type or a value's `TypeDefault` payload.
|
|
fn normalize_input_stored_type(input: &mut NodeInput) {
|
|
match input {
|
|
NodeInput::Import { import_type, .. } => *import_type = import_type.clone().normalize_rank(),
|
|
NodeInput::Value { tagged_value, .. } => {
|
|
if let TaggedValue::TypeDefault(ty) = &**tagged_value {
|
|
let normalized = ty.clone().normalize_rank();
|
|
if normalized != *ty {
|
|
*tagged_value = TaggedValue::TypeDefault(normalized).into();
|
|
}
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
impl NodeNetwork {
|
|
/// Normalizes every stored type in the network (exports and each node's types) to the structural form, recursively.
|
|
pub fn normalize_stored_types(&mut self) {
|
|
for export in &mut self.exports {
|
|
normalize_input_stored_type(export);
|
|
}
|
|
for node in self.nodes.values_mut() {
|
|
node.normalize_stored_types();
|
|
}
|
|
for definition in self.definitions.values_mut() {
|
|
definition.normalize_stored_types();
|
|
}
|
|
}
|
|
|
|
/// Replace all references in the graph of a node ID with a new node ID defined by the function `f`.
|
|
pub fn map_ids(&mut self, f: impl Fn(NodeId) -> NodeId + Copy) {
|
|
self.exports.iter_mut().for_each(|output| {
|
|
if let NodeInput::Node { node_id, .. } = output {
|
|
*node_id = f(*node_id)
|
|
}
|
|
});
|
|
self.scope_injections.values_mut().for_each(|(id, _ty)| *id = f(*id));
|
|
let nodes = std::mem::take(&mut self.nodes);
|
|
self.nodes = nodes
|
|
.into_iter()
|
|
.map(|(id, mut node)| {
|
|
node.inputs.iter_mut().for_each(|input| input.map_ids(f));
|
|
node.original_location.dependants.iter_mut().for_each(|deps| deps.iter_mut().for_each(|id| *id = f(*id)));
|
|
(f(id), node)
|
|
})
|
|
.collect();
|
|
}
|
|
|
|
/// Create a [`RecursiveNodeIter`] that iterates over all [`DocumentNode`]s, including ones that are deeply nested.
|
|
pub fn recursive_nodes(&self) -> RecursiveNodeIter<'_> {
|
|
let nodes = self.nodes.iter().map(|(id, node)| (id, node, Vec::new())).collect();
|
|
RecursiveNodeIter {
|
|
definitions: &self.definitions,
|
|
visited_definitions: FxHashSet::default(),
|
|
nodes,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// An iterator over all [`DocumentNode`]s, including ones that are deeply nested.
|
|
/// Each definition body is visited once, under the path of the first instance reached.
|
|
pub struct RecursiveNodeIter<'a> {
|
|
definitions: &'a FxHashMap<NetworkId, NodeNetwork>,
|
|
visited_definitions: FxHashSet<NetworkId>,
|
|
nodes: Vec<(&'a NodeId, &'a DocumentNode, Vec<NodeId>)>,
|
|
}
|
|
|
|
impl<'a> Iterator for RecursiveNodeIter<'a> {
|
|
type Item = (&'a NodeId, &'a DocumentNode, Vec<NodeId>);
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
let (current_id, node, path) = self.nodes.pop()?;
|
|
let nested_network = match &node.implementation {
|
|
DocumentNodeImplementation::Network(network) => Some(network),
|
|
DocumentNodeImplementation::Definition(id) if self.visited_definitions.insert(*id) => self.definitions.get(id),
|
|
_ => None,
|
|
};
|
|
if let Some(network) = nested_network {
|
|
self.nodes.extend(network.nodes.iter().map(|(id, node)| {
|
|
let mut nested_path = path.clone();
|
|
nested_path.push(*current_id);
|
|
(id, node, nested_path)
|
|
}));
|
|
}
|
|
Some((current_id, node, path))
|
|
}
|
|
}
|
|
|
|
impl core_types::graphene_hash::CacheHash for DocumentNode {
|
|
fn cache_hash<H: core::hash::Hasher>(&self, state: &mut H) {
|
|
self.inputs.cache_hash(state);
|
|
self.call_argument.cache_hash(state);
|
|
self.implementation.cache_hash(state);
|
|
self.visible.cache_hash(state);
|
|
self.compile_metadata.cache_hash(state);
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
pub(crate) mod document_tests;
|