Add sample search for Adapt registration
authorjani <jani@asema.com>
Mon, 5 Aug 2019 19:45:42 +0000 (22:45 +0300)
committerjani <jani@asema.com>
Mon, 5 Aug 2019 19:45:42 +0000 (22:45 +0300)
Common/Python/SmartAPI/agents/Agent.py
Common/Python/SmartAPI/agents/SearchAgent.py
Common/Python/SmartAPI/model/Authorization.py
Examples/Python/AdaptDataService/AdaptDataService.py
Examples/Python/AdaptDataService/SampleSearchClient.py [new file with mode: 0755]

index a4011e0e4eabed37d9c6eb504cf1b114166385ad..6bccbb7319dafe0bf3a5f6715924d8b741813532 100755 (executable)
@@ -44,16 +44,13 @@ class Agent(object):
     def getServerAddress(self):
         return self.serverAddress
     
-    
     def setServerAddress(self, serverAddress):
         print "setting address", self, serverAddress
         self.serverAddress = serverAddress
     
-    
     def debugMode(self):
         return self.debug
     
-    
     def setDebugMode(self, mode):
         self.debug = mode
     
index 72651a48370a5e172f06abd61ebca97b7eb3b8e0..fdd3ba422a84e8072e43983eda8a0935ce0ffe1a 100755 (executable)
@@ -9,6 +9,7 @@ import urllib, urllib2
 import datetime
 from pytz import timezone
 from urlparse import urlparse
+from SmartAPI.agents.Agent import Agent
 from SmartAPI.model.Entity import Entity
 from SmartAPI.model.Activity import Activity
 from SmartAPI.model.Coordinates import Coordinates
@@ -30,11 +31,12 @@ import traceback
 from rdflib.term import URIRef
 
 
-class SearchAgent(object):
+class SearchAgent(Agent):
     remoteURL = FIND_URI
     debug = False
     
     def __init__(self, myUri=''):
+        Agent.__init__(self)
         self.generatedBy = myUri
         self.entity = Entity()
         #self.entity.clearTypes()
@@ -44,7 +46,13 @@ class SearchAgent(object):
     @classmethod
     def setRegistrationServiceUri(cls, serverURI):
         cls.setServerAddress(serverURI)
-        
+    
+    def setEntity(self, e):
+        self.entity = e
+    
+    def doSearch(self):
+        return self.search()
+    
     def search(self):
         from SmartAPI.model.Response import Response
         
@@ -147,7 +155,7 @@ class SearchAgent(object):
         return agent.search()
     
     @classmethod
-    def fetchBySmartAPIId(cls, myId, idToFetch): 
+    def fetchBySmartAPIId(cls, myId, idToFetch):
         '''
         only return one entity if there is.
         '''  
@@ -157,7 +165,7 @@ class SearchAgent(object):
         agent.entity = e
         res = agent.search()
         if res is not None and len(res) > 0:
-            return res[0]   
+            return res[0]
         
     @classmethod
     def searchByPoint(cls, myId, latitude, longitude, distanceInKm, entityType=None):
@@ -269,14 +277,14 @@ class SearchAgent(object):
                 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.
@@ -299,7 +307,6 @@ class SearchAgent(object):
         duration = Duration(minutes=minutes)
         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 
index 852135a80edd822cae276485dd8d457909b19a60..5a7e051b5e0b10e5fe22e344c00cc9b0061a564d 100644 (file)
@@ -38,6 +38,7 @@ class Authorization(Obj):
                self.methods.append(m)
                
        def _parseStatement(self, statement, custom_classes = None):
+               from SmartAPI.model.Person import Person
                self.parse_property(statement, PROPERTY.PERSON, self.setPerson, Person, custom_classes = custom_classes)
                self.parse_property(statement, PROPERTY.METHOD, self.addMethod, Variant, custom_classes = custom_classes)
                super(Authorization, self)._parseStatement(statement, custom_classes = custom_classes)
index 38e0a53d5cd1fd51355ff13cdec2fdd8a41ba0b7..1306536e37543c3cee4c8e940b9e6cffc9330c54 100755 (executable)
@@ -28,6 +28,7 @@ from SmartAPI.model.ValueObject import ValueObject
 myIdentity = "http://adapt.asema.com/demos/python/datasource/"
 adaptServiceIdentity = "http://adapt.asema.com"
 #registrationServerUri = "http://find.smart-api.io/smart/v1.0e1.0/access"
+#registrationServerKeyUri = "http://find.smart-api.io/smart/v1.0e1.0/key"
 registrationServerUri = "http://192.168.2.96:8080/smartapifind-core/smart/v1.0e1.0/access"
 registrationServerKeyUri = "http://192.168.2.96:8080/smartapifind-core/smart/v1.0e1.0/key"
 
diff --git a/Examples/Python/AdaptDataService/SampleSearchClient.py b/Examples/Python/AdaptDataService/SampleSearchClient.py
new file mode 100755 (executable)
index 0000000..fe0d0c3
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/python
+
+from SmartAPI.agents.SearchAgent import SearchAgent
+
+from SmartAPI.common.Tools import Tools
+from SmartAPI.common.RESOURCE import RESOURCE
+
+from SmartAPI.model.Activity import Activity
+from SmartAPI.model.Condition import Condition
+from SmartAPI.model.Entity import Entity
+
+
+myIdentity = "http://adapt.asema.com/demos/python/datasourcesearch/"
+adaptServiceIdentity = "http://adapt.asema.com"
+registrationServerUri = "http://find.smart-api.io/smart/v1.0e1.0/access"
+#registrationServerUri = "http://192.168.2.96:8080/smartapifind-core/smart/v1.0e1.0/access"
+
+
+class SampleSearch(object):
+
+       def __init__(self):
+               pass
+
+       def do(self):
+               agent = SearchAgent(myIdentity)
+               agent.setServerAddress(registrationServerUri)
+               agent.setDebugMode(True)
+
+               e = Entity()
+               e.setServedBy(adaptServiceIdentity)
+               agent.setEntity(e)
+               
+               result = agent.doSearch()
+               
+               if len(result) > 0:
+                       print "FOUND THE FOLLOWING:"
+                       for r in result:
+                               r.turtlePrint()
+               else:
+                       print "No results found."
+
+def main():
+       s = SampleSearch()
+       s.do()
+
+
+if __name__ == '__main__':
+       main()
+