Formal Context

A formal context is a triple K = (G, M, I), where G is a set of objects, M is a set of attributes, and I ⊆ G × M is a binary relation called incidence that expresses which objects have which attributes (objects_attributes).

Predicate gIm designates object g's having attribute m. For a subset A ⊆ G of objects and a subset B ⊆ M of attributes, one defines two derivation operators as follows:

  • A' = {m ∈ M | ∀ g ∈ A, gIm}, and dually
  • B' = {g ∈ G | ∀ m ∈ B, gIm}.

Introduced properties

fun attributes: HashSet[A]

fca :: FormalContext :: attributes

Attributes considered to build concepts
protected fun attributes=(attributes: HashSet[A])

fca :: FormalContext :: attributes=

Attributes considered to build concepts
fun formal_concepts: Set[FormalConcept[O, A]]

fca :: FormalContext :: formal_concepts

Derive the set of formal concepts from the objects and attributes
fun objects: HashSet[O]

fca :: FormalContext :: objects

Objects in the context
protected fun objects=(objects: HashSet[O])

fca :: FormalContext :: objects=

Objects in the context
fun objects_attributes: HashMap[O, Set[A]]

fca :: FormalContext :: objects_attributes

Association between objects and attributes
protected fun objects_attributes=(objects_attributes: HashMap[O, Set[A]])

fca :: FormalContext :: objects_attributes=

Association between objects and attributes
fun set_object_attribute(object: O, attribute: A)

fca :: FormalContext :: set_object_attribute

Associate an attribute to object
fun set_object_attributes(object: O, attributes: Collection[A])

fca :: FormalContext :: set_object_attributes

Associate a set of attributes to object

Redefined properties

redef type SELF: FormalContext[O, A]

fca $ FormalContext :: SELF

Type of this instance, automatically specialized in every class

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
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
fun attributes: HashSet[A]

fca :: FormalContext :: attributes

Attributes considered to build concepts
protected fun attributes=(attributes: HashSet[A])

fca :: FormalContext :: attributes=

Attributes considered to build concepts
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 formal_concepts: Set[FormalConcept[O, A]]

fca :: FormalContext :: formal_concepts

Derive the set of formal concepts from the objects and attributes
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
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.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun objects: HashSet[O]

fca :: FormalContext :: objects

Objects in the context
protected fun objects=(objects: HashSet[O])

fca :: FormalContext :: objects=

Objects in the context
fun objects_attributes: HashMap[O, Set[A]]

fca :: FormalContext :: objects_attributes

Association between objects and attributes
protected fun objects_attributes=(objects_attributes: HashMap[O, Set[A]])

fca :: FormalContext :: objects_attributes=

Association between objects and attributes
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 serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun set_object_attribute(object: O, attribute: A)

fca :: FormalContext :: set_object_attribute

Associate an attribute to object
fun set_object_attributes(object: O, attributes: Collection[A])

fca :: FormalContext :: set_object_attributes

Associate a set of attributes to object
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
package_diagram fca::FormalContext FormalContext core::Object Object fca::FormalContext->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

fca $ FormalContext
# Formal Context
#
# A formal context is a triple *K = (G, M, I)*, where *G* is a set of `objects`,
# *M* is a set of `attributes`, and *I ⊆ G × M* is a binary relation called incidence
# that expresses which objects have which attributes (`objects_attributes`).
#
# Predicate *gIm* designates object *g*'s having attribute *m*.
# For a subset *A ⊆ G* of objects and a subset *B ⊆ M* of attributes, one defines
# two derivation operators as follows:
#
# * *A' = {m ∈ M | ∀ g ∈ A, gIm}*, and dually
# * *B' = {g ∈ G | ∀ m ∈ B, gIm}*.
class FormalContext[O: Object, A: Object]

	# Objects in the context
	var objects = new HashSet[O]

	# Attributes considered to build concepts
	var attributes = new HashSet[A]

	# Association between objects and attributes
	var objects_attributes = new HashMap[O, Set[A]]

	# Associate a set of `attributes` to `object`
	fun set_object_attributes(object: O, attributes: Collection[A]) do
		for attribute in attributes do
			set_object_attribute(object, attribute)
		end
	end

	# Associate an `attribute` to `object`
	fun set_object_attribute(object: O, attribute: A) do
		attributes.add attribute
		objects.add object
		if not objects_attributes.has_key(object) then
			objects_attributes[object] = new HashSet[A]
		end
		objects_attributes[object].add attribute
	end

	# Derive the set of formal concepts from the objects and attributes
	fun formal_concepts: Set[FormalConcept[O, A]] do
		# black magic!

		var concepts = new HashSet[FormalConcept[O, A]]

		var extentsByAttr = new HashMap[Set[A], Set[O]]
		for attribute in attributes do
			var ka = new HashSet[A].from([attribute])
			extentsByAttr[ka] = new HashSet[O]
			for object in objects do
				if not objects_attributes[object].has(attribute) then continue
				extentsByAttr[ka].add(object)
			end
		end

		var nextents = new HashMap[Set[A], Set[O]]
		for k1, v1 in extentsByAttr do
			for k2, v2 in extentsByAttr do
				if k1 == k2 then continue
				var n = v1.intersection(v2)
				if extentsByAttr.values.has(n) then continue
				var ka = k1.union(k2)
				nextents[ka] = n
			end
		end
		extentsByAttr.add_all nextents

		var contained = true
		for k1, v1 in extentsByAttr do
			if not contained then break
			for k2, v2 in extentsByAttr do
				if k1 == k2 then continue
				var n = v1.intersection(v2)
				if extentsByAttr.values.has(n) then continue
				contained = false
				break
			end
		end

		if contained then
			extentsByAttr[new HashSet[A]] = new HashSet[O].from(objects)
		end

		var extents = new HashSet[Set[O]]
		for objects in extentsByAttr.values do
			extents.add objects
		end

		for extent in extents do
			var intents: Set[A] = new HashSet[A]
			var count = 0
			var cl = new FormalConcept[O, A]
			if extent.is_empty then
				intents.add_all(attributes)
			else
				for object in objects do
					if not extent.has(object) then continue
					var prev = objects_attributes[object]
					if count > 0 then
						intents = prev.intersection(intents)
					else
						intents = prev
					end
					count += 1
					cl.objects.add(object)
				end
			end
			cl.attributes.add_all intents
			concepts.add cl
		end

		return concepts
	end
end
lib/fca/fca.nit:66,1--180,3