From eb56ee160fb0e690071cdce3b1cb504acc9a913a Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sat, 7 Dec 2019 15:27:09 +0100
Subject: [PATCH] add basic relation properties detection

---
 alib2aux/src/relation/IsReflexive.cpp  | 23 ++++++++++
 alib2aux/src/relation/IsReflexive.h    | 62 ++++++++++++++++++++++++++
 alib2aux/src/relation/IsSymmetric.cpp  | 22 +++++++++
 alib2aux/src/relation/IsSymmetric.h    | 61 +++++++++++++++++++++++++
 alib2aux/src/relation/IsTransitive.cpp | 22 +++++++++
 alib2aux/src/relation/IsTransitive.h   | 62 ++++++++++++++++++++++++++
 6 files changed, 252 insertions(+)
 create mode 100644 alib2aux/src/relation/IsReflexive.cpp
 create mode 100644 alib2aux/src/relation/IsReflexive.h
 create mode 100644 alib2aux/src/relation/IsSymmetric.cpp
 create mode 100644 alib2aux/src/relation/IsSymmetric.h
 create mode 100644 alib2aux/src/relation/IsTransitive.cpp
 create mode 100644 alib2aux/src/relation/IsTransitive.h

diff --git a/alib2aux/src/relation/IsReflexive.cpp b/alib2aux/src/relation/IsReflexive.cpp
new file mode 100644
index 0000000000..3e1d0d015c
--- /dev/null
+++ b/alib2aux/src/relation/IsReflexive.cpp
@@ -0,0 +1,23 @@
+/*
+* IsReflexive.cpp
+ *
+ *  Created on: 2. 10. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#include "IsReflexive.h"
+#include <registration/AlgoRegistration.hpp>
+
+#include <object/Object.h>
+
+namespace {
+
+auto IsReflexive = registration::AbstractRegister < relation::IsReflexive, bool, const ext::set < ext::pair < object::Object, object::Object > > &, const ext::set < object::Object > & > ( relation::IsReflexive::isReflexive, "relation", "universe" ).setDocumentation (
+"Checks whether a relation is reflexive\n\
+\n\
+@param relation the tested relation\n\
+@param universe the universe of items participating in the relation\n\
+\n\
+@return true if the relation is reflexive, false otherwise");
+
+} /* namespace */
diff --git a/alib2aux/src/relation/IsReflexive.h b/alib2aux/src/relation/IsReflexive.h
new file mode 100644
index 0000000000..b119384c52
--- /dev/null
+++ b/alib2aux/src/relation/IsReflexive.h
@@ -0,0 +1,62 @@
+/*
+ * IsReflexive.h
+ *
+ * This file is part of Algorithms library toolkit.
+ * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
+
+ * Algorithms library toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * Algorithms library toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with Algorithms library toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  Created on: 2. 10. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#ifndef IS_REFLEXIVE_H_
+#define IS_REFLEXIVE_H_
+
+#include <alib/set>
+#include <alib/pair>
+
+namespace relation {
+
+/**
+ * Computes the complement relation in given universe.
+ */
+class IsReflexive {
+public:
+	/**
+	 * Checks whether a relation is reflexive.
+	 *
+	 * @tparam T Type of the items in relation.
+	 *
+	 * @param relation the tested relation
+	 * @param universe the universe of items participating in the relation
+	 *
+	 * @return true if the relation is reflexive, false otherwise
+	 */
+	template < class T >
+	static bool isReflexive ( const ext::set < ext::pair < T, T > > & relation, const ext::set < T > & universe );
+};
+
+template < class T >
+bool IsReflexive::isReflexive ( const ext::set < ext::pair < T, T > > & relation, const ext::set < T > & universe ) {
+	for ( const T & state : universe )
+		if ( ! relation.contains ( ext::make_pair ( state, state ) ) )
+			return false;
+
+	return true;
+}
+
+} /* namespace relation */
+
+#endif /* IS_REFLEXIVE_H_ */
diff --git a/alib2aux/src/relation/IsSymmetric.cpp b/alib2aux/src/relation/IsSymmetric.cpp
new file mode 100644
index 0000000000..4cf6ae88d3
--- /dev/null
+++ b/alib2aux/src/relation/IsSymmetric.cpp
@@ -0,0 +1,22 @@
+/*
+* IsSymmetric.cpp
+ *
+ *  Created on: 2. 10. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#include "IsSymmetric.h"
+#include <registration/AlgoRegistration.hpp>
+
+#include <object/Object.h>
+
+namespace {
+
+auto IsSymmetric = registration::AbstractRegister < relation::IsSymmetric, bool, const ext::set < ext::pair < object::Object, object::Object > > & > ( relation::IsSymmetric::isSymmetric, "relation" ).setDocumentation (
+"Checks whether a relation is symmetric.\n\
+\n\
+@param relation the tested relation\n\
+\n\
+@return true if the relation is symmetric, false otherwise");
+
+} /* namespace */
diff --git a/alib2aux/src/relation/IsSymmetric.h b/alib2aux/src/relation/IsSymmetric.h
new file mode 100644
index 0000000000..e33bd70cd3
--- /dev/null
+++ b/alib2aux/src/relation/IsSymmetric.h
@@ -0,0 +1,61 @@
+/*
+ * IsSymmetric.h
+ *
+ * This file is part of Algorithms library toolkit.
+ * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
+
+ * Algorithms library toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * Algorithms library toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with Algorithms library toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  Created on: 2. 10. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#ifndef IS_SYMMETRIC_H_
+#define IS_SYMMETRIC_H_
+
+#include <alib/set>
+#include <alib/pair>
+
+namespace relation {
+
+/**
+ * Computes the complement relation in given universe.
+ */
+class IsSymmetric {
+public:
+	/**
+	 * Checks whether a relation is symmetric.
+	 *
+	 * @tparam T Type of the items in relation.
+	 *
+	 * @param relation the tested relation
+	 *
+	 * @return true if the relation is symmetric, false otherwise
+	 */
+	template < class T >
+	static bool isSymmetric ( const ext::set < ext::pair < T, T > > & relation );
+};
+
+template < class T >
+bool IsSymmetric::isSymmetric ( const ext::set < ext::pair < T, T > > & relation ) {
+	for ( const ext::pair < T, T > & item : relation )
+		if ( ! relation.contains ( ext::make_pair ( item.second, item.first ) ) )
+			return false;
+
+	return true;
+}
+
+} /* namespace relation */
+
+#endif /* IS_SYMMETRIC_H_ */
diff --git a/alib2aux/src/relation/IsTransitive.cpp b/alib2aux/src/relation/IsTransitive.cpp
new file mode 100644
index 0000000000..faa0e8db2a
--- /dev/null
+++ b/alib2aux/src/relation/IsTransitive.cpp
@@ -0,0 +1,22 @@
+/*
+* IsTransitive.cpp
+ *
+ *  Created on: 2. 10. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#include "IsTransitive.h"
+#include <registration/AlgoRegistration.hpp>
+
+#include <object/Object.h>
+
+namespace {
+
+auto IsTransitive = registration::AbstractRegister < relation::IsTransitive, bool, const ext::set < ext::pair < object::Object, object::Object > > & > ( relation::IsTransitive::isTransitive, "relation" ).setDocumentation (
+"Checks whether a relation is transitive.\n\
+\n\
+@param relation the tested relation\n\
+\n\
+@return true if the relation is transitive, false otherwise");
+
+} /* namespace */
diff --git a/alib2aux/src/relation/IsTransitive.h b/alib2aux/src/relation/IsTransitive.h
new file mode 100644
index 0000000000..8b20741c4c
--- /dev/null
+++ b/alib2aux/src/relation/IsTransitive.h
@@ -0,0 +1,62 @@
+/*
+ * IsTransitive.h
+ *
+ * This file is part of Algorithms library toolkit.
+ * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
+
+ * Algorithms library toolkit is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+
+ * Algorithms library toolkit is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+
+ * You should have received a copy of the GNU General Public License
+ * along with Algorithms library toolkit.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  Created on: 2. 10. 2019
+ *	  Author: Jan Travnicek
+ */
+
+#ifndef IS_TRANSITIVE_H_
+#define IS_TRANSITIVE_H_
+
+#include <alib/set>
+#include <alib/pair>
+
+namespace relation {
+
+/**
+ * Computes the complement relation in given universe.
+ */
+class IsTransitive {
+public:
+	/**
+	 * Checks whether a relation is transitive.
+	 *
+	 * @tparam T Type of the items in relation.
+	 *
+	 * @param relation the tested relation
+	 *
+	 * @return true if the relation is transitive, false otherwise
+	 */
+	template < class T >
+	static bool isTransitive ( const ext::set < ext::pair < T, T > > & relation );
+};
+
+template < class T >
+bool IsTransitive::isTransitive ( const ext::set < ext::pair < T, T > > & relation ) {
+	for ( const ext::pair < T, T > & ab : relation )
+		for ( const ext::pair < T, T > & bc : relation )
+			if ( ab.second == bc.first && ! relation.contains ( ext::make_pair ( ab.first, bc.second ) ) )
+				return false;
+
+	return true;
+}
+
+} /* namespace relation */
+
+#endif /* IS_TRANSITIVE_H_ */
-- 
GitLab