<commit_before>/*
 * opencog/atoms/TypeNode.h
 *
 * Copyright (C) 2015 <NAME>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program 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 Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _OPENCOG_TYPE_NODE_H
#define _OPENCOG_TYPE_NODE_H

#include <opencog/atoms/base/ClassServer.h>
#include <opencog/atoms/base/Node.h>

namespace opencog
{
/** \addtogroup grp_atomspace
 *  @{
 *
 * Experimental TypeNode class. This is a rough sketch for how things
 * like this might be done. It is not necessarily a good idea, and might
 * be replaced by something completely different, someday ...
 */

class TypeNode : public Node
{
protected:
	Type value;

public:
	TypeNode(const std::string& s)
		// Convert to number and back to string to avoid miscompares.
		: Node(TYPE_NODE, s),
		  value(classserver().getType(s))
	{
		if (NOTYPE == value)
			throw InvalidParamException(TRACE_INFO,
				"Not a valid typename: '%s'", s.c_str());
	}

	TypeNode(Type t)
		: Node(TYPE_NODE, classserver().getTypeName(t)),
		  value(t)
	{}

	TypeNode(Node &n)
		: Node(n),
		  value(classserver().getType(n.getName()))
	{
		OC_ASSERT(classserver().isA(n.getType(), TYPE_NODE),
			"Bad TypeNode constructor!");

		if (NOTYPE == value)
			throw InvalidParamException(TRACE_INFO,
				"Not a valid typename: '%s'", n.getName().c_str());
	}

	static void validate(const std::string& str)
	{
		Type t = classserver().getType(str);
		if (NOTYPE == t)
			throw InvalidParamException(TRACE_INFO,
				"Not a valid typename: '%s'", str.c_str());
	}
<commit_msg>Bug-fix the DefinedTypeNode to not throw.<commit_after>/*
 * opencog/atoms/TypeNode.h
 *
 * Copyright (C) 2015 <NAME>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * This program 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 Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _OPENCOG_TYPE_NODE_H
#define _OPENCOG_TYPE_NODE_H

#include <opencog/atoms/base/ClassServer.h>
#include <opencog/atoms/base/Node.h>

namespace opencog
{
/** \addtogroup grp_atomspace
 *  @{
 *
 * Experimental TypeNode class. This is a rough sketch for how things
 * like this might be done. It is not necessarily a good idea, and might
 * be replaced by something completely different, someday ...
 */

class TypeNode : public Node
{
protected:
	Type value;

public:
	TypeNode(const std::string& s)
		// Convert to number and back to string to avoid miscompares.
		: Node(TYPE_NODE, s),
		  value(classserver().getType(s))
	{
		if (NOTYPE == value)
			throw InvalidParamException(TRACE_INFO,
				"Not a valid typename: '%s'", s.c_str());
	}

	TypeNode(Type t)
		: Node(TYPE_NODE, classserver().getTypeName(t)),
		  value(t)
	{}

	TypeNode(Node &n)
		: Node(n),
		  value(classserver().getType(n.getName()))
	{
		OC_ASSERT(classserver().isA(n.getType(), TYPE_NODE),
			"Bad TypeNode constructor!");

		if (DEFINED_TYPE_NODE != _type and NOTYPE == value)
			throw InvalidParamException(TRACE_INFO,
				"Not a valid typename: '%s'", n.getName().c_str());

		if (DEFINED_TYPE_NODE == _type and NOTYPE != value)
			throw InvalidParamException(TRACE_INFO,
				"Redefinition of a built-in typename: '%s'", n.getName().c_str());
	}

	static void validate(const std::string& str)
	{
		Type t = classserver().getType(str);
		// XXX TODO ... Some types are defined. In this case,
		// verify that the string occurs as a name inside
		// some DefineLink... if it does, then it's valid.
		// If it does not, then it's invalid.
		if (NOTYPE == t)
			throw InvalidParamException(TRACE_INFO,
				"Not a valid typename: '%s'", str.c_str());
	}

{'fraction_non_alphanumeric': 0.11145898559799625, 'fraction_numerical': 0.006679190148194531, 'mean_word_length': 3.9048106448311155, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1325811', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1483, 'n_words': 536}