Example #1
0
 def get_w3c(self, bundle=None):
     if bundle is None:
         bundle = ProvBundle()
     w3c_record = ProvEntity(bundle, self.identifier, self.attributes)
     w3c_record.add_asserted_type(
         self._prov_type)  # self.__class__.__name__)
     return bundle.add_record(w3c_record)
Example #2
0
 def get_w3c(self, bundle=None):
     """get this element in the prov version which is an implementation of the W3C PROV-DM standard"""
     if bundle is None:
         bundle = ProvBundle()
     w3c_record = ProvEntity(bundle, self.identifier, self.attributes)
     w3c_record.add_asserted_type(self._prov_type)  # self.__class__.__name__)
     return bundle.add_record(w3c_record)
Example #3
0
def _create_module_dep(module: Module, document: provo.ProvBundle, suffix=""):
    document.entity("module{}{}".format(module.id, suffix),
                    [(provo.PROV_LABEL, module.name),
                     (provo.PROV_TYPE, "moduleDependency"),
                     ("version", module.version),
                     (provo.PROV_LOCATION, truncate(module.path)),
                     ("codeHash", module.code_hash),
                     ("id", module.id) if suffix else (None, None)])
Example #4
0
def _create_env_attr(document: provo.ProvBundle,
                     env_attr: EnvironmentAttr,
                     suffix=""):
    document.entity("environmentAttribute{}{}".format(env_attr.id, suffix),
                    [(provo.PROV_LABEL, env_attr.name),
                     (provo.PROV_VALUE, truncate(env_attr.value)),
                     (provo.PROV_TYPE, "environmentAttribute"),
                     ("id", env_attr.id) if suffix else (None, None)])
Example #5
0
def collections():
    g = ProvBundle()
    ex = Namespace("ex", "http://example.org/")

    c1 = g.collection(ex["c1"])
    e1 = g.entity("ex:e1")
    g.hadMember(c1, e1)

    return g
Example #6
0
def collections():
    g = ProvBundle()
    ex = Namespace('ex', 'http://example.org/')
    
    c1 = g.collection(ex['c1'])
    e1 = g.entity('ex:e1')
    g.hadMember(c1, e1)
    
    return g
Example #7
0
    def obj_create(self, bundle, request=None, **kwargs):
        prov_bundle = ProvBundle()
        prov_bundle._decode_JSON_container(bundle.data['content'])
        
        account = PDBundle.create(bundle.data['rec_id'], bundle.data['asserter'], request.user)
        account.save_bundle(prov_bundle)

        bundle.obj = account
        return bundle
Example #8
0
def long_literals():
    g = ProvBundle()

    long_uri = "http://Lorem.ipsum/dolor/sit/amet/consectetur/adipiscing/elit/Quisque/vel/sollicitudin/felis/nec/venenatis/massa/Aenean/lectus/arcu/sagittis/sit/amet/nisl/nec/varius/eleifend/sem/In/hac/habitasse/platea/dictumst/Aliquam/eget/fermentum/enim/Curabitur/auctor/elit/non/ipsum/interdum/at/orci/aliquam/"
    ex = Namespace('ex', long_uri)
    g.add_namespace(ex)

    g.entity('ex:e1', {'prov:label': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec pellentesque luctus nulla vel ullamcorper. Donec sit amet ligula sit amet lorem pretium rhoncus vel vel lorem. Sed at consequat metus, eget eleifend massa. Fusce a facilisis turpis. Lorem volutpat.'})

    return g
Example #9
0
class BundleForm(Form):
    ''' Form for creating a Bundle '''
    
    rec_id = forms.CharField(label=('Bundle ID'))
    public = forms.BooleanField(label=('Public'), required = False)
    submission = forms.FileField(label=('Original File'), required = False)
    license = LicenseMultipleChoiceField(License.objects, widget=CheckboxSelectMultiple, required=False)
    url = forms.URLField(label='URL to the bundle file:', required=False)
    content = forms.CharField(label=('Content (in JSON format)'), widget=Textarea(attrs={'class': 'span7'}), required=False)
    
    def clean(self):
        self.bundle = ProvBundle()
        ''' Try to parse content or download and parse URL - one at least needed'''
        if self.cleaned_data['content']:
            try:
                self.bundle._decode_JSON_container(loads(self.cleaned_data['content']))
            except ValueError:
                raise forms.ValidationError(u'Wrong syntax in the JSON content.')
        elif self.cleaned_data['url']:
            try:
                source = urlopen(self.cleaned_data['url'], timeout=5)
                url_content = source.read()
                source.close()
            except URLError:
                raise forms.ValidationError(u'There was a problem accessing the URL.')
            try:
                self.bundle._decode_JSON_container(loads(url_content))
            except ValueError:
                raise forms.ValidationError(u'Wrong syntax in the JSON content at the URL.')
        else:
            raise forms.ValidationError(u'No content or URL provided.')
        return self.cleaned_data
    
    def save(self, owner, commit=True):
        if self.errors:
            raise ValueError("The %s could not be %s because the data didn't"
                         " validate." % ('Container', 'created'))
            
        container = Container.create(self.cleaned_data['rec_id'], self.bundle, owner, self.cleaned_data['public'])
        save = False
        
        if 'submission' in self.files:
            file_sub = self.files['submission']
            sub = Submission.objects.create()
            sub.content.save(sub.timestamp.strftime('%Y-%m-%d%H-%M-%S')+file_sub._name, file_sub)
            container.submission = sub
            save = True
            
        for l in self.cleaned_data['license']:
            container.license.add(l)
            save = True
            
        if save:
            container.save()
        return container
def get_unit_agent(prov_bundle: ProvBundle, unit_id: int,
                   unit_specific_type: str) -> ProvAgent:
    unit_agent_id = ns_unit[str(unit_id)]
    unit_records = prov_bundle.get_record(unit_agent_id)
    attributes = [
        (PROV_TYPE, ns_type["Ward"]),
    ]
    if unit_specific_type is not None:
        attributes.append((PROV_TYPE, ns_type[unit_specific_type]))
    return (unit_records[0] if unit_records else prov_bundle.agent(
        unit_agent_id, attributes))
Example #11
0
def _create_file_access(document: provo.ProvBundle,
                        f_access: FileAccess,
                        suffix=""):
    document.activity("fileAccess{}{}".format(f_access.id, suffix), None, None,
                      [(provo.PROV_LOCATION, f_access.name),
                       (provo.PROV_TYPE, f_access.mode),
                       (provo.PROV_ATTR_TIME, f_access.timestamp),
                       ("buffering", f_access.buffering),
                       ("contentHashBefore", f_access.content_hash_before),
                       ("contentHashAfter", f_access.content_hash_after),
                       ("id", f_access.id) if suffix else (None, None)])
Example #12
0
def decode_json_document(content, document):
    bundles = dict()
    if 'bundle' in content:
        bundles = content['bundle']
        del content['bundle']

    decode_json_container(content, document)

    for bundle_id, bundle_content in bundles.items():
        bundle = ProvBundle(document=document)
        decode_json_container(bundle_content, bundle)
        document.add_bundle(bundle, bundle.valid_qualified_name(bundle_id))
def get_staff_agent(prov_bundle: ProvBundle,
                    care_giver: db.CareGiver) -> ProvAgent:
    staff_id = ns_staff[str(care_giver.cgid)]
    staff_records = prov_bundle.get_record(staff_id)
    return (staff_records[0] if staff_records else prov_bundle.agent(
        staff_id,
        {
            "prov:type": PROV["Person"],
            "prov:label": care_giver.label,
            ns_attrs["description"]: care_giver.description,
        },
    ))
Example #14
0
def decode_json_document(content, document):
    bundles = dict()
    if 'bundle' in content:
        bundles = content['bundle']
        del content['bundle']

    decode_json_container(content, document)

    for bundle_id, bundle_content in bundles.items():
        bundle = ProvBundle(document=document)
        decode_json_container(bundle_content, bundle)
        document.add_bundle(bundle, bundle.valid_qualified_name(bundle_id))
Example #15
0
def create_bundle_mop_login(user_id, session_key):
    '''User id 2 logs into the system'''
    bundle_id = 'b:%s/login' % session_key
    b = ProvBundle(namespaces=DEFAULT_NAMESPACES)
    u = b.agent('mopuser:%d' % user_id)
    ag = b.agent('mopuser:%d/%s' % (user_id, session_key))
    now = datetime.datetime.now()
    a = b.activity('log:%d/login/%s' % (user_id, session_key), now, now, other_attributes=[('prov:type', 'act:MopAccountLogin')])
    b.wasAssociatedWith(a, u)
    b.wasGeneratedBy(ag, a)
    b.specializationOf(ag, u)
    return bundle_id, b
Example #16
0
def export(trial: Trial, document: provo.ProvBundle):
    print_msg("  Exporting file accesses")

    for f_access in trial.file_accesses:  # type: FileAccess
        _create_file_access(document, f_access)

        if document.get_record("functionActivation{}".format(
                f_access.function_activation_id)):
            document.wasInformedBy(
                "fileAccess{}".format(f_access.id),
                "functionActivation{}".format(f_access.function_activation_id),
                "fileAcc{}ByFuncAct{}".format(f_access.id,
                                              f_access.function_activation_id),
                [(provo.PROV_TYPE, "fileAccess")])
Example #17
0
    def get_w3c(self, bundle=None):
        """get this relation in the prov version which is an implementation of the W3C PROV-DM standard"""
        if bundle is None:
            bundle = ProvBundle()
        attribute = self.extra_attributes
        relation_formal_attribute = self.formal_attributes[0:2]

        w3c_record = ProvInfluence(bundle, self.identifier, attribute)
        namespaces = [list(i) for i in w3c_record.formal_attributes]
        for i in range(0, 2):
            namespaces[i][1] = relation_formal_attribute[i][1]
        w3c_record.add_attributes(namespaces)
        w3c_record.add_asserted_type(
            self._prov_type)  # self.__class__.__name__)
        return bundle.add_record(w3c_record)
Example #18
0
    def test_add_bundle_document(self):
        d1 = self.document_1()
        d2 = self.document_2()

        def sub_test_1():
            d1.add_bundle(d2)
        self.assertRaises(ProvException, sub_test_1)

        ex2_b2 = d2.valid_qualified_name('ex:b2')
        d1.add_bundle(d2, 'ex:b2')
        self.assertEqual(ex2_b2, first(d1.bundles).identifier)
        self.assertNotIn(d2, d1.bundles)
        b2 = ProvBundle()
        b2.update(d2)
        self.assertIn(b2, d1.bundles)
    def test_add_bundle_document(self):
        d1 = self.document_1()
        d2 = self.document_2()

        def sub_test_1():
            d1.add_bundle(d2)

        self.assertRaises(ProvException, sub_test_1)

        ex2_b2 = d2.valid_qualified_name('ex:b2')
        d1.add_bundle(d2, 'ex:b2')
        self.assertEqual(ex2_b2, first(d1.bundles).identifier)
        self.assertNotIn(d2, d1.bundles)
        b2 = ProvBundle()
        b2.update(d2)
        self.assertIn(b2, d1.bundles)
Example #20
0
    def get_document(self, doc_id, format=None, flattened=False, view=None):
        """Returns a ProvBundle object of the document with the ID provided or raises ApiNotFoundError"""

        extension = format if format is not None else 'json'
        view = "/views/%s" % view if view in ['data', 'process', 'responsibility'] else ""
        url = "documents/%d%s%s.%s" % (doc_id, "/flattened" if flattened else "", view, extension)
        response = self.request(url, raw=True)

        if format is None:
            # Try to decode it as a ProvBundle
            prov_document = ProvBundle()
            prov_document._decode_JSON_container(json.loads(response))
            return prov_document
        else:
            # return the raw response
            return response
Example #21
0
 def test_merging_records_json(self):
     test_json = """
     {
         "entity": {
             "e1": [
                 {"prov:label": "First instance of e1"},
                 {"prov:label": "Second instance of e1"}
             ]
         },
         "activity": {
             "a1": [
                 {"prov:label": "An activity with no time (yet)"},
                 {"prov:startTime": "2011-11-16T16:05:00"},
                 {"prov:endTime": "2011-11-16T16:06:00"}
             ]
         }
     }"""
     g = ProvBundle.from_provjson(test_json)
     e1 = g.get_record("e1")
     self.assertEqual(
         len(e1.get_attribute("prov:label")), 2, "e1 was not merged correctly, expecting two prov:label attributes"
     )
     a1 = g.get_record("a1")
     self.assertIsNotNone(a1.get_startTime(), "a1 was not merged correctly, expecting startTime set.")
     self.assertIsNotNone(a1.get_endTime(), "a1 was not merged correctly, expecting startTime set.")
     self.assertEqual(
         len(a1.get_attribute("prov:label")), 1, "a1 was not merged correctly, expecting one prov:label attribute"
     )
Example #22
0
 def setUp(self):
     logging.debug('Setting up user and checking the URL file...')
     self.check_u = User.objects.get_or_create(username='******')
     self.user = self.check_u[0]
     self.check_k = ApiKey.objects.get_or_create(user=self.user)
     self.key = self.check_k[0]
     self.auth = 'ApiKey' + self.user.username + ':' + self.key.key
     self.check_u = self.check_u[1]
     self.check_k = self.check_k[1]
     self.url = 'http://users.ecs.soton.ac.uk/ab9g10/test.json'
     source = urlopen(self.url)
     url_content = ProvBundle()
     url_content._decode_JSON_container(json.loads(source.read()))
     source.close()
     self.content = PDBundle.create('url_test')
     self.content.save_bundle(url_content)
     self.content = self.content.get_prov_bundle()
Example #23
0
def create_bundle_mop_logout(user_id, session_key):
    bundle_id = 'b:%s/logout' % session_key
    b = ProvBundle(namespaces=DEFAULT_NAMESPACES)
    b.add_namespace('ns', b.valid_identifier(bundle_id + '/').get_uri())

    ag = b.agent('mopuser:%d/%s' % (user_id, session_key))
    now = datetime.datetime.now()
    a = b.activity('log:%d/login/%s' % (user_id, session_key), now, now, other_attributes=[('prov:type', 'act:MopAccountLogout')])
    b.wasInvalidatedBy(ag, a)  # This user+session no longer exists after this
    return bundle_id, b
Example #24
0
def datatypes():
    g = ProvBundle()
    ex = Namespace('ex', 'http://example.org/')
    g.add_namespace(ex)

    attributes = {'ex:int': 100,
                  'ex:float': 100.123456,
                  'ex:long': 123456789000,
                  'ex:bool': True,
                  'ex:str': 'Some string',
                  'ex:unicode': u'Some unicode string with accents: Huỳnh Trung Đông',
                  'ex:timedate': datetime.datetime(2012, 12, 12, 14, 7, 48)}
    multiline = """Line1
    Line2
Line3"""
    attributes['ex:multi-line'] = multiline
    g.entity('ex:e1', attributes)
    return g
Example #25
0
def create_bundle_cron_register(cron):
    '''An cron user account is created'''
    bundle_id = 'b:registration/%d' % cron.id
    b = ProvBundle(namespaces=DEFAULT_NAMESPACES)
    s = b.agent("server:1")
    u = b.agent('cronuser:%d' % cron.id, [('foaf:name', cron.user.username)])
    now = datetime.datetime.now()
    a = b.activity('log:%d/register' % cron.id, now, now, other_attributes=[('prov:type', 'act:CronAccountRegistration')])
    b.wasGeneratedBy(u, a)
    b.wasAssociatedWith(a, s)
    return bundle_id, b
Example #26
0
def create_bundle_mop_register(cron, mop, session_key):
    '''An mop user account is created'''
    bundle_id = 'b:registration/%d' % mop.id
    b = ProvBundle(namespaces=DEFAULT_NAMESPACES)
    s = b.agent('cronuser:%d/%s' % (cron.id, session_key))
    u = b.agent('mopuser:%d' % mop.id, [('foaf:name', mop.user.username)])
    now = datetime.datetime.now()
    a = b.activity('log:%d/register' % mop.id, now, now, other_attributes=[('prov:type', 'act:MopAccountRegistration')])
    b.wasGeneratedBy(u, a)
    b.wasAssociatedWith(a, s)
    return bundle_id, b
Example #27
0
def export(trial: Trial, document: provo.ProvBundle):
    print_msg("  Exporting module dependencies")

    collection = document.collection("moduleDependencies")

    for module in trial.modules:  # type: Module
        _create_module_dep(module, document)

    for module in trial.modules:  # type: Module
        collection.hadMember("module{}".format(module.id))
Example #28
0
def export(trial: Trial, document: provo.ProvBundle):
    print_msg("  Exporting environment conditions")

    collection = document.collection("environmentAttributes")

    for env_attr in trial.environment_attrs:  # type: EnvironmentAttr
        _create_env_attr(document, env_attr)

    for env_attr in trial.environment_attrs:  # type: EnvironmentAttr
        collection.hadMember("environmentAttribute{}".format(env_attr.id))
Example #29
0
def datatypes():
    g = ProvBundle()
    ex = Namespace("ex", "http://example.org/")
    g.add_namespace(ex)

    attributes = {
        "ex:int": 100,
        "ex:float": 100.123456,
        "ex:long": 123456789000,
        "ex:bool": True,
        "ex:str": "Some string",
        "ex:unicode": u"Some unicode string with accents: Huỳnh Trung Đông",
        "ex:timedate": datetime.datetime(2012, 12, 12, 14, 7, 48),
    }
    multiline = """Line1
    Line2
Line3"""
    attributes["ex:multi-line"] = multiline
    g.entity("ex:e1", attributes)
    return g
def get_process_entity(prov_bundle: ProvBundle, item: db.Item) -> ProvEntity:
    try:
        entity_process = procedure_entities[item.itemid]
        if not prov_bundle.get_record(entity_process.identifier):
            # the entity was created in another admission
            prov_bundle.add_record(entity_process)
        return entity_process
    except KeyError:
        entity_id = ns_process[str(item.itemid)]
        entity_process = prov_bundle.entity(
            entity_id,
            {
                "prov:type": ns_type["Process"],
                "prov:label": item.label,
                ns_attrs["abbreviation"]: item.abbreviation,
                ns_attrs["category"]: item.category,
                ns_attrs["dbsource"]: item.dbsource,
            },
        )
        procedure_entities[item.itemid] = entity_process
        return entity_process
Example #31
0
def create(request):
    if request.method == 'POST':
        if 'convert' in request.POST or 'randomize' in request.POST:
            graphml_str = request.POST["graphml"]
            filename = request.POST["filename"]
            provn_str, output = convert_graphml_string(graphml_str)
            output_list = output.splitlines()
            output_list.sort()
 
            valid, validation_url = validate(provn_str)
            if valid:
                json_str = provn_str.get_provjson()
            else:
                json_str={}
                
            if 'randomize' in request.POST:
                json_str = json.dumps(get_random_graph(json.loads(json_str)))
            
            return render_to_response('provmanager/create.html', {"output_list":output_list, "filename":filename, "graphml_str": graphml_str, "json_str": json_str, "valid":valid, "validation_url":validation_url}, context_instance=RequestContext(request))

        elif 'save' in request.POST:
            graphml_str = request.POST["graphml"]
            provn_str, output = convert_graphml_string(graphml_str)
            valid, validation_url = validate(provn_str)
            if valid:
                name = request.POST["filename"]
                if name=="":
                    name = "PLEASE ENTER A NAME"
                
                inconsistencies_list, clean_graph = get_inconsistencies(json.loads(provn_str.get_provjson()))

                if inconsistencies_list:
                    attribute1 = json.dumps(inconsistencies_list[0])
                    attribute2 = json.dumps(inconsistencies_list[1])
                    type = Provenance.TYPE_CRON
                else:
                    attribute1 = None
                    attribute2 = None
                    #TODO: is there a better way to check the type?
                    #if there are no inconsistencies, then we assume it is a MOP_TEMPLATE provenance document
                    type = Provenance.TYPE_MOP_TEMPLATE
  
                bundle = ProvBundle.from_provjson(json.dumps(clean_graph))
                
                store_id = API.submit_document(bundle, name, public=False)

                provenance = Provenance(name=name, store_id=store_id, attribute1=attribute1, attribute2=attribute2, type=type)
                provenance.save()
                return HttpResponseRedirect(reverse('provmanager_index'))
            
            
    return render_to_response('provmanager/create.html', context_instance=RequestContext(request))
Example #32
0
    def _create_bundle(self, version):
        """
        Initialise NIDM-Results bundle.
        """
        # *** Bundle entity
        if not hasattr(self, 'bundle_ent'):
            self.bundle_ent = NIDMResultsBundle(nidm_version=version['num'])

        self.bundle = ProvBundle(identifier=self.bundle_ent.id)

        self.bundle_ent.export(self.version, self.export_dir)

        # # provn export
        # self.bundle = ProvBundle(identifier=bundle_id)

        self.doc.entity(self.bundle_ent.id,
                        other_attributes=self.bundle_ent.attributes)

        # *** NIDM-Results Export Activity
        if version['num'] not in ["1.0.0", "1.1.0"]:
            if not hasattr(self, 'export_act'):
                self.export_act = NIDMResultsExport()
            self.export_act.export(self.version, self.export_dir)
            # self.doc.update(self.export_act.p)
            self.doc.activity(self.export_act.id,
                              other_attributes=self.export_act.attributes)

        # *** bundle was Generated by NIDM-Results Export Activity
        if not hasattr(self, 'export_time'):
            self.export_time = str(datetime.datetime.now().time())

        if version['num'] in ["1.0.0", "1.1.0"]:
            self.doc.wasGeneratedBy(entity=self.bundle_ent.id,
                                    time=self.export_time)
        else:
            # provn
            self.doc.wasGeneratedBy(entity=self.bundle_ent.id,
                                    activity=self.export_act.id,
                                    time=self.export_time)

        # *** NIDM-Results Exporter (Software Agent)
        if version['num'] not in ["1.0.0", "1.1.0"]:
            if not hasattr(self, 'exporter'):
                self.exporter = self._get_exporter()
            self.exporter.export(self.version, self.export_dir)
            # self.doc.update(self.exporter.p)
            self.doc.agent(self.exporter.id,
                           other_attributes=self.exporter.attributes)

            self.doc.wasAssociatedWith(self.export_act.id, self.exporter.id)
Example #33
0
 def testAllExamples(self):
     num_graphs = len(examples.tests)
     logger.info('Testing %d provenance graphs' % num_graphs)
     counter = 0
     for name, graph in examples.tests:
         counter += 1
         logger.info('%d. Testing the %s example' % (counter, name))
         g1 = graph()
         logger.debug('Original graph in PROV-N\n%s' % g1.get_provn())
         json_str = g1.get_provjson(indent=4)
         logger.debug('Original graph in PROV-JSON\n%s' % json_str)
         g2 = ProvBundle.from_provjson(json_str)
         logger.debug('Graph decoded from PROV-JSON\n%s' % g2.get_provn())
         self.assertEqual(g1, g2, 'Round-trip JSON encoding/decoding failed:  %s.' % name)
Example #34
0
 def testLoadAllJSON(self):
     json_path = os.path.dirname(os.path.abspath(__file__)) + '/json/'
     filenames = os.listdir(json_path)
     fails = []
     for filename in filenames:
         if filename.endswith('.json'):
             with open(json_path + filename) as json_file:
                 try:
                     g1 = json.load(json_file, cls=ProvBundle.JSONDecoder)
                     json_str = g1.get_provjson(indent=4)
                     g2 = ProvBundle.from_provjson(json_str)
                     self.assertEqual(g1, g2, 'Round-trip JSON encoding/decoding failed:  %s.' % filename)
                 except:
                     fails.append(filename)
     self.assertFalse(fails, 'Failed to load %d JSON files (%s)' % (len(fails), ', '.join(fails)))
 def __init__(self, prov_bundle: ProvBundle, db_patient):
     self.prov_bundle = prov_bundle  # type: ProvBundle
     self.subject_id = db_patient.subject_id  # type: int
     patient_id = ns_patients[str(self.subject_id)]
     attributes = [
         (PROV_TYPE, PROV["Person"]),
         (ns_type["gender"], db_patient.gender),
         (ns_type["dob"], db_patient.dob),
     ]
     if db_patient.dod is not None:
         attributes.append((ns_type["dod"], db_patient.dod))
     self.prov_entity = prov_bundle.entity(patient_id, attributes)
     self.specializations = dict()
     self.versions = dict(
     )  # type: MutableMapping[datetime.datetime, ProvEntity]
Example #36
0
 def test_datetime_with_tz(self):
     """ test that timezone is taken in to account while parsing json"""
     test_json = """
     {
         "activity": {
             "a1": [
                 {"prov:label": "An activity with timezone"},
                 {"prov:startTime": "2011-11-16T16:05:00.123456+03:00"},
                 {"prov:endTime": "2011-11-16T16:06:00.654321"}
             ]
         }
     }"""
     g = ProvBundle.from_provjson(test_json)
     a1 = g.get_record("a1")
     self.assertEqual(
         a1.get_startTime().isoformat(), "2011-11-16T16:05:00.123456+03:00", "timezone is not set correctly"
     )
     self.assertEqual(a1.get_endTime().isoformat(), "2011-11-16T16:06:00.654321", "timezone is not set correctly")
Example #37
0
def randomize_document(mopDocument):
    #bundle = API.get_document(task.provenance.store_id)
    #TODO randomize provn/json
    json_graph = getProvJson(mopDocument.provenance)
    random_graph = get_random_graph(json_graph)
    inconsistencies_list, clean_graph = get_inconsistencies(random_graph)

    attribute1 = json.dumps(inconsistencies_list[0])
    attribute2 = json.dumps(inconsistencies_list[1])

    bundle = ProvBundle.from_provjson(json.dumps(clean_graph))
    name = "%s (randomized)" % (mopDocument.provenance.name)
    store_id = API.submit_document(bundle, name, public=False)
    provenance = Provenance(name=name, store_id=store_id, attribute1=attribute1, attribute2=attribute2, type=Provenance.TYPE_MOP_INSTANCE)
    provenance.save()
        
    randomizedDocument = RandomizedDocument.objects.create(mopDocument=mopDocument, provenance=provenance)
    return randomizedDocument
Example #38
0
    def _create_bundle(self, version):
        """ 
        Initialise NIDM-Results bundle.
        """
        software_lc = self.software.name.lower()
        software_uc = self.software.name.upper()

        bundle_id = NIIRI[str(uuid.uuid4())]
        self.bundle = ProvBundle(identifier=bundle_id)

        self.doc.entity(bundle_id,
                        other_attributes=((
                            PROV['type'],
                            PROV['Bundle'],
                        ), (PROV['label'], software_uc + " Results"),
                                          (NIDM['objectModel'],
                                           NIDM[software_uc + 'Results']),
                                          (NIDM['version'], version)))

        self.doc.wasGeneratedBy(NIIRI[software_lc + '_results_id'],
                                time=str(datetime.datetime.now().time()))
Example #39
0
 def clean(self):
     self.bundle = ProvBundle()
     ''' Try to parse content or download and parse URL - one at least needed'''
     if self.cleaned_data['content']:
         try:
             self.bundle._decode_JSON_container(loads(self.cleaned_data['content']))
         except ValueError:
             raise forms.ValidationError(u'Wrong syntax in the JSON content.')
     elif self.cleaned_data['url']:
         try:
             source = urlopen(self.cleaned_data['url'], timeout=5)
             url_content = source.read()
             source.close()
         except URLError:
             raise forms.ValidationError(u'There was a problem accessing the URL.')
         try:
             self.bundle._decode_JSON_container(loads(url_content))
         except ValueError:
             raise forms.ValidationError(u'Wrong syntax in the JSON content at the URL.')
     else:
         raise forms.ValidationError(u'No content or URL provided.')
     return self.cleaned_data
    def test_add_bundle_simple(self):
        d1 = self.document_1()
        b0 = self.bundle_0()

        def sub_test_1():
            d1.add_bundle(b0)

        self.assertRaises(ProvException, sub_test_1)
        self.assertFalse(d1.has_bundles())

        d1.add_bundle(b0, 'ex:b0')
        self.assertTrue(d1.has_bundles())
        self.assertIn(b0, d1.bundles)

        def sub_test_2():
            ex2_b0 = b0.identifier
            d1.add_bundle(ProvBundle(identifier=ex2_b0))

        self.assertRaises(ProvException, sub_test_2)

        d1.add_bundle(ProvBundle(), 'ex:b0')
        self.assertEqual(len(d1.bundles), 2)
Example #41
0
    def test3(self):
        target = ProvBundle()
        target.activity('ex:compose', other_attributes=(('prov:role', "ex:dataToCompose1"), ('prov:role', "ex:dataToCompose2")))

        result = ProvBundle()
        result.activity('ex:compose', other_attributes={'prov:role': "ex:dataToCompose1"})
        result_inner = ProvBundle(identifier="ex:bundle1")
        result_inner.activity('ex:compose', other_attributes=(('prov:role', "ex:dataToCompose1"), ('prov:role', "ex:dataToCompose2")))
        result.add_bundle(result_inner)
        self.assertEqual(result.get_flattened(), target)
Example #42
0
    def test1(self):
        target = ProvBundle()
        target.activity('ex:correct', '2012-03-31T09:21:00', '2012-04-01T15:21:00')

        result = ProvBundle()
        result.activity('ex:correct', '2012-03-31T09:21:00')
        result_inner = ProvBundle(identifier="ex:bundle1")
        result_inner.activity('ex:correct', None, '2012-04-01T15:21:00')
        result.add_bundle(result_inner)
        self.assertEqual(result.get_flattened(), target)
Example #43
0
 def __init__(self, export_dir=None, coordinate_space_id=None):
     self.export_dir = export_dir
     self.coordinate_space_id = coordinate_space_id
     self.p = ProvBundle()
     self.id = None
Example #44
0
    def test_non_unifiable_document(self):
        g = ProvBundle()
        g.add_namespace("ex", "http://www.example.com/")
        g.activity('ex:compose', other_attributes={'prov:role': "ex:dataToCompose1"})
        g.used('ex:compose', 'ex:testEntity')
        with self.assertRaises(ProvExceptionCannotUnifyAttribute):
            g.activity('ex:testEntity')

        h = g.bundle('ex:bundle')
        h.add_namespace("ex", "http://www.example.com/")
        h.entity('ex:compose', other_attributes={'prov:label': "impossible!!!"})

        with self.assertRaises(ProvExceptionCannotUnifyAttribute):
            g.get_flattened()
Example #45
0
    def test_inferred_retyping_in_flattened_documents(self):
        g = ProvBundle()
        g.add_namespace("ex", "http://www.example.com/")
        g.wasGeneratedBy('ex:Bob', time='2012-05-25T11:15:00')
        b1 = g.bundle('ex:bundle')
        b1.agent('ex:Bob')

        h = ProvBundle()
        h.add_namespace("ex", "http://www.example.com/")
        h.agent('ex:Bob')
        h.wasGeneratedBy('ex:Bob', time='2012-05-25T11:15:00')

        self.assertEqual(g.get_flattened(), h)
Example #46
0
def bundles1():
    # https://github.com/lucmoreau/ProvToolbox/blob/master/asn/src/test/resources/prov/bundles1.provn
    #===============================================================================
    # bundle
    # 
    #  prefix ex  <http://example.org/example/>
    # 
    #  prefix alice  <http://example.org/alice/>
    #  prefix bob  <http://example.org/bob/>
    # 
    #  entity(bob:bundle1, [prov:type='prov:Bundle'])
    #  wasGeneratedBy(bob:bundle1, -, 2012-05-24T10:30:00)
    #  agent(ex:Bob)
    #  wasAttributedTo(bob:bundle1, ex:Bob)
    # 
    #  entity(alice:bundle2, [ prov:type='prov:Bundle' ])
    #  wasGeneratedBy(alice:bundle2, -, 2012-05-25T11:15:00)
    #  agent(ex:Alice)
    #  wasAttributedTo(alice:bundle2, ex:Alice)
    # 
    #  bundle bob:bundle1
    #    entity(ex:report1, [ prov:type="report", ex:version=1 ])
    #    wasGeneratedBy(ex:report1, -, 2012-05-24T10:00:01)
    #  endBundle
    # 
    #  bundle alice:bundle2
    #    entity(ex:report1)
    #    entity(ex:report2, [ prov:type="report", ex:version=2 ])
    #    wasGeneratedBy(ex:report2, -, 2012-05-25T11:00:01)
    #    wasDerivedFrom(ex:report2, ex:report1)
    #  endBundle
    # 
    # endBundle    
    #===============================================================================
    EX = Namespace("ex","http://www.example.com/")
    
    g = ProvBundle()
    g.add_namespace(EX)
    g.add_namespace('alice', 'http://example.org/alice/')
    g.add_namespace('bob', 'http://example.org/bob/')
    
    
    g.entity('bob:bundle1', {'prov:type': PROV['Bundle']})
    g.wasGeneratedBy('bob:bundle1', time='2012-05-24T10:30:00')
    g.agent('ex:Bob')
    g.wasAttributedTo('bob:bundle1', 'ex:Bob')
    
    g.entity('alice:bundle2', {'prov:type': PROV['Bundle']})
    g.wasGeneratedBy('alice:bundle2', time='2012-05-25T11:15:00')
    g.agent('ex:Alice')
    g.wasAttributedTo('alice:bundle2', 'ex:Alice')
    
    b1 = g.bundle('bob:bundle1')
    b1.entity('ex:report1', {'prov:type': "report", 'ex:version': 1})
    b1.wasGeneratedBy('ex:report1', time='2012-05-24T10:00:01')
    
    b2 = g.bundle('alice:bundle2')
    b2.entity('ex:report1')
    b2.entity('ex:report2', {'prov:type': "report", 'ex:version': 2})
    b2.wasGeneratedBy('ex:report2', time='2012-05-25T11:00:01')
    b2.wasDerivedFrom('ex:report2', 'ex:report1')
      
    return g
Example #47
0
    def testOAuthAccess(self):
        self.user = User.objects.create_user('jane', '*****@*****.**', 'toto')
        self.resource = Resource.objects.get_or_create(name='api', url='/api/')
        self.CONSUMER_KEY = 'dpf43f3p2l4k3l03'
        self.CONSUMER_SECRET = 'kd94hf93k423kf44'
        self.consumer, _ = Consumer.objects.get_or_create(
            key=self.CONSUMER_KEY, secret=self.CONSUMER_SECRET,
            defaults={
                'name': 'Test',
                'description': 'Testing...'
        })
        pb = ProvBundle()
        pb._decode_JSON_container('')
        self.bundle = Container.create('test_bundle', pb, self.user)
        
        c = Client()
        response = c.get("/oauth/request_token/")
        self.assertEqual(response.status_code, 401)
        import time
        parameters = {
            'oauth_consumer_key': self.CONSUMER_KEY,
            'oauth_signature_method': 'PLAINTEXT',
            'oauth_signature': '%s&' % self.CONSUMER_SECRET,
            'oauth_timestamp': str(int(time.time())),
            'oauth_nonce': 'requestnonce',
            'oauth_version': '1.0',
            'oauth_callback': 'http://test/request_token_ready',
            'scope': 'api',
            }
        response = c.get("/oauth/request_token/", parameters)
        self.assertEqual(response.status_code, 200)
        token = list(Token.objects.all())[-1]
        self.assertIn(token.key, response.content)
        self.assertIn(token.secret, response.content)
        self.assertTrue(token.callback_confirmed)

        parameters = {'oauth_token': token.key,}
        response = c.get("/oauth/authorize/", parameters)
        self.assertEqual(response.status_code, 302)
        self.assertIn(token.key, response['Location'])
        c.login(username='******', password='******')
        self.assertFalse(token.is_approved)
        response = c.get("/oauth/authorize/", parameters)
        self.assertEqual(response.status_code, 200)
        
        # fake authorization by the user
        parameters['authorize_access'] = 1
        response = c.post("/oauth/authorize/", parameters)
        self.assertEqual(response.status_code, 302)
        token = Token.objects.get(key=token.key)
        self.assertIn(token.key, response['Location'])
        self.assertTrue(token.is_approved)
        c.logout()
        
        # Exchange the Request token for an Access token
        parameters = {
            'oauth_consumer_key': self.CONSUMER_KEY,
            'oauth_token': token.key,
            'oauth_signature_method': 'PLAINTEXT',
            'oauth_signature': '%s&%s' % (self.CONSUMER_SECRET, token.secret),
            'oauth_timestamp': str(int(time.time())),
            'oauth_nonce': 'accessnonce',
            'oauth_version': '1.0',
            'oauth_verifier': token.verifier,
            'scope': 'api',
            }
        response = c.get("/oauth/access_token/", parameters)
        self.assertEqual(response.status_code, 200)
        access_token = list(Token.objects.filter(token_type=Token.ACCESS))[-1]
        self.assertIn(access_token.key, response.content)
        self.assertIn(access_token.secret, response.content)
        self.assertEqual(access_token.user.username, self.user.username)
        
        # Generating signature base string
        parameters = {
            'oauth_consumer_key': self.CONSUMER_KEY,
            'oauth_token': access_token.key,
            'oauth_signature_method': 'HMAC-SHA1',
            'oauth_timestamp': str(int(time.time())),
            'oauth_nonce': 'accessresourcenonce',
            'oauth_version': '1.0',
        }
        url_path = "/api/v0/bundle/%d/" % self.bundle.id
        oauth_request = oauth.Request.from_token_and_callback(access_token, http_url='http://testserver' + url_path, parameters=parameters)
        signature_method = oauth.SignatureMethod_HMAC_SHA1()
        signature = signature_method.sign(oauth_request, self.consumer, access_token)
        parameters['oauth_signature'] = signature
        response = c.get(url_path + '?format=json', parameters)
        self.assertEqual(response.status_code, 200)
Example #48
0
def w3c_publication_2():
    # https://github.com/lucmoreau/ProvToolbox/blob/master/asn/src/test/resources/prov/w3c-publication2.prov-asn
    #===========================================================================
    # bundle
    # 
    # prefix ex <http://example.org/>
    # prefix rec <http://example.org/record>
    # 
    # prefix w3 <http://www.w3.org/TR/2011/>
    # prefix hg <http://dvcs.w3.org/hg/prov/raw-file/9628aaff6e20/model/releases/WD-prov-dm-20111215/>
    # 
    # 
    # entity(hg:Overview.html, [ prov:type="file in hg" ])
    # entity(w3:WD-prov-dm-20111215, [ prov:type="html4" ])
    # 
    # 
    # activity(ex:rcp,-,-,[prov:type="copy directory"])
    # 
    # wasGeneratedBy(rec:g; w3:WD-prov-dm-20111215, ex:rcp, -)
    # 
    # entity(ex:req3, [ prov:type="http://www.w3.org/2005/08/01-transitions.html#pubreq" %% xsd:anyURI ])
    # 
    # used(rec:u; ex:rcp,hg:Overview.html,-)
    # used(ex:rcp, ex:req3, -)
    # 
    # 
    # wasDerivedFrom(w3:WD-prov-dm-20111215, hg:Overview.html, ex:rcp, rec:g, rec:u)
    # 
    # agent(ex:webmaster, [ prov:type='prov:Person' ])
    # 
    # wasAssociatedWith(ex:rcp, ex:webmaster, -)
    # 
    # endBundle
    #===========================================================================

    ex = Namespace('ex', 'http://example.org/')
    rec = Namespace('rec', 'http://example.org/record')
    w3 = Namespace('w3', 'http://www.w3.org/TR/2011/')
    hg = Namespace('hg', 'http://dvcs.w3.org/hg/prov/raw-file/9628aaff6e20/model/releases/WD-prov-dm-20111215/')
    
    
    g = ProvBundle()
    
    g.entity(hg['Overview.html'], {'prov:type': "file in hg"})
    g.entity(w3['WD-prov-dm-20111215'], {'prov:type': "html4"})

    g.activity(ex['rcp'], None, None, {'prov:type': "copy directory"})

    g.wasGeneratedBy('w3:WD-prov-dm-20111215', 'ex:rcp', identifier=rec['g'])

    g.entity('ex:req3', {'prov:type': Identifier("http://www.w3.org/2005/08/01-transitions.html#pubreq")})
    
    g.used('ex:rcp', 'hg:Overview.html', identifier='rec:u')
    g.used('ex:rcp', 'ex:req3')
    
    g.wasDerivedFrom('w3:WD-prov-dm-20111215', 'hg:Overview.html', 'ex:rcp', 'rec:g', 'rec:u')

    g.agent('ex:webmaster', {'prov:type': "Person"})

    g.wasAssociatedWith('ex:rcp', 'ex:webmaster')
        
    return g
Example #49
0
def bundles2():
    # https://github.com/lucmoreau/ProvToolbox/blob/master/asn/src/test/resources/prov/bundles2.provn
    #===========================================================================
    # bundle
    # 
    #  prefix ex  <http://example.org/example/>
    # 
    #  prefix alice  <http://example.org/alice/>
    #  prefix bob  <http://example.org/bob/>
    # 
    #  entity(bob:bundle4, [prov:type='prov:Bundle'])
    #  wasGeneratedBy(bob:bundle4, -, 2012-05-24T10:30:00)
    #  agent(ex:Bob)
    #  wasAttributedTo(bob:bundle4, ex:Bob)
    # 
    #  entity(alice:bundle5, [ prov:type='prov:Bundle' ])
    #  wasGeneratedBy(alice:bundle5, -, 2012-05-25T11:15:00)
    #  agent(ex:Alice)
    #  wasAttributedTo(alice:bundle5, ex:Alice)
    # 
    #  bundle bob:bundle4
    #    entity(ex:report1, [ prov:type="report", ex:version=1 ])
    #    wasGeneratedBy(ex:report1, -, 2012-05-24T10:00:01)
    #  endBundle
    # 
    #  bundle alice:bundle5
    #    entity(ex:report1bis)
    #    mentionOf(ex:report1bis, ex:report1, bob:bundle4)
    #    entity(ex:report2, [ prov:type="report", ex:version=2 ])
    #    wasGeneratedBy(ex:report2, -, 2012-05-25T11:00:01)
    #    wasDerivedFrom(ex:report2, ex:report1bis)
    #  endBundle
    # 
    # endBundle
    #===========================================================================
   
    g = ProvBundle()
    g.add_namespace("ex","http://www.example.com/")
    g.add_namespace('alice', 'http://example.org/alice/')
    g.add_namespace('bob', 'http://example.org/bob/')
    
    
    g.entity('bob:bundle4', {'prov:type': PROV['Bundle']})
    g.wasGeneratedBy('bob:bundle4', time='2012-05-24T10:30:00')
    g.agent('ex:Bob')
    g.wasAttributedTo('bob:bundle4', 'ex:Bob')
    
    g.entity('alice:bundle5', {'prov:type': PROV['Bundle']})
    g.wasGeneratedBy('alice:bundle5', time='2012-05-25T11:15:00')
    g.agent('ex:Alice')
    g.wasAttributedTo('alice:bundle5', 'ex:Alice')
    
    b4 = g.bundle('bob:bundle4')
    b4.entity('ex:report1', {'prov:type': "report", 'ex:version': 1})
    b4.wasGeneratedBy('ex:report1', time='2012-05-24T10:00:01')
    
    b5 = g.bundle('alice:bundle5')
    b5.entity('ex:report1bis')
    b5.mentionOf('ex:report1bis', 'ex:report1', 'bob:bundle4')
    b5.entity('ex:report2', [ ('prov:type', "report"), ('ex:version', 2) ])
    b5.wasGeneratedBy('ex:report2', time='2012-05-25T11:00:01')
    b5.wasDerivedFrom('ex:report2', 'ex:report1bis')
      
    return g
 def bundle_0(self):
     b = ProvBundle(namespaces={'ex': EX2_URI})
     return b
 def sub_test_2():
     ex2_b0 = b0.identifier
     d1.add_bundle(ProvBundle(identifier=ex2_b0))