View of an object in a poset

This class is a helper to handle specific queries on a same object

For instance, one common usage is to add a specific attribute for each poset a class belong.

class Thing
    var in_some_relation: POSetElement[Thing]
    var in_other_relation: POSetElement[Thing]
end
var t: Thing
# ...
t.in_some_relation.greaters

Introduced properties

fun <(t: E): Bool

poset :: POSetElement :: <

Is t != element and is there an edge from element to t?
fun <=(t: E): Bool

poset :: POSetElement :: <=

Is there an edge from element to t?
init defaultinit(poset: POSet[E], element: E, count: Int)

poset :: POSetElement :: defaultinit

fun depth: Int

poset :: POSetElement :: depth

The length of the shortest path to the root of the poset hierarchy
fun direct_greaters: Collection[E]

poset :: POSetElement :: direct_greaters

Return the set of all elements t that have a direct edge from element to t.
fun direct_smallers: Collection[E]

poset :: POSetElement :: direct_smallers

Return the set of all elements f that have an edge from f to element.
fun element: E

poset :: POSetElement :: element

The real object behind the view
protected fun element=(element: E)

poset :: POSetElement :: element=

The real object behind the view
fun greaters: Collection[E]

poset :: POSetElement :: greaters

Return the set of all elements t that have an edge from element to t.
fun poset: POSet[E]

poset :: POSetElement :: poset

The poset self belong to
protected fun poset=(poset: POSet[E])

poset :: POSetElement :: poset=

The poset self belong to
fun smallers: Collection[E]

poset :: POSetElement :: smallers

Return the set of all elements f that have an edge from f to element.

Redefined properties

redef type SELF: POSetElement[E]

poset $ POSetElement :: SELF

Type of this instance, automatically specialized in every class
redef fun core_serialize_to(serializer: Serializer)

poset $ POSetElement :: core_serialize_to

Actual serialization of self to serializer
redef init from_deserializer(v: Deserializer)

poset $ POSetElement :: from_deserializer

Create an instance of this class from the deserializer

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun <(t: E): Bool

poset :: POSetElement :: <

Is t != element and is there an edge from element to t?
fun <=(t: E): Bool

poset :: POSetElement :: <=

Is there an edge from element to t?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
protected fun accept_json_serializer(v: JsonSerializer)

serialization :: Serializable :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON
protected fun accept_msgpack_attribute_counter(v: AttributeCounter)

serialization :: Serializable :: accept_msgpack_attribute_counter

Hook to customize the behavior of the AttributeCounter
protected fun accept_msgpack_serializer(v: MsgPackSerializer)

serialization :: Serializable :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)

serialization :: Serializable :: add_to_bundle

Called by []= to dynamically choose the appropriate method according
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun core_serialize_to(serializer: Serializer)

serialization :: Serializable :: core_serialize_to

Actual serialization of self to serializer
init defaultinit(poset: POSet[E], element: E, count: Int)

poset :: POSetElement :: defaultinit

fun depth: Int

poset :: POSetElement :: depth

The length of the shortest path to the root of the poset hierarchy
fun direct_greaters: Collection[E]

poset :: POSetElement :: direct_greaters

Return the set of all elements t that have a direct edge from element to t.
fun direct_smallers: Collection[E]

poset :: POSetElement :: direct_smallers

Return the set of all elements f that have an edge from f to element.
fun element: E

poset :: POSetElement :: element

The real object behind the view
protected fun element=(element: E)

poset :: POSetElement :: element=

The real object behind the view
init from_deserializer(deserializer: Deserializer)

serialization :: Serializable :: from_deserializer

Create an instance of this class from the deserializer
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun greaters: Collection[E]

poset :: POSetElement :: greaters

Return the set of all elements t that have an edge from element to t.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
protected fun msgpack_extra_array_items: Int

serialization :: Serializable :: msgpack_extra_array_items

Hook to request a larger than usual metadata array
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun poset: POSet[E]

poset :: POSetElement :: poset

The poset self belong to
protected fun poset=(poset: POSet[E])

poset :: POSetElement :: poset=

The poset self belong to
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(plain: nullable Bool): Bytes

serialization :: Serializable :: serialize_msgpack

Serialize self to MessagePack bytes
fun serialize_to(serializer: Serializer)

serialization :: Serializable :: serialize_to

Serialize self to serializer
fun serialize_to_json(plain: nullable Bool, pretty: nullable Bool): String

serialization :: Serializable :: serialize_to_json

Serialize self to JSON
fun smallers: Collection[E]

poset :: POSetElement :: smallers

Return the set of all elements f that have an edge from f to element.
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun to_json: String

serialization :: Serializable :: to_json

Serialize self to plain JSON
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_pretty_json: String

serialization :: Serializable :: to_pretty_json

Serialize self to plain pretty JSON
fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram poset::POSetElement POSetElement serialization::Serializable Serializable poset::POSetElement->serialization::Serializable core::Object Object serialization::Serializable->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

interface Serializable

serialization :: Serializable

Instances of this class can be passed to Serializer::serialize

Class definitions

poset $ POSetElement
# View of an object in a poset
# This class is a helper to handle specific queries on a same object
#
# For instance, one common usage is to add a specific attribute for each poset a class belong.
#
# ~~~nitish
# class Thing
#     var in_some_relation: POSetElement[Thing]
#     var in_other_relation: POSetElement[Thing]
# end
# var t: Thing
# # ...
# t.in_some_relation.greaters
# ~~~
class POSetElement[E]
	super Serializable

	# The poset self belong to
	var poset: POSet[E]

	# The real object behind the view
	var element: E

	private var tos = new HashSet[E]
	private var froms = new HashSet[E]
	private var dtos = new HashSet[E]
	private var dfroms = new HashSet[E]

	# The rank of the
	# This attribute is used to force a total order for POSet#compare
	private var count: Int

	# Return the set of all elements `t` that have an edge from `element` to `t`.
	# Since the POSet is reflexive, element is included in the set.
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D"])
	# assert pos["B"].greaters.has_exactly(["B", "C", "D"])
	# ~~~~
	fun greaters: Collection[E]
	do
		return self.tos
	end

	# Return the set of all elements `t` that have a direct edge from `element` to `t`.
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D"])
	# assert pos["B"].direct_greaters.has_exactly(["C"])
	# ~~~~
	fun direct_greaters: Collection[E]
	do
		return self.dtos
	end

	# Return the set of all elements `f` that have an edge from `f` to `element`.
	# Since the POSet is reflexive, element is included in the set.
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D"])
	# assert pos["C"].smallers.has_exactly(["A", "B", "C"])
	# ~~~~
	fun smallers: Collection[E]
	do
		return self.froms
	end

	# Return the set of all elements `f` that have an edge from `f` to `element`.
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D"])
	# assert pos["C"].direct_smallers.has_exactly(["B"])
	# ~~~~
	fun direct_smallers: Collection[E]
	do
		return self.dfroms
	end

	# Is there an edge from `element` to `t`?
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D"])
	# assert     pos["B"] <= "D"
	# assert     pos["B"] <= "C"
	# assert     pos["B"] <= "B"
	# assert not pos["B"] <= "A"
	# ~~~~
	fun <=(t: E): Bool
	do
		return self.tos.has(t)
	end

	# Is `t != element` and is there an edge from `element` to `t`?
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D"])
	# assert     pos["B"] < "D"
	# assert     pos["B"] < "C"
	# assert not pos["B"] < "B"
	# assert not pos["B"] < "A"
	# ~~~~
	fun <(t: E): Bool
	do
		return t != self.element and self.tos.has(t)
	end

	# The length of the shortest path to the root of the poset hierarchy
	#
	# ~~~~
	# var pos = new POSet[String]
	# pos.add_chain(["A", "B", "C", "D"])
	# assert pos["A"].depth == 3
	# assert pos["D"].depth == 0
	# ~~~~
	fun depth: Int do
		if direct_greaters.is_empty then
			return 0
		end
		var min = -1
		for p in direct_greaters do
			var d = poset[p].depth + 1
			if min == -1 or d < min then
				min = d
			end
		end
		return min
	end

	redef fun core_serialize_to(serializer)
	do
		serializer.serialize_attribute("poset", poset)
		serializer.serialize_attribute("element", element)
		serializer.serialize_attribute("tos", tos)
		serializer.serialize_attribute("froms", froms)
		serializer.serialize_attribute("dtos", dtos)
		serializer.serialize_attribute("dfroms", dfroms)
		serializer.serialize_attribute("count", count)

		# Don't serialize `froms`, `dtos` and `tos` as they duplicate information.
		# TODO serialize them if a flag for extra info is set on `serializer`.
	end

	redef init from_deserializer(v)
	do
		# Code generated by the serialization_phase from the compiler frontend,
		# copied here for compatibility with nith.

		super
		v.notify_of_creation self

		var poset = v.deserialize_attribute("poset", "POSet[nullable Object]")
		if v.deserialize_attribute_missing then
			v.errors.add new Error("Deserialization Error: attribute `{class_name}::poset` missing from JSON object")
		else if not poset isa POSet[E] then
			v.errors.add new AttributeTypeError(self, "poset", poset, "POSet[nullable Object]")
			if v.keep_going == false then return
		else
			self.poset = poset
		end

		var element = v.deserialize_attribute("element", "nullable Object")
		if v.deserialize_attribute_missing then
			v.errors.add new Error("Deserialization Error: attribute `{class_name}::element` missing from JSON object")
		else if not element isa E then
			v.errors.add new AttributeTypeError(self, "element", element, "nullable Object")
			if v.keep_going == false then return
		else
			self.element = element
		end

		var tos = v.deserialize_attribute("tos", "HashSet[nullable Object]")
		if v.deserialize_attribute_missing then
		else if not tos isa HashSet[E] then
			v.errors.add new AttributeTypeError(self, "tos", tos, "HashSet[nullable Object]")
			if v.keep_going == false then return
		else
			self.tos = tos
		end

		var froms = v.deserialize_attribute("froms", "HashSet[nullable Object]")
		if v.deserialize_attribute_missing then
		else if not froms isa HashSet[E] then
			v.errors.add new AttributeTypeError(self, "froms", froms, "HashSet[nullable Object]")
			if v.keep_going == false then return
		else
			self.froms = froms
		end

		var dtos = v.deserialize_attribute("dtos", "HashSet[nullable Object]")
		if v.deserialize_attribute_missing then
		else if not dtos isa HashSet[E] then
			v.errors.add new AttributeTypeError(self, "dtos", dtos, "HashSet[nullable Object]")
			if v.keep_going == false then return
		else
			self.dtos = dtos
		end

		var dfroms = v.deserialize_attribute("dfroms", "HashSet[nullable Object]")
		if v.deserialize_attribute_missing then
		else if not dfroms isa HashSet[E] then
			v.errors.add new AttributeTypeError(self, "dfroms", dfroms, "HashSet[nullable Object]")
			if v.keep_going == false then return
		else
			self.dfroms = dfroms
		end

		var count = v.deserialize_attribute("count", "Int")
		if v.deserialize_attribute_missing then
			v.errors.add new Error("Deserialization Error: attribute `{class_name}::count` missing from JSON object")
		else if not count isa Int then
			v.errors.add new AttributeTypeError(self, "count", count, "Int")
			if v.keep_going == false then return
		else
			self.count = count
		end
	end
end
lib/poset/poset.nit:509,1--731,3