Exemplo n.º 1
0
    def _createStandardActivity(cls, identifier, interfaceAddress, methodUri,
                                temporalContext, timeSeries, valueObjects):
        '''
		@type identifier, methodUri: string; 
		@type valueObjects: a list of ValueObjects.
		others are object 
		'''
        from SmartAPI.model.Activity import Activity
        from SmartAPI.model.Entity import Entity

        if identifier is not None:
            a = Activity(identifier)
        else:
            a = Activity()
        a.setMethod(methodUri)
        e = Entity()
        if valueObjects is not None:
            for vo in valueObjects:
                e.addValueObject(vo)

        a.addEntity(e)
        if interfaceAddress is not None:
            a.addInterface(interfaceAddress)
        if temporalContext is not None:
            a.setTemporalContext(temporalContext)
        if timeSeries is not None:
            a.addTimeSerie(timeSeries)

        return a
Exemplo n.º 2
0
 def setSigner(self, signer):
     if isinstance(signer, Entity):
         self.signer = signer
     elif isinstance(signer, str):
         e = Entity()
         e.setSameAs(signer)
         self.setSigner(e)
Exemplo n.º 3
0
 def __init__(self, identity=None):
     Agent.__init__(self)
     self.identity = identity
     self.entity = Entity()
     self.entity.clearTypes()
     self.server_uri = REGISTRY_SEARCH_SERVER_ADDRESS
     self.serialization = SERIALIZATION.TURTLE
Exemplo n.º 4
0
 def __init__(self, uri=None):
     Entity.__init__(self, uri)
     self.velocity = None
     self.orientation = None
     self.direction = None
     self.size = None
     self.weight = None
     self.setType(RESOURCE.PHYSICALENTITY)
Exemplo n.º 5
0
 def fetchById(self, myUri, idToFetch):
     agent = RegistrySearchAgent()
     agent.clear()
     agent.setSenderUri(myUri)
     entity = Entity(idToFetch)
     entity.clearTypes()
     agent.setEntity(entity)
     resEntities = agent.search()
     if len(resEntities) > 0:
         return resEntities[0]
     else:
         return None
Exemplo n.º 6
0
    def parseStatement(self, statement):
        # get predicate
        predicate = str(statement.getPredicate())

        if predicate == PROPERTY.SIGNER:
            try:
                self.setSigner(Entity.parse(statement.getResource()))
            except:
                print "Unable to interpret seas:signer value as resource."
                traceback.print_exc()
            return
        if predicate == PROPERTY.MESSAGE:
            try:
                self.setMessage(Message.parse(statement.getResource()))
            except:
                print "Unable to interpret message value as resource."
                traceback.print_exc()
            return

        super(Transaction, self).parseStatement(statement)
Exemplo n.º 7
0
 def __init__(self, uri=None):
     Entity.__init__(self, uri)
Exemplo n.º 8
0
 def __init__(self, uri=None):
     Entity.__init__(self, uri)
     self.setType(RESOURCE.ORGANIZATION)
Exemplo n.º 9
0
 def clear(self):
     self.entity = Entity()
Exemplo n.º 10
0
class RegistrySearchAgent(Agent):
    def __init__(self, identity=None):
        Agent.__init__(self)
        self.identity = identity
        self.entity = Entity()
        self.entity.clearTypes()
        self.server_uri = REGISTRY_SEARCH_SERVER_ADDRESS
        self.serialization = SERIALIZATION.TURTLE

    def setSenderUri(self, generatedBy):
        self.identity = generatedBy

    def search(self):
        payload = self.generateSearchMessage()
        try:
            if debug:
                print "Request to SMARTAPI Search Service:"
                print payload

            resp_str = self.runQuery(self.server_uri, content_type,
                                     content_type, payload)

            response = Response().fromString(resp_str, self.serialization)
            if response != None:
                if debug:
                    print "Response from SMARTAPI Search Service:"
                    print resp_str
                return response.getEntities()
            else:
                if debug:
                    print "Response from SMARTAPI Search Service:"
                    print resp_str
                return None
        except:
            print "Exception while sending an HTTP request to " + REGISTRY_SEARCH_SERVER_ADDRESS
            traceback.print_exc()
            return None

    def clear(self):
        self.entity = Entity()

    def setEntity(self, searchEntity):
        self.entity = searchEntity

    def generateSearchMessage(self):
        request = RequestFactory().create(self.identity)
        request.setMethod(RESOURCE.READ)
        request.addEntity(self.entity)
        return Tools().toString(request, self.serialization)

    def setServiceUri(self, serviceUri):
        self.server_uri = serviceUri

    def ofDescription(self, searchString):
        condition = Condition()
        condition.addRegex("(?i)" + searchString)
        self.entity.add(URIRef(PROPERTY.COMMENT), condition)

    def anyOfNames(self, searchStrings):
        if len(searchStrings) > 0:
            condition = Condition()
            for s in searchStrings:
                condition.addRegex("(?i)" + s)
            self.entity.add(URIRef(PROPERTY.RDFS_LABEL), condition)

    def ofName(self, searchString, exactMatch=False):
        if exactMatch:
            self.entity.setName(searchString)
        else:
            condition = Condition()
            condition.addRegex("(?i)" + searchString)
            self.entity.add(URIRef(PROPERTY.RDFS_LABEL), condition)

    def ofPartialId(self, searchString):
        condition = Condition()
        condition.addRegex("(?i)" + searchString)
        self.entity.add(URIRef(PROPERTY.ID), condition)

    def ofType(self, type):
        self.entity.addType(type)

    def anyOfTypes(self, types):
        if len(types) == 1:
            self.ofType(types[0])
        if len(types) > 1:
            condition = Condition()
            for type in types:
                condition.addOr(Obj(type))
            self.entity.setType(condition)

    def clearTypes(self):
        self.entity.clearTypes()

    def ofInputType(self, type):
        if self.entity.hasCapability():
            a = self.entity.getCapabilities()[0]
            i = Input()
            i.setType(type)
            a.addInput(i)
        else:
            a = Activity()
            i = Input()
            i.setType(type)
            a.addInput(i)
            self.entity.addCapability(a)

    def ofOutputType(self, type):
        if self.entity.hasCapability():
            a = self.entity.getCapabilities()[0]
            o = Output()
            o.setType(type)
            a.addOutput(o)
        else:
            a = Activity()
            o = Output()
            o.setType(type)
            a.addOutput(o)
            self.entity.addCapability(a)

    def polygonSearchArea(self, polygon):
        self.entity.add(URIRef(RESOURCE.POLYGON), polygon)

    def multipolygonSearchArea(self, polygons):
        for polygon in polygons:
            self.entity.add(URIRef(RESOURCE.POLYGON), polygon)

    def rectangeSearchArea(self, minCoords, maxCoords):
        self.entity.add(URIRef(PROPERTY.MINLOCATION), minCoords)
        self.entity.add(URIRef(PROPERTY.MAXLOCATION), maxCoords)

    def pointSearchArea(self, center, kilometers):
        ring = Ring()
        ring.add(URIRef(PROPERTY.LAT), center.getLatitude())
        ring.add(URIRef(PROPERTY.LONG), center.getLongitude())
        maxR = ValueObject()
        maxR.setQuantity(RESOURCE.LENGTH)
        maxR.setUnit(RESOURCE.KILOMETER)
        maxR.setValue(Variant(kilometers))
        ring.setMaxRadius(maxR)
        self.entity.add(URIRef(NS.SMARTAPI + "ring"), ring)

    def debugMode(self, value):
        global debug
        debug = value

    def searchByPointAndType(self, myUri, latitude, longitude, distanceInKm,
                             types):
        agent = RegistrySearchAgent()
        agent.clear()
        agent.setSenderUri(myUri)
        agent.anyOfTypes(types)
        coords = Coordinates()
        coords.setLatitude(latitude)
        coords.setLongitude(longitude)
        agent.pointSearchArea(coords, distanceInKm)
        return agent.search()

    def searchByNameAndType(self, myUri, keywords, types):
        agent = RegistrySearchAgent()
        agent.clear()
        agent.setSenderUri(myUri)
        agent.anyOfTypes(types)
        agent.anyOfNames(keywords)
        return agent.search()

    def searchServicesByOutputType(self, myUri, type):
        agent = RegistrySearchAgent()
        agent.clear()
        agent.setSenderUri(myUri)
        agent.ofType(RESOURCE.SERVICE)
        agent.ofOutputType(type)
        return agent.search()

    def searchServicesByInputType(self, myUri, type):
        agent = RegistrySearchAgent()
        agent.clear()
        agent.setSenderUri(myUri)
        agent.ofType(RESOURCE.SERVICE)
        agent.ofInputType(type)
        return agent.search()

    def searchByDescription(self,
                            myUri,
                            searchString,
                            types=[RESOURCE.ENTITY]):
        agent = RegistrySearchAgent()
        agent.clear()
        agent.setSenderUri(myUri)
        agent.anyOfTypes(types)
        agent.ofDescription(searchString)
        return agent.search()

    def searchByPartialId(self, myUri, searchString):
        agent = RegistrySearchAgent()
        agent.clear()
        agent.setSenderUri(myUri)
        agent.ofPartialId(searchString)
        return agent.search()

    def fetchById(self, myUri, idToFetch):
        agent = RegistrySearchAgent()
        agent.clear()
        agent.setSenderUri(myUri)
        entity = Entity(idToFetch)
        entity.clearTypes()
        agent.setEntity(entity)
        resEntities = agent.search()
        if len(resEntities) > 0:
            return resEntities[0]
        else:
            return None
Exemplo n.º 11
0
 def __init__(self, myUri=''):        
     self.generatedBy = myUri
     self.entity = Entity()
     self.entity.clearTypes()
     self.httpClient = HttpClient()
     self.serialization = 'text/turtle'
Exemplo n.º 12
0
class SearchAgent(object):
    # uri of the SEAS registration service
    rsUri = "http://seas.asema.com/webapps/rs/v1.0e1.2/search" 
    debug = False
    
    def __init__(self, myUri=''):        
        self.generatedBy = myUri
        self.entity = Entity()
        self.entity.clearTypes()
        self.httpClient = HttpClient()
        self.serialization = 'text/turtle'
    
    def search(self):
        from SmartAPI.model.Response import Response
        
        # serialize data
        messageBody = self._generateSearchMessage()
        if self.debug:
            print '\n Request to SEAS Search Service:'
            print messageBody, '\n'
        
        try:
            # send message
            responseStr = self.httpClient.sendPost(self.rsUri, messageBody, self.serialization, self.serialization, seasMethod=self.httpClient.SMARTAPI_METHOD_REQUEST)[0]
            response = Response.fromString(responseStr, self.serialization)
            if response is not None:
                if self.debug:
                    print '\n Response from SEAS Search Service:'
                    print Tools.toString(response, self.serialization)
                    Tools.printErrors(response)
                return response.getEntities()
            else:
                if self.debug:
                    print '\n Response from SEAS Search Service:'
                    print responseStr, '\n'
                return None
        except:
            print 'Exception while sending an HTTP request to ', self.rsUri
            traceback.print_exc()
        
        
    def _generateSearchMessage(self):
        from SmartAPI.factory.RequestFactory import RequestFactory
    
        request = RequestFactory().create(self.generatedBy)        
        request.setMethod(RESOURCE.READ)        
        request.addEntity(self.entity)
        
        return Tools.toString(request, self.serialization)       
    
    @classmethod
    def searchByNameAndType(cls, mySeasUri, freshnessInDays, keywords, types):
        '''
        @type keywords, types: list of Strings
        @type freshnessInDays: integer
        @type mySeasUri: String
        '''
        agent = SearchAgent(mySeasUri)
        agent.anyOfTypes(types)
        agent.daysOldData(freshnessInDays)
        agent.anyOfNames(keywords)
        return agent.search()
    
    @classmethod
    def searchByDescription(cls,  mySeasUri, freshnessInDays, searchString, types = [RESOURCE.ENTITY]):
        agent = SearchAgent(mySeasUri)        
        agent.anyOfTypes(types)
        agent.daysOldData(freshnessInDays)
        agent.ofDescription(searchString)
        return agent.search()
    
    @classmethod
    def searchById(cls, mySeasUri, freshnessInDays, searchString):
        agent = SearchAgent(mySeasUri)
        agent.ofType(RESOURCE.ENTITY);
        agent.ofId(searchString)
        agent.daysOldData(freshnessInDays)
        return agent.search()
    
    @classmethod
    def searchByPointAndType(cls, mySeasUri, freshnessInDays, latitude, longitude, distanceInKm, types):
        agent = SearchAgent(mySeasUri)
        agent.anyOfTypes(types)
        agent.daysOldData(freshnessInDays)
        
        coords = Coordinates(latitude=latitude, longitude=longitude)        
        agent.pointSearchArea(coords, distanceInKm)
        return agent.search()
    
    def pointSearchArea(self, center, kilometers):
        '''
        define circular search area
        '''
        ring = Ring()
        ring.add(URIRef(PROPERTY.LAT), center.getLatitude())
        ring.add(URIRef(PROPERTY.LONG), center.getLongitude())
        maxR = ValueObject()
        maxR.setQuantity(RESOURCE.LENGTH)
        maxR.setUnit(RESOURCE.KILOMETER)
        maxR.setValue(Variant(kilometers))
        ring.setMaxRadius(maxR)
        self.entity.add(URIRef(NS.SMARTAPI + "ring"), ring)
    
    def ofId(self, searchString):
        condition = Condition()
        condition.addRegex("(?i)" + searchString)
        self.entity.add(URIRef(PROPERTY.ID), condition)
    
    def ofDescription(self, searchString):
        condition = Condition()
        condition.addRegex("(?i)" + searchString)
        self.entity.add(URIRef(PROPERTY.COMMENT), condition)
       
    def ofType(self, type):
        '''
        define type to search
        '''
        if isinstance(type, str):
            type = URIRef(type)
        self.entity.addType(type)
        
    def anyOfTypes(self, types):
        '''
        Define all the types that can match
        '''
        if len(types) == 1:
            self.ofType(types[0])
        elif len(types) > 1:
            condition = Condition()
            for type in types:
                condition.addOr(Obj(type))
            self.entity.clearTypes()
            self.entity.addType(condition)
            
    def daysOldData(self, days):
        '''
        Define how many days old data will be searched.
        '''
        duration = Duration(days=days)
        self.entity.add(URIRef(PROPERTY.FRESHNESS), duration)
        
    def monthsOldData(self, months):
        '''
        Define how many months old data will be searched.
        '''
        duration = Duration(months=months)
        self.entity.add(URIRef(PROPERTY.FRESHNESS), duration)
        
    def anyOfNames(self, searchStrings):
        '''
        Define multiple search strings for the entity name (label). Will return also partial hits 
        for any of the strings.
        '''
        if len(searchStrings) > 0:
            condition = Condition()
            for stri in searchStrings:
                condition.addRegex("(?i)" + stri)
            
            self.entity.add(URIRef(PROPERTY.RDFS_LABEL), condition);            
Exemplo n.º 13
0
 def __init__(self, uri=None):
     Entity.__init__(self, uri)
     self.realizedBy = None
     self.setType(RESOURCE.SYSTEMOFINTEREST)
Exemplo n.º 14
0
    def parseStatement(self, statement):
        from SmartAPI.rdf.Variant import Variant
        from SmartAPI.model.Activity import Activity
        from SmartAPI.model.Device import Device
        from SmartAPI.model.Entity import Entity
        from SmartAPI.model.Input import Input
        from SmartAPI.model.Output import Output
        from SmartAPI.model.Parameter import Parameter
        from SmartAPI.model.SystemOfInterest import SystemOfInterest
        from SmartAPI.model.TemporalContext import TemporalContext
        from SmartAPI.model.InterfaceAddress import InterfaceAddress
        from SmartAPI.model.TimeSeries import TimeSeries
        from SmartAPI.common.Tools import Tools

        if statement is None:
            raise NonePointerException("Unable to parse None to Evaluation.")
            return

        # get predicate
        predicate = str(statement.getPredicate())

        # category
        if predicate == PROPERTY.CATEGORY:
            try:
                self.addCategory(str(statement.getResource().toString()))
            except:
                print "Unable to interpret seas:category value as resource."
                traceback.print_exc()
            return

        # systemofinterest
        if predicate == PROPERTY.SYSTEMOFINTEREST:
            try:
                self.setSystemOfInterest(SystemOfInterest().parse(
                    statement.getResource()))
            except:
                print "Unable to interpret seas:systemOhInterest value as resource."
                traceback.print_exc()
            return

        # interfaceaddress
        if predicate == PROPERTY.INTERFACE:
            try:
                self.addInterface(InterfaceAddress().parse(
                    statement.getResource()))
            except:
                print "Unable to interpret seas:interface value as resource."
                traceback.print_exc()
            return

        # timeserie
        if predicate == PROPERTY.TIMESERIES:
            try:
                self.addTimeSerie(TimeSeries().parse(statement.getResource()))
            except:
                print "Unable to interpret seas:timeSeries value as resource."
                traceback.print_exc()
            return

        # value
        if predicate == PROPERTY.RDF_VALUE:
            self.setValue(Variant().parse(statement))
            return

        # valueobject
        if predicate == PROPERTY.VALUEOBJECT:
            try:
                self.addValueObject(ValueObject().parse(
                    statement.getResource()))
            except:
                print "Unable to interpret seas:valueObject value as resource."
                traceback.print_exc()
            return

        # input data
        if predicate == PROPERTY.HASINPUT:
            try:
                self.addInput(Input().parse(statement.getResource()))
            except:
                print "Unable to interpret seas:hasInput value as resource."
                traceback.print_exc()
            return

        # input reference
        if predicate == PROPERTY.HASREFINPUT:
            try:
                input = Input().parse(statement.getResource())
                input.setInputType(Input.TYPE_REFERENCE)
                self.addInput(input)
            except:
                print "Unable to interpret seas:hasRefInput value as resource."
                traceback.print_exc()
            return

        # output
        if predicate == PROPERTY.HASOUTPUT:
            try:
                self.addOutput(Output().parse(statement.getResource()))
            except:
                print "Unable to interpret seas:hasOutput value as resource."
                traceback.print_exc()
            return

        # activities
        if predicate == PROPERTY.ACTIVITY:
            try:
                self.addActivity(Activity().parse(statement.getResource()))
            except:
                print "Unable to interpret seas:activity value as resource."
                print statement
                print "------------------------"
                traceback.print_exc()
                raise
            return
        """"
        if predicate == PROPERTY.PARAMETER:
            try:
                self.addParameter(Parameter().parse(statement.getResource()))
            except:
                print "Unable to interpret seas:parameter value as resource."
                traceback.print_exc()
            return
        """

        # entity
        if predicate == PROPERTY.ENTITY:
            try:
                self.addEntity(Entity().parse(statement.getResource()))
            except:
                print "Unable to interpret seas:entity value (of type seas:Entity) as resource."
                traceback.print_exc()
            return

#       # entity
#       if predicate == PROPERTY.ENTITY:
#           types = None
#           try:
#               types = Tools().getResourceTypes(statement.getResource())
#           except:
#               print "Unable to interpret seas:entity value as resource."
#               traceback.print_exc()
#
#           # service
#           if types is not None and RESOURCE.SERVICE in types:
#               try:
#                   self.addEntity(Service().parse(statement.getResource()))
#               except:
#                   print "Unable to interpret seas:entity value (of type seas:Service) as resource."
#                   traceback.print_exc()
#               return
#
#           # device
#           if types is not None and RESOURCE.DEVICE in types:
#               try:
#                   self.addEntity(Device().parse(statement.getResource()))
#               except:
#                   print "Unable to interpret seas:entity value (of type seas:Device) as resource."
#                   traceback.print_exc()
#               return
#
#
#           # serviceprovider
#
#           # weatherserviceprovider
#
#           # could not match with any entity subtype
#           try:
#               self.addEntity(Entity().parse(statement.getResource()))
#           except:
#               print "Unable to interpret seas:entity value (of type seas:Entity) as resource."
#               traceback.print_exc()
#           return

# generatedat
        if predicate == PROPERTY.LIST:
            try:
                list_python, listType = statement.getResource().toList(
                    Evaluation)
                self.setList(list_python, listType)
            except:
                print "Unable to interpret seas:list value as list."
                traceback.print_exc()
            return

        # generatedat
        if predicate == PROPERTY.MAP:
            try:
                self.setMap(Map().parse(statement.getResource()))
            except:
                print "Unable to interpret seas:map value as resource."
                traceback.print_exc()
            return

        # pass on to Obj
        super(Evaluation, self).parseStatement(statement)