From 14af2e49ffbd50e115db5f3b4f5075395e58827a Mon Sep 17 00:00:00 2001 From: balberski Date: Sat, 9 Aug 2014 18:16:11 +0200 Subject: [PATCH] [#18] AnyVertexTypeWithId --- .../bio4j/dynamograph/AnyDynamoVertex.scala | 2 +- .../dynamograph/AnyVertexTypeWitId.scala | 22 ++++++++++++++++++ .../dynamograph/model/GeneralSchema.scala | 5 ++++ .../model/go/GoImplementation.scala | 2 +- .../bio4j/dynamograph/model/go/GoSchema.scala | 7 +++--- .../dynamograph/model/vertexTables.scala | 23 +++++++++++-------- .../bio4j/dynamograph/reader/GoReaders.scala | 2 +- .../dynamograph/reader/VertexReader.scala | 2 +- .../dynamograph/writer/VertexWriter.scala | 7 +++++- .../dynamograph/writer/EdgeWriterTest.scala | 16 ------------- 10 files changed, 54 insertions(+), 34 deletions(-) create mode 100644 src/main/scala/com/bio4j/dynamograph/AnyVertexTypeWitId.scala diff --git a/src/main/scala/com/bio4j/dynamograph/AnyDynamoVertex.scala b/src/main/scala/com/bio4j/dynamograph/AnyDynamoVertex.scala index b9663ed..901cd92 100644 --- a/src/main/scala/com/bio4j/dynamograph/AnyDynamoVertex.scala +++ b/src/main/scala/com/bio4j/dynamograph/AnyDynamoVertex.scala @@ -13,7 +13,7 @@ trait AnyDynamoVertex extends AnySealedVertex { dynamoVertex => // TODO move this to have a VertexTable and use that - type Tpe <: Singleton with AnySealedVertexType + type Tpe <: Singleton with AnyVertexTypeWithId val dao: AnyDynamoDbDao = ServiceProvider.dao diff --git a/src/main/scala/com/bio4j/dynamograph/AnyVertexTypeWitId.scala b/src/main/scala/com/bio4j/dynamograph/AnyVertexTypeWitId.scala new file mode 100644 index 0000000..19a5619 --- /dev/null +++ b/src/main/scala/com/bio4j/dynamograph/AnyVertexTypeWitId.scala @@ -0,0 +1,22 @@ +package com.bio4j.dynamograph + +import ohnosequences.typesets._ +import ohnosequences.scarph.AnySealedVertexType + + +trait AnyVertexTypeWithId extends AnySealedVertexType{ + type Id <: Singleton with AnyProperty.ofValue[String] + val id : Id + val containId : Id ∈ Record#Properties +} + +abstract class VertexTypeWithId[P <: Singleton with AnyProperty.ofValue[String],R <: AnyRecord]( + val id: P, + val label: String, + val record: R) +( + implicit containId : P ∈ R#Properties +) extends AnyVertexTypeWithId{ + type Record = R + type Id = P +} \ No newline at end of file diff --git a/src/main/scala/com/bio4j/dynamograph/model/GeneralSchema.scala b/src/main/scala/com/bio4j/dynamograph/model/GeneralSchema.scala index 734a58f..5ec9590 100644 --- a/src/main/scala/com/bio4j/dynamograph/model/GeneralSchema.scala +++ b/src/main/scala/com/bio4j/dynamograph/model/GeneralSchema.scala @@ -1,6 +1,7 @@ package com.bio4j.dynamograph.model import ohnosequences.typesets._ +import ohnosequences.scarph._ object GeneralSchema { @@ -14,3 +15,7 @@ object GeneralSchema { case object comment extends Property[String] } + + + + diff --git a/src/main/scala/com/bio4j/dynamograph/model/go/GoImplementation.scala b/src/main/scala/com/bio4j/dynamograph/model/go/GoImplementation.scala index ada7b7a..5c57364 100644 --- a/src/main/scala/com/bio4j/dynamograph/model/go/GoImplementation.scala +++ b/src/main/scala/com/bio4j/dynamograph/model/go/GoImplementation.scala @@ -1,6 +1,6 @@ package com.bio4j.dynamograph.model.go -import com.bio4j.dynamograph.{DynamoEdge, DynamoVertex} +import com.bio4j.dynamograph._ import com.bio4j.dynamograph.model.go.GoSchema._ diff --git a/src/main/scala/com/bio4j/dynamograph/model/go/GoSchema.scala b/src/main/scala/com/bio4j/dynamograph/model/go/GoSchema.scala index 8d91fa5..993e7c8 100644 --- a/src/main/scala/com/bio4j/dynamograph/model/go/GoSchema.scala +++ b/src/main/scala/com/bio4j/dynamograph/model/go/GoSchema.scala @@ -2,19 +2,20 @@ package com.bio4j.dynamograph.model.go import ohnosequences.scarph._ import com.bio4j.dynamograph.model.GeneralSchema._ +import com.bio4j.dynamograph.VertexTypeWithId import ohnosequences.typesets._ import ohnosequences.typesets.Represented._ object GoSchema { - val goTermAttributes = id :~: name :~: comment :~: definition :~: ∅ + val goTermAttributes = id :~: name :~: comment :~: definition :~: ∅ case object GoTermRecord extends Record(goTermAttributes) - object GoTermType extends SealedVertexType("GoTerm", GoTermRecord) + object GoTermType extends VertexTypeWithId(id, "GoTerm", GoTermRecord) implicit val GoTermType_properties = GoTermType has goTermAttributes val goNamespacesAttributes = id :~: ∅ case object GoNamespacesRecord extends Record(goNamespacesAttributes) - object GoNamespacesType extends SealedVertexType("GoNamespace", GoNamespacesRecord) + object GoNamespacesType extends VertexTypeWithId(id, "GoNamespace", GoNamespacesRecord) implicit val GoNamespacesType_properties = GoNamespacesType has goNamespacesAttributes diff --git a/src/main/scala/com/bio4j/dynamograph/model/vertexTables.scala b/src/main/scala/com/bio4j/dynamograph/model/vertexTables.scala index 2820bd5..39f76cf 100644 --- a/src/main/scala/com/bio4j/dynamograph/model/vertexTables.scala +++ b/src/main/scala/com/bio4j/dynamograph/model/vertexTables.scala @@ -4,17 +4,16 @@ import ohnosequences.scarph._ import ohnosequences.typesets._ import ohnosequences.tabula._, impl.ImplicitConversions._, toSDKRep._, fromSDKRep._ import shapeless._ -import com.bio4j.dynamograph.{AnyDynamoVertex, AnyDynamoEdge} -import com.bio4j.dynamograph.model.GeneralSchema._ +import com.bio4j.dynamograph._ /* This type creates a link between a sealed vertex type and a DynamoDB table */ trait AnyVertexTable { vertexTable => - type VertexType <: Singleton with AnySealedVertexType + type VertexType <: Singleton with AnyVertexTypeWithId val vertexType: VertexType - + type Region <: AnyRegion val region: Region @@ -26,11 +25,13 @@ trait AnyVertexTable { vertexTable => type Record = vertexType.Record val record: Record = vertexType.record - type VertexId <: Singleton with AnyProperty.ofValue[String] - val vertexId: VertexId + type VertexId = vertexType.Id + val vertexId: VertexId = vertexType.id // provided implicitly at construction val recordValuesAreOK: everyElementOf[VertexType#Record#Values]#isOneOf[ValidValues] + type ContainId = VertexId ∈ Record#Properties + val containId: ContainId type VertexItem <: Singleton with AnyItem with AnyItem.ofTable[vertexTable.Table] with @@ -41,10 +42,10 @@ trait AnyVertexTable { vertexTable => object AnyVertexTable { - type withVertexType[V <: Singleton with AnySealedVertexType] = AnyVertexTable { type VertexType = V } + type withVertexType[V <: Singleton with AnyVertexTypeWithId] = AnyVertexTable { type VertexType = V } } -class VertexTable[VT <: Singleton with AnySealedVertexType, R <: AnyRegion]( +class VertexTable[VT <: Singleton with AnyVertexTypeWithId, R <: AnyRegion]( val vertexType : VT, val tableName : String, val region: R @@ -57,8 +58,10 @@ extends AnyVertexTable { type VertexType = VT type Region = R - type VertexId = id.type - val vertexId = id + type VertexId = VertexType#Id + val vertexId = vertexType.id + + val containId = vertexType.containId type Record = VertexType#Record val record = vertexType.record diff --git a/src/main/scala/com/bio4j/dynamograph/reader/GoReaders.scala b/src/main/scala/com/bio4j/dynamograph/reader/GoReaders.scala index 0614124..510217d 100644 --- a/src/main/scala/com/bio4j/dynamograph/reader/GoReaders.scala +++ b/src/main/scala/com/bio4j/dynamograph/reader/GoReaders.scala @@ -20,7 +20,7 @@ object GoReaders { case object namespaceEdgeReader extends EdgeReader(NamespaceTables, ServiceProvider.dynamoDbExecutor) def reader[VT <: Singleton with AnySealedVertexType](vertex : VT) : AnyVertexReader.withVertexType[VT] = vertexReaders(vertex).asInstanceOf[AnyVertexReader.withVertexType[VT]] - + val vertexReaders = Map[AnySealedVertexType, AnyVertexReader] ( GoTermType -> goTermVertexReader, GoNamespacesType -> goNamespaceVertexReader diff --git a/src/main/scala/com/bio4j/dynamograph/reader/VertexReader.scala b/src/main/scala/com/bio4j/dynamograph/reader/VertexReader.scala index f4141f5..c2cb03d 100644 --- a/src/main/scala/com/bio4j/dynamograph/reader/VertexReader.scala +++ b/src/main/scala/com/bio4j/dynamograph/reader/VertexReader.scala @@ -26,7 +26,7 @@ trait AnyVertexReader { vertexReader => val record : Record = vertexTable.record import ServiceProvider.executors._ - def read(identifier : vertexTable.VertexId#Value) : Either[String,record.Rep] = { + def read(identifier : vertexTable.VertexId#Value)(implicit from : ToItem[SDKRep, Item]) : Either[String,record.Rep] = { val getResult = ServiceProvider.service please (FromHashKeyTable(table, Active ( table, ServiceProvider.service.account, diff --git a/src/main/scala/com/bio4j/dynamograph/writer/VertexWriter.scala b/src/main/scala/com/bio4j/dynamograph/writer/VertexWriter.scala index 49682f7..3cfa7f6 100644 --- a/src/main/scala/com/bio4j/dynamograph/writer/VertexWriter.scala +++ b/src/main/scala/com/bio4j/dynamograph/writer/VertexWriter.scala @@ -26,9 +26,14 @@ trait AnyVertexWriter { vertexWriter => type Record = vertexTable.Record val record = vertexTable.record + + type VertexId = vertexTable.VertexId + val vertexId = vertexTable.vertexId + + implicit val containId : VertexId ∈ Record#Properties // write an item - def write(vertexItemValue: TaggedWith[Item])(implicit transf: From.Item[Item, SDKRep]): List[AnyPutItemAction] = { + def write(vertexItemValue: TaggedWith[Item])(implicit transf: From.Item[Item, SDKRep], containId : VertexId ∈ Record#Properties): List[AnyPutItemAction] = { // fails to compile, and it is ok because we need to extract the id from the sealed vertex type val action = InHashKeyTable ( table, diff --git a/src/test/scala/com/bio4j/dynamograph/writer/EdgeWriterTest.scala b/src/test/scala/com/bio4j/dynamograph/writer/EdgeWriterTest.scala index 4d0ca88..9a459ef 100644 --- a/src/test/scala/com/bio4j/dynamograph/writer/EdgeWriterTest.scala +++ b/src/test/scala/com/bio4j/dynamograph/writer/EdgeWriterTest.scala @@ -25,22 +25,6 @@ class EdgeWriterTest extends Specification { result.map(x => x.table.name) must containTheSameElementsAs(tableNames) } - -// "throw exception if relationId is not provided" in new context { -// val incorrectRep = testEdge ->> Map(sourceId.label -> new AttributeValue().withS("sourceId"), targetId.label -> new AttributeValue().withS("targetId")) -// writer.write(incorrectRep) must throwA[NoSuchElementException] -// } -// -// "throw exception if source is not provided" in new context { -// val incorrectRep = testEdge ->> Map(relationId.label -> new AttributeValue().withS("relationId"), targetId.label -> new AttributeValue().withS("targetId")) -// writer.write(incorrectRep) must throwA[ NoSuchElementException] -// } -// -// "throw exception if target is not provided" in new context { -// val incorrectRep = testEdge ->> Map(relationId.label -> new AttributeValue().withS("relationId"), sourceId.label -> new AttributeValue().withS("sourceId")) -// writer.write(incorrectRep) must throwA[NoSuchElementException] -// } - } trait context extends Scope {