Scala Domain Modeling and Architecture

5,996 views
5,790 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,996
On SlideShare
0
From Embeds
0
Number of Embeds
247
Actions
Shares
0
Downloads
99
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Scala Domain Modeling and Architecture

  1. 1. Scala  Domain  Modeling  and  Architecture  Experience  Report   Hossam  Karim  
  2. 2. for {! ! locus Chromosome ⤞ Gene ⤞ Locus! if Locus range!! path locus ⤞ Sequence ⤞ nucleotide! if nucleotide alignment (_ > 89)!!} yield path!
  3. 3.  inputMessage >>= fasta >>= {   case n: Nucleotide ⇒ ! ("media" → "nucleotide") ~> n.giNumber     case _: AminoAcid ⇒ ! fail[String](! "Expected a ‘nucleotide’ Sequence")   case _ ⇒ !     fail[String](! "Expected a ‘single’ Sequence representation”)     } >>= { for {       query ← meta.Sequence ⤞ meta.nucleotide   if meta.nucleotide.giNumber === _ } yield query } >>= xml  !
  4. 4.  inputMessage >>= fasta >>= {   case n: Nucleotide ⇒ ! ("media" → "nucleotide") ~> n.giNumber     case _: AminoAcid ⇒ ! fail[String](! "Expected a ‘nucleotide’ Sequence")   case _ ⇒ !     fail[String](! "Expected a ‘single’ Sequence representation”)     } >>= { for {       query ← meta.Sequence ⤞ meta.nucleotide   if meta.nucleotide.giNumber === _ } yield query } >>= xml  !
  5. 5.  inputMessage >>= fasta >>= {   case n: Nucleotide ⇒ ! ("media" → "nucleotide") ~> n.giNumber     case _: AminoAcid ⇒ ! fail[String](! "Expected a ‘nucleotide’ Sequence")   case _ ⇒ !     fail[String](! "Expected a ‘single’ Sequence representation”)     } >>= { for {       query ← meta.Sequence ⤞ meta.nucleotide   if meta.nucleotide.giNumber === _ } yield query } >>= xml  !
  6. 6.  inputMessage >>= fasta >>= {   case n: Nucleotide ⇒ ! ("media" → "nucleotide") ~> n.giNumber     case _: AminoAcid ⇒ ! fail[String](! "Expected a ‘nucleotide’ Sequence")   case _ ⇒ !     fail[String](! "Expected a ‘single’ Sequence representation”)     } >>= { for {       query ← meta.Sequence ⤞ meta.nucleotide   if meta.nucleotide.giNumber === _ } yield query } >>= xml  !
  7. 7.  inputMessage >>= fasta >>= {   case n: Nucleotide ⇒ ! ("media" → "nucleotide") ~> n.giNumber     case _: AminoAcid ⇒ ! fail[String](! "Expected a ‘nucleotide’ Sequence")   case _ ⇒ !     fail[String](! "Expected a ‘single’ Sequence representation”)     } >>= { for {       query ← meta.Sequence ⤞ meta.nucleotide   if meta.nucleotide.giNumber === _ } yield query } >>= xml  !
  8. 8. trait Message[A, H] {   val body: Validation[H, Option[A]]   val headers: H   }        trait MessageBuilder {   def build[A, H: Monoid](   body: Validation[H, Option[A]],   headers: H): Message[A, H]   }  
  9. 9. trait Message[A, H] {   val body: Validation[H, Option[A]]   val headers: H   }        trait MessageBuilder {   def build[A, H: Monoid](   body: Validation[H, Option[A]],   headers: H): Message[A, H]   }  
  10. 10. implicit def m2m[H]! (implicit builder: MessageBuilder, ! monoid: Monoid[H]) =!  new Monad[({type λ[α] = Message[α, H]})#λ] {     def pure[A](a: => A): Message[A, H] = . . .!   def bind[A, B]! (a: Message[A, H], ! f: (A) => Message[B, H]): Message[B, H] = . . .  }  
  11. 11. def pure[A](a: => A) = ! builder.build[A, H](Success(Option(a)), ∅[H])    
  12. 12. def bind[A, B](!     m: Message[A, H], ! f: (A) => Message[B, H]): Message[B, H] = {!   val mb: Message[B, H] = m.body match { case Success(Some(value)) ⇒ f(value)   case Success(None) ⇒! builder.build[B, H](! Success(Option.empty[B]), ∅[H])   case Failure(a) ⇒! builder.build[B, H](! Failure(a), ∅[H] |+| a)     }!   builder.build[B, H](mb.body, m.headers |+| mb.headers)}
  13. 13. implicit object BasicMessageBuilder extends MessageBuilder {   def build[A, H: Monoid](! body: Validation[H, Option[A]], headers: H) =   BasicMessage(body, headers)  }  !implicit object DiagnosisMessageBuilder extends! MessageBuilder {   def build[A, H: Monoid](! body: Validation[H, Option[A]], headers: H) =   DiagnosisMessage(body, headers)  }     def body[A](value: A)(implicit builder: MessageBuilder) :! Message[A, HL] =   builder.build(Success(Some(value)), List.empty[Header])      
  14. 14. import Basic._!//import Diagnosis._!//import Transactional._!!  gene map { for (! e ← meta.Chromosome ⤞ meta.Gene ! if meta.Gene.uuid === _.uuid! ) yield e  } >>= search  
  15. 15. (genex <**> geney) (_ ++ _) >>=   header("media-type", "application/vnd.x.gene+json") >>=   json        for {   m ← body(<gene xmlns="urn:model:gene:1.0">...</gene>)   gene ← xml(m)   } yield gene        gene map {   for {   e ← meta.Gene ⤞ meta.Gene.uuid   if meta.Gene.uuid === _.uuid   } yield e  } >>= search    
  16. 16. import scalaz._!import Scalaz._!// profit!!
  17. 17. trait Resource {! val name: String!} !   trait Ontology extends Resource {! val nestingOntology: Option[Ontology]! val nestedOntology: List[Ontology]! val ownedType: List[Type]! val ownedRelation: List[Relation]!}!  
  18. 18. trait GraphResource {   this: Resource =>   }     trait GraphVertex extends GraphResource {   this: Entity =>   val graphFeatures: List[PrimitiveFeature]   val master: Boolean   val rootToMasterEdge: GraphEdge with Relation   val masterToSelf: Option[GraphEdge with Relation] = None   }     trait GraphEdge extends GraphResource {   this: Relation =>   }    
  19. 19. trait RelationalResource {   this: Resource =>   }     trait NamedRelationalResource extends RelationalResource {   this: Resource =>   val relationalName: String   }       trait RelationalEntity extends NamedRelationalResource {   this: Entity =>   }     trait RelationalCompositeFeature extends RelationalResource {   this: CompositeFeature =>   val mapping: Map[String, String]   }  
  20. 20. object Chromosome extends   Entity   with RelationalEntity!    with GraphVertex   with XmlElement {   self =>     sealed trait ChromosomePart {   val ownerType = self   }     // Ontology Trait   val featuringType = self   val ownedFeature = chromatine :: Nil     // XML Trait   val namespace = "urn:domain:chromosome:1.0"   val prefix = "chr"     // Features   val chromatine =   new Chromatine(   name = "chromatine",   ownerType = Chromosome,   mapping = Map.empty[String, String])   }    
  21. 21. implicit def enrich[A <: DomainModel](model: A) = new {   def metamodel: Option[Type] = Ontology.typeOf(model)  }    def xmlFilter[A <: DomainModel] =   (model: A) ⇒ model.metamodel match {   case Some(_: XmlElement) ⇒ body(XmlModel[A](model))   case _ ⇒ fail[XmlModel[A]]! ("No XmlElement meta-model definition could be found")  }    def ingoingEdges[A <: DomainModel] =   (model: A) ⇒ model.metamodel match {   case Some(vertex: GraphVertex) ⇒ ! Ontology.edges.filter(_.target == vertex)   case _ ⇒ List.empty[GraphEdge]  }  
  22. 22. implicit def enrich[A <: DomainModel](model: A) = new {   def metamodel: Option[Type] = Ontology.typeOf(model)  }    def xmlFilter[A <: DomainModel] =   (model: A) ⇒ model.metamodel match {   case Some(_: XmlElement) ⇒ body(XmlModel[A](model))   case _ ⇒ fail[XmlModel[A]]! ("No XmlElement meta-model definition could be found")  }    def ingoingEdges[A <: DomainModel] =   (model: A) ⇒ model.metamodel match {   case Some(vertex: GraphVertex) ⇒ ! Ontology.edges.filter(_.target == vertex)   case _ ⇒ List.empty[GraphEdge]  }  
  23. 23. trait Type extends Resource   trait SimpleType extends Type   trait Entity extends Type!! trait Relation extends Type     trait Feature[+T <: Type] extends Resource   trait SimpleFeature[+T <: SimpleType] extends Feature[T]   trait PrimitiveFeature extends SimpleFeature[Primitive]   trait EnumerationFeature extends SimpleFeature[Enumeration]   trait CompositeFeature extends SimpleFeature[Composite]     trait Primitive extends SimpleType   trait Enumeration extends SimpleType   trait Composite extends SimpleType      
  24. 24. trait PrimitiveLogic {     val resource: Primitive         def ===[A](value: Primitive[A]): Operator = . . .              def in[A](values: PrimitiveList[A]): Operator = . . .  }        def find(operator: Operator): Option[T]  def list(operator: Operator): List[T]!     import PrimitiveLogic._    dao list (Locus.locusUUID in list)     dao find (Locus.locusUUID === uuid)    
  25. 25. import Logic._  val validation =   Sequence.nucleotide.accession.accessionNumber !== x    import GraphOps._  val path =   Sequence.nucleotide ⤞   Sequence.protein ⤞   Locus.typeOfGene where (_ !== y)    
  26. 26. for {! ! locus Chromosome ⤞ Gene ⤞ Locus! if Locus range!! path locus ⤞ Sequence ⤞ nucleotide! if nucleotide alignment (_ > 89)!!} yield path!
  27. 27. trait Qvt[PIM, Query, View, PSM] {     def query(pim: PIM): List[Query]     def view(query: Query): View     def transform(view: View): PSM     }  
  28. 28. class GraphSimpleQvt(   ontologyProfile: OntologyProfile,   graphProfile: GraphProfile)   extends SimpleQvt[Model, Package, GraphOntology] {     def query(pim: Model) = {   walk[Package](pim)(_.getNestedPackages).   filter(graphProfile.graphPredicate)   }     def transform(view: Package) = graph(view)     def graph(element: Package): GraphOntology = {   ...   }   .   .   .  }  
  29. 29. def walk[A]   (element: A)   (f: A => Iterable[A]): List[A] = {   val children = f(element).toList   children ++ ! (children.flatMap(walk(_, f)))  }  
  30. 30. def packageName(element: Package): String =   Stream! .iterate(element)(_.getNestingPackage)   .takeWhile(!_.isInstanceOf[Model])   .map(_.getName)   .reverse   .mkString(".") //.scala rocks !!  
  31. 31. Thank  You  

×