Beispiel #1
0
 def from_grpc_value_object(grpc_value_object):
     whichone = grpc_value_object.WhichOneof('value')
     # check the one is in the known datatypes
     known_datatypes = [e.name.lower() for e in enums.DataType]
     if whichone.lower() not in known_datatypes:
         raise ClientError(
             "Unknown value object value key: {0}, not in {1}".format(
                 whichone, known_datatypes))
     if whichone == 'string':
         return grpc_value_object.string
     elif whichone == 'boolean':
         return grpc_value_object.boolean
     elif whichone == 'integer':
         return grpc_value_object.integer
     elif whichone == 'long':
         return grpc_value_object.long
     elif whichone == 'float':
         return grpc_value_object.float
     elif whichone == 'double':
         return grpc_value_object.double
     elif whichone == 'date':
         return grpc_value_object.date
     else:
         raise ClientError(
             "Unknown datatype in enum but not handled in from_grpc_value_object"
         )
Beispiel #2
0
 def sup(self, super_concept=None):
     """ 
     Get or set super schema concept.
     If used as a setter returns self
     """
     if super_concept is None:
         # get direct super schema concept
         get_sup_req = RequestBuilder.ConceptMethod.SchemaConcept.get_sup()
         method_response = self._tx_service.run_concept_method(
             self.id, get_sup_req)
         get_sup_response = method_response.schemaConcept_getSup_res
         # check if received a Null or Concept
         whichone = get_sup_response.WhichOneof('res')
         if whichone == 'schemaConcept':
             grpc_schema_concept = get_sup_response.schemaConcept
             concept = ConceptFactory.create_concept(
                 self._tx_service, grpc_schema_concept)
             return concept
         elif whichone == 'null':
             return None
         else:
             raise ClientError(
                 "Unknown response concent for getting super schema concept: {0}"
                 .format(whichone))
     else:
         # set direct super SchemaConcept of this SchemaConcept
         set_sup_req = RequestBuilder.ConceptMethod.SchemaConcept.set_sup(
             super_concept)
         method_response = self._tx_service.run_concept_method(
             self.id, set_sup_req)
         return self
Beispiel #3
0
    def send(self, request):
        if self._closed:
            # TODO integrate this into TransactionService to throw a "Transaction is closed" rather than "connection is closed..."
            raise ClientError("This connection is closed")
        try:
            self._add_request(request)
            response = next(self._response_iterator)
        except Exception as e:  # specialize into different gRPC exceptions?
            # on any GRPC exception, close the stream
            self.close()
            raise ClientError(
                "Server/network error: {0}\n\n generated from request: {1}".
                format(e, request))

        if response is None:
            raise ClientError("No response received")

        return response
Beispiel #4
0
 def get_schema_concept(tx_service, grpc_get_concept):
     which_one = grpc_get_concept.WhichOneof("res")
     if which_one == "schemaConcept":
         grpc_concept = grpc_get_concept.schemaConcept
         return ConceptFactory.create_concept(tx_service, grpc_concept)
     elif which_one == "null":
         return None
     else:
         raise ClientError(
             "Unknown get_schema_concept response: {0}".format(which_one))
Beispiel #5
0
 def collect_concepts(self):
     """ Helper method to retrieve concepts from a query() method """
     concepts = []
     for answer in self:
         if type(answer) != ConceptMap:
             raise ClientError(
                 "Only use .collect_concepts on ConceptMaps returned by query()"
             )
         concepts.extend(
             answer.map().values())  # get concept map => concepts
     return concepts
Beispiel #6
0
 def get(self, var=None):
     """ Get the indicated variable's Concept from the map or this ConceptMap """
     if var is None:
         return self
     else:
         if var not in self._concept_map:
             # TODO specialize exception
             raise ClientError(
                 "Variable {0} is not in the ConceptMap".format(var))
         return self._concept_map[var]
         """ Return ConceptMap """
         return self
Beispiel #7
0
 def get_then(self):
     """ Retrieve the `then` clause for this rule """
     then_req = RequestBuilder.ConceptMethod.Rule.then()
     method_response = self._tx_service.run_concept_method(
         self.id, then_req)
     response = method_response.rule_then_res
     whichone = response.WhichOneof('res')
     if whichone == 'pattern':
         return response.pattern
     elif whichone == 'null':
         return None
     else:
         raise ClientError(
             "Unknown field in get_then or `rule`: {0}".format(whichone))
Beispiel #8
0
 def data_type(self):
     """ Get the DataType enum (grakn.DataType) corresponding to the type of this attribute """
     get_data_type_req = RequestBuilder.ConceptMethod.AttributeType.data_type(
     )
     method_response = self._tx_service.run_concept_method(
         self.id, get_data_type_req)
     response = method_response.attributeType_dataType_res
     whichone = response.WhichOneof('res')
     if whichone == 'dataType':
         # iterate over enum DataType enum to find matching data type
         for e in enums.DataType:
             if e.value == response.dataType:
                 return e
         else:
             # loop exited normally
             raise ClientError("Reported datatype NOT in enum: {0}".format(
                 response.dataType))
     elif whichone == 'null':
         return None
     else:
         raise ClientError(
             "Unknown datatype response for AttributeType: {0}".format(
                 whichone))
Beispiel #9
0
    def transaction(self, tx_type):
        """ Open a transaction to Grakn on this keyspace

        Can be used as `with session.transaction(grakn.TxType.READ) as tx: ...`
        Don't forget to commit within the `with`!
        Alternatively you can still do `tx = session.transaction(...); ...; tx.close()`

        :param grakn.TxType tx_type: The type of transaction to open as indicated by the tx_type enum
        """
        if self._closed:
            raise ClientError("Session is closed")

        # create a transaction service which hides GRPC usage
        transaction_service = TransactionService(self.keyspace, tx_type,
                                                 self.credentials,
                                                 self._stub.transaction)
        return Transaction(transaction_service)
Beispiel #10
0
 def attribute(self, value):
     """ Retrieve an attribute instance by value if it exists """
     self_data_type = self.data_type()
     get_attribute_req = RequestBuilder.ConceptMethod.AttributeType.attribute(
         value, self_data_type)
     method_response = self._tx_service.run_concept_method(
         self.id, get_attribute_req)
     response = method_response.attributeType_attribute_res
     whichone = response.WhichOneof('res')
     if whichone == 'attribute':
         return ConceptFactory.create_concept(self._tx_service,
                                              response.attribute)
     elif whichone == 'null':
         return None
     else:
         raise ClientError(
             "Unknown `res` key in AttributeType `attribute` response: {0}".
             format(whichone))
Beispiel #11
0
    def convert(tx_service, grpc_answer):
        which_one = grpc_answer.WhichOneof('answer')

        if which_one == 'conceptMap':
            return AnswerConverter._create_concept_map(tx_service,
                                                       grpc_answer.conceptMap)
        elif which_one == 'answerGroup':
            return AnswerConverter._create_answer_group(
                tx_service, grpc_answer.answerGroup)
        elif which_one == 'conceptList':
            return AnswerConverter._create_concept_list(
                tx_service, grpc_answer.conceptList)
        elif which_one == 'conceptSet':
            return AnswerConverter._create_concept_set(tx_service,
                                                       grpc_answer.conceptSet)
        elif which_one == 'conceptSetMeasure':
            return AnswerConverter._create_concept_set_measure(
                tx_service, grpc_answer.conceptSetMeasure)
        elif which_one == 'value':
            return AnswerConverter._create_value(tx_service, grpc_answer.value)
        else:
            raise ClientError(
                'Unknown gRPC Answer.answer message type: {0}'.format(
                    which_one))