DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Rodolfo has posted 3 posts at DZone. View Full User Profile

A Work In Progress With Scala And Streamlined Object Modeling

08.22.2008
| 3655 views |
  • submit to reddit
        // this is just a brainstorm, but feel free to hate it ;)
// the motivation is to write rich domain objects 
// instead of DDD style, I am using SOM style
// the best reference is: 
// http://www.streamlinedmodeling.com/papers/appendixB.pdf 


package org.sompoc

// Streamlined Object Modeling proof of concept (a work in progress) 
package som {

    // collaborations side types 
    sealed case class CollaborationSide
    case class Actor extends CollaborationSide
    case class Role extends CollaborationSide   
    case class Item extends CollaborationSide
    case class SpecificItem extends CollaborationSide
    case class CompositeTransaction extends CollaborationSide
    case class LineItem extends CollaborationSide
    case class OuterPlace extends CollaborationSide
    case class Place extends CollaborationSide
    case class Assembly extends CollaborationSide
    case class Part extends CollaborationSide
    case class Container extends CollaborationSide
    case class Content extends CollaborationSide
    case class Group extends CollaborationSide
    case class Member extends CollaborationSide
    case class Transaction extends CollaborationSide
    case class FollowUpTransaction extends CollaborationSide

    // collaboration rules 
    sealed case class CollaborationRule
    case class TypeRule extends CollaborationRule
    case class MultiplicityRule extends CollaborationRule
    case class PropertyRule extends CollaborationRule
    case class StateRule extends CollaborationRule
    case class ConflictRule extends CollaborationRule

    import scala.collection.mutable.HashMap

    abstract class SomCollaboration {
       
       type CS1 <: CollaborationSide  
       type CS2 <: CollaborationSide
       
       type E1   // the user types //
       type E2
       
       def doAction = println("do something here !")
       def ignoreAction = println("ignore it, please..")

       def getRulesFor(collabSide: AnyRef): Map[AnyRef, Unit]

    }

    abstract case class ActorRoleCollaboration extends SomCollaboration {

	    type CS1 = Actor
	    type CS2 = Role

	    def getRulesFor(collabSide: AnyRef): Map[AnyRef, Unit] = {
	        collabSide match {
	           case Actor => return Map[AnyRef, Unit](TypeRule -> ignoreAction,
	                                  MultiplicityRule -> ignoreAction,
	                                  PropertyRule -> ignoreAction,
	                                  StateRule-> ignoreAction,
	                                  ConflictRule-> ignoreAction)
	           case Role => return Map[AnyRef, Unit](TypeRule -> doAction,
	                                 MultiplicityRule -> doAction,
	                                 PropertyRule -> doAction,
	                                 StateRule-> doAction,
	                                 ConflictRule-> doAction)
	        }
	    }
    }

}

package domain {

    trait PersonProfile {
      def birthday = "aniversario"
    }

    case class Person extends PersonProfile {
          def thinkAbout() = println("I am thinking...")
        }

    trait WorkerProfile  {
      def salary = 80
          def name = "Myname"
    }
       
    case class Worker extends WorkerProfile {
      def work() = println("I am working..")
    }

    // the collaborations between Person and Worker 
    class PersonWorkerCollaboration extends som.ActorRoleCollaboration {
	    type E1 = domain.Person
	    type E2 = domain.Worker
    }
    
}

object myPoc1 {
  def main(args : Array[String]) : Unit = {
 
    import som._
    import domain._
   
    val p = new ActorRoleCollaboration{type E1 = domain.Person ; type E2 = domain.Worker} ;

    println("behavior for Actor") 
    println (p.getRulesFor(som.Actor))
    
    println("behavior for Role")
    println (p.getRulesFor(som.Role))
   
    // yeap, so far this is useless 
    // currently looking at
    //           Qi4J - generic mixins
    //           Lift - the domain class and its respective meta singleton
    //           Specs - to test generic mixins behaviour       
    
    // container.set(Person, Worker).as(Actor, Role)
    
  }
}