summaryrefslogtreecommitdiff
path: root/freebase-api/freebase/fcl/mktype.py
blob: 30f8df78ba3157807e0d536583e4c26ab1fe4c03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# ==================================================================
# Copyright (c) 2007,2008,2009 Metaweb Technologies, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
#       copyright notice, this list of conditions and the following
#       disclaimer in the documentation and/or other materials provided
#       with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY METAWEB TECHNOLOGIES AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL METAWEB
# TECHNOLOGIES OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ====================================================================

import os, sys, re, time
from fbutil import *


def cmd_mkobj(fb, id, typeid='/common/topic', name=''):
    """create a new object with a given type  -- EXPERIMENTAL
    %prog mkobj new_id typeid name

    create a new object with type typeid at the given
    namespace location.

    if present, name gives the display name of the new object.
    
    """
    id = fb.absid(id)
    nsid, key = dirsplit(id)

    typeid = fb.absid(typeid)

    if name == '':
        name = key

    wq = { 'create': 'unless_exists',
           'id': None,
           'name': name,
           'type': typeid,
           'key':{
               'namespace': nsid,
               'value': key
           },
         }

    # TODO add included types

    r = fb.mss.mqlwrite(wq)
    print r.id,r.create

def cmd_mktype(fb, id, name=''):
    """create a new type  -- EXPERIMENTAL
    %prog mktype new_id name

    create a new object with type Type at the given
    namespace location.

    this doesn't create any type hints.

    if present, name gives the display name of the new property
    """
    id = fb.absid(id)

    nsid, key = dirsplit(id)

    if name == '':
        name = key

    wq = { 'create': 'unless_exists',
           'id': None,
           'name': name,
           'type': '/type/type',
           'key':{
               'namespace': nsid,
               'value': key
           },
         }

    r = fb.mss.mqlwrite(wq)
    print r.id,r.create

def mkprop(fb, typeid, key, name='', vtype=None, master_property=None):
    """helper to create a new property
    """
    if name == '':
        name = key

    wq = { 'create': 'unless_exists',
           'id': None,
           'type': '/type/property',
           'name': name,
           'schema': typeid,
           'key': {
               'namespace': typeid,
               'value': key
            }
         }

    if vtype is not None:
        wq['expected_type'] = vtype
    if master_property is not None:
        wq['master_property'] = master_property

    return fb.mss.mqlwrite(wq)


def cmd_mkprop(fb, id, name='', vtype=None, revkey=None, revname=''):
    """create a new property  -- EXPERIMENTAL
    %prog mkprop new_id [name] [expected_type] [reverse_property] [reverse_name]

    create a new object with type Property at the given
    location.  creates both the "schema" and "key" links
    for the property, but doesn't create any freebase property
    hints.

    if present, name gives the display name of the new property
    """
    id = fb.absid(id)
    if vtype is not None:
        vtype = fb.absid(vtype)

    typeid, key = dirsplit(id)


    r = mkprop(fb, typeid, key, name, vtype)

    # write the reverse property if specified

    print r.id, r.create

    if revkey is None:
        return

    assert vtype is not None

    rr = mkprop(fb, vtype, revkey, revname, typeid, id)
    print rr.id, rr.create


def cmd_publish_type(fb, typeid):
    """try to publish a freebase type for the client
    %prog publish_type typeid

    set /freebase/type_profile/published to the /freebase/type_status
    instance named 'Published'
  

    should also try to set the domain to some namespace that
    has type:/type/domain

    """
    id = fb.absid(typeid)

    w = {
        'id': id,
        '/freebase/type_profile/published': {
            'connect': 'insert',
            'type': '/freebase/type_status',
            'name': 'Published'
        }
    }
    r = fb.mss.mqlwrite(w)

    print r['/freebase/type_profile/published']['connect']