Esempio n. 1
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    y_predict = None
    X = []
    y = []
    data_pts = []
    for c in children:
        opVarValue = c.instantiation_value(c.get(tt.OPVAR))
        if utils.is_numeric(opVarValue) and utils.is_numeric(c.get(tt.TIME)):
            x_val = utils.get_number(c.get(tt.TIME), None)
            y_val = utils.get_number(opVarValue, None)
            X.append([x_val])
            y.append(y_val)
            data_pts.append([x_val, y_val])
    X = np.array(X)
    y = np.array(y)
    reg = LinearRegression().fit(X, y)
    x_predict = utils.get_number(alist.get(tt.TIME), None)
    y_predict = reg.predict(np.array([[x_predict]]))[0]
    prediction = [x_predict, y_predict]
    coeffs = [v for v in reg.coef_]
    coeffs.insert(0, reg.intercept_)
    fnStr = 'LIN;' + ';'.join([str(v) for v in reg.coef_])
    fnAndData = \
        """{{"function":{coeffs}, "data":{data_pts}, "prediction":{prediction}}}""".format(
            coeffs=coeffs, data_pts=data_pts, prediction=prediction)

    alist.instantiate_variable(alist.get(tt.OPVAR), y_predict)
    alist.set(tt.FNPLOT, fnAndData)

    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children,
                             len(data_pts) == len(children), alist.get(tt.OP),
                             len(children)))
    return alist
Esempio n. 2
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    variables = alist.variables()
    data = [x.instantiation_value(alist.get(tt.OPVAR)) for x in children
            if (x not in list(variables.keys()) and x not in list(variables.values()))]
    alist.instantiate_variable(alist.get(tt.OPVAR), len(data))

    alist.instantiate_variable(tt.COV, estimate_uncertainty(
        children, False, alist.get(tt.OP), len(children)
    ))
    return alist
Esempio n. 3
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    data = [str(x.instantiation_value(alist.get(tt.OPVAR))) for x in children]
    data_str = ','.join(data)
    alist.instantiate_variable(alist.get(tt.OPVAR), data_str)

    # TODO: port code for cov calculations
    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children, False, alist.get(tt.OP), len(children)))
    return alist
Esempio n. 4
0
def projections(parent: Alist, alists_to_propagate):
    ''' propagate projection from selected child alist to its parent [in place]'''

    # copy projection vars of min alist to parent
    for c in alists_to_propagate:
        projVars = c.projection_variables()
        if projVars:
            for pvkey, pv in projVars.items():
                parent.instantiate_variable(
                    pvkey, c.instantiation_value(pvkey), insert_missing=True)
Esempio n. 5
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    data = [
        utils.get_number(x.instantiation_value(alist.get(tt.OPVAR)), 0)
        for x in children
    ]
    alist.instantiate_variable(alist.get(tt.OPVAR), sum(data))

    # TODO: port code for cov calculations
    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children, True, alist.get(tt.OP), len(children)))
    return alist
Esempio n. 6
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    data = {x: utils.get_number(x.instantiation_value(
        alist.get(tt.OPVAR)), 999999999999999) for x in children}
    minAlist = min(data, key=data.get)
    minValue = data[minAlist]
    alist.instantiate_variable(alist.get(tt.OPVAR), minValue)

    propagate.projections(alist, (minAlist,))

    alist.instantiate_variable(tt.COV, estimate_uncertainty(
        children, True, alist.get(tt.OP), len(children)
    ))
    return alist
Esempio n. 7
0
 def test_instantiateVariables(self):
     alist = Alist(
         **{
             tt.ID: '1',
             tt.SUBJECT: 'Africa',
             tt.PROPERTY: 'P1082',
             tt.OBJECT: '?x',
             tt.TIME: '2010',
             tt.OPVAR: '?x',
             tt.COST: 1
         })
     alist.set('#d', '')
     alist.set('?x', '#d')
     alist.instantiate_variable('#d', 99)
     self.assertEqual(alist.get('?x'), 99, "OBJECT should be 99.")
Esempio n. 8
0
class Test_Inference(unittest.TestCase):

    def setUp(self):
        G = InferenceGraph()
        self.infer = Infer(G)
        self.alist = Alist(**{tt.ID: '1', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082',
                              tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1})

        self.c1 = Alist(**{tt.ID: '2', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082',
                           tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1})
        self.c1.attributes['?x'] = ''
        self.c1.instantiate_variable('?x', '120')
        self.c1.state = states.REDUCIBLE
        self.c1.data_sources = ['wikidata', 'worldbank']

        # self.c2 = Alist(**{tt.ID: '3', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082',
        #                    tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1})
        # self.c2.attributes['?x'] = ''
        # self.c2.instantiate_variable('?x', '122')
        # self.c2.state = states.REDUCIBLE
        # self.c2.data_sources = ['conceptnet', 'worldbank']

        # self.c3 = Alist(**{tt.ID: '4', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082',
        #                    tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1})
        # self.c3.attributes['?x'] = ''
        # self.c3.instantiate_variable('?x', '126')
        # self.c3.state = states.REDUCIBLE
        # self.c3.data_sources.add('ciafactbook')

        # self.alist.link_child(self.c1)
        # self.alist.link_child(self.c2)
        # self.alist.link_child(self.c3)

    def test_inference(self):
        alist = Alist(**{tt.ID: '1', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'population',
                         tt.OBJECT: '', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1})
        # infer = Infer().runFrank(alist)
        self.assertTrue(True)

    def test_aggregate(self):
        res = self.infer.aggregate(self.alist.id)
        self.assertTrue(res)
Esempio n. 9
0
 def test_getInstantiationValue(self):
     alist = Alist(
         **{
             tt.ID: '1',
             tt.SUBJECT: 'Africa',
             tt.PROPERTY: 'P1082',
             tt.OBJECT: '?x',
             tt.TIME: '2010',
             tt.OPVAR: '?x',
             tt.COST: 1
         })
     alist.set('#d', '')
     alist.set('?x', '#d')
     alist.set('$y', '')
     alist.instantiate_variable('#d', 99)
     results = (alist.instantiation_value('#d'),
                alist.instantiation_value('?x'),
                alist.instantiation_value(tt.OBJECT),
                alist.instantiation_value(tt.TIME),
                alist.instantiation_value('$y'))
     self.assertEqual(results, (99, 99, 99, '2010', ''))
Esempio n. 10
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    delimiter = ';;'
    total = 0.0
    numList = []
    nonNumList = []
    inst_vars = alist.instantiated_attributes().keys()
    for c in children:
        for k, v in c.instantiated_attributes().items():
            if k not in inst_vars and k in alist.attributes and k != tt.OP:
                c.instantiate_variable(k, v)

        opVarValue = c.get(c.get(tt.OPVAR))
        if isinstance(opVarValue, str):
            opVarValue = list(map(str, opVarValue.split(delimiter)))
        else:
            opVarValue = [opVarValue]
        for opval in opVarValue:
            if utils.is_numeric(opval):
                total += float(opval)
                numList.append(float(opval))
                if not str(opval).startswith(vx.NESTING):
                    nonNumList.append(str(opval))

            else:
                # if not c.get(c.get(tt.OPVAR)).startswith(vx.NESTING):
                #     nonNumList.append(c.get(c.get(tt.OPVAR)))
                nonNumList.append(opval)

    if numList or nonNumList:
        if len(numList) >= len(nonNumList):
            opVar = alist.get(tt.OPVAR)
            valueToReturn = total / len(children)
            if opVar == alist.get(tt.TIME):
                valueToReturn = str(int(valueToReturn))
            alist.instantiate_variable(opVar, valueToReturn)
        else:
            # # get modal value
            # valueToReturn = max(nonNumList, key=nonNumList.count)
            counts = dict(Counter(nonNumList))
            counts_set = set(counts.values())
            max_val = max(counts_set)
            items = [x for x, y in counts.items() if y == max_val]
            valueToReturn = f'{delimiter} '.join(map(str, set(items)))

            # if len(nonNumList) == 1:
            #     valueToReturn = nonNumList[0]
            # else:
            #     # return list of different values
            #     valueToReturn = ', '.join(map(str,set(nonNumList)))

            alist.instantiate_variable(alist.get(tt.OPVAR), valueToReturn)
    else:
        return None

    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children,
                             len(numList) == len(children), alist.get(tt.OP),
                             len(children)))
    return alist
Esempio n. 11
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    # do comparisons
    print(children)
    vars_to_compare = alist.get(tt.OPVAR).split(' ')

    # # copy projection vars of min alist to parent
    # for c in children:
    #   if c.get(tt.OP) != 'comp':
    #     for vc in vars_to_compare:
    #       if vc in c.attributes:
    #         projVars = c.projection_variables()
    #         alist.instantiate_variable(vc, c.instantiation_value(vc), insert_missing=True)

    # for x in vars_to_compare:
    #   if alist.is_instantiated(x) == False:
    #     # return None
    #     pass

    # propagate projection vars to parent
    propagate.projections(alist, tuple(children))
    response_var = "?_eq_"
    if len(vars_to_compare) == 0:
        alist.set(response_var, "false")
        return alist

    result = True
    for x in vars_to_compare:
        result = (alist.instantiation_value(x) ==
                  alist.instantiation_value(vars_to_compare[0])) and result

    alist.instantiate_variable(response_var, str(
        result).lower(), insert_missing=True)

    # alist.instantiate_variable(tt.COV, estimate_uncertainty(
    #   children, True, alist.get(tt.OP), len(children)
    # ))
    return alist
Esempio n. 12
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    allNumeric = True
    y_predict = None
    X = []
    y = []
    data_pts = []
    for c in children:
        opVarValue = c.instantiation_value(c.get(tt.OPVAR))
        if utils.is_numeric(opVarValue) and utils.is_numeric(c.get(tt.TIME)):
            x_val = utils.get_number(c.get(tt.TIME), None)
            y_val = utils.get_number(opVarValue, None)
            X.append([x_val])
            y.append(y_val)
            data_pts.append([x_val, y_val])
        else:
            allNumeric = False
    X = np.array(X)
    y = np.array(y)

    x_to_predict = utils.get_number(alist.get(tt.TIME), None)
    if not x_to_predict:
        return None
    else:
        x_to_predict = np.array([x_to_predict])

    gp_prediction = do_gpregress(X, y, x_to_predict,
                                 (np.max(y) - np.min(y))**2, 1)

    if gp_prediction is None:
        return None

    y_predict = gp_prediction[0]['y']
    try:
        prediction = [x_to_predict, y_predict]
        alist.instantiate_variable(alist.get(tt.OPVAR), y_predict)
        alist.instantiate_variable(tt.COV,
                                   gp_prediction[0]['stdev'] / y_predict)

        alist.instantiate_variable(
            tt.COV,
            estimate_uncertainty(children, allNumeric, alist.get(tt.OP),
                                 len(children)))
    except Exception as ex:
        print(ex)
        return None

    return alist
Esempio n. 13
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    sum = 0.0
    allNumeric = True
    for c in children:
        for k, v in c.instantiated_attributes().items():
            if k in alist.attributes:
                alist.instantiate_variable(k, v)

        opVarValue = c.get(c.get(tt.OPVAR))
        if utils.is_numeric(opVarValue):
            sum += float(opVarValue)
        else:
            allNumeric = False

    alist.instantiate_variable(alist.get(tt.OPVAR), sum / len(children))

    alist.instantiate_variable(
        tt.COV,
        estimate_uncertainty(children, allNumeric, alist.get(tt.OP),
                             len(children)))
    return alist
Esempio n. 14
0
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph):
    sum = 0.0
    nonNumList = []
    for c in children:
        for k, v in c.instantiated_attributes().items():
            if k in alist.attributes:
                alist.instantiate_variable(k, v)

        opVarValue = c.get(c.get(tt.OPVAR))

        if not c.get(c.get(tt.OPVAR)).startswith(vx.NESTING):
            nonNumList.append(c.get(c.get(tt.OPVAR)))

    # get modal value
    valueToReturn = max(nonNumList, key=nonNumList.count)
    alist.instantiate_variable(alist.get(tt.OPVAR), valueToReturn)

    # todo: propagate projection variables of modal alist to parent

    alist.instantiate_variable(tt.COV, estimate_uncertainty(
        children, len(nonNumList) > 0, 'value', len(children)
    ))
    return alist
Esempio n. 15
0
class TestReduce(unittest.TestCase):
    def setUp(self):
        self.G = InferenceGraph()
        self.alist = Alist(
            **{
                tt.ID: '1',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2020',
                tt.OPVAR: '?x',
                tt.COST: 1
            })

        self.c1 = Alist(
            **{
                tt.ID: '2',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2010',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c1.instantiate_variable('?x', '120')

        self.c2 = Alist(
            **{
                tt.ID: '3',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2011',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c2.instantiate_variable('?x', '122')

        self.c3 = Alist(
            **{
                tt.ID: '4',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2012',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c3.instantiate_variable('?x', '126')

        self.c4 = Alist(
            **{
                tt.ID: '5',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2013',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c4.instantiate_variable('?x', '125')

        self.c5 = Alist(
            **{
                tt.ID: '5',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2014',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c5.instantiate_variable('?x', '126')

        self.c6 = Alist(
            **{
                tt.ID: '6',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2015',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c6.instantiate_variable('?x', '128')
        self.c7 = Alist(
            **{
                tt.ID: '7',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2016',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c7.instantiate_variable('?x', '129')

        self.G.add_alist(self.alist)
        self.G.link(self.alist, self.c1)
        self.G.link(self.alist, self.c2)
        self.G.link(self.alist, self.c3)
        self.G.link(self.alist, self.c4)
        self.G.link(self.alist, self.c5)
        self.G.link(self.alist, self.c6)
        self.G.link(self.alist, self.c7)

        self.G2 = InferenceGraph()
        self.alist2 = Alist(
            **{
                tt.ID: '1',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2020',
                tt.OPVAR: '?x',
                tt.COST: 1
            })

        self.c21 = Alist(
            **{
                tt.ID: '2',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2010',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c21.instantiate_variable('?x', 'a')

        self.c22 = Alist(
            **{
                tt.ID: '3',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2011',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c22.instantiate_variable('?x', 'b')

        self.c23 = Alist(
            **{
                tt.ID: '4',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2012',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c23.instantiate_variable('?x', 'c')

        self.c24 = Alist(
            **{
                tt.ID: '5',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2013',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c24.instantiate_variable('?x', 'd')

        self.c25 = Alist(
            **{
                tt.ID: '5',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2014',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': ''
            })
        self.c25.instantiate_variable('?x', 'a')

        self.G2.add_alist(self.alist2)
        self.G2.link(self.alist2, self.c21)
        self.G2.link(self.alist2, self.c22)
        self.G2.link(self.alist2, self.c23)
        self.G2.link(self.alist2, self.c24)
        self.G2.link(self.alist2, self.c25)

    def test_value(self):
        a = frank.reduce.value.reduce(self.alist,
                                      self.G.child_alists(self.alist.id),
                                      self.G)
        self.assertTrue(a.instantiation_value(tt.OBJECT), '124')

    def test_value2(self):
        a = frank.reduce.value.reduce(self.alist2,
                                      self.G2.child_alists(self.alist2.id),
                                      self.G2)
        self.assertTrue(a.instantiation_value(tt.OBJECT), 'a')

    def test_values(self):
        a = frank.reduce.values.reduce(self.alist,
                                       self.G.child_alists(self.alist.id),
                                       self.G)
        self.assertEqual(a.instantiation_value(tt.OBJECT),
                         '120,122,126,125,126,128,129')

    def test_sum(self):
        a = frank.reduce.sum.reduce(self.alist,
                                    self.G.child_alists(self.alist.id), self.G)
        self.assertEqual(float(a.instantiation_value(tt.OPVAR)), 876.0)

    def test_max(self):
        a = frank.reduce.max.reduce(self.alist,
                                    self.G.child_alists(self.alist.id), self.G)
        self.assertEqual(int(a.instantiation_value(tt.OPVAR)), 129)

    def test_min(self):
        a = frank.reduce.min.reduce(self.alist,
                                    self.G.child_alists(self.alist.id), self.G)
        self.assertEqual(a.instantiation_value(tt.OPVAR), '120')

    def test_count(self):
        a = frank.reduce.count.reduce(self.alist,
                                      self.G.child_alists(self.alist.id),
                                      self.G)
        self.assertEqual(a.instantiation_value(tt.OPVAR), 7)

    def test_product(self):
        a = frank.reduce.product.reduce(self.alist,
                                        self.G.child_alists(self.alist.id),
                                        self.G)
        self.assertEqual(a.instantiation_value(tt.OPVAR), 479724456960000.0)

    def test_regress(self):
        a = frank.reduce.regress.reduce(self.alist,
                                        self.G.child_alists(self.alist.id),
                                        self.G)
        print(a)
        self.assertAlmostEqual(a.instantiation_value(tt.OPVAR),
                               134.89,
                               places=2)

    def test_gpregress(self):
        a = frank.reduce.gpregress.reduce(self.alist,
                                          self.G.child_alists(self.alist.id),
                                          self.G)
        print(a)
        self.assertAlmostEqual(a.instantiation_value(tt.OPVAR), 134, places=0)

    def test_gpregress_2(self):
        alist = Alist(
            **{
                tt.ID: '101',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2020',
                tt.OPVAR: '?x',
                tt.COST: 1
            })

        c1 = Alist(
            **{
                tt.ID: '21011',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2019.0',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': 1839758040765.62
            })
        c2 = Alist(
            **{
                tt.ID: '21012',
                tt.SUBJECT: 'Ghana',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2018.0',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '?x': 1885482534238.33
            })
        G = InferenceGraph()
        G.add_alist(alist)
        G.link(alist, c1)
        G.link(alist, c2)

        a = frank.reduce.gpregress.reduce(alist, G.child_alists(alist.id), G)
        print(a)
        self.assertAlmostEqual(a.instantiation_value(tt.OPVAR),
                               1792866444829.7,
                               places=1)

    def test_do_gpregress(self):
        data = [[2019.0, 1839758040765.62], [2018.0, 1885482534238.33],
                [2017.0, 2055505502224.73], [2016.0, 1793989048409.29],
                [2015.0, 1802214373741.32], [2014.0, 2455993625159.37],
                [2013.0, 2472806919901.67], [2012.0, 2465188674415.03],
                [2011.0, 2616201578192.25], [2010.0, 2208871646202.82],
                [2009.0, 1667019780934.28], [2008.0, 1695824571927.15],
                [2007.0, 1397084345950.39], [2006.0, 1107640297889.95]]
        X, y = [], []
        for d in data:
            X.append([d[0]])
            y.append(d[1])
        X = np.array(X)
        y = np.array(y)
        predict = frank.reduce.gpregress.do_gpregress(X, y, np.array(
            [2022.]), (np.max(y) - np.min(y))**2, 1)
        y_predict = predict[0]['y']
        self.assertAlmostEqual(y_predict, 1324535292167, places=0)

    @unittest.skip
    def test_nnpredict(self):
        a = frank.reduce.nnpredict.reduce(self.alist,
                                          self.G.child_alists(self.alist.id),
                                          self.G)
        self.assertAlmostEqual(a.instantiation_value(tt.OPVAR),
                               158.97,
                               places=2)

    def test_comp(self):
        # root = Alist(**{tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082',
        #                  tt.OBJECT: '?x', tt.TIME: '2016', tt.OPVAR: '?x', tt.COST: 1})
        # node101 = Alist(**{tt.OP:'comp', tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082',
        #                  tt.OBJECT: '?x', tt.TIME: '2016', tt.OPVAR: '?x', tt.COST: 1})
        a = Alist(
            **{
                tt.ID: '1',
                tt.SUBJECT: '$y',
                tt.PROPERTY: 'P1082',
                tt.OBJECT: '?x',
                tt.TIME: '2010',
                tt.OPVAR: '?x',
                tt.COST: 1,
                '$y': {
                    "$is": "Ghana"
                }
            })
        G = InferenceGraph()
        G.add_alist(a)
        normalize.Normalize().decompose(a, G)
        child1 = G.child_alists(a.id)[0]
        result = frank.reduce.comp.reduce(child1, G.child_alists(child1.id), G)
        self.assertTrue(result != None)

    def test_eq(self):
        a = Alist(**{
            tt.ID: '1',
            tt.OPVAR: '$x $y',
            '$x': '?x1',
            '$y': '?y1',
            '?_eq_': ''
        })
        b = Alist(**{tt.ID: '2', tt.OPVAR: '?x1', '?x1': 20})
        c = Alist(**{tt.ID: '3', tt.OPVAR: '?y1', '?y1': 20})
        G = InferenceGraph()
        G.add_alist(a)
        G.link(a, b)
        G.link(a, c)
        result = frank.reduce.eq.reduce(a, [b, c], G)
        self.assertTrue(True if result.instantiation_value('?_eq_') ==
                        'true' else False)

    def test_gt(self):
        a = Alist(**{
            tt.ID: '1',
            tt.OPVAR: '$x $y',
            '$x': '?x1',
            '$y': '?y1',
            '?_gt_': ''
        })
        b = Alist(**{tt.ID: '2', tt.OPVAR: '?x1', '?x1': 36})
        c = Alist(**{tt.ID: '3', tt.OPVAR: '?y1', '?y1': 33})

        G = InferenceGraph()
        G.add_alist(a)
        G.link(a, b)
        G.link(a, c)
        result = frank.reduce.gt.reduce(a, [b, c], G)
        self.assertTrue(True if result.instantiation_value('?_gt_') ==
                        'true' else False)

    def test_gte(self):
        a = Alist(**{
            tt.ID: '1',
            tt.OPVAR: '$x $y',
            '$x': '?x1',
            '$y': '?y1',
            '?_gte_': ''
        })
        b = Alist(**{tt.ID: '2', tt.OPVAR: '?x1', '?x1': 33})
        c = Alist(**{tt.ID: '3', tt.OPVAR: '?y1', '?y1': 33})

        G = InferenceGraph()
        G.add_alist(a)
        G.link(a, b)
        G.link(a, c)
        result = frank.reduce.gte.reduce(a, [b, c], G)
        self.assertTrue(True if result.instantiation_value('?_gte_') ==
                        'true' else False)

    def test_lt(self):
        a = Alist(**{
            tt.ID: '1',
            tt.OPVAR: '$x $y',
            '$x': '?x1',
            '$y': '?y1',
            '?_lt_': ''
        })
        b = Alist(**{tt.ID: '2', tt.OPVAR: '?x1', '?x1': 20})
        c = Alist(**{tt.ID: '3', tt.OPVAR: '?y1', '?y1': 30})

        G = InferenceGraph()
        G.add_alist(a)
        G.link(a, b)
        G.link(a, c)
        result = frank.reduce.lt.reduce(a, [b, c], G)
        self.assertTrue(True if result.instantiation_value('?_lt_') ==
                        'true' else False)

    def test_lte(self):
        a = Alist(**{
            tt.ID: '1',
            tt.OPVAR: '$x $y',
            '$x': '?x1',
            '$y': '?y1',
            '?_lte_': ''
        })
        b = Alist(**{tt.ID: '2', tt.OPVAR: '?x1', '?x1': 30})
        c = Alist(**{tt.ID: '3', tt.OPVAR: '?y1', '?y1': 30})

        G = InferenceGraph()
        G.add_alist(a)
        G.link(a, b)
        G.link(a, c)
        result = frank.reduce.lte.reduce(a, [b, c], G)
        self.assertTrue(True if result.instantiation_value('?_lte_') ==
                        'true' else False)