[Bf-blender-cvs] [47ea90771da] functions: initial type relations and improved inferencer

Jacques Lucke noreply at git.blender.org
Sun Feb 17 21:57:29 CET 2019


Commit: 47ea90771da4de42ae797c7272fb4bf741e8e4ef
Author: Jacques Lucke
Date:   Sun Feb 17 17:44:21 2019 +0100
Branches: functions
https://developer.blender.org/rB47ea90771da4de42ae797c7272fb4bf741e8e4ef

initial type relations and improved inferencer

===================================================================

M	source/blender/functions/CMakeLists.txt
M	source/blender/functions/FN_functions.hpp
M	source/blender/functions/c_wrapper.cpp
M	source/blender/functions/core/type_inferencing.cpp
M	source/blender/functions/core/type_inferencing.hpp
A	source/blender/functions/core/type_relations.cpp
A	source/blender/functions/core/type_relations.hpp
M	source/blender/functions/types/numeric.cpp
M	source/blender/functions/types/numeric.hpp

===================================================================

diff --git a/source/blender/functions/CMakeLists.txt b/source/blender/functions/CMakeLists.txt
index eaca171318e..a8af79b343c 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -23,6 +23,8 @@ set(SRC
 	core/graph_to_function.cpp
 	core/type_inferencing.hpp
 	core/type_inferencing.cpp
+	core/type_relations.hpp
+	core/type_relations.cpp
 
 	FN_functions.hpp
 
diff --git a/source/blender/functions/FN_functions.hpp b/source/blender/functions/FN_functions.hpp
index b7f13998859..96c6fd46b57 100644
--- a/source/blender/functions/FN_functions.hpp
+++ b/source/blender/functions/FN_functions.hpp
@@ -3,6 +3,7 @@
 #include "core/core.hpp"
 #include "core/data_flow_graph.hpp"
 #include "core/graph_to_function.hpp"
+#include "core/type_relations.hpp"
 #include "core/type_inferencing.hpp"
 
 #include "core/cpu.hpp"
diff --git a/source/blender/functions/c_wrapper.cpp b/source/blender/functions/c_wrapper.cpp
index d036028f6ae..361203c3023 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -15,8 +15,11 @@ WRAPPERS(BLI::RefCounted<FN::Type> *, FnType);
 WRAPPERS(FN::Tuple *, FnTuple);
 WRAPPERS(const FN::TupleCallBody *, FnCallable);
 
+void FN_test_inferencer(void);
+
 void FN_initialize()
 {
+	FN_test_inferencer();
 }
 
 void FN_function_call(FnCallable fn_call, FnTuple fn_in, FnTuple fn_out)
@@ -208,4 +211,45 @@ void FN_testing(bNodeTree *bnodetree)
 	FN::FunctionNodes::FunctionNodeTree tree(bnodetree);
 	auto graph = tree.to_data_flow_graph();
 	std::cout << graph->to_dot() << std::endl;
+}
+
+void FN_test_inferencer()
+{
+	FN::SharedType &float_ty = FN::Types::get_float_type();
+	FN::SharedType &int32_ty = FN::Types::get_int32_type();
+	FN::SharedType &fvec3_ty = FN::Types::get_fvec3_type();
+	FN::SharedType &float_list_ty = FN::Types::get_float_list_type();
+
+	{
+		FN::Inferencer inferencer;
+		inferencer.insert_final_type(0, float_ty);
+		inferencer.insert_final_type(1, int32_ty);
+		inferencer.insert_final_type(2, fvec3_ty);
+		inferencer.insert_equality_relation({6, 7});
+		inferencer.insert_equality_relation({0, 5, 6});
+		inferencer.insert_equality_relation({1, 4});
+
+		BLI_assert(inferencer.inference());
+
+		BLI_assert(inferencer.get_final_type(5) == float_ty);
+		BLI_assert(inferencer.get_final_type(6) == float_ty);
+		BLI_assert(inferencer.get_final_type(7) == float_ty);
+		BLI_assert(inferencer.get_final_type(4) == int32_ty);
+	}
+	{
+		FN::Inferencer inferencer;
+		inferencer.insert_final_type(0, float_ty);
+		inferencer.insert_final_type(1, int32_ty);
+		inferencer.insert_equality_relation({0, 2});
+		inferencer.insert_equality_relation({1, 2});
+
+		BLI_assert(!inferencer.inference());
+	}
+	{
+		FN::Inferencer inferencer;
+		inferencer.insert_final_type(0, float_ty);
+		inferencer.insert_list_relation({1}, {0});
+
+		BLI_assert(inferencer.inference());
+	}
 }
\ No newline at end of file
diff --git a/source/blender/functions/core/type_inferencing.cpp b/source/blender/functions/core/type_inferencing.cpp
index daa97cbc2b3..a790f8eef13 100644
--- a/source/blender/functions/core/type_inferencing.cpp
+++ b/source/blender/functions/core/type_inferencing.cpp
@@ -1,24 +1,53 @@
 #include "type_inferencing.hpp"
 
 namespace FN {
-	void Inferencer::finalize_id(uint64_t id, SharedType &type)
+	bool Inferencer::finalize_id(uint64_t id, SharedType &type)
 	{
 		if (m_final_types.contains(id)) {
-			BLI_assert(m_final_types.lookup_ref(id) == type);
+			bool same_type = m_final_types.lookup_ref(id) == type;
+			return same_type;
 		}
 		else {
 			m_final_types.add(id, type);
+			return true;
 		}
 	}
 
+	bool Inferencer::finalize_ids(
+		SmallVector<uint64_t> ids,
+		SharedType &type)
+	{
+		for (uint64_t id : ids) {
+			if (!this->finalize_id(id, type)) {
+				return false;
+			}
+		}
+		return true;
+	}
+
 	void Inferencer::insert_final_type(uint64_t id, SharedType &type)
 	{
 		this->finalize_id(id, type);
 	}
 
-	void Inferencer::insert_type_equality(uint64_t a, uint64_t b)
+	void Inferencer::insert_equality_relation(SmallVector<uint64_t> ids)
 	{
-		m_equalities.append(Link(a, b));
+		if (ids.size() >= 2) {
+			EqualityRelation relation;
+			relation.ids = ids;
+			m_equality_relations.append(relation);
+		}
+	}
+
+	void Inferencer::insert_list_relation(
+		SmallVector<uint64_t> list_ids,
+		SmallVector<uint64_t> base_ids)
+	{
+		BLI_assert(list_ids.size() > 0);
+		ListRelation relation;
+		relation.list_ids = list_ids;
+		relation.base_ids = base_ids;
+		m_list_relations.append(relation);
 	}
 
 	SharedType &Inferencer::get_final_type(uint64_t id)
@@ -31,28 +60,26 @@ namespace FN {
 		return m_final_types.contains(id);
 	}
 
-	void Inferencer::inference()
+	bool Inferencer::inference()
 	{
-		while (!m_equalities.empty()) {
-			for (uint i = 0; i < m_equalities.size(); i++) {
-				Link link = m_equalities[i];
-				bool done = false;
-
-				if (this->has_final_type(link.a)) {
-					this->finalize_id(link.b, this->get_final_type(link.a));
-					done = true;
-				}
-				if (this->has_final_type(link.b)) {
-					this->finalize_id(link.a, this->get_final_type(link.b));
-					done = true;
-				}
+		while (!m_equality_relations.empty()) {
+			for (uint i = 0; i < m_equality_relations.size(); i++) {
+				EqualityRelation group = m_equality_relations[i];
 
-				if (done) {
-					m_equalities.remove_and_reorder(i);
-					i--;
+				for (uint64_t id : group.ids) {
+					if (this->has_final_type(id)) {
+						if (!this->finalize_ids(group.ids, this->get_final_type(id))) {
+							return false;
+						}
+						m_equality_relations.remove_and_reorder(i);
+						i--;
+						break;
+					}
 				}
 			}
 		}
+
+		return true;
 	}
 
 } /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/core/type_inferencing.hpp b/source/blender/functions/core/type_inferencing.hpp
index 1fdf643a4f4..2ca3085e110 100644
--- a/source/blender/functions/core/type_inferencing.hpp
+++ b/source/blender/functions/core/type_inferencing.hpp
@@ -4,24 +4,30 @@ namespace FN {
 
 	class Inferencer {
 	private:
-		struct Link {
-			uint64_t a, b;
-			Link(uint64_t a, uint64_t b)
-				: a(a), b(b) {}
+		struct EqualityRelation {
+			SmallVector<uint64_t> ids;
+		};
+		struct ListRelation {
+			SmallVector<uint64_t> list_ids, base_ids;
 		};
 
 		SmallMap<uint64_t, SharedType> m_final_types;
-		SmallVector<Link> m_equalities;
+		SmallVector<EqualityRelation> m_equality_relations;
+		SmallVector<ListRelation> m_list_relations;
 
-		void finalize_id(uint64_t id, SharedType &type);
+		bool finalize_id(uint64_t id, SharedType &type);
+		bool finalize_ids(SmallVector<uint64_t> ids, SharedType &type);
 
 	public:
 		Inferencer() = default;
 
 		void insert_final_type(uint64_t id, SharedType &type);
-		void insert_type_equality(uint64_t a, uint64_t b);
+		void insert_equality_relation(SmallVector<uint64_t> ids);
+		void insert_list_relation(
+			SmallVector<uint64_t> list_ids,
+			SmallVector<uint64_t> base_ids = {});
 
-		void inference();
+		bool inference();
 
 		SharedType &get_final_type(uint64_t id);
 		bool has_final_type(uint64_t id);
diff --git a/source/blender/functions/core/type_relations.cpp b/source/blender/functions/core/type_relations.cpp
new file mode 100644
index 00000000000..913d391baf0
--- /dev/null
+++ b/source/blender/functions/core/type_relations.cpp
@@ -0,0 +1,16 @@
+#include "type_relations.hpp"
+
+namespace FN {
+
+	const char *ListTypeInfo::identifier_in_composition()
+	{
+		return "List Type Info";
+	}
+
+	void ListTypeInfo::free_self(void *value)
+	{
+		ListTypeInfo *v = (ListTypeInfo *)value;
+		delete v;
+	}
+
+} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/core/type_relations.hpp b/source/blender/functions/core/type_relations.hpp
new file mode 100644
index 00000000000..2092e4fb726
--- /dev/null
+++ b/source/blender/functions/core/type_relations.hpp
@@ -0,0 +1,17 @@
+#include "core.hpp"
+
+namespace FN {
+
+	class ListTypeInfo {
+	private:
+		SharedType m_base_type;
+
+	public:
+		static const char *identifier_in_composition();
+		static void free_self(void *value);
+
+		ListTypeInfo(SharedType &base_type)
+			: m_base_type(base_type) {}
+	};
+
+} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/types/numeric.cpp b/source/blender/functions/types/numeric.cpp
index 3ab99babc34..c5d1e5d10a9 100644
--- a/source/blender/functions/types/numeric.cpp
+++ b/source/blender/functions/types/numeric.cpp
@@ -28,4 +28,12 @@ namespace FN::Types {
 		return type;
 	}
 
+	LAZY_INIT_NO_ARG(SharedType &, SharedType, get_float_list_type)
+	{
+		SharedType type = SharedType::New("Float List");
+		type->extend(new CPPTypeInfoForType<SmallVector<float>>());
+		type->extend(new ListTypeInfo(get_float_type()));
+		return type;
+	}
+
 } /* namespace FN::Types */
\ No newline at end of file
diff --git a/source/blender/functions/types/numeric.hpp b/source/blender/functions/types/numeric.hpp
index 1ecc1a810dc..08f9d19ce27 100644
--- a/source/blender/functions/types/numeric.hpp
+++ b/source/blender/functions/types/numeric.hpp
@@ -7,5 +7,6 @@ namespace FN::Types {
 	SharedType &get_float_type();
 	SharedType &get_int32_type();
 	SharedType &get_fvec3_type();
+	SharedType &get_float_list_type();
 
 } /* namespace FN::Types */
\ No newline at end of file



More information about the Bf-blender-cvs mailing list