Exemple #1
0
 def rank(self, graph=None):
     """ Returns a list of (concept1, [concept2, ...])-tuples sorted by weight.
     The weight is calculated according to nodes' eigenvalue in a graph.
     Be aware that tuples with the same weight may shuffle.
     For example:
     cmp = perception.compare_objects("is more important than")
     for concept1, comparisons in cmp.rank():
         for concept2 in comparisons:
             print concept1, cmp.comparison, concept2
     >>> life is more important than money
     >>> life is more important than kindness
     >>> ...
     """
     # Nodes with the highest weight are at the front of the list.
     eigensort = lambda graph, id1, id2: (graph.node(id1).weight < graph.node(id2).weight)*2-1
     if graph == None:
         graph = self.graph()
     r = []
     for node in graph.nodes:
         # Get the id's of all the nodes that point to this node.
         # Sort them according to weight.
         links = [n.id for n in node.links if node.links.edge(n).node1 == n]
         links.sort(cmp=lambda a, b: eigensort(graph, a, b))
         r.append((node.id, links))
     r.sort(cmp=lambda a, b: eigensort(graph, a[0], b[0]))
     return r
    def rank(self, graph=None):
        """ Returns a list of (concept1, [concept2, ...])-tuples sorted by weight.
		The weight is calculated according to nodes' eigenvalue in a graph.
		Be aware that tuples with the same weight may shuffle.
		For example:
		cmp = perception.compare_objects("is more important than")
		for concept1, comparisons in cmp.rank():
			for concept2 in comparisons:
				print concept1, cmp.comparison, concept2
		>>> life is more important than money
		>>> life is more important than kindness
		>>> ...
		"""
        # Nodes with the highest weight are at the front of the list.
        eigensort = lambda graph, id1, id2: (graph.node(id1).weight < graph.
                                             node(id2).weight) * 2 - 1
        if graph == None:
            graph = self.graph()
        r = []
        for node in graph.nodes:
            # Get the id's of all the nodes that point to this node.
            # Sort them according to weight.
            links = [n.id for n in node.links if node.links.edge(n).node1 == n]
            links.sort(cmp=lambda a, b: eigensort(graph, a, b))
            r.append((node.id, links))
        r.sort(cmp=lambda a, b: eigensort(graph, a[0], b[0]))
        return r
Exemple #3
0
 def __init__(self, output_vocabulary):
     self.vocab = output_vocabulary
     self.state = 0
     self.stack = []
     self.graph = node()
     self.top = self.graph
     self.sql_stack = []
     self.next_negation = False
Exemple #4
0
def makeGraph(parsedInput):                     # membuat graph yang berupa list of node
    graph = list()
    index = int()
    for adjArray in parsedInput.adjMatrix:      # membaca ajadcency matrix
        n = parsedInput.nodes[index]            # menyimpan node yang berupa tuple
        adjNodes = dict()
        i = int()
        for n1 in adjArray:
            if int(n1) == 1:                    # untuk setiap node tetangga
                key = parsedInput.nodes[i]      # menyimpan dictionary node tetangga dengan key
                                                # berupa tuple nama, lat, long dan value berupa distance
                temp = dict([(key, getDistance(n[1], n[2], key[1], key[2]))])
                adjNodes.update(temp)
            i += 1
        graph.append(g.node(n[0], n[1], n[2], adjNodes))
        index += 1
    return graph
Exemple #5
0
    def update(self, token):
        label = self.vocab.label2id[self.vocab.token_to_label(token)]
        # print("state: ", self.state)
        #print(token, label)
        # initial state, key words, columns & aggregators are allowed
        if self.state == 0:
            # eg: select [ max ] -> table
            if label == 1:
                self.state = 1
                # first aggregator
                if not self.top.column:
                    self.top.aggregate = token
                else:
                    self.top.additional_agg.append(token)
            # eg: select [ distinct ] -> table
            elif label == 2:
                self.state = 0
                self.top.distinct = True
            # eg: select [ table ] -> column
            elif label == 10:
                self.state = 2
                if self.top.column:
                    self.top.additional_col.append(token)
            else:
                raise (AssertionError("Exception Occurred!"))
        # select max/distinct ...
        elif self.state == 1:
            assert label == 10
            # eg: select [ table ] -> column
            self.state = 2
            if self.top.column:
                self.top.additional_col.append(token)
        # select table...
        elif self.state == 2:
            # eg: select distinct table [ column ]
            if label != 9:
                if token != "dual_carrier":
                    raise (AssertionError("Error Occurred!"))
            self.state = 3
            if not self.top.column:
                self.top.column = token
            else:
                self.top.additional_col[-1] += "." + token
                if len(self.top.additional_col) != len(
                        self.top.additional_agg):
                    self.top.additional_agg.append("")
        # select table.column ...
        elif self.state == 3:
            # eg: table.column, [ max ] -> table
            if label == 1:
                self.state = 1
                self.top.additional_agg.append(token)
            # eg: table.column from table where [ <C> ]
            elif label == 5:
                self.stack.append('C')
                self.sql_stack.append(self.top)
                self.top.constraint = Constraint()
                self.top = self.top.constraint
                self.state = 4
            # eg: table.column [ table ] -> column
            elif label == 10:
                self.state = 2
                self.top.additional_col.append(token)
            elif label == 15:
                self.state = 17
            else:
                raise (AssertionError("Exception Occurred!"))
        # ...from table where <C>...
        elif self.state == 4:
            # eg: [ not ] -> table
            if label == 11:
                self.state = 4
                #self.top.negation = True
                self.next_negation = True
            # eg: where [ ( ] -> <C>
            elif label == 12:
                self.state = 5
                self.top.cons.append(Constraint())
                self.sql_stack.append(self.top)
                self.top = self.top.cons[-1]
                if self.next_negation:
                    self.top.negation = True
                    self.next_negation = False

            # eg: where [ table] -> column
            elif label == 10:
                self.state = 6
                self.top.cons.append(node())
                self.sql_stack.append(self.top)
                self.top = self.top.cons[-1]
                self.top.table = token
                if self.next_negation:
                    self.top.negation = True
                    self.next_negation = False
            else:
                if token != '1' and label != 8:
                    raise (AssertionError("Exception Occurred!"))
                else:
                    self.top.cons.append(node())
                    self.sql_stack.append(self.top)
                    self.top = self.top.cons[-1]
                    self.top.column = token
                    self.state = 7
                if self.next_negation:
                    self.top.negation = True
                    self.next_negation = False
        # ... ( ...
        elif self.state == 5:
            assert label == 5
            # eg: ( [ <C> ]
            self.state = 4
            self.stack.append('C')
        # where table...
        elif self.state == 6:
            # eg: where table [ column ] -> operator
            assert label == 9
            self.state = 7
            self.top.column = token
        # where table.column...
        elif self.state == 7:
            # eg: where table.column [ operator ] -> value/<S>/null
            assert label == 0
            self.state = 8
            if token == 'is null' or token == 'is not null':
                self.state = 9
            if token == 'between' or token == 'not between':
                self.state = 14
            self.top.operation = token
        # where table.column op...
        elif self.state == 8:
            # eg: where table.column > [ value ]
            if label == 7 or label == 8:
                self.top.value = token
                self.top.constraint = Constraint()
                self.state = 9
            # eg: where table.column in [ <S> ]
            elif label == 5:
                self.state = 0
                if self.top.operation == 'in':
                    self.top.value = [self.top.table, self.top.column]
                    self.top.table = None
                    self.top.column = None
                    self.top.create_subgraph = True
                else:
                    self.top.value = node()
                    self.sql_stack.append(self.top)
                    self.top = self.top.value
                self.stack.append('S')
            # eg: where table.column > [ all ] -> <S>
            elif label == 4:
                self.state = 11
                self.top.operation += " " + token
            # eg: where table.column < [ table ] -> column
            elif label == 10:
                self.state = 10
                self.top.value = token
            else:
                raise (AssertionError("Exception Occurred!"))
        # where <C>...
        elif self.state == 9:
            # eg: where <C> [ group by ] -> table
            if label == 2:
                print(token)
                assert len(self.sql_stack) == 1
                self.state = 12
            # eg: where <C> [ and ] -> not/(<C>)/table
            elif label == 3:
                self.state = 5
                self.top.conjunction = token
            # eg: where <C> [ <EOT> ]
            elif label == 13:
                if not self.stack:
                    self.state = END
                    self.top = self.sql_stack.pop(-1)
                else:
                    top = self.stack.pop(-1)
                    self.state = 9
                    self.top = self.sql_stack.pop(-1)
                    if isinstance(self.top, node):
                        if isinstance(self.top.value, node):
                            self.top = self.sql_stack.pop(-1)
            else:
                raise (AssertionError("Exception Occurred!"))
        # where table.column > table...
        elif self.state == 10:
            # eg: table.column > table [ column ] -> and/<EOT>/group by
            assert label == 9
            self.state = 9
            self.top.value += "." + token
        # table.column > all...
        elif self.state == 11:
            # eg: table.column > all [ <S> ]
            assert label == 5
            self.stack.append('S')
            self.top.value = node()
            self.top = self.top.value
            self.state = 0
        # ...group by...
        elif self.state == 12:
            assert label == 10
            # eg: group by [ table ] -> column
            self.state = 13
            self.sql_stack[0].group_by = token
        elif self.state == 13:
            assert label == 9
            # eg: group by table [ column ] -> <EOT>
            self.state = 16
            self.sql_stack[0].group_by += "." + token
        elif self.state == 14:
            # eg: between [ value ] -> value
            assert label == 7 or label == 8
            self.state = 15
            self.top.value = [token]
        elif self.state == 15:
            # eg: between value and [ value ]
            assert label == 7 or label == 8
            self.state = 9
            self.top.value.append(token)
        elif self.state == 16:
            # eg: group by table.column [ <EOT> ]
            assert label == 13
            self.top = self.sql_stack.pop(-1)
            self.state = END
        elif self.state == 17:
            # eg: select table.column from [ table ]
            if label == 5:
                self.stack.append('C')
                self.sql_stack.append(self.top)
                self.top.constraint = Constraint()
                self.top = self.top.constraint
                self.state = 4
            elif label == 10:
                if self.top.table:
                    self.top.additional_table.append(token)
                else:
                    self.top.table = token

        elif self.state == END:
            assert label == 14
Exemple #6
0
 def initialize(self):
     self.graph = node()
     self.top = self.graph
     self.state = 0
Exemple #7
0
        def draw_fsm_circles():
            g = self.gve

            #figure out where centroids should be
            coords = []
            [coords.append([n.x, n.y]) for n in g.nodes]
            coords = np.matrix(coords).T
            centroid = np.median(coords, 1)
            if len(coords) == 0:
                return

            #calculate where radii should be
            radius = np.max(np.power(np.sum(np.power((coords - centroid), 2), 0), .5)) + gm.GraphModel.NODE_RADIUS*2
            radius = max(radius, 200.)
            container_style = g.styles.graph_circle
            container_stroke = container_style.stroke
            
            ##
            #Draw fsm_stack
            stack = copy.copy(properties_dict['fsm_stack'])
            #stack.reverse()
            #smallest_radii = radius
            largest_radii = radius + len(stack) * self.radii_increment
            color = self.fsm_start_color
            if len(stack) > 0:
                color_incre = (self.fsm_start_color - self.fsm_end_color) / len(stack)

            #draw stack
            for el in stack:
                #smallest_radii = smallest_radii + self.radii_increment
                name = el.model.document.get_name()#el.document.get_name()

                #Draw node
                stack_node = graph.node(g, radius = largest_radii, id = name)
                stack_node.x, stack_node.y = centroid[0,0], centroid[1,0]
                el.graph_node = stack_node
                container_style.fill = self.context.color(color, color, color, 1.)
                container_style.stroke = self.context.color(self.fsm_stroke_color, self.fsm_stroke_color, 1.)
                gs.node(container_style, stack_node, g.alpha)

                #Draw label
                node_label_node_ = graph.node(g, radius = largest_radii, id = name)
                node_label_node_.x, node_label_node_.y = centroid[0,0], centroid[1,0] - largest_radii
                gs.node_label(container_style, node_label_node_, g.alpha)

                color -= color_incre
                largest_radii -= self.radii_increment

            ##
            #Draw node

            #Draw node circle
            graph_name_node = graph.node(g, radius=radius, id = properties_dict['name'])
            graph_name_node.x, graph_name_node.y = centroid[0,0], centroid[1,0]
            self.fsm_current_context_node = graph_name_node
            container_style.fill = self.context.color(self.fsm_end_color, self.fsm_end_color, self.fsm_end_color, 1.)
            container_style.stroke = container_stroke
            gs.node(container_style, graph_name_node, g.alpha)

            #draw node label
            node_label_node = graph.node(g, radius=radius, id = properties_dict['name'])
            node_label_node.x, node_label_node.y = centroid[0,0], centroid[1,0] - radius
            gs.node_label(container_style, node_label_node, g.alpha)
    "flow",
    "start",
    flow_id,
    {"title": "After the Apocalypse", "description": "Post-apocalypse character generation in a Fallout vein"},
)

questions = [
    ("q1", "When the bombs started falling where were your parents?"),
    ("s1", "Which section did your parents work in?"),
    ("w1", "Were they affected by the radiation from the fallout?"),
]

for q_id, question in questions:

    graph.create_unique_node("questions", flow_id, q_id, {"id": q_id, "text": question})
    graph.link(flow_node, graph.node("questions", flow_id, q_id), "Question")

answers = [
    ("q1a1", "They had a place in the shelters"),
    ("q1a2", "They had nowhere to run"),
    ("s1a1", "Administration"),
    ("s1a2", "Security"),
    ("s1a3", "Science"),
    ("w1a1", "I don't know whether they were tough or lucky but no"),
    ("w1a2", "Why do you think I look this way?"),
]

for a_id, answer in answers:
    graph.create_unique_node("answers", flow_id, a_id, {"id": a_id, "text": answer})

rewards = [
Exemple #9
0
def handle_where_clause(head_node, sql_str):
    new_constraint = Constraint()
    cons = []
    negation_flag = False
    next_negation = False
    i = 0
    word = sql_str[i]
    while (word != ')' and word != ';') or negation_flag:
        # nesting
        if word == '(':
            nesting_bound = get_right_bracket(sql_str[i:])
            nested_constraint = handle_where_clause(
                head_node, sql_str[i + 1:i + nesting_bound])
            cons.append(nested_constraint)
            i += nesting_bound
        # negation
        elif word == 'not':
            negation_flag = True
            negation_bound = i + 1 + get_right_bracket(sql_str[i + 1:])
            nested_constraint = handle_where_clause(
                head_node, sql_str[i + 2:negation_bound])
            nested_constraint.negation = True
            cons.append(nested_constraint)
            cur_len = len(cons)
            i = negation_bound
        # conjunction
        elif word == 'AND' or word == 'OR':
            new_constraint.conjunction = word
        # where column op value
        elif sql_str[i + 1] in operation_list:
            # value is a single word
            all_flag = False
            op = sql_str[i + 1]
            if '.' in sql_str[i]:
                table = sql_str[i].split('.')[0]
                col = sql_str[i].split('.')[1]
            else:
                table = None
                col = sql_str[i]
            if sql_str[i + 2] == 'ALL':
                op += " all"
                i += 1
            elif sql_str[i + 2] == 'ANY':
                op += " any"
                i += 1
            if sql_str[i + 2] != '(':
                val = sql_str[i + 2]
                cons.append(node(table, col, op, val))
                i += 2
            # value is a subgraph or something else
            else:
                index = get_right_bracket(sql_str[i + 2:])
                if sql_str[i + 3] != 'SELECT':
                    raise exception("value in a bracket but not a subgraph!")
                new_node = construct_graph(sql_str[i + 3:i + 2 + index])
                cons.append(node(table, col, op, new_node))
                i = i + 2 + index
        # where column between value and value
        elif sql_str[i + 1] == 'BETWEEN':
            assert sql_str[i + 3] == 'AND'
            if '.' in sql_str[i]:
                cons.append(node(sql_str[i].split('.')[0], sql_str[i].split('.')[1], 'between', [sql_str[i+2], \
                                                                                                 sql_str[i+4]]))
            else:
                cons.append(
                    node(None, sql_str[i], 'between',
                         [sql_str[i + 2], sql_str[i + 4]]))
            i += 4
        # where column is not null/is null
        elif sql_str[i + 1] == 'IS' or sql_str[i + 1] == 'is':
            if sql_str[i + 2] == 'NULL':
                cons.append(
                    node(sql_str[i].split('.')[0], sql_str[i].split('.')[1],
                         'is null', None))
                i += 2
            else:
                assert sql_str[i + 2] == 'NOT NULL'
                cons.append(
                    node(sql_str[i].split('.')[0], sql_str[i].split('.')[1],
                         'is not null', None))
                i += 2
        # where column in (subgraph)
        elif sql_str[i + 1] == 'IN':
            assert sql_str[i + 2] == '('
            bracket_index = get_right_bracket(sql_str[i + 2:])
            new_node = construct_graph(sql_str[i + 3:i + 2 + bracket_index])
            new_node.value = [
                sql_str[i].split('.')[0], sql_str[i].split('.')[1]
            ]
            if next_negation:
                new_node.negation = True
                next_negation = False
            cons.append(new_node)
            i = i + 2 + bracket_index
        elif sql_str[i + 1] == 'NOT':
            assert (sql_str[i + 2] == 'BETWEEN'
                    and sql_str[i + 4] == 'AND') or sql_str[i + 2] == 'IN'
            if sql_str[i + 2] == 'BETWEEN':
                if '.' in sql_str[i]:
                    cons.append(node(sql_str[i].split('.')[0], sql_str[i].split('.')[1], 'not between', [sql_str[i+3], \
                                                                                                 sql_str[i+5]]))
                else:
                    cons.append(
                        node(None, sql_str[i], 'not between',
                             [sql_str[i + 3], sql_str[i + 5]]))
                i += 5
            else:
                next_negation = True
                sql_str[i + 1] = sql_str[i]
        elif sql_str[i + 1] == 'not':
            assert sql_str[i + 2] == 'IN'
            next_negation = True
            # pass the table/column to the next position.
            sql_str[i + 1] = sql_str[i]

        elif word == 'GROUP' and sql_str[i + 1] == 'BY':
            head_node.group_by = sql_str[i + 2]
            i += 2
        else:
            raise AssertionError("else occured!")
        i += 1
        """
        if negation_flag:
            # to ensure not assigning negation to the previously existing constraint
            if cons and len(cons) > cur_len:
                cons[-1].negation = True
            if i > negation_bound:
                negation_flag = False
        """
        if i >= len(sql_str):
            break
        word = sql_str[i]
    new_constraint.cons = cons
    return new_constraint
Exemple #10
0
def construct_graph(sql):
    head_node = node()
    sql_str = sql
    i = 0
    selecting_column = 1
    while i < len(sql_str):
        word = sql_str[i]
        if word == 'SELECT':
            head_node.create_subgraph = True
            # select distinct table.column from table
            if sql_str[i + 1] == 'DISTINCT':
                head_node.column = sql_str[i + 2].split('.')[1]
                head_node.table = sql_str[i + 2].split('.')[0]
                head_node.distinct = True
                i += 3
            # select aggregate(table.column) from table
            elif sql_str[i + 1] in aggregator_list:
                assert sql_str[i + 2] == '('
                for j, word in enumerate(sql_str[i + 2:]):
                    if word == ')':
                        record = j
                        break
                if sql_str[i + 3] == 'DISTINCT':
                    head_node.distinct = True
                    sql_str[i + 3] = sql_str[i + 4]
                if '.' in sql_str[i + 3]:
                    head_node.column = sql_str[i + 3].split('.')[1]
                    head_node.table = sql_str[i + 3].split('.')[0]
                else:
                    head_node.column = sql_str[i + 3]
                head_node.aggregate = sql_str[i + 1]
                i += 4
                if head_node.distinct:
                    i += 1
            # select table.column from table
            else:
                head_node.column = sql_str[i + 1].split('.')[1]
                head_node.table = sql_str[i + 1].split('.')[0]
                i += 2
        elif word == ',':
            if selecting_column:
                if '.' in sql_str[i + 1]:
                    head_node.additional_col.append(sql_str[i + 1])
                    head_node.additional_agg.append("")
                # select col1, agg(col2) from table
                elif sql_str[i + 1] in aggregator_list:
                    assert sql_str[i + 2] == '(' and sql_str[i + 4] == ')'
                    head_node.additional_agg.append(sql_str[i + 1])
                    head_node.additional_col.append(sql_str[i + 3])
                    i += 2
                else:
                    raise AssertionError('multi-columns without table!')
            else:
                head_node.additional_table.append(sql_str[i + 1])
            i += 2
        elif word == 'FROM':
            head_node.table = sql_str[i + 1]
            selecting_column = 0
            i += 2
        elif word == 'WHERE':
            i += 1
            new_constraint = handle_where_clause(head_node, sql_str[i:])
            head_node.constraint = new_constraint
            break
        # group by table.column
        elif word == 'GROUP':
            assert sql_str[i + 1] == 'BY' and '.' in sql_str[i + 2]
            head_node.group_by = sql_str[i + 2]
            i += 3
        else:
            i += 1
    return head_node