summaryrefslogtreecommitdiff
path: root/lib/kolab_api_service.php
diff options
context:
space:
mode:
authorAleksander Machniak <machniak@kolabsys.com>2014-05-07 11:26:06 (GMT)
committerAleksander Machniak <machniak@kolabsys.com>2014-05-07 11:26:06 (GMT)
commit9dd7e69e631b63c35f5c575e37548f3c7f0cd890 (patch)
tree44ab8a9d4c651ad738d2e395b88e787bc481e695 /lib/kolab_api_service.php
parent94caeb4a74c5567475ca8e21a032cd71b2f4f716 (diff)
downloadkolab-wap-9dd7e69e631b63c35f5c575e37548f3c7f0cd890.tar.gz
Improve object type detection by degrading class score if object contains
more attributes than defined in object type
Diffstat (limited to 'lib/kolab_api_service.php')
-rw-r--r--lib/kolab_api_service.php21
1 files changed, 15 insertions, 6 deletions
diff --git a/lib/kolab_api_service.php b/lib/kolab_api_service.php
index 7b4ba62..2c57d9d 100644
--- a/lib/kolab_api_service.php
+++ b/lib/kolab_api_service.php
@@ -111,8 +111,9 @@ abstract class kolab_api_service
}
$object_class = array_map('strtolower', $object_class);
- $object_keys = array_keys($attributes);
+ $object_keys = array_diff(array_keys($attributes), array(self::unique_attribute()));
$keys_count = count($object_keys);
+ $class_count = count($object_class);
$type_score = null;
$type_id = null;
@@ -129,6 +130,7 @@ abstract class kolab_api_service
$elem_keys_score = 0;
$elem_values_score = 0;
+ $delta = 0;
// Eliminate the duplicates between the $data_ocs and $ref_ocs
$_object_class = array_diff($object_class, $ref_class);
@@ -136,8 +138,8 @@ abstract class kolab_api_service
// Object classes score
$differences = count($_object_class) + count($_ref_class);
- $commonalities = count($object_class) - $differences;
- $elem_score = $differences > 0 ? ($commonalities / $differences) : $commonalities;
+ $commonalities = $class_count - $differences;
+ $elem_score = $differences > 0 ? round($commonalities / $differences, 2) : $commonalities;
// Attributes score
if ($keys_count) {
@@ -184,11 +186,18 @@ abstract class kolab_api_service
}
}
+ // degrade class score if object contains more attributes
+ // than defined in object type
+ if ($keys_count && $elem_keys_score < $keys_count) {
+ $delta -= $class_count - round(($keys_count / $elem_keys_score) * $class_count, 2);
+ if ($delta > 0) {
+ $elem_score -= $delta;
+ }
+ }
+
$elem_score .= ':' . $elem_keys_score . ':' . $elem_values_score;
-// Log::trace("\$object_class not in \$ref_class (" . $elem['key'] . "): " . implode(", ", $_object_class));
-// Log::trace("\$ref_class not in \$object_class (" . $elem['key'] . "): " . implode(", ", $_ref_class));
- Log::trace("Score for $object_name type " . $elem['name'] . ": " . $elem_score . " (" . $commonalities . "/" . $differences . ")");
+ Log::trace("Score for $object_name type " . $elem['name'] . ": $elem_score ($commonalities/$differences/$delta)");
// Compare last and current element (object type) score
if ($this->score_compare($elem_score, $type_score)) {