Object-Oriented Probabilistic Relational Model

Creative Commons License

aGrUM

interactive online version

In [1]:
import pyagrum as gum
import pyagrum.lib.notebook as gnb

O3PRM is inspired by relational language, directly based on the BN and improved by object oriented paradigms, where the focus is set on classes of objects and by defining relations among these objects (see http://o3prm.gitlab.io/ for more details)

18afc24064a242a69d6a3181cc888fe0

O3PRM syntax

In [2]:
with open("res/Asia.o3prm", "r") as prm:
  for line in prm.readlines():
    print(line, end="")
class Asia {

    boolean visitToAsia {
        [ 0.99,  // False
          0.01 ] // True
    };

    boolean tuberculosis dependson visitToAsia {
        // False | True
        [    0.99, 0.95,  // False
             0.01, 0.05 ] // True
    };

    boolean smoking {
        [ 0.50,  // False
          0.50 ] // True
    };

    boolean lungCancer dependson smoking {
        // False | True => smoking
        [    0.99, 0.90,  // False
             0.01, 0.10 ] // True
    };

    boolean bronchitis dependson smoking {
        // False | True => smoking
        [    0.70, 0.40,  // False
             0.30, 0.60 ] // True
    };

    boolean tubOrCancer dependson tuberculosis, lungCancer {
        //      False    ||     True      => tuberculosis
        //  False | True || False | True  => lungCancer
        [     1.00, 0.00,     0.00, 0.00,  // False
              0.00, 1.00,     1.00, 1.00 ] // True
    };

    boolean positiveXRay dependson tubOrCancer {
        // False | True => tubOrCancer
        [ 0.95, 0.02,  // False
          0.05, 0.98 ] // True
    };

    boolean dyspnea dependson tubOrCancer, bronchitis {
        //      False    ||     True      => tubOrCancer
        //  False | True || False | True  => bronchitis
        [     0.90, 0.20,     0.30, 0.10,  // False
              0.10, 0.80,     0.70, 0.90 ] // True
    };
}

Using o3prm syntax for creating BayesNet

In [3]:
bn = gum.loadBN("res/Asia.o3prm", verbose=False)
bn
Out[3]:
G lungCancer lungCancer tubOrCancer tubOrCancer lungCancer->tubOrCancer tuberculosis tuberculosis tuberculosis->tubOrCancer visitToAsia visitToAsia visitToAsia->tuberculosis positiveXRay positiveXRay tubOrCancer->positiveXRay dyspnea dyspnea tubOrCancer->dyspnea bronchitis bronchitis bronchitis->dyspnea smoking smoking smoking->lungCancer smoking->bronchitis
In [4]:
bn = gum.loadBN("res/aSys.o3prm")
bn
Out[4]:
G A0.Cost A0.Cost A2.Capacity A2.Capacity A2.Cost A2.Cost A2.Capacity->A2.Cost A0.Capacity A0.Capacity A0.Capacity->A0.Cost A0.Productivity A0.Productivity A0.Productivity->A0.Cost A2.Agg_Productivity A2.Agg_Productivity A0.Productivity->A2.Agg_Productivity A1.Cost A1.Cost A1.Productivity A1.Productivity A1.Productivity->A1.Cost A1.Productivity->A2.Agg_Productivity A1.Capacity A1.Capacity A1.Capacity->A1.Cost A2.Productivity A2.Productivity A2.Productivity->A2.Cost A2.Agg_Productivity->A2.Productivity
In [5]:
classpath = "res/ComplexPrinters"
filename = "res/ComplexPrinters/fr/lip6/printers/system.o3prm"

system = "Work"
bn = gum.loadBN(filename, system=system, classpath=classpath)
In [6]:
# the inference will take place in a rather large junction tree
gnb.showJunctionTreeMap(bn, scaleClique=0.1, scaleSep=0.05, lenEdge=1.2, size="8!")
../_images/notebooks_25-Models_o3prm_10_0.svg
In [7]:
gum.saveBN(bn, "out/bnprinters.bifxml")
In [8]:
bn2 = gum.loadBN("out/bnprinters.bifxml")
gnb.showInference(bn2, size="25!")
../_images/notebooks_25-Models_o3prm_12_0.svg
In [9]:
import pyagrum.lib.image as gimg

gimg.export(bn, filename="out/ComplexPrinters.pdf")
In [10]:
bn = gum.loadBN("res/aSys.o3prm")
gnb.sideBySide(gnb.getBN(bn, size="5"), gnb.getInference(bn, size="5"))
G A0.Cost A0.Cost A2.Capacity A2.Capacity A2.Cost A2.Cost A2.Capacity->A2.Cost A0.Capacity A0.Capacity A0.Capacity->A0.Cost A0.Productivity A0.Productivity A0.Productivity->A0.Cost A2.Agg_Productivity A2.Agg_Productivity A0.Productivity->A2.Agg_Productivity A1.Cost A1.Cost A1.Productivity A1.Productivity A1.Productivity->A1.Cost A1.Productivity->A2.Agg_Productivity A1.Capacity A1.Capacity A1.Capacity->A1.Cost A2.Productivity A2.Productivity A2.Productivity->A2.Cost A2.Agg_Productivity->A2.Productivity
structs Inference in   3.42ms A1.Productivity 2025-10-29T15:36:59.690933 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A1.Cost 2025-10-29T15:36:59.724866 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A1.Productivity->A1.Cost A2.Agg_Productivity 2025-10-29T15:36:59.851031 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A1.Productivity->A2.Agg_Productivity A1.Capacity 2025-10-29T15:36:59.708668 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A1.Capacity->A1.Cost A0.Productivity 2025-10-29T15:36:59.750239 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A0.Cost 2025-10-29T15:36:59.784453 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A0.Productivity->A0.Cost A0.Productivity->A2.Agg_Productivity A0.Capacity 2025-10-29T15:36:59.767181 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A0.Capacity->A0.Cost A2.Productivity 2025-10-29T15:36:59.800319 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A2.Cost 2025-10-29T15:36:59.833657 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A2.Productivity->A2.Cost A2.Capacity 2025-10-29T15:36:59.816422 image/svg+xml Matplotlib v3.10.7, https://matplotlib.org/ A2.Capacity->A2.Cost A2.Agg_Productivity->A2.Productivity

Exploring Probabilistic Relational Model

In [11]:
classpath = "res/ComplexPrinters"
filename = "res/ComplexPrinters/complexprinters_system.o3prm"
explor = gum.PRMexplorer()
explor.load(filename)
In [12]:
for cl in explor.classes():
  print("Class : " + cl)
  print("  - Super class : " + ("None" if explor.getSuperClass(cl) == None else explor.getSuperClass(cl)))
  print("  - Implemented interface : ")
  for inter in explor.classImplements(cl):
    print("      " + inter)
  print("  - Direct sub-types : ")
  for ext in explor.getDirectSubClass(cl):
    print("      " + ext)
  print("  - Attributes : ")
  for t, n, depensons in explor.classAttributes(cl):
    s = ""
    for depenson in depensons:
      s = s + depenson + " "
    print("      " + t + " " + n + " (" + s + ")")
  print("  - References : ")
  for t, n, isArray in explor.classReferences(cl):
    print("      " + t + ("[]" if isArray else "") + " " + n)
  print("  - Aggragates : ")
  for t, n, g, l, slots in explor.classAggregates(cl):
    s = ""
    for slot in slots:
      s = s + slot + " "
    print("      " + t + " " + n + " " + g + " " + ("NoLabel" if l == None else l) + " (" + s + ")")
  print("  - SlotChains : ")
  for t, n, isMultiple in explor.classSlotChains(cl):
    print("      " + t + " " + n + " " + ("[]" if isMultiple else ""))
  print("  - Parameters : ")
  for param in explor.classParameters(cl):
    print("      " + param)
  # print("  - Dag : ")
  # (dic, dotString) = explor.classDag(cl)
  # print(dic)
  # print(dotString)

  print()
Class : Computer
  - Super class : None
  - Implemented interface :
      Equipment
  - Direct sub-types :
  - Attributes :
      t_degraded equipState (room.power.state )
      boolean can_print (equipState working_printer )
  - References :
      Room room
      Printer[] printers
  - Aggragates :
      boolean functional_printer exists OK (printers.equipState )
      mycount count_printers count true (degraded_printer functional_printer )
      boolean degraded_printer exists Degraded (printers.equipState )
      boolean working_printer exists true (degraded_printer functional_printer )
  - SlotChains :
      t_state room.power.state
      t_degraded printers.equipState []
  - Parameters :

Class : ParamClass<lambda=0.4,t=4>
  - Super class : ParamClass
  - Implemented interface :
  - Direct sub-types :
  - Attributes :
      t_degraded equipState (room.power.state hasInk hasPaper )
      t_paper hasPaper ()
      t_ink hasInk ()
  - References :
      Room room
  - Aggragates :
  - SlotChains :
      t_state room.power.state
  - Parameters :
      t
      lambda

Class : PowerSupply
  - Super class : None
  - Implemented interface :
  - Direct sub-types :
  - Attributes :
      t_state state ()
  - References :
  - Aggragates :
  - SlotChains :
  - Parameters :

Class : SafeComputer
  - Super class : None
  - Implemented interface :
      Equipment
  - Direct sub-types :
  - Attributes :
      boolean can_print (working_printer equipState )
      t_degraded equipState (room.power.(t_state)state )
  - References :
      Room room
      Printer[] printers
  - Aggragates :
      boolean degraded_printer exists Degraded (printers.(t_degraded)equipState )
      boolean working_printer exists true (functional_printer degraded_printer )
      boolean functional_printer exists OK (printers.(t_state)equipState )
  - SlotChains :
      t_state room.power.(t_state)state
      t_state printers.(t_state)equipState []
      t_degraded printers.(t_degraded)equipState []
  - Parameters :

Class : ParamClass<lambda=0.001,t=4>
  - Super class : ParamClass
  - Implemented interface :
  - Direct sub-types :
  - Attributes :
      t_ink hasInk ()
      t_paper hasPaper ()
      t_degraded equipState (room.power.state hasInk hasPaper )
  - References :
      Room room
  - Aggragates :
  - SlotChains :
      t_state room.power.state
  - Parameters :
      lambda
      t

Class : Room
  - Super class : None
  - Implemented interface :
  - Direct sub-types :
  - Attributes :
  - References :
      PowerSupply power
  - Aggragates :
  - SlotChains :
  - Parameters :

Class : ColorPrinter
  - Super class : None
  - Implemented interface :
      Printer
  - Direct sub-types :
  - Attributes :
      t_paper hasPaper ()
      t_ink cyan ()
      t_ink yellow ()
      t_ink magenta ()
      t_ink black ()
      t_degraded equipState (room.power.state hasPaper hasInk black )
  - References :
      Room room
  - Aggragates :
      boolean hasInk forall NotEmpty (yellow cyan black magenta )
  - SlotChains :
      t_state room.power.state
  - Parameters :

Class : ParamClass
  - Super class : None
  - Implemented interface :
  - Direct sub-types :
      ParamClass<lambda=0.4,t=4>
      ParamClass<lambda=0.001,t=4>
  - Attributes :
      t_degraded equipState (room.power.state hasInk hasPaper )
      t_paper hasPaper ()
      t_ink hasInk ()
  - References :
      Room room
  - Aggragates :
  - SlotChains :
      t_state room.power.state
  - Parameters :
      t
      lambda

Class : BWPrinter
  - Super class : None
  - Implemented interface :
      Printer
  - Direct sub-types :
  - Attributes :
      t_ink hasInk ()
      t_degraded equipState (room.power.state hasPaper hasInk )
      t_paper hasPaper ()
  - References :
      Room room
  - Aggragates :
  - SlotChains :
      t_state room.power.state
  - Parameters :

In [13]:
print("The following lists the systems of the prm:\n")
systems = explor.getalltheSystems()
sys1 = systems[0]
print("Name of the system: " + sys1[0] + "\n")
print("Nodes : dict(id: [name,type])")
print(sys1[1])
print("\n")
print("Arcs : List[(tail, head),(tail, head)...]")
print(sys1[2])
The following lists the systems of the prm:

Name of the system: aSys

Nodes : dict(id: [name,type])
{0: ('pow', 'PowerSupply'), 1: ('r', 'Room'), 2: ('bw_printers[0]', 'BWPrinter'), 3: ('bw_printers[1]', 'BWPrinter'), 4: ('bw_printers[2]', 'BWPrinter'), 5: ('bw_printers[3]', 'BWPrinter'), 6: ('bw_printers[4]', 'BWPrinter'), 7: ('bw_printers[5]', 'BWPrinter'), 8: ('bw_printers[6]', 'BWPrinter'), 9: ('bw_printers[7]', 'BWPrinter'), 10: ('bw_printers[8]', 'BWPrinter'), 11: ('bw_printers[9]', 'BWPrinter'), 12: ('color_printers[0]', 'ColorPrinter'), 13: ('color_printers[1]', 'ColorPrinter'), 14: ('c1', 'Computer'), 15: ('c2', 'Computer'), 16: ('p', 'ParamClass<lambda=0.4,t=4>'), 17: ('paramBis', 'ParamClass<lambda=0.001,t=4>')}


Arcs : List[(tail, head),(tail, head)...]
[]
In [14]:
gnb.showTensor(explor.cpf("Computer", "equipState"))
t_degraded-0x719c28900
room.power.state
OK
Dysfunctional
Degraded
OK
0.90000.09000.0100
NOK
0.00001.00000.0000
In [15]:
for cl in explor.types():
  print("Type : " + cl)
  print("  - Super type : " + ("None" if explor.getSuperType(cl) == None else explor.getSuperType(cl)))
  print("  - Direct sub-types : ")
  for name in explor.getDirectSubTypes(cl):
    print("      " + name)
  print("  - Labels : ")
  for t in explor.getLabels(cl):
    print("      " + t)
  print("  - Labels mapping : ")
  for key, val in dict().items() if explor.getLabelMap(cl) == None else explor.getLabelMap(cl).items():
    print("      " + key + " -> " + val)
  print()
Type : boolean
  - Super type : None
  - Direct sub-types :
      t_state
  - Labels :
      false
      true
  - Labels mapping :

Type : t_ink
  - Super type : t_state-0x719c1d6c0
  - Direct sub-types :
  - Labels :
      NotEmpty
      Empty
  - Labels mapping :
      NotEmpty -> OK
      Empty -> NOK

Type : t_paper
  - Super type : t_state-0x719c1d6c0
  - Direct sub-types :
  - Labels :
      Ready
      Jammed
      Empty
  - Labels mapping :
      Ready -> OK
      Jammed -> NOK
      Empty -> NOK

Type : t_state
  - Super type : boolean-0x71ac4fe80
  - Direct sub-types :
      t_ink
      t_paper
      t_degraded
  - Labels :
      OK
      NOK
  - Labels mapping :
      OK -> true
      NOK -> false

Type : t_degraded
  - Super type : t_state-0x719c1d6c0
  - Direct sub-types :
  - Labels :
      OK
      Dysfunctional
      Degraded
  - Labels mapping :
      OK -> OK
      Dysfunctional -> NOK
      Degraded -> NOK

Type : mycount
  - Super type : None
  - Direct sub-types :
  - Labels :
      0
      1
      2
      3
      4
      5
  - Labels mapping :

In [16]:
for cl in explor.interfaces():
  print("Interface : " + cl)
  print("  - Super interface : " + ("None" if explor.getSuperInterface(cl) == None else explor.getSuperInterface(cl)))
  print("  - Direct sub-interfaces : ")
  for name in explor.getDirectSubInterfaces(cl):
    print("      " + name)
  print("  - Implementations : ")
  for impl in explor.getImplementations(cl):
    print("      " + impl)
  print("  - Attributes : ")
  for t, n in explor.interAttributes(cl, allAttributes=True):
    print("      " + t + " " + n)
  print("  - References : ")
  for t, n, isArray in explor.interReferences(cl):
    print("      " + t + ("[]" if isArray else "") + " " + n)
  print()
Interface : Equipment
  - Super interface : None
  - Direct sub-interfaces :
      Printer
  - Implementations :
      Computer
      SafeComputer
  - Attributes :
      t_degraded equipState
      boolean equipState
      t_state equipState
  - References :
      Room room

Interface : Printer
  - Super interface : Equipment
  - Direct sub-interfaces :
  - Implementations :
      ColorPrinter
      BWPrinter
  - Attributes :
      boolean equipState
      t_degraded equipState
      t_state equipState
      boolean hasPaper
      boolean hasInk
  - References :
      Room room

In [17]:
print(explor.isType("fr.lip6.printers.base.Printer"))
print(explor.isClass("fr.lip6.printers.base.Printer"))
print(explor.isInterface("fr.lip6.printers.base.Printer"))
False
False
False
In [ ]: