import { When } from "@cucumber/cucumber" ; import DataTable from "@cucumber/cucumber/lib/models/data_table" ; import { AttributeType , Relation } from "../../../../../dist" ; import { parseVar } from "../../../config/Parameters" ; import { tx } from "../../../connection/ConnectionStepsBase" ; import { assertThrows } from "../../../util/Util" ; import { get , put } from "../ThingSteps" ; import assert = require ( "assert" ) ; When ( "{var} = relation\\({type_label}) create new instance" , async ( var0 , typeLabel ) => { put ( var0 , await ( await tx ( ) . concepts . getRelationType ( typeLabel ) ) . asRemote ( tx ( ) ) . create ( ) ) ; } ) ; When ( "relation\\({type_label}) create new instance; throws exception" , async ( typeLabel ) => { await assertThrows ( async ( ) => await ( await tx ( ) . concepts . getRelationType ( typeLabel ) ) . asRemote ( tx ( ) ) . create ( ) ) ; } ) ; When ( "{var} = relation\\({type_label}) create new instance with key\\({type_label}): {bool}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asBoolean ( ) ) . asRemote ( tx ( ) ) . put ( value ) ; const relation = await ( await tx ( ) . concepts . getRelationType ( thingTypeLabel ) ) . asRemote ( tx ( ) ) . create ( ) ; await relation . asRemote ( tx ( ) ) . setHas ( key ) ; put ( thingName , relation ) ; } ) ; When ( "{var} = relation\\({type_label}) create new instance with key\\({type_label}): {int}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asLong ( ) ) . asRemote ( tx ( ) ) . put ( value ) ; const relation = await ( await tx ( ) . concepts . getRelationType ( thingTypeLabel ) ) . asRemote ( tx ( ) ) . create ( ) ; await relation . asRemote ( tx ( ) ) . setHas ( key ) ; put ( thingName , relation ) ; } ) ; When ( "{var} = relation\\({type_label}) create new instance with key\\({type_label}): {word}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asString ( ) ) . asRemote ( tx ( ) ) . put ( value ) ; const relation = await ( await tx ( ) . concepts . getRelationType ( thingTypeLabel ) ) . asRemote ( tx ( ) ) . create ( ) ; await relation . asRemote ( tx ( ) ) . setHas ( key ) ; put ( thingName , relation ) ; } ) ; When ( "{var} = relation\\({type_label}) create new instance with key\\({type_label}): {datetime}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asDateTime ( ) ) . asRemote ( tx ( ) ) . put ( value ) ; const relation = await ( await tx ( ) . concepts . getRelationType ( thingTypeLabel ) ) . asRemote ( tx ( ) ) . create ( ) ; await relation . asRemote ( tx ( ) ) . setHas ( key ) ; put ( thingName , relation ) ; } ) ; When ( "{var} = relation\\({type_label}) get instance with key\\({type_label}): {bool}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asBoolean ( ) ) . asRemote ( tx ( ) ) . get ( value ) ; for await ( const owner of key . asRemote ( tx ( ) ) . getOwners ( ) ) { if ( owner . type . label . scopedName === thingTypeLabel ) { put ( thingName , owner ) ; return ; } } put ( thingName , null ) ; } ) ; When ( "{var} = relation\\({type_label}) get instance with key\\({type_label}): {int}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asLong ( ) ) . asRemote ( tx ( ) ) . get ( value ) ; for await ( const owner of key . asRemote ( tx ( ) ) . getOwners ( ) ) { if ( owner . type . label . scopedName === thingTypeLabel ) { put ( thingName , owner ) ; return ; } } put ( thingName , null ) ; } ) ; When ( "{var} = relation\\({type_label}) get instance with key\\({type_label}): {word}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asString ( ) ) . asRemote ( tx ( ) ) . get ( value ) ; for await ( const owner of key . asRemote ( tx ( ) ) . getOwners ( ) ) { if ( owner . type . label . scopedName === thingTypeLabel ) { put ( thingName , owner ) ; return ; } } put ( thingName , null ) ; } ) ; When ( "{var} = relation\\({type_label}) get instance with key\\({type_label}): {datetime}" , async ( thingName , thingTypeLabel , keyTypeLabel , value ) => { const key = await ( ( await tx ( ) . concepts . getAttributeType ( keyTypeLabel ) ) . asDateTime ( ) ) . asRemote ( tx ( ) ) . get ( value ) ; for await ( const owner of key . asRemote ( tx ( ) ) . getOwners ( ) ) { if ( ( await owner . asRemote ( tx ( ) ) . type ) . equals ( await tx ( ) . concepts . getRelationType ( thingTypeLabel ) ) ) { put ( thingName , owner ) ; return ; } } put ( thingName , null ) ; } ) ; When ( "relation\\({type_label}) get instances contain: {var}" , async ( typeLabel , variableName ) => { for await ( const instance of ( await tx ( ) . concepts . getRelationType ( typeLabel ) ) . asRemote ( tx ( ) ) . getInstances ( ) ) { if ( instance . equals ( get ( variableName ) ) ) return ; } assert . fail ( ) ; } ) ; When ( "relation\\({type_label}) get instances do not contain: {var}" , async ( typeLabel , variableName ) => { for await ( const instance of ( await tx ( ) . concepts . getRelationType ( typeLabel ) ) . asRemote ( tx ( ) ) . getInstances ( ) ) { if ( instance . equals ( get ( variableName ) ) ) assert . fail ( ) ; } } ) ; When ( "relation\\({type_label}) get instances is empty" , async ( typeLabel ) => { assert . strictEqual ( ( await ( await tx ( ) . concepts . getRelationType ( typeLabel ) ) . asRemote ( tx ( ) ) . getInstances ( ) . collect ( ) ) . length , 0 ) ; } ) ; When ( "relation {var} add player for role\\({type_label}): {var}" , async ( relationName , typeLabel , playerName ) => { const relation = get ( relationName ) . asRelation ( ) ; const roleType = await relation . type . asRemote ( tx ( ) ) . getRelates ( typeLabel ) ; const player = get ( playerName ) ; await relation . asRemote ( tx ( ) ) . addPlayer ( roleType , player ) ; } ) ; When ( "relation {var} add player for role\\({type_label}): {var}; throws exception" , async ( relationName , typeLabel , playerName ) => { await assertThrows ( async ( ) => { const relation = get ( relationName ) . asRelation ( ) ; const roleType = await relation . type . asRemote ( tx ( ) ) . getRelates ( typeLabel ) ; const player = get ( playerName ) ; await relation . asRemote ( tx ( ) ) . addPlayer ( roleType , player ) ; } ) ; } ) ; When ( "relation {var} remove player for role\\({type_label}): {var}" , async ( relationName , typeLabel , playerName ) => { const relation = get ( relationName ) . asRelation ( ) ; const roleType = await relation . type . asRemote ( tx ( ) ) . getRelates ( typeLabel ) ; const player = get ( playerName ) ; await relation . asRemote ( tx ( ) ) . removePlayer ( roleType , player ) ; } ) ; When ( "relation {var} get players contain:" , async ( var1 , players ) => { const relation = get ( var1 ) . asRelation ( ) ; const playersByRoleType = await relation . asRemote ( tx ( ) ) . getPlayersByRoleType ( ) ; for ( const [ roleLabel , var2Raw ] of players . raw ( ) ) { const var2 = parseVar ( var2Raw ) ; const roleType = Array . from ( playersByRoleType . keys ( ) ) . find ( x => x . label . name == roleLabel ) ; assert ( roleType ) ; assert ( playersByRoleType . get ( roleType ) . some ( x => x . equals ( get ( var2 ) ) ) ) ; } } ) ; When ( "relation {var} get players do not contain:" , async ( var1 , players ) => { const relation = get ( var1 ) . asRelation ( ) ; const playersByRoleType = await relation . asRemote ( tx ( ) ) . getPlayersByRoleType ( ) ; for ( const [ roleLabel , var2Raw ] of players . raw ( ) ) { const var2 = parseVar ( var2Raw ) ; const roleType = Array . from ( playersByRoleType . keys ( ) ) . find ( x => x . label . scopedName == roleLabel ) ; assert ( ! playersByRoleType . get ( roleType ) ?. some ( x => x . equals ( get ( var2 ) ) ) ) ; } } ) ; When ( "relation {var} get players contain: {var}" , async ( var1 , var2 ) => { assert ( await get ( var1 ) . asRelation ( ) . asRemote ( tx ( ) ) . getPlayers ( ) . some ( x => x . equals ( get ( var2 ) ) ) ) ; } ) ; When ( "relation {var} get players do not contain: {var}" , async ( var1 , var2 ) => { assert ( ! ( await get ( var1 ) . asRelation ( ) . asRemote ( tx ( ) ) . getPlayers ( ) . some ( x => x . equals ( get ( var2 ) ) ) ) ) ; } ) ; When ( "relation {var} get players for role\\({type_label}) contain: {var}" , async ( var1 , roleLabel , var2 ) => { const roleType = await get ( var1 ) . asRelation ( ) . type . asRemote ( tx ( ) ) . getRelates ( roleLabel ) ; assert ( await get ( var1 ) . asRelation ( ) . asRemote ( tx ( ) ) . getPlayers ( [ roleType ] ) . some ( x => x . equals ( get ( var2 ) ) ) ) ; } ) ; When ( "relation {var} get players for role\\({type_label}) do not contain: {var}" , async ( var1 , roleLabel , var2 ) => { const roleType = await get ( var1 ) . asRelation ( ) . type . asRemote ( tx ( ) ) . getRelates ( roleLabel ) ; assert ( ! ( await get ( var1 ) . asRelation ( ) . asRemote ( tx ( ) ) . getPlayers ( [ roleType ] ) . some ( x => x . equals ( get ( var2 ) ) ) ) ) ; } ) ;