jueves, 21 de febrero de 2019

Interface inheritance in Java 8 an example

Java 8 interface allows default implementations, static method declaration and static final attributes declarations: Here we show an example (just to show the concept) of interface declaration and inheritance. We can add that with the default code, inherited attributes are hidden and we will show the final solution.

Example: In the code bellow we had to redeclare the inherited default method in each interface because the static attributes "from" and "to" were hidden by the super interface Locationable. You can try to remove the methods in subinterfaces to verify.

public interface Locationable {
 float from = 0;
 float to = 0;
 public  default int  closeTo( float other) {
  System.out.println(from + " and " + to ); 
  if( other>=from && other<=to)
    return 1;
   return -1;
  }
 
}

public interface Southern extends Locationable {
 float from = 100;
 float to = 1000;
 public  default int  closeTo( float other) {
  System.out.println(from + " and " + to ); 
  if( other>=from && other<=to)
    return 1;
   return -1;
  }
}

public interface Northern extends Locationable {
 float from = 5000;
 float to = 7000;
 public  default int  closeTo( float other) {
  System.out.println(from + " and " + to ); 
  if( other>=from && other<=to)
    return 1;
   return -1;
  }
}
public interface Midthern extends Locationable {
 float from = 1000;
 float to = 5000;
 public  default int  closeTo( float other) {
  System.out.println(from + " and " + to ); 
  if( other>=from && other<=to)
    return 1;
   return -1;
  }
}


public class Walker implements Southern,Northern,Midthern{
 
 
 public int closeTo(float pos) {
      int r = -1;
  if( (Southern.super.closeTo(pos) == 1)) {
  
   System.out.println("Hey I'm in the South!!");
   r = 1;
   }
   else if( Northern.super.closeTo(pos) == 1) {
    System.out.println("Hey I'm in the North!!");
    r = 1;
  }
   else if( Midthern.super.closeTo(pos) == 1) {
    System.out.println("Hey I'm in the Midlands!!");
    r = 1;
  }else
   System.out.println("Hey I'm in the Lymbus!!!!");
 
      return r;

 }

}


void interfaces_exec() {

Walker walker = new Walker();

for(int i= 0; i < 20; i++) {
 float pos = (float)(Math.random() * 7000) % 7000;
 
 walker.closeTo(pos);
}
 

domingo, 17 de febrero de 2019

JPA inheritance tecniques

This article discuses different techniques of JPA inheritance.

It explains: 1) Mapped superclass, 2) Table per class, 3)Single table, 4)Joined table.

It gives an example and a discussion about what technique is appropiate depending on the case.

Link:

https://thoughts-on-java.org/complete-guide-inheritance-strategies-jpa-hibernate/

sábado, 2 de febrero de 2019

10 Architectural Patterns

The article describes and gives some examples of application of the following Architectural patterns:

  1. Layered pattern
  2. Client-server pattern
  3. Master-slave pattern
  4. Pipe-filter pattern
  5. Broker pattern
  6. Peer-to-peer pattern
  7. Event-bus pattern
  8. Model-view-controller pattern
  9. Blackboard pattern
  10. Interpreter pattern

It can be found

at

: 10 Common Software Architectural Patterns in a nutshell

viernes, 1 de febrero de 2019

AI: Causal models and Rothman's model applied in bioscience

AI: Causal Model and RothMan's model from biosciences

AI: Causal model and Rothman's model applied in bioscience

1.AI: Causal model

causal model, or a model of causality, is a representation of a domain that predicts the results of interventions. An intervention is an action that forces a variable to have a particular value. That is, an intervention changes the value in some way other than manipulating other variables in the model. To predict the effect of interventions, a causal model represents how the cause implies its effect. When the cause is changed, its effect should be changed. An evidential model represents a domain in the other direction – from effect to cause. Note that we do not assume that there is “the cause” of an effect; rather there are propositions, which together may cause the effect to become true.

Example

In the electrical domain depicted in Figure 5.2, consider the relationship between poles p1 and p2 and alarm a1. Assume all components are working properly. Alarm a1 sounds whenever both poles are + or both poles are -. Thus,

sounds_a1↔(+_p1 ⇔ +_p2) (*)
This is logically equivalent to
+_p1↔(sounds_a1 ⇔ +_p2)

This formula is symmetric between the three propositions; it is true if and only if an odd number of the propositions are true. However, in the world, the relationship between these propositions is not symmetric. Suppose both poles were positive and the alarm sounded . Putting p1 to negative  does not make p2 go negative to preserve a1 keep sounding. Instead, putting p1 to negative  makes sounds_a1 false, and positive_p2 remains true. Thus, to predict the result of interventions, we require more than proposition (*) above.

A causal model is

sounds_a1 ← positive_p1 ∧ positive_p2 (1) sounds_a1 ← ! positive_p1 ∧ ! positive_p2 (2)

The completion of this is equivalent to proposition (*); however, it makes reasonable predictions when one of the values is changed. Changing one of the pole positions changes whether the alarm sounds, but changing whether the alarm sounds (by some other mechanism) does not change whether the poles are positive or negative.

An evidential model is

positive_p1← sounds_a1 ∧ positive_p2 (1) positive_p1 ← ! sounds_a1 ∧ ! positive_p 2 (2)

This can be used to answer questions about whether p1 is + based on the charge of p2 and whether a1 sounds. Its completion is also equivalent to formula (*).

However, it does not accurately predict the effect of interventions For most purposes, it is preferable to use a causal model of the world as it is more transparent, stable and modular than an evidential model.

2. The Rothman's model (applied in biosciences)

Rothman’s model is a theoretical model that takes into account multi-causal relationship (has its origins on epimediology studies)

Definitions:

Cause: Event, condition or characteristic that plays an essential role in a generation of an effect (a consequence)
Cause types:

1- Component cause: A cause that contributes to generate a “conglomerate” that will produce a “Sufficient () cause”
2- Sufficient cause: A set of (component) causes that will produce an Effect
3- Necessary Cause: (To define)

Model characteristics:
I) None of the component causes is unnecessary
II) The Effect does not depend on a specific Sufficient Cause
III) A component cause can be part of more that one sufficient cause that produces the same Effect
IV) A component cause can be part of different sufficient causes that produces different Effects
V) The component causes of a sufficient cause are linked with other component causes of that sufficient cause (interelation)