Skip to content

Instantly share code, notes, and snippets.

@drdozer
Created December 17, 2012 19:09
Show Gist options
  • Save drdozer/4321002 to your computer and use it in GitHub Desktop.
Save drdozer/4321002 to your computer and use it in GitHub Desktop.
package sbol.device
import java.net.URI
import sbol.util.{Annotated, Described, Identified}
/**
* A Device is the unit of functional design composition.
*
* @author Matthew Pocock
*/
sealed trait Device extends Identified with Described with Annotated {
def function: URI
def inputs: Set[InPort]
def outputs: Set[OutPort]
}
/**
* A device that presents a functional view of a DnaComponent.
*
* @author Matthew Pocock
*/
trait AtomicDevice extends Device {
def part: DNAComponent
}
trait CompositeDevice extends Device {
def subDevices: Set[Device]
def connections: Set[Connection] // connect inputs from one sub-model to outputs from another
}
sealed trait Port {
def uri: Option[URI]
def role: URI
def restriction: RDF
}
trait InPort extends Port
trait OutPort extends Port
trait Connection {
def uri: URI
def input: InPort
def output: OutPort
}
package sbol.ext
/**
* Provides operations for some type `T`.
*
* @author Matthew Pocock
*/
trait ExtOps[T] {
def views: T
// in your implementation, fill in ops
}
trait OpsProvider[Ops <: ExtOps[T], T] {
def opsFor(t: T): Ops
}
package sbol.util
import java.net.URI
/**
* Any entity that is identified by a URI. All instances of this type should use this URI as their resource when
* represented as RDF.
*
* @author Matthew Pocock
*/
trait Identified {
def uri: URI
}
/**
* An entity that is described by an identifier, name and description. This corresponds to the appropriate fields of
* Dublin Core.
*
* @author Matthew Pocock
*/
trait Described {
self: Identified =>
def displayId: String
def name: Option[String]
def description: Option[String]
}
trait Annotated {
self: Identified =>
def annotations: Set[Annotation]
}
trait Annotation {
def predicate: URI
def `object`: URI // should be URI, datatype, root of an RDF expression tree, an RDF-able object
}
package sbol.rdf
import java.net.URI
sealed trait ObjectExpr
case class SubjectExpr(subject: URI, predicates: Seq[PredicateExpr]) extends ObjectExpr
case class UntypedLiteral(value: String) extends ObjectExpr
case class PredicateExpr(predicate: URI, objects: Seq[ObjectExpr])
package sbol.regulation
import sbol.ext.{OpsProvider, ExtOps}
import sbol.device.{Port, Device}
import sbol.util.Identified
import java.net.URI
/**
* Utilities for managing regulation information.
*
* @author Matthew Pocock
*/
object Regulation extends OpsProvider[RegulationOps, Device] {
def opsFor(d: Device): RegulationOps = new RegulationOps {
def views = d
def regulations = ???
def regulations_=(rs: Set[Regulation]) {}
}
}
/**
* Regulatory information.
*
* @author Matthew Pocock
*/
trait Regulation extends Identified {
def players: Set[Player]
def `type`: URI
}
trait Player {
def port: Port
def role: URI
}
/**
* Operations for manipulating the regulation information associated with a device.
*
* @author Matthew Pocock
*/
trait RegulationOps extends ExtOps[Device] {
def regulations: Set[Regulation]
def regulations_=(rs: Set[Regulation])
}
package sbol.sequence
import sbol.util.{Described, Identified}
import java.net.URI
/**
* Created with IntelliJ IDEA.
* User: nmrp3
* Date: 03/12/12
* Time: 17:51
* To change this template use File | Settings | File Templates.
*/
trait DnaSequence extends Identified {
def nucleotides: String
}
trait DnaComponent extends Identified with Described {
def sequenceType: Set[URI]
def dnaSequence: Option[DnaSequence]
def annotations: Set[SequenceAnnotation]
}
trait SequenceAnnotation extends Identified {
def bioStart: Option[Int]
def bioEnd: Option[Int]
def strand: Strand
def preceeds: Set[SequenceAnnotation]
def subComponent: DnaComponent
}
sealed trait Strand
object Strand {
case object + extends Strand
case object - extends Strand
}
trait Collection extends Identified with Described {
def components: Set[DnaComponent]
}
package sbol.util
import java.net.URI
/**
* A resource of type `T` or a URI that identifies it.
*
* @tparam T the type of the resource
* @author Matthew Pocock
*/
sealed trait ReferenceOrInstance[T]
/**
* The case where we have a URI.
*
* @param uri the URI that identifies a resource of type `T`
* @tparam T the type of the resource
*
* @author Matthew pocock
*/
case class Reference[T](uri: URI) extends ReferenceOrInstance[T]
/**
* The case where we have an instance.
*
* @param t the resource of type `T`
* @tparam T the type of the resource
*/
case class Instance[T](t: T) extends ReferenceOrInstance[T]
/**
* Utilities for working with references and instances.
*
* @author Matthew Pocock
*/
object ReferenceOrInstance {
/** Automatically wrap up a URI as a reference.
*
* @param uri the URI to wrap
* @tparam T the type of the resource
* @return a `Reference[T]` wrapping `uri`
*/
implicit def reference[T](uri: URI): Reference[T] = Reference(uri)
/** Automatically wrap up a resource as a reference.
*
* @param t the reference to wrap
* @tparam T the type of the resource
* @return an `Instance[T]` wrapping `t`
*/
implicit def instance[T](t: T): Instance[T] = Instance(t)
/** Automatically dereference an instance to the wrapped instance.
*
* @param inst the `Instance[T]` to dereference
* @tparam T the type of the resource
* @return the wrapped instance
*/
implicit def dereference[T](inst: Instance[T]): T = inst.t
/** Automatically attempt to dereference a reference.
* The details of how URIs are resolved to objects is encapsulated by the implicit `lookup` function. In different
* contexts and at different times, a `URI` may resolve to no or different instances.
*
* @param ref the `Reference[T]` to resolve
* @param lookup an implicit lookup function
* @tparam T the type of the resource
* @return `Some[T]` containing an instance found by resolving the reference if resolution succeeded, `None`
* otherwise
*/
implicit def dereference[T](ref: Reference[T])(implicit lookup: URI => Option[T]): Option[T] = lookup(ref.uri)
/** Automatically attempt to dereference a reference or instance.
* In the case of `roi` being an instance, the wrapped instance is returned. In the case of `roi` being a reference,
* the implicit `lookup` function is used to attempt to resolve the reference.
*
* @param roi the `ReferenceOrInstance[T]` to resolve
* @param lookup an implicit lookup function
* @tparam T the type of the resource
* @return `Some[T]` containing an instance if dereferencing succeeded, `None` otherwise
*/
implicit def dereference[T](roi: ReferenceOrInstance[T])(implicit lookup: URI => T): Option[T] = roi match {
case inst : Instance[T] => Some(dereference(inst))
case ref : Reference[T] => dereference(ref)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment