Пример #1
0
    def setEnd(self, end):
        '''
		@param end: it can be a Variant, datetime.date, datetime.time, datetime.datetime type,
		it can also be string, which requires a valid ISO8601 date-time string, like "2017-03-28T15:18:54.842"   
		'''
        if isinstance(end, Variant):
            self.end = end
        elif isinstance(end, str):
            self.end = Variant(isodate.parse_datetime(end))
        else:
            self.end = Variant(end)
Пример #2
0
    def setStart(self, start):
        '''
		@param start: it can be a Variant, datetime.date, datetime.time or datetime.datetime type,
		it can also be ISO8601 date-time string, like "2017-03-28T15:18:54.842"   
		'''
        if isinstance(start, Variant):
            self.start = start
        elif isinstance(start, str):
            self.start = Variant(isodate.parse_datetime(start))
        else:
            self.start = Variant(start)
Пример #3
0
    def parseStatement(self, statement):
        # get predicate
        predicate = str(statement.getPredicate())

        # start
        if predicate == PROPERTY.START:
            self.setStart(Variant().parse(statement))
# 			s = statement.getObject().toPython()
# 			if s is not None and not isinstance(s, datetime.datetime):
# 				try:
# 					s = parser.parse(s)
# 				except:
# 					print "Unknown date format for TemporalContext start:", s
# 			self.setStart(s)
# 			return

# end
        if predicate == PROPERTY.END:
            self.setEnd(Variant().parse(statement))
# 			e = statement.getObject().toPython()
# 			if e is not None and not isinstance(e, datetime.datetime):
# 				try:
# 					e = parser.parse(e)
# 				except:
# 					print "Unknown date format for TemporalContext end:", e
# 			self.setEnd(e)
# 			return

# duration
        if predicate == PROPERTY.DURATION:
            try:
                self.setDuration(statement.getString())
            except:
                print "Unable to interpret seas:duration value as literal."
                traceback.print_exc()
            return

        # during
        if predicate == PROPERTY.DURING:
            try:
                self.setDuring(statement.getResource().toString())
            except:
                print "Unable to interpret seas:during value as resource."
                traceback.print_exc()
            return

        # pass on to Object
        super(TemporalContext, self).parseStatement(statement)
Пример #4
0
    def parseStatement(self, statement):

        predicate = str(statement.getPredicate())

        # percentage
        if predicate == PROPERTY.PERCENTAGE:
            self.setPercentage(Variant().parse(statement))
            return

        # total
        if predicate == PROPERTY.TOTAL:
            self.setTotal(statement.getInt())
            return

        # completed
        if predicate == PROPERTY.COMPLETED:
            self.setCompleted(statement.getInt())
            return

        # temporalcontext
        if predicate == PROPERTY.TEMPORALCONTEXT:
            self.setTemporalContext(TemporalContext().parse(
                statement.getResource()))
            return

        # pass on to Obj
        super(Status, self).parseStatement(statement)
Пример #5
0
	def parseStatement(self, statement):
		from SmartAPI.model.SystemOfInterest import SystemOfInterest
		from SmartAPI.rdf.Variant import Variant
		from SmartAPI.rdf.Resource import Resource
	
		# get predicate
		predicate = str(statement.getPredicate())
		
		# percentage
		if predicate == PROPERTY.PERCENTAGE:
			try:
				self.setPercentage(Variant().parse(statement))
			except:
				print "Unable to interpret seas:percentage 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:systemOfInterest value as resource."
				traceback.print_exc() 
			return
		
		# pass on to ValueObject
		super(Capacity, self).parseStatement(statement)
Пример #6
0
	def parseStatement(self, statement):
		
			# get predicate
			predicate = str(statement.getPredicate())
			
			if predicate == PROPERTY.KEY:
				
				try:
					self.setKey(statement.getString())
				except:
					print "Unable to interpret seas:key value as literal for Parameter."
					traceback.print_exc()
				return
			
			if predicate == PROPERTY.RDF_VALUE:
				target_class = Tools().getResourceClass(statement.getResource())

				if target_class is not None:
					try:
						self.addValue(target_class().parse(statement.getResource()))
					except:
						print "Unable to interpret rdf:value as value for Parameter."
						traceback.print_exc()
					return
				elif statement.getResource().model.is_list(statement.getObject()):
					l = []
					statement.getResource().model.parse_list(l, statement.getObject())
					self.addValue(l)
				else:
					self.addValue(Variant().parse(statement))
				return
			
			# pass on to Object
			super(Parameter, self).parseStatement(statement)
Пример #7
0
 def addListItem(self, item):
     from SmartAPI.rdf.Variant import Variant
     if not isinstance(item, Variant) and not isinstance(item, Obj):
         item = Variant(item)
     if not self.hasList():
         self.setList(LinkedList.LinkedList())
     self.list.add_items(item)
Пример #8
0
    def parseStatement(self, statement):

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

        # regex
        if predicate.equals(PROPERTY.REGEX):
            try:
                self.addRegex(statement.getString())
            except:
                print "Unable to interpret seas:regex value as literal string."
                traceback.print_exc()
            return

        # or
        if predicate.equals(PROPERTY.OR):
            try:
                self.addOr(Variant().parse(statement))
            except:
                print "Unable to interpret seas:or value as resource."
                traceback.print_exc()
            return

        # and
        if predicate.equals(PROPERTY.AND):
            try:
                self.addAnd(Variant().parse(statement))
            except:
                print "Unable to interpret seas:and value as resource."
                traceback.print_exc()

            return

        # xor
        if predicate.equals(PROPERTY.XOR):
            try:
                self.addXor(Variant().parse(statement))
            except:
                print "Unable to interpret seas:xor value as resource."
                traceback.print_exc()

            return

        # pass on to Obj
        super(Condition, self).parseStatement(statement)
Пример #9
0
	def calculatePercentage(self):
		if self.hasValue() and self.hasMaximum():
			v = self.value.getValue()
			m = self.maximum.getValue()
			if v is not None and m is not None:
				if m != 0:							
					self.setPercentage(Variant(v/m*100))
					return True
		return False
Пример #10
0
 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)
Пример #11
0
    def get(self, property):
        from SmartAPI.rdf.Variant import Variant
        ret = []

        if self.properties.has_key(property):
            for e in self.properties[property]:
                if isinstance(e, Variant):
                    ret.append(e)
                else:
                    ret.append(Variant(e))

        return ret
Пример #12
0
    def getProperties(self):
        from SmartAPI.rdf.Variant import Variant
        map = {}

        for key, value in self.properties.iteritems():
            for e in value:
                # if a key does not exist yet, create new entry
                if not map.has_key(key):
                    map[key] = []

                # add to arraylist
                map[key].append(Variant(e))

        return map
Пример #13
0
    def _parse_list_entry(self, entry, klass=None, from_nude=False):
        from SmartAPI.model.ValueObject import ValueObject
        from SmartAPI.rdf.Variant import Variant
        from SmartAPI.common.Tools import Tools

        if from_nude and klass is not None:
            item = klass()
            item.fromNude(entry)
            return item

        if isinstance(entry, Literal):
            return Variant(entry.toPython())
        elif isinstance(entry, URIRef):
            if entry == RDF.nil:
                return None
            return Variant(entry)
        else:
            if klass is None:
                types = []
                sl = self.listStatements(subject=entry,
                                         predicate=URIRef(PROPERTY.RDF_TYPE),
                                         object=None)
                for s in sl:
                    types.append(s.getResource().toString())
                klass = Tools().mapper.getClass(types, default=Variant)

            item = klass()
            for s in self.find_statements_for_node(entry):
                if s.predicate == NS.SMARTAPI + "valueObject":
                    itemv = ValueObject()
                    for sv in self.find_statements_for_node(s.object):
                        itemv.parse(sv)
                    item.addValueObject(itemv)
                else:
                    item.parseStatement(s)
            return item
Пример #14
0
    def createValueObject(self,
                          name=None,
                          unit=None,
                          quantity=None,
                          description=None,
                          datatype=None):
        from SmartAPI.model.ValueObject import ValueObject
        from SmartAPI.rdf.Variant import Variant

        vo = None
        try:
            vo = ValueObject()
            if name is not None: vo.setName(name)
            if quantity is not None: vo.setQuantity(quantity)
            if unit is not None: vo.setUnit(unit)
            if description is not None: vo.setDescription(description)
            if datatype is not None: vo.setValue(Variant(datatype))
        except:
            print "Exception while creating a ValueObject"
            print sys.exc_info()[1]
            vo = None
        return vo
Пример #15
0
 def addXor(self, o):
     if isinstance(o, Obj):
         o = Variant(o)
     self.xors.append(o)
Пример #16
0
class PriceSpecification(GrObj):
    '''
    classdocs
    '''
    def __init__(self, uri=None):
        super(PriceSpecification, self).__init__(uri)
        #String
        self.currency = None
        #Float
        self.currencyValue = None
        #Data
        self.validFrom = None
        #Date
        self.validThrough = None
        # Boolean
        self.valueAddedTaxIncluded = None
        # Variant
        self.vatPercentage = None
        # String
        self.quantity = None
        # String
        self.unit = None

    def hasCurrency(self):
        return self.currency is not None

    def getCurrency(self):
        return self.currency

    def setCurrency(self, cur):
        self.currency = cur

    def hasQuantity(self):
        return self.quantity is not None

    def getQuantity(self):
        return self.quantity

    def setQuantity(self, q):
        '''
        @param q: URL string of this Quantity
        '''
        self.quantity = q

    def hasUnit(self):
        return self.unit is not None

    def getUnit(self):
        return self.unit

    def setUnit(self, u):
        '''
        @param u: URL string of this unit
        '''
        self.unit = u

    def hasCurrencyValue(self):
        return self.currencyValue is not None

    def getCurrencyValue(self):
        return self.currencyValue

    def setCurrencyValue(self, cv):
        self.currencyValue = cv

    def hasValidThrough(self):
        return self.validThrough is not None

    def getValidThrough(self):
        return self.validThrough

    def setValidThrough(self, vt):
        self.validThrough = vt

    def hasValidFrom(self):
        return self.validFrom is not None

    def getValidFrom(self):
        return self.validFrom

    def setValidFrom(self, vf):
        self.validFrom = vf

    def hasValueAddedTaxIncluded(self):
        return self.valueAddedTaxIncluded is not None

    def getValueAddedTaxIncluded(self):
        return self.valueAddedTaxIncluded

    def setValueAddedTaxIncluded(self, vat):
        self.valueAddedTaxIncluded = vat

    def hasVatPercentage(self):
        return self.vatPercentage is not None

    def getVatPercentage(self):
        return self.vatPercentage

    def setVatPercentage(self, vp):
        if isinstance(vp, Variant):
            self.vatPercentage = vp
        else:  # if int, double,...
            self.vatPercentage = Variant(vp)

    def serialize(self, model):
        if self.serializeAsReference:
            return self.serializeToReference(model)

        if Tools.serializedObjs.has_key(self):
            return Tools.serializedObjs.get(self)

        resource = super(PriceSpecification, self).serialize(model)

        #currency
        if self.hasCurrency():
            resource.addProperty(
                model.createProperty(PROPERTY.HASCURRENCY),
                model.createTypedLiteral(self.currency, XSD.string))

        #currencyValue
        if self.hasCurrencyValue():
            resource.addProperty(
                model.createProperty(PROPERTY.HASCURRENCYVALUE),
                model.createTypedLiteral(self.getCurrencyValue(), XSD.float))

        #validThrough
        if self.hasValidThrough():
            resource.addProperty(model.createProperty(PROPERTY.VALIDTHROUGH),
                                 model.createLiteral(self.getValidThrough()))

        #validFrom
        if self.hasValidFrom():
            resource.addProperty(model.createProperty(PROPERTY.VALIDFROM),
                                 model.createLiteral(self.getValidFrom()))

        # valueAddedTaxIncluded
        if self.hasValueAddedTaxIncluded():
            resource.addProperty(
                model.createProperty(PROPERTY.VALUEADDEDTAXINCLUDED),
                model.createTypedLiteral(self.getValueAddedTaxIncluded(),
                                         XSD.boolean))

        # vatPercentage
        if self.hasVatPercentage():
            resource.addProperty(model.createProperty(PROPERTY.VATPERCENTAGE),
                                 self.vatPercentage.serialize(model))

        # unit
        if self.hasUnit():
            resource.addProperty(model.createProperty(PROPERTY.UNIT),
                                 model.createResource(self.getUnit()))

        # quantity
        if self.hasQuantity():
            resource.addProperty(model.createProperty(PROPERTY.QUANTITYKIND),
                                 model.createResource(self.getQuantity()))

        return resource

    def parseStatement(self, statement):

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

        if predicate == PROPERTY.HASCURRENCY:
            try:
                self.setCurrency(statement.getString())
            except:
                print "Unable to interpret gr:hasCurrency value as literal string."
                traceback.print_exc()
            return

        if predicate == PROPERTY.HASCURRENCYVALUE:
            try:
                self.setCurrencyValue(statement.getFloat())
            except:
                print "Unable to interpret gr:hasCurrencyValue value as literal float."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VALIDTHROUGH:
            try:
                self.setValidThrough(statement.getObject().toPython())
            except:
                print "Unable to interpret gr:validThrough value as date literal."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VALIDFROM:
            try:
                self.setValidFrom(statement.getObject().toPython())
            except:
                print "Unable to interpret gr:validFrom value as date literal."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VALUEADDEDTAXINCLUDED:
            try:
                self.setValueAddedTaxIncluded(statement.getBoolean())
            except:
                print "Unable to interpret gr:valueAddedTaxIncluded value as literal boolean."
                traceback.print_exc()
            return

        if predicate == PROPERTY.UNIT:
            try:
                self.setUnit(statement.getResource().toString())
            except:
                print "Unable to interpret seas:unit value as resource."
                traceback.print_exc()
            return

        if predicate == PROPERTY.QUANTITYKIND:
            try:
                self.setQuantity(statement.getResource().toString())
            except:
                print "Unable to interpret seas:quantityKind value as resource."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VATPERCENTAGE:
            self.setVatPercentage(Variant().parse(statement))
            return

        super(PriceSpecification, self).parseStatement(statement)
Пример #17
0
    def parseStatement(self, statement):

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

        if predicate == PROPERTY.HASCURRENCY:
            try:
                self.setCurrency(statement.getString())
            except:
                print "Unable to interpret gr:hasCurrency value as literal string."
                traceback.print_exc()
            return

        if predicate == PROPERTY.HASCURRENCYVALUE:
            try:
                self.setCurrencyValue(statement.getFloat())
            except:
                print "Unable to interpret gr:hasCurrencyValue value as literal float."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VALIDTHROUGH:
            try:
                self.setValidThrough(statement.getObject().toPython())
            except:
                print "Unable to interpret gr:validThrough value as date literal."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VALIDFROM:
            try:
                self.setValidFrom(statement.getObject().toPython())
            except:
                print "Unable to interpret gr:validFrom value as date literal."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VALUEADDEDTAXINCLUDED:
            try:
                self.setValueAddedTaxIncluded(statement.getBoolean())
            except:
                print "Unable to interpret gr:valueAddedTaxIncluded value as literal boolean."
                traceback.print_exc()
            return

        if predicate == PROPERTY.UNIT:
            try:
                self.setUnit(statement.getResource().toString())
            except:
                print "Unable to interpret seas:unit value as resource."
                traceback.print_exc()
            return

        if predicate == PROPERTY.QUANTITYKIND:
            try:
                self.setQuantity(statement.getResource().toString())
            except:
                print "Unable to interpret seas:quantityKind value as resource."
                traceback.print_exc()
            return

        if predicate == PROPERTY.VATPERCENTAGE:
            self.setVatPercentage(Variant().parse(statement))
            return

        super(PriceSpecification, self).parseStatement(statement)
Пример #18
0
 def setVatPercentage(self, vp):
     if isinstance(vp, Variant):
         self.vatPercentage = vp
     else:  # if int, double,...
         self.vatPercentage = Variant(vp)
Пример #19
0
 def setInstant(self, instant):
     self.instant = Variant(instant)
Пример #20
0
 def addAnd(self, a):
     if isinstance(a, Obj):
         a = Variant(a)
     self.ands.append(a)
Пример #21
0
 def setPercentage(self, p):
     if not isinstance(p, Variant):
         self.percentage = Variant(p)
     else:
         self.percentage = p
Пример #22
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)