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
poset :: POSetElement :: defaultinit
poset :: POSetElement :: depth
The length of the shortest path to the root of the poset hierarchyposet :: POSetElement :: direct_greaters
Return the set of all elementst
that have a direct edge from element
to t
.
poset :: POSetElement :: direct_smallers
Return the set of all elementsf
that have an edge from f
to element
.
poset :: POSetElement :: element=
The real object behind the viewposet :: POSetElement :: greaters
Return the set of all elementst
that have an edge from element
to t
.
poset :: POSetElement :: smallers
Return the set of all elementsf
that have an edge from f
to element
.
poset $ POSetElement :: SELF
Type of this instance, automatically specialized in every classposet $ POSetElement :: core_serialize_to
Actual serialization ofself
to serializer
poset $ POSetElement :: from_deserializer
Create an instance of this class from thedeserializer
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Object :: defaultinit
poset :: POSetElement :: defaultinit
poset :: POSetElement :: depth
The length of the shortest path to the root of the poset hierarchyposet :: POSetElement :: direct_greaters
Return the set of all elementst
that have a direct edge from element
to t
.
poset :: POSetElement :: direct_smallers
Return the set of all elementsf
that have an edge from f
to element
.
poset :: POSetElement :: element=
The real object behind the viewserialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
poset :: POSetElement :: greaters
Return the set of all elementst
that have an edge from element
to t
.
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
poset :: POSetElement :: smallers
Return the set of all elementsf
that have an edge from f
to element
.
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
Serializer::serialize
# 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