def complex_op_chain(gc): # All road junctions in the South West that were heavily used by buses in year 2000. junctions = gc.execute_operations( operations=[ g.GetAdjacentIds( input=[g.EntitySeed(vertex='South West')], view=g.View( edges=[ g.ElementDefinition( group='RegionContainsLocation', group_by=[] ) ] ) ), g.GetAdjacentIds( view=g.View( edges=[ g.ElementDefinition( group='LocationContainsRoad', group_by=[] ) ] ) ), g.ToSet(), g.GetAdjacentIds( view=g.View( edges=[ g.ElementDefinition( group='RoadHasJunction', group_by=[] ) ] ) ), g.GetElements( view=g.View( entities=[ g.ElementDefinition( group='JunctionUse', group_by=[], transient_properties=[ g.Property('busCount', 'java.lang.Long') ], pre_aggregation_filter_functions=[ g.PredicateContext( selection=['startDate'], predicate=g.IsMoreThan( value={'java.util.Date': 946684800000}, or_equal_to=True ) ), g.PredicateContext( selection=['startDate'], predicate=g.IsLessThan( value={'java.util.Date': 978307200000}, or_equal_to=False ) ) ], post_aggregation_filter_functions=[ g.PredicateContext( selection=['startDate'], predicate=g.IsMoreThan( value={'java.util.Date': 946684800000}, or_equal_to=True ) ), g.PredicateContext( selection=['countByVehicleType'], predicate=g.PredicateMap( predicate=g.IsMoreThan( value={'java.lang.Long': 1000}, or_equal_to=False ), key='BUS' ) ) ], transform_functions=[ g.FunctionContext( selection=['countByVehicleType'], function=g.Function( class_name='uk.gov.gchq.gaffer.types.function.FreqMapExtractor', fields={'key': 'BUS'} ), projection=['busCount'] ) ] ) ] ), include_incoming_out_going=g.InOutType.OUT ), g.ToCsv( element_generator={ 'class': 'uk.gov.gchq.gaffer.data.generator.CsvGenerator', 'fields': { 'VERTEX': 'Junction', 'busCount': 'Bus Count' }, 'quoted': False, 'header': 'Junction,Bus Count' } ) ] ) print( 'All road junctions in the South West that were heavily used by buses in year 2000.') print(junctions) print()
class GafferPredicatesTest(unittest.TestCase): examples = [ [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.AgeOff", "ageOffTime" : 100000 } ''', g.AgeOff( age_off_time=100000 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.And", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 3 }, { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 0 } ] } ''', g.And( predicates=[ g.IsLessThan( value=3, or_equal_to=False ), g.IsMoreThan( value=0, or_equal_to=False ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.And", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 2 }, "selection" : [ 0 ] }, { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 5 }, "selection" : [ 1 ] } ] } ''', g.And( predicates=[ g.NestedPredicate( selection=[ 0 ], predicate=g.IsLessThan( value=2, or_equal_to=False ) ), g.NestedPredicate( selection=[ 1 ], predicate=g.IsMoreThan( value=5, or_equal_to=False ) ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.AreIn", "values" : [ 1, 2, 3 ] } ''', g.AreIn( values=[ 1, 2, 3 ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.CollectionContains", "value" : 1 } ''', g.CollectionContains( value=1 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Exists" } ''', g.Exists() ], [ ''' { "class" : "uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan", "orEqualTo" : false, "value" : 2 } ''', g.HyperLogLogPlusIsLessThan( value=2, or_equal_to=False ) ], [ ''' { "class" : "uk.gov.gchq.gaffer.sketches.datasketches.cardinality.predicate.HllSketchIsLessThan", "orEqualTo" : false, "value" : 2 } ''', g.HllSketchIsLessThan( value=2, or_equal_to=False ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsA", "type" : "java.lang.String" } ''', g.IsA( type="java.lang.String" ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsEqual", "value" : 5 } ''', g.IsEqual( value=5 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsFalse" } ''', g.IsFalse() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsIn", "values" : [ 5, { "java.lang.Long" : 5 }, "5", { "java.lang.Character" : "5" } ] } ''', g.IsIn( values=[ 5, {'java.lang.Long': 5}, "5", {'java.lang.Character': '5'} ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 5 } ''', g.IsLessThan( value=5, or_equal_to=False ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : true, "value" : 5 } ''', g.IsLessThan( value=5, or_equal_to=True ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 5 } ''', g.IsMoreThan( value=5, or_equal_to=False ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsShorterThan", "maxLength" : 4, "orEqualTo" : false } ''', g.IsShorterThan( or_equal_to=False, max_length=4 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsTrue" } ''', g.IsTrue() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsXLessThanY" } ''', g.IsXLessThanY() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY" } ''', g.IsXMoreThanY() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.MapContains", "key" : "a" } ''', g.MapContains( key="a" ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.MapContainsPredicate", "keyPredicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.Regex", "value" : { "java.util.regex.Pattern" : "a.*" } } } ''', g.MapContainsPredicate( key_predicate=g.Regex( value={'java.util.regex.Pattern': 'a.*'} ) ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.MultiRegex", "value" : [ { "java.util.regex.Pattern" : "[a-d]" }, { "java.util.regex.Pattern" : "[0-4]" } ] } ''', g.MultiRegex( value=[ {'java.util.regex.Pattern': '[a-d]'}, {'java.util.regex.Pattern': '[0-4]'} ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Not", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.Exists" } } ''', g.Not( predicate=g.Exists() ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Or", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 2 }, { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsEqual", "value" : 5 }, { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 10 } ] } ''', g.Or( predicates=[ g.IsLessThan( value=2, or_equal_to=False ), g.IsEqual( value=5 ), g.IsMoreThan( value=10, or_equal_to=False ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Or", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 2 }, "selection" : [ 0 ] }, { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 10 }, "selection" : [ 1 ] } ] } ''', g.Or( predicates=[ g.NestedPredicate( selection=[ 0 ], predicate=g.IsLessThan( value=2, or_equal_to=False ) ), g.NestedPredicate( selection=[ 1 ], predicate=g.IsMoreThan( value=10, or_equal_to=False ) ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.predicate.PredicateMap", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : { "java.lang.Long" : 2 } }, "key" : "key1" } ''', g.PredicateMap( key="key1", predicate=g.IsMoreThan( or_equal_to=False, value={'java.lang.Long': 2} ) ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Regex", "value" : { "java.util.regex.Pattern" : "[a-d0-4]" } } ''', g.Regex( value={'java.util.regex.Pattern': '[a-d0-4]'} ) ], [ ''' { "class":"uk.gov.gchq.koryphe.impl.predicate.StringContains", "value":"someValue", "ignoreCase":false } ''', g.StringContains( value='someValue', ignore_case=False ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLongerThan", "minLength" : 10, "orEqualTo" : true } ''', g.IsLongerThan( min_length=10, or_equal_to=True ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.If", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.MapContains", "key" : "testKey" }, "then" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLongerThan", "minLength" : 20, "orEqualTo" : true }, "otherwise" : { "class" : "uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan", "value" : 10, "orEqualTo" : false } } ''', g.pred.If( predicate=g.MapContains( key='testKey' ), then=g.IsLongerThan( min_length=20, or_equal_to=True ), otherwise=g.HyperLogLogPlusIsLessThan( value=10, or_equal_to=False ) ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.If", "condition" : true, "then" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLongerThan", "minLength" : 20, "orEqualTo" : true } } ''', g.pred.If( condition=True, then=g.IsLongerThan( min_length=20, or_equal_to=True ) ) ], [ ''' {"class":"uk.gov.gchq.koryphe.impl.predicate.range.InTimeRangeDual","start":"2017/01/01","end":"2017/02/01","timeUnit":"MICROSECOND","startFullyContained":true,"endFullyContained":true,"timeZone":"Etc/GMT+0"} ''', g.InTimeRangeDual( start='2017/01/01', end='2017/02/01', time_unit='MICROSECOND', start_fully_contained=True, end_fully_contained=True, time_zone='Etc/GMT+0' ) ], [ ''' {"class":"uk.gov.gchq.koryphe.impl.predicate.range.InTimeRange","start":"2017/01/01","end":"2017/02/01","timeUnit":"MICROSECOND","timeZone":"Etc/GMT+0"} ''', g.InTimeRange( start='2017/01/01', end='2017/02/01', time_unit='MICROSECOND', time_zone='Etc/GMT+0' ) ], [ ''' { "class": "uk.gov.gchq.gaffer.data.element.comparison.ElementJoinComparator" } ''', g.ElementJoinComparator() ], [ ''' { "class": "uk.gov.gchq.gaffer.data.element.comparison.ElementJoinComparator", "groupByProperties": [ "test1", "test2" ] } ''', g.ElementJoinComparator(group_by_properties=["test1", "test2"]) ], [ ''' { "class": "uk.gov.gchq.gaffer.access.predicate.user.DefaultUserPredicate", "auths": [ "a", "test2" ], "creatingUserId": "user1" } ''', g.DefaultUserPredicate(auths=["a", "test2"], creating_user_id="user1") ], [ ''' { "class": "uk.gov.gchq.gaffer.access.predicate.user.UnrestrictedAccessUserPredicate" } ''', g.UnrestrictedAccessUserPredicate() ], [ ''' { "class": "uk.gov.gchq.gaffer.access.predicate.user.NoAccessUserPredicate" } ''', g.NoAccessUserPredicate() ], [ ''' { "class": "uk.gov.gchq.gaffer.data.elementdefinition.view.access.predicate.user.NamedViewWriteUserPredicate", "auths": [ "a", "test2" ], "creatingUserId": "user1" } ''', g.NamedViewWriteUserPredicate(auths=["a", "test2"], creating_user_id="user1") ] ] def test_examples(self): for example in self.examples: self.assertEqual( g.json.loads(example[0]), example[1].to_json(), "json failed: \n" + example[0] ) g.JsonConverter.from_json(example[0], validate=True)
class GafferPredicatesTest(unittest.TestCase): examples = [ [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.AgeOff", "ageOffTime" : 100000 } ''', g.AgeOff( age_off_time=100000 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.And", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 3 }, { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 0 } ] } ''', g.And( predicates=[ g.IsLessThan( value=3, or_equal_to=False ), g.IsMoreThan( value=0, or_equal_to=False ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.And", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 2 }, "selection" : [ 0 ] }, { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 5 }, "selection" : [ 1 ] } ] } ''', g.And( predicates=[ g.NestedPredicate( selection=[ 0 ], predicate=g.IsLessThan( value=2, or_equal_to=False ) ), g.NestedPredicate( selection=[ 1 ], predicate=g.IsMoreThan( value=5, or_equal_to=False ) ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.AreIn", "values" : [ 1, 2, 3 ] } ''', g.AreIn( values=[ 1, 2, 3 ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.CollectionContains", "value" : 1 } ''', g.CollectionContains( value=1 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Exists" } ''', g.Exists() ], [ ''' { "class" : "uk.gov.gchq.gaffer.sketches.clearspring.cardinality.predicate.HyperLogLogPlusIsLessThan", "orEqualTo" : false, "value" : 2 } ''', g.HyperLogLogPlusIsLessThan( value=2, or_equal_to=False ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsA", "type" : "java.lang.String" } ''', g.IsA( type="java.lang.String" ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsEqual", "value" : 5 } ''', g.IsEqual( value=5 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsFalse" } ''', g.IsFalse() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsIn", "values" : [ 5, { "java.lang.Long" : 5 }, "5", { "java.lang.Character" : "5" } ] } ''', g.IsIn( values=[ 5, {'java.lang.Long': 5}, "5", {'java.lang.Character': '5'} ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 5 } ''', g.IsLessThan( value=5, or_equal_to=False ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : true, "value" : 5 } ''', g.IsLessThan( value=5, or_equal_to=True ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 5 } ''', g.IsMoreThan( value=5, or_equal_to=False ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsShorterThan", "maxLength" : 4, "orEqualTo" : false } ''', g.IsShorterThan( or_equal_to=False, max_length=4 ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsTrue" } ''', g.IsTrue() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsXLessThanY" } ''', g.IsXLessThanY() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsXMoreThanY" } ''', g.IsXMoreThanY() ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.MapContains", "key" : "a" } ''', g.MapContains( key="a" ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.MapContainsPredicate", "keyPredicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.Regex", "value" : { "java.util.regex.Pattern" : "a.*" } } } ''', g.MapContainsPredicate( key_predicate=g.Regex( value={'java.util.regex.Pattern': 'a.*'} ) ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.MultiRegex", "value" : [ { "java.util.regex.Pattern" : "[a-d]" }, { "java.util.regex.Pattern" : "[0-4]" } ] } ''', g.MultiRegex( value=[ {'java.util.regex.Pattern': '[a-d]'}, {'java.util.regex.Pattern': '[0-4]'} ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Not", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.Exists" } } ''', g.Not( predicate=g.Exists() ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Or", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 2 }, { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsEqual", "value" : 5 }, { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 10 } ] } ''', g.Or( predicates=[ g.IsLessThan( value=2, or_equal_to=False ), g.IsEqual( value=5 ), g.IsMoreThan( value=10, or_equal_to=False ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Or", "predicates" : [ { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsLessThan", "orEqualTo" : false, "value" : 2 }, "selection" : [ 0 ] }, { "class" : "uk.gov.gchq.koryphe.tuple.predicate.IntegerTupleAdaptedPredicate", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : 10 }, "selection" : [ 1 ] } ] } ''', g.Or( predicates=[ g.NestedPredicate( selection=[ 0 ], predicate=g.IsLessThan( value=2, or_equal_to=False ) ), g.NestedPredicate( selection=[ 1 ], predicate=g.IsMoreThan( value=10, or_equal_to=False ) ) ] ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.predicate.PredicateMap", "predicate" : { "class" : "uk.gov.gchq.koryphe.impl.predicate.IsMoreThan", "orEqualTo" : false, "value" : { "java.lang.Long" : 2 } }, "key" : "key1" } ''', g.PredicateMap( key="key1", predicate=g.IsMoreThan( or_equal_to=False, value={'java.lang.Long': 2} ) ) ], [ ''' { "class" : "uk.gov.gchq.koryphe.impl.predicate.Regex", "value" : { "java.util.regex.Pattern" : "[a-d0-4]" } } ''', g.Regex( value={'java.util.regex.Pattern': '[a-d0-4]'} ) ], [ ''' { "class":"uk.gov.gchq.koryphe.impl.predicate.StringContains", "value":"someValue", "ignoreCase":false } ''', g.StringContains( value='someValue', ignore_case=False ) ] ] def test_examples(self): for example in self.examples: self.assertEqual( json.loads(example[0]), example[1].to_json(), "json failed: \n" + example[0] ) g.JsonConverter.from_json(example[0], validate=True)