You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB
Python

from cozo import *
if __name__ == '__main__':
db = CozoDb('_test', destroy_on_exit=True)
res = db.tx_attr([
PutAttr('person.idd', Typing.string, index=Indexing.identity),
PutAttr('person.first_name', Typing.string, index=Indexing.indexed),
PutAttr('person.last_name', Typing.string, index=Indexing.indexed),
PutAttr('person.age', Typing.int, index=Indexing.indexed),
PutAttr('person.friend', Typing.ref, cardinality=Cardinality.many, index=Indexing.indexed),
PutAttr('person.weight', Typing.float, index=Indexing.indexed),
PutAttr('person.covid', Typing.bool),
])
print(res)
print(db.tx_attr([
PutAttr('person.id', Typing.string, id=res['results'][0][0], cardinality=Cardinality.one,
index=Indexing.identity),
RetractAttr('person.covid', Typing.bool, id=res['results'][-1][0], cardinality=Cardinality.one, history=False,
index=Indexing.indexed),
]))
print(db.tx([
Put({'_temp_id': 'alice',
'person.first_name': 'Alice',
'person.age': 7,
'person.last_name': 'Amorist',
'person.id': 'alice_amorist',
'person.weight': 25,
'person.friend': 'eve'}),
Put({'_temp_id': 'bob',
'person.first_name': 'Bob',
'person.age': 70,
'person.last_name': 'Wonderland',
'person.id': 'bob_wonderland',
'person.weight': 100,
'person.friend': 'alice'
}),
Put({'_temp_id': 'eve',
'person.first_name': 'Eve',
'person.age': 18,
'person.last_name': 'Faking',
'person.id': 'eve_faking',
'person.weight': 50,
'person.friend': [
'alice',
'bob',
{'person.first_name': 'Charlie',
'person.age': 22,
'person.last_name': 'Goodman',
'person.id': 'charlie_goodman',
'person.weight': 120,
'person.friend': 'eve'}
]
}),
Put({'_temp_id': 'david',
'person.first_name': 'David',
'person.age': 7,
'person.last_name': 'Dull',
'person.id': 'david_dull',
'person.weight': 25,
'person.friend': {
'_temp_id': 'george',
'person.first_name': 'George',
'person.age': 7,
'person.last_name': 'Geomancer',
'person.id': 'george_geomancer',
'person.weight': 25,
'person.friend': 'george'}
}),
]))
res = db.run([
R.ff(['?a', '?b'],
T.person.friend('?a', '?b')),
R.ff(['?a', '?b'],
T.person.friend('?a', '?c'),
R.ff('?c', '?b')),
Q(['?a'],
T.person.first_name('?a', '?n'),
T.person.first_name('?alice', 'Alice'),
NotExists(R.ff('?a', '?alice'))
),
],
out={'friend': Pull('?a', ['person.first_name'])})
print(res)