Skip to content

Commit

Permalink
Change serialization of output
Browse files Browse the repository at this point in the history
  • Loading branch information
julianmendez committed Oct 28, 2024
1 parent 4463315 commit a7cb6b8
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Main

yaml_parser = YamlParser .mk

yaml_serializer = YamlSerializer .mk

operation_parser = OperationParser .mk

operation_processor = OperationProcessor .mk
Expand All @@ -26,17 +28,9 @@ class Main
)
)

_serialize_market (m : Market) : String =
"Account balances:\n" +
(m .accounts .map (lambda account --> account .toString) .mkString (", ") ) + "\n\n" +
"Items:\n" +
(m .items .map (lambda item -->
"(" + item .owner .toString + ", " + item .price .toString + ")" ) .mkString (" , ") ) +
"\n\n"

serialize_market (maybe_market : Option [Market] ) : String =
match maybe_market
case Some (market) ==> _serialize_market (market)
case Some (market) ==> yaml_serializer .serialize_market (market)
case None ==> "Undefined market"

execute (arguments : List [String] ) : Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import soda.se.umu.cs.soda.prototype.example.market.core.Money
import soda.se.umu.cs.soda.prototype.example.market.core.OperationProcessor
import soda.se.umu.cs.soda.prototype.example.market.parser.OperationParser
import soda.se.umu.cs.soda.prototype.example.market.parser.YamlParser
import soda.se.umu.cs.soda.prototype.example.market.serializer.YamlSerializer

/**
* This is the main entry point.
Expand All @@ -31,6 +32,8 @@ trait Main

lazy val yaml_parser = YamlParser .mk

lazy val yaml_serializer = YamlSerializer .mk

lazy val operation_parser = OperationParser .mk

lazy val operation_processor = OperationProcessor .mk
Expand All @@ -43,17 +46,9 @@ trait Main
)
)

private def _serialize_market (m : Market) : String =
"Account balances:\n" +
(m .accounts .map ( account => account .toString) .mkString (", ") ) + "\n\n" +
"Items:\n" +
(m .items .map ( item =>
"(" + item .owner .toString + ", " + item .price .toString + ")" ) .mkString (" , ") ) +
"\n\n"

def serialize_market (maybe_market : Option [Market] ) : String =
maybe_market match {
case Some (market) => _serialize_market (market)
case Some (market) => yaml_serializer .serialize_market (market)
case None => "Undefined market"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ import
soda.se.umu.cs.soda.prototype.example.market.core.OperationProcessor
soda.se.umu.cs.soda.prototype.example.market.parser.OperationParser
soda.se.umu.cs.soda.prototype.example.market.parser.YamlParser
soda.se.umu.cs.soda.prototype.example.market.serializer.YamlSerializer
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

class MarketSerializer

abstract

serialize_accounts (accounts : List [Money] ) : List [Operation] =
accounts
.zipWithIndex
.map (lambda pair --> OpDeposit .mk (pair ._2) (pair ._1) )

serialize_items (items : List [Item] ) : List [Operation] =
items
.zipWithIndex
.flatMap (lambda pair -->
List [Operation] (
OpAssign .mk (pair ._2) (pair ._1 .owner) ,
OpPrice .mk (pair ._2) (pair ._1 .price)
)
)

serialize (m : Market) : List [Operation] =
serialize_accounts (m .accounts) .++ (
serialize_items (m .items)
)

end

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package soda.se.umu.cs.soda.prototype.example.market.serializer

import soda.se.umu.cs.soda.prototype.example.market.core.Operation
import soda.se.umu.cs.soda.prototype.example.market.core.OperationEnum
import soda.se.umu.cs.soda.prototype.example.market.core.Item
import soda.se.umu.cs.soda.prototype.example.market.core.Market
import soda.se.umu.cs.soda.prototype.example.market.core.Money
import soda.se.umu.cs.soda.prototype.example.market.core.OpAssign
import soda.se.umu.cs.soda.prototype.example.market.core.OpAssign_
import soda.se.umu.cs.soda.prototype.example.market.core.OpDeposit
Expand All @@ -12,9 +13,47 @@ import soda.se.umu.cs.soda.prototype.example.market.core.OpSell
import soda.se.umu.cs.soda.prototype.example.market.core.OpSell_
import soda.se.umu.cs.soda.prototype.example.market.core.OpUndefined
import soda.se.umu.cs.soda.prototype.example.market.core.OpUndefined_
import soda.se.umu.cs.soda.prototype.example.market.core.Operation
import soda.se.umu.cs.soda.prototype.example.market.core.OperationEnum
import soda.se.umu.cs.soda.prototype.example.market.core.OperationProcessor





trait MarketSerializer
{



def serialize_accounts (accounts : List [Money] ) : List [Operation] =
accounts
.zipWithIndex
.map ( pair => OpDeposit .mk (pair ._2) (pair ._1) )

def serialize_items (items : List [Item] ) : List [Operation] =
items
.zipWithIndex
.flatMap ( pair =>
List [Operation] (
OpAssign .mk (pair ._2) (pair ._1 .owner) ,
OpPrice .mk (pair ._2) (pair ._1 .price)
)
)

def serialize (m : Market) : List [Operation] =
serialize_accounts (m .accounts) .++ (
serialize_items (m .items)
)

}

case class MarketSerializer_ () extends MarketSerializer

object MarketSerializer {
def mk : MarketSerializer =
MarketSerializer_ ()
}


trait OperationSerializer
Expand Down Expand Up @@ -50,3 +89,36 @@ object OperationSerializer {
OperationSerializer_ ()
}


trait YamlSerializer
{



lazy val market_serializer = MarketSerializer .mk

lazy val operation_serializer = OperationSerializer .mk

def serialize_operations (operations : List [Operation] ) : String =
"operations:" +
"\n- " +
operations
.map ( operation => operation_serializer .serialize (operation) )
.mkString ("\n- ") +
"\n\n"

def serialize_market (m : Market) : String =
"---\n" +
serialize_operations (
market_serializer .serialize (m)
)

}

case class YamlSerializer_ () extends YamlSerializer

object YamlSerializer {
def mk : YamlSerializer =
YamlSerializer_ ()
}

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package soda.se.umu.cs.soda.prototype.example.market.serializer

import
soda.se.umu.cs.soda.prototype.example.market.core.Operation
soda.se.umu.cs.soda.prototype.example.market.core.OperationEnum
soda.se.umu.cs.soda.prototype.example.market.core.Item
soda.se.umu.cs.soda.prototype.example.market.core.Market
soda.se.umu.cs.soda.prototype.example.market.core.Money
soda.se.umu.cs.soda.prototype.example.market.core.OpAssign
soda.se.umu.cs.soda.prototype.example.market.core.OpAssign_
soda.se.umu.cs.soda.prototype.example.market.core.OpDeposit
Expand All @@ -13,4 +14,7 @@ import
soda.se.umu.cs.soda.prototype.example.market.core.OpSell_
soda.se.umu.cs.soda.prototype.example.market.core.OpUndefined
soda.se.umu.cs.soda.prototype.example.market.core.OpUndefined_
soda.se.umu.cs.soda.prototype.example.market.core.Operation
soda.se.umu.cs.soda.prototype.example.market.core.OperationEnum
soda.se.umu.cs.soda.prototype.example.market.core.OperationProcessor

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

class YamlSerializer

abstract

market_serializer = MarketSerializer .mk

operation_serializer = OperationSerializer .mk

serialize_operations (operations : List [Operation] ) : String =
"operations:" +
"\n- " +
operations
.map (lambda operation --> operation_serializer .serialize (operation) )
.mkString ("\n- ") +
"\n\n"

serialize_market (m : Market) : String =
"---\n" +
serialize_operations (
market_serializer .serialize (m)
)

end

58 changes: 29 additions & 29 deletions core/src/test/resources/example/example0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,64 +39,64 @@ operations:
- deposit 24 1750
- deposit 25 2350
- assign 0 12
- assign 1 7
- assign 2 20
- assign 3 15
- assign 4 8
- assign 5 23
- assign 6 14
- assign 7 2
- assign 8 19
- assign 9 5
- assign 10 22
- assign 11 13
- assign 12 4
- assign 13 18
- assign 14 9
- assign 15 24
- assign 16 11
- assign 17 3
- assign 18 17
- assign 19 10
- assign 20 21
- assign 21 16
- assign 22 1
- assign 23 25
- assign 24 6
- assign 25 0
- assign 26 15
- assign 27 10
- assign 28 20
- assign 29 5
- price 0 40
- assign 1 7
- price 1 50
- assign 2 20
- price 2 25
- assign 3 15
- price 3 70
- assign 4 8
- price 4 100
- assign 5 23
- price 5 60
- assign 6 14
- price 6 30
- assign 7 2
- price 7 40
- assign 8 19
- price 8 25
- assign 9 5
- price 9 20
- assign 10 22
- price 10 90
- assign 11 13
- price 11 50
- assign 12 4
- price 12 100
- assign 13 18
- price 13 150
- assign 14 9
- price 14 30
- assign 15 24
- price 15 30
- assign 16 11
- price 16 25
- assign 17 3
- price 17 200
- assign 18 17
- price 18 20
- assign 19 10
- price 19 30
- assign 20 21
- price 20 20
- assign 21 16
- price 21 100
- assign 22 1
- price 22 100
- assign 23 25
- price 23 20
- assign 24 6
- price 24 30
- assign 25 0
- price 25 20
- assign 26 15
- price 26 25
- assign 27 10
- price 27 40
- assign 28 20
- price 28 80
- assign 29 5
- price 29 100
- sell 10 25

Loading

0 comments on commit a7cb6b8

Please sign in to comment.