# Copyright (C) 2018-2019 DLR
#
# All rights reserved. This program and the accompanying materials are made
# available under the terms of the 3-Clause BSD License which accompanies this
# distribution, and is available at
# https://opensource.org/licenses/BSD-3-Clause
#
# Contributors:
# Christoph Suerig <[email protected]>
# Don't connect with the Copyright comment above!
# Version: 07.06.2019
from rafcon.utils import log
logger = log.get_logger(__name__)
class PredicateMerger:
"""
This class merges predicates with the same identifier, but different types of the same branch together.
"""
__name_index = 0
def __init__(self, datastore):
"""
:param datastore: A datastore containing available types.
"""
self.__datastore = datastore
def merge_predicates(self, predicates):
"""
Merge predicates merges all predicates, sets all available predicates in datastore
and returns all merged predicates as strings.
:param predicates: A list [string] with predicates.
:return: ([String],[(String,[(String,int)])]): A tuple (list [string] with merged predicates, and a list [of format ('LOCATED',[(VEHICLE,1),(PHYSOBJ,3)])] all available predicates)
"""
if predicates is None:
raise ValueError('predicates can not be None!')
preds_map = {}
available_predicates = []
merged_preds_as_string = []
for predicate in predicates:
parsed_pred = self.__parse_predicate(predicate)
if parsed_pred[0] in preds_map.keys():
preds_map[parsed_pred[0]].append(parsed_pred)
else:
preds_map[parsed_pred[0]] = [parsed_pred]
for key in preds_map.keys():
c_pred = self.__reduce_predicate_list(preds_map[key])
available_predicates.append(c_pred)
merged_preds_as_string.append(self.__tuple_to_predicate_string(c_pred))
return (merged_preds_as_string, available_predicates)
def __parse_predicate(self, predicate_string):
"""
parse_predicate gets a predicate string and parses it into a useful tuple of (predicate_Name,[(type_name,occurance)]).
:param predicate_string: A predicate as string e.g (LOCATED ?VEH - VEHICLE ?OBJ ?sObj ?thirdObj - PHYSOBJ).
:return: (String,[(String,int)]): A parsed predicate as tuple e.g. ('LOCATED',[(VEHICLE,1),(PHYSOBJ,3)]).
"""
pred_name = None
pred_types = []
pred = predicate_string
if '(' in predicate_string and '?' in predicate_string:
start = predicate_string.index('(')
end = predicate_string.index('?')
if start + 1 >= end - 1:
logger.error("Can't parse predicate: " + predicate_string)
raise ValueError("Can't parse predicate: " + predicate_string)
pred_name = predicate_string[start + 1:end - 1].replace(' ', '')
pred = predicate_string[end:]
else:
logger.error("Can't parse predicate: " + predicate_string)
raise ValueError("Can't parse predicate: " + predicate_string)
if not '-' in pred:
logger.error("Can't parse predicate: " + predicate_string)
raise ValueError("Can't parse predicate: " + predicate_string)
while '-' in pred:
c_type_s = pred.index('-') + 1
c_type_e = 0
if '?' in pred[c_type_s:]:
c_type_e += pred[c_type_s:].index('?') + c_type_s
elif ')' in pred:
c_type_e += pred.index(')')
else:
logger.error("Can't parse predicate: " + predicate_string)
raise ValueError("Can't parse predicate: " + predicate_string)
pred_types.append((pred[c_type_s:c_type_e].replace(' ', ''), pred[:c_type_e].count('?')))
pred = pred[c_type_e:]
return (pred_name, pred_types)
def __reduce_predicate_list(self, predicate_list):
"""
reduce_predicate_list gets a list of predicates, with the same name but different types,
and reduces them to one predicate, with the most open types.
:param predicate_list: A list of predicates with the same name, format: ('LOCATED',[(VEHICLE,1),(PHYSOBJ,3)])
:return: (String,[(String,int)]): One predicate tuple, containing the most general types. of format ('LOCATED',[(VEHICLE,1),(PHYSOBJ,3)])
"""
type_tree = self.__datastore.get_available_types()
# the resulting predicate
result_predicate = predicate_list[0]
for predicate in predicate_list:
err_str = "Can't merge predicates, they are Incompatible! (variable names where changed) first: " + \
self.__tuple_to_predicate_string(result_predicate) + \
" second: " + self.__tuple_to_predicate_string(predicate)
# cant merge, if they have different names, or different number of argument types.
if result_predicate[0] != predicate[0] or len(result_predicate[1]) != len(predicate[1]):
logger.error(err_str)
raise ValueError(err_str)
for index, type_tuple in enumerate(predicate[1]):
# cant merge, if they have different number of arguments per type.
if type_tuple[1] != result_predicate[1][index][1]:
logger.error(err_str)
raise ValueError(err_str)
# try to merge the types used in predicates e.g. Robot or Vehicle
if result_predicate[1][index][0] != type_tuple[0]:
smallest_parent = type_tree.get_smallest_parent(type_tuple[0], result_predicate[1][index][0])
if smallest_parent:
# set smallest_parent type as predicate type
result_predicate[1][index] = (smallest_parent, result_predicate[1][index][1])
# just to warn the user, that a predicate is a root-type-predicate.
if (type_tree.get_parent_of(smallest_parent) is None):
logger.warn('Predicate merged to root Type predicate: ' + self.__tuple_to_predicate_string(
result_predicate))
else:
logger.error(err_str)
raise ValueError(err_str)
return result_predicate
def __tuple_to_predicate_string(self, predicate_tuple):
"""
Receives a predicate tuple and returns it as predicate string.
:param predicate_tuple: A tuple in format (PREDICATE_NAME,[(TYPE,NUM_VARIABLES)])
:return: String: A predicate string e.g (PREDICATENAME ?0 ?1 - Type).
"""
pred_string = '(' + predicate_tuple[0]
tuple_counter = 0 # need this counter do guarantee distinct variable names.
for type_tup in predicate_tuple[1]:
variable_counter = 0
tuple_counter += 1
while variable_counter < type_tup[
1]: # NUM_VARIABLES: type_tup[1] contains the number of variables of one type.
pred_string += ' ?' + type_tup[0][:1] + str(tuple_counter) + str(variable_counter)
variable_counter += 1
pred_string += ' - ' + type_tup[0]
pred_string += ')'
return str(pred_string)
# testing/exclusions.py
# Copyright (C) 2005-2014 the SQLAlchemy authors and contributors <see AUTHORS file>
#
# This module is part of SQLAlchemy and is released under
# the MIT License: http://www.opensource.org/licenses/mit-license.php
import operator
from .plugin.plugin_base import SkipTest
from ..util import decorator
from . import config
from .. import util
import contextlib
import inspect
class skip_if(object):
def __init__(self, predicate, reason=None):
self.predicate = _as_predicate(predicate)
self.reason = reason
_fails_on = None
def __add__(self, other):
def decorate(fn):
return other(self(fn))
return decorate
@property
def enabled(self):
return self.enabled_for_config(config._current)
def enabled_for_config(self, config):
return not self.predicate(config)
@contextlib.contextmanager
def fail_if(self, name='block'):
try:
yield
except Exception as ex:
if self.predicate(config._current):
print(("%s failed as expected (%s): %s " % (
name, self.predicate, str(ex))))
else:
raise
else:
if self.predicate(config._current):
raise AssertionError(
"Unexpected success for '%s' (%s)" %
(name, self.predicate))
def __call__(self, fn):
@decorator
def decorate(fn, *args, **kw):
if self.predicate(config._current):
if self.reason:
msg = "'%s' : %s" % (
fn.__name__,
self.reason
)
else:
msg = "'%s': %s" % (
fn.__name__, self.predicate
)
raise SkipTest(msg)
else:
if self._fails_on:
with self._fails_on.fail_if(name=fn.__name__):
return fn(*args, **kw)
else:
return fn(*args, **kw)
return decorate(fn)
def fails_on(self, other, reason=None):
self._fails_on = skip_if(other, reason)
return self
def fails_on_everything_except(self, *dbs):
self._fails_on = skip_if(fails_on_everything_except(*dbs))
return self
class fails_if(skip_if):
def __call__(self, fn):
@decorator
def decorate(fn, *args, **kw):
with self.fail_if(name=fn.__name__):
return fn(*args, **kw)
return decorate(fn)
def only_if(predicate, reason=None):
predicate = _as_predicate(predicate)
return skip_if(NotPredicate(predicate), reason)
def succeeds_if(predicate, reason=None):
predicate = _as_predicate(predicate)
return fails_if(NotPredicate(predicate), reason)
class Predicate(object):
@classmethod
def as_predicate(cls, predicate):
if isinstance(predicate, skip_if):
return NotPredicate(predicate.predicate)
elif isinstance(predicate, Predicate):
return predicate
elif isinstance(predicate, list):
return OrPredicate([cls.as_predicate(pred) for pred in predicate])
elif isinstance(predicate, tuple):
return SpecPredicate(*predicate)
elif isinstance(predicate, util.string_types):
tokens = predicate.split(" ", 2)
op = spec = None
db = tokens.pop(0)
if tokens:
op = tokens.pop(0)
if tokens:
spec = tuple(int(d) for d in tokens.pop(0).split("."))
return SpecPredicate(db, op, spec)
elif util.callable(predicate):
return LambdaPredicate(predicate)
else:
assert False, "unknown predicate type: %s" % predicate
class BooleanPredicate(Predicate):
def __init__(self, value, description=None):
self.value = value
self.description = description or "boolean %s" % value
def __call__(self, config):
return self.value
def _as_string(self, negate=False):
if negate:
return "not " + self.description
else:
return self.description
def __str__(self):
return self._as_string()
class SpecPredicate(Predicate):
def __init__(self, db, op=None, spec=None, description=None):
self.db = db
self.op = op
self.spec = spec
self.description = description
_ops = {
'<': operator.lt,
'>': operator.gt,
'==': operator.eq,
'!=': operator.ne,
'<=': operator.le,
'>=': operator.ge,
'in': operator.contains,
'between': lambda val, pair: val >= pair[0] and val <= pair[1],
}
def __call__(self, config):
engine = config.db
if "+" in self.db:
dialect, driver = self.db.split('+')
else:
dialect, driver = self.db, None
if dialect and engine.name != dialect:
return False
if driver is not None and engine.driver != driver:
return False
if self.op is not None:
assert driver is None, "DBAPI version specs not supported yet"
version = _server_version(engine)
oper = hasattr(self.op, '__call__') and self.op \
or self._ops[self.op]
return oper(version, self.spec)
else:
return True
def _as_string(self, negate=False):
if self.description is not None:
return self.description
elif self.op is None:
if negate:
return "not %s" % self.db
else:
return "%s" % self.db
else:
if negate:
return "not %s %s %s" % (
self.db,
self.op,
self.spec
)
else:
return "%s %s %s" % (
self.db,
self.op,
self.spec
)
def __str__(self):
return self._as_string()
class LambdaPredicate(Predicate):
def __init__(self, lambda_, description=None, args=None, kw=None):
spec = inspect.getargspec(lambda_)
if not spec[0]:
self.lambda_ = lambda db: lambda_()
else:
self.lambda_ = lambda_
self.args = args or ()
self.kw = kw or {}
if description:
self.description = description
elif lambda_.__doc__:
self.description = lambda_.__doc__
else:
self.description = "custom function"
def __call__(self, config):
return self.lambda_(config)
def _as_string(self, negate=False):
if negate:
return "not " + self.description
else:
return self.description
def __str__(self):
return self._as_string()
class NotPredicate(Predicate):
def __init__(self, predicate):
self.predicate = predicate
def __call__(self, config):
return not self.predicate(config)
def __str__(self):
return self.predicate._as_string(True)
class OrPredicate(Predicate):
def __init__(self, predicates, description=None):
self.predicates = predicates
self.description = description
def __call__(self, config):
for pred in self.predicates:
if pred(config):
self._str = pred
return True
return False
_str = None
def _eval_str(self, negate=False):
if self._str is None:
if negate:
conjunction = " and "
else:
conjunction = " or "
return conjunction.join(p._as_string(negate=negate)
for p in self.predicates)
else:
return self._str._as_string(negate=negate)
def _negation_str(self):
if self.description is not None:
return "Not " + (self.description % {"spec": self._str})
else:
return self._eval_str(negate=True)
def _as_string(self, negate=False):
if negate:
return self._negation_str()
else:
if self.description is not None:
return self.description % {"spec": self._str}
else:
return self._eval_str()
def __str__(self):
return self._as_string()
_as_predicate = Predicate.as_predicate
def _is_excluded(db, op, spec):
return SpecPredicate(db, op, spec)(config._current)
def _server_version(engine):
"""Return a server_version_info tuple."""
# force metadata to be retrieved
conn = engine.connect()
version = getattr(engine.dialect, 'server_version_info', ())
conn.close()
return version
def db_spec(*dbs):
return OrPredicate(
[Predicate.as_predicate(db) for db in dbs]
)
def open():
return skip_if(BooleanPredicate(False, "mark as execute"))
def closed():
return skip_if(BooleanPredicate(True, "marked as skip"))
def fails():
return fails_if(BooleanPredicate(True, "expected to fail"))
@decorator
def future(fn, *arg):
return fails_if(LambdaPredicate(fn), "Future feature")
def fails_on(db, reason=None):
return fails_if(SpecPredicate(db), reason)
def fails_on_everything_except(*dbs):
return succeeds_if(
OrPredicate([
SpecPredicate(db) for db in dbs
])
)
def skip(db, reason=None):
return skip_if(SpecPredicate(db), reason)
def only_on(dbs, reason=None):
return only_if(
OrPredicate([SpecPredicate(db) for db in util.to_list(dbs)])
)
def exclude(db, op, spec, reason=None):
return skip_if(SpecPredicate(db, op, spec), reason)
def against(config, *queries):
assert queries, "no queries sent!"
return OrPredicate([
Predicate.as_predicate(query)
for query in queries
])(config)
"""
This module houses the GEOS ctypes prototype functions for the
unary and binary predicate operations on geometries.
"""
from ctypes import c_char, c_char_p, c_double
from django.contrib.gis.geos.libgeos import GEOM_PTR, GEOSFuncFactory
from django.contrib.gis.geos.prototypes.errcheck import check_predicate
# ## Binary & unary predicate factories ##
class UnaryPredicate(GEOSFuncFactory):
"For GEOS unary predicate functions."
argtypes = [GEOM_PTR]
restype = c_char
errcheck = staticmethod(check_predicate)
class BinaryPredicate(UnaryPredicate):
"For GEOS binary predicate functions."
argtypes = [GEOM_PTR, GEOM_PTR]
# ## Unary Predicates ##
geos_hasz = UnaryPredicate('GEOSHasZ')
geos_isclosed = UnaryPredicate('GEOSisClosed')
geos_isempty = UnaryPredicate('GEOSisEmpty')
geos_isring = UnaryPredicate('GEOSisRing')
geos_issimple = UnaryPredicate('GEOSisSimple')
geos_isvalid = UnaryPredicate('GEOSisValid')
# ## Binary Predicates ##
geos_contains = BinaryPredicate('GEOSContains')
geos_covers = BinaryPredicate('GEOSCovers')
geos_crosses = BinaryPredicate('GEOSCrosses')
geos_disjoint = BinaryPredicate('GEOSDisjoint')
geos_equals = BinaryPredicate('GEOSEquals')
geos_equalsexact = BinaryPredicate('GEOSEqualsExact', argtypes=[GEOM_PTR, GEOM_PTR, c_double])
geos_intersects = BinaryPredicate('GEOSIntersects')
geos_overlaps = BinaryPredicate('GEOSOverlaps')
geos_relatepattern = BinaryPredicate('GEOSRelatePattern', argtypes=[GEOM_PTR, GEOM_PTR, c_char_p])
geos_touches = BinaryPredicate('GEOSTouches')
geos_within = BinaryPredicate('GEOSWithin')
String.prototype.trim = function () {
return this.replace(/(^\s*)|(\s*$)/g, "");
}
Array.prototype.indexOf = function (valueOrPredicate) {
var predicate = getFunction(valueOrPredicate);
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
return i;
}
}
return -1;
}
Array.prototype.lastIndexOf = function (valueOrPredicate) {
var predicate = getFunction(valueOrPredicate);
for (var i = this.length - 1; i >= 0 ; i--) {
var value = this[i];
if (predicate(value, i, this)) {
return i;
}
}
return -1;
}
Array.prototype.all = function (predicate) {
isFunction(predicate);
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (!predicate(value, i, this)) {
return false;
}
}
return true;
}
Array.prototype.any = function (predicate) {
isFunction(predicate);
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
return true;
}
}
return false;
}
Array.prototype.contains = function (valueOrPredicate) {
var predicate = getFunction(valueOrPredicate);
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
return true;
}
}
return false;
}
Array.prototype.count = function (predicate) {
if (typeof predicate != "function") {
return this.length;
}
var count = 0;
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value)) {
count++;
}
}
return count;
}
Array.prototype.distinct = function (predicate) {//æbugï¼å¯¹[1,"1",1]è¿ç±»åæ°ç»ï¼åç¬¦ä¸²åæ°å䏿 ·ï¼å¤çä¼åºé
var arr = [],
table = {},
i = 0,
len = this.length,
result;
if (typeof predicate == "undefined") {
for (; i < len; i++) {
result = this[i];
if (table[result] !== result) {
arr.push(result);
table[result] = result;
}
}
}
//else {
// for (; i < len; i++) {
// var value1 = this[i]
// value2 = this[i + 1];
// if (typeof value2 == "undefined") {
// break;
// }
// if (!predicate(value1, value2)) {
// arr.push(value1);
// arr.push(value2);
// }
// }
// if (arr.length == 0) {
// arr.push(this[i]);
// }
//}
return arr;
}
Array.prototype.first = function (predicate) {
if (typeof predicate == "undefined") {
return this[0];
}
else {
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
return value;
}
}
}
}
Array.prototype.select = function (predicate) {
if (typeof predicate == "undefined") {
return this;
}
else {
var newArray = [];
for (var i = 0; i < this.length; i++) {
var value = this[i];
newArray.push(predicate(value, i, this));
}
return newArray;
}
}
Array.prototype.skip = function (predicate) {
if (typeof predicate == "undefined") {
return this;
}
else if (typeof predicate == "number") {
return this.slice(predicate);
}
else {
var newArray = [];
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (!predicate(value, i, this)) {
newArray.push(value);
}
}
return newArray;
}
}
Array.prototype.take = function (predicate) {
if (typeof predicate == "undefined") {
return this;
}
else if (typeof predicate == "number") {
return this.slice(0, predicate);
}
else {
var newArray = [];
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
newArray.push(value);
}
}
return newArray;
}
}
Array.prototype.where = function (predicate) {
isFunction(predicate);
var newArray = [];
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
newArray.push(value);
}
}
return newArray;
}
Array.prototype.remove = function (valueOrPredicate) {
var predicate = getFunction(valueOrPredicate);
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
this.splice(i, 1);
i--;
}
}
return this;
}
Array.prototype.removeFirst = function (valueOrPredicate) {
var predicate = getFunction(valueOrPredicate);
for (var i = 0; i < this.length; i++) {
var value = this[i];
if (predicate(value, i, this)) {
this.splice(i, 1);
i--;
break;
}
}
return this;
}
Array.prototype.removeAll = function () {
this.length = 0;
return this;
}
Array.prototype.insert = function (value, index) {
if (typeof index != "number") {
throw index + " is not a number";
}
this.splice(index, 0, value);
return this;
}
Array.prototype.forEach = function (predicate) {
var predicate = getFunction(predicate);
for (var i = 0; i < this.length; i++) {
var value = this[i];
predicate(value, i, this);
}
return this;
}
//=========è¾
婿¹æ³===========
function isFunction(predicate) {
if (typeof predicate !== "function") {
throw predicate + " is not a function";
}
}
function getFunction(valueOrPredicate) {
return (typeof valueOrPredicate === "function")
? valueOrPredicate
: function (value) { return value === valueOrPredicate; };
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview ChromeVox predicates for the automation extension API.
*/
goog.provide('AutomationPredicate');
goog.provide('AutomationPredicate.Binary');
goog.provide('AutomationPredicate.Unary');
goog.scope(function() {
var RoleType = chrome.automation.RoleType;
/**
* @constructor
*/
AutomationPredicate = function() {};
/**
* @typedef {function(chrome.automation.AutomationNode) : boolean}
*/
AutomationPredicate.Unary;
/**
* @typedef {function(chrome.automation.AutomationNode,
* chrome.automation.AutomationNode) : boolean}
*/
AutomationPredicate.Binary;
/**
* Constructs a predicate given a role.
* @param {RoleType} role
* @return {AutomationPredicate.Unary}
*/
AutomationPredicate.withRole = function(role) {
return function(node) {
return node.role == role;
};
};
/** @type {AutomationPredicate.Unary} */
AutomationPredicate.checkBox = AutomationPredicate.withRole(RoleType.checkBox);
/** @type {AutomationPredicate.Unary} */
AutomationPredicate.comboBox = AutomationPredicate.withRole(RoleType.comboBox);
/** @type {AutomationPredicate.Unary} */
AutomationPredicate.editText = AutomationPredicate.withRole(RoleType.textField);
/** @type {AutomationPredicate.Unary} */
AutomationPredicate.heading = AutomationPredicate.withRole(RoleType.heading);
/** @type {AutomationPredicate.Unary} */
AutomationPredicate.inlineTextBox =
AutomationPredicate.withRole(RoleType.inlineTextBox);
/** @type {AutomationPredicate.Unary} */
AutomationPredicate.link = AutomationPredicate.withRole(RoleType.link);
/** @type {AutomationPredicate.Unary} */
AutomationPredicate.table = AutomationPredicate.withRole(RoleType.table);
/**
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.button = function(node) {
return /button/i.test(node.role);
};
/**
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.formField = function(node) {
switch (node.role) {
case 'button':
case 'buttonDropDown':
case 'checkBox':
case 'comboBox':
case 'date':
case 'dateTime':
case 'details':
case 'disclosureTriangle':
case 'form':
case 'menuButton':
case 'menuListPopup':
case 'popUpButton':
case 'radioButton':
case 'searchBox':
case 'slider':
case 'spinButton':
case 'switch':
case 'tab':
case 'textField':
case 'time':
case 'toggleButton':
case 'tree':
return true;
}
return false;
};
/**
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.landmark = function(node) {
switch (node.role) {
case 'application':
case 'banner':
case 'complementary':
case 'contentInfo':
case 'form':
case 'main':
case 'navigation':
case 'search':
return true;
}
return false;
};
/**
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.visitedLink = function(node) {
return node.state.visited;
};
/**
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.focused = function(node) {
return node.state.focused;
};
/**
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.leaf = function(node) {
return !node.firstChild ||
node.role == RoleType.button ||
node.role == RoleType.buttonDropDown ||
node.role == RoleType.popUpButton ||
node.role == RoleType.slider ||
node.role == RoleType.textField ||
node.children.every(function(n) {
return n.state.invisible;
});
};
/**
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.leafWithText = function(node) {
return AutomationPredicate.leaf(node) &&
!!(node.name || node.value);
};
/**
* @param {chrome.automation.AutomationNode} first
* @param {chrome.automation.AutomationNode} second
* @return {boolean}
*/
AutomationPredicate.linebreak = function(first, second) {
// TODO(dtseng): Use next/previousOnLin once available.
var fl = first.location;
var sl = second.location;
return fl.top != sl.top ||
(fl.top + fl.height != sl.top + sl.height);
};
/**
* Leaf nodes that should be ignored while traversing the automation tree. For
* example, apply this predicate when moving to the next element.
* @param {chrome.automation.AutomationNode} node
* @return {boolean}
*/
AutomationPredicate.shouldIgnoreLeaf = function(node) {
return AutomationPredicate.leaf(node) &&
(node.role == RoleType.client ||
node.role == RoleType.div ||
(node.role == 'image' && node.name == '') ||
(node.role == 'staticText' && node.value == ''));
};
}); // goog.scope
import React, { Component, PropTypes } from "react";
import Dropzone from "react-dropzone";
import isEqual from "lodash/isEqual";
import isEmpty from "lodash/isEmpty";
import uuidv4 from "uuid/v4";
import ProcessorListLabel from "./ProcessorListLabel";
import ProcessorEdit from "./ProcessorEdit";
import PropertyListItem from "./../PropertyListItem";
import "./../../style/Processors.less";
import * as R from "ramda";
const format = new ol.format.GeoJSON();
const processorStyle = new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(255, 0, 0, 0.1)"
}),
stroke: new ol.style.Stroke({
color: "#f00",
width: 1
})
});
const newPredicateStyle = new ol.style.Style({
fill: new ol.style.Fill({
color: "rgba(0, 0, 255, 0.1)"
}),
stroke: new ol.style.Stroke({
color: "#00f",
width: 1
})
});
class ProcessorView extends Component {
constructor(props) {
super(props);
this.state = {
addingPredicate: false,
editing: false,
editingPredicate: false,
editingProcessor: false,
creating: false,
drawing: false,
uploading: false,
fileUploaded: false,
uploadedFile: false,
uploadErr: false,
predicate_comparator: "geowithin",
activePredicates: {}
};
this.predicateLayers = {};
this.onSave = this.onSave.bind(this);
this.onCancel = this.onCancel.bind(this);
this.onDraw = this.onDraw.bind(this);
this.onUpload = this.onUpload.bind(this);
this.onDrop = this.onDrop.bind(this);
this.onDelete = this.onDelete.bind(this);
this.onAddPredicate = this.onAddPredicate.bind(this);
this.onPredicateComparatorChange = this.onPredicateComparatorChange.bind(
this
);
this.onEditProcessor = this.onEditProcessor.bind(this);
this.onCancelEditProcessor = this.onCancelEditProcessor.bind(this);
this.onEditProcessorSave = this.onEditProcessorSave.bind(this);
this.togglePredicate = this.togglePredicate.bind(this);
this.onEditPredicate = this.onEditPredicate.bind(this);
this.onDeletePredicate = this.onDeletePredicate.bind(this);
}
componentDidMount() {
this.createMap();
this.addPredicates(this.props.processor);
// window.addEventListeer("resize", () => {
// this.createMap();
// });
}
componentWillReceiveProps(nextProps) {
if (
!isEqual(
nextProps.processor.definition.predicates,
this.props.processor.definition.predicates
)
) {
this.addPredicates(nextProps.processor);
}
if (this.props.menu.open !== nextProps.menu.open) {
// wait for menu to transition
setTimeout(() => this.map.updateSize(), 200);
}
}
onCancel() {
this.map.removeInteraction(this.modify);
this.map.removeInteraction(this.create);
this.select.getFeatures().clear();
this.newPredicateSource.clear();
this.setState({
editing: false,
creating: false,
drawing: false,
uploading: false,
fileUploaded: false,
uploadErr: false,
uploadedFile: false,
editingPredicate: false,
editingProcessor: false
});
}
onSave() {
this.setState({
editing: false,
creating: false,
drawing: false,
uploading: false,
uploadedFile: false
});
this.map.removeInteraction(this.modify);
this.map.removeInteraction(this.create);
this.select.getFeatures().clear();
const fcId = `${this.props.processor.id}.${this.props.processor.definition
.predicates.length + 1}`;
const fs = this.newPredicateSource.getFeatures().map((f, i) => {
f.setId(`${fcId}.${i}`);
return f;
});
const gj = JSON.parse(
format.writeFeatures(fs, {
dataProjection: "EPSG:4326",
featureProjection: "EPSG:3857"
})
);
gj.id = fcId;
gj.features = gj.features.map(f => ({
...f,
properties: {}
}));
const newPredicate = {
id: uuidv4(),
definition: gj,
type: this.state.predicate_comparator
};
const { processor } = this.props;
const newProcessor = R.assocPath(
["definition", "predicates"],
R.concat(processor.definition.predicates, [newPredicate]),
this.props.processor
);
this.newPredicateSource.clear();
this.props.actions.updateProcessor(newProcessor);
}
onDraw() {
this.setState({ drawing: true });
this.map.addInteraction(this.create);
}
onUpload() {
this.setState({ uploading: true });
}
onDrop(acceptedFiles) {
if (acceptedFiles.length) {
const file = acceptedFiles[0];
const reader = new FileReader();
reader.onload = e => {
try {
const gj = JSON.parse(e.target.result);
this.setState({
uploadErr: false,
uploadedFile: file.name
});
const features = format.readFeatures(gj);
features.forEach(feature => {
feature.getGeometry().transform("EPSG:4326", "EPSG:3857");
this.newPredicateSource.addFeature(feature);
});
this.map
.getView()
.fit(this.newPredicateSource.getExtent(), this.map.getSize());
} catch (err) {
this.setState({ uploadErr: "Not valid GeoJSON" });
}
};
reader.readAsText(file);
}
}
onDelete() {
this.props.actions.deleteProcessor(this.props.processor);
}
onPredicateComparatorChange(e) {
this.setState({
predicate_comparator: e.target.value
});
}
onAddPredicate() {
this.setState({ creating: true });
}
onEditProcessor() {
this.setState({ editingProcessor: true });
}
onCancelEditProcessor() {
this.setState({ editingProcessor: false }, () => {
this.createMap();
});
}
onEditProcessorSave(processor) {
this.props.actions.updateProcessor(processor);
this.setState({ editingProcessor: false }, () => {
this.createMap();
});
}
onEditPredicate(predicate) {
const layer = this.predicateLayers[predicate.id];
const fs = layer
.getSource()
.getFeatures()
.map(f => f.clone());
this.setState({ editingPredicate: predicate.id });
this.select.getFeatures().clear();
this.newPredicateSource.clear();
this.map.getView().fit(layer.getSource().getExtent(), this.map.getSize());
this.map.removeLayer(layer);
this.newPredicateSource.addFeatures(fs);
this.modify = new ol.interaction.Modify({
features: new ol.Collection(this.newPredicateSource.getFeatures())
});
this.map.addInteraction(this.modify);
}
onSavePredicate(predicate) {
const fcId = `${this.props.processor.id}.${predicate.id}`;
const fs = this.newPredicateSource.getFeatures().map((f, i) => {
f.setId(`${fcId}.${i}`);
return f;
});
const gj = JSON.parse(
format.writeFeatures(fs, {
dataProjection: "EPSG:4326",
featureProjection: "EPSG:3857"
})
);
gj.id = fcId;
gj.features = gj.features.map(f => ({
...f,
properties: {}
}));
const newPredicate = {
...predicate,
rhs: gj
};
const newProcessor = {
...this.props.processor,
predicates: this.props.processor.definition.predicates.map(r => {
if (r.id === newPredicate.id) {
return newPredicate;
}
return r;
})
};
this.setState({
editingPredicate: false
});
this.map.removeInteraction(this.modify);
this.newPredicateSource.clear();
this.props.actions.updateProcessor(newProcessor);
}
onDeletePredicate(predicate) {
const predicates = this.props.processor.definition.predicates.filter(
r => r.id !== predicate.id
);
const newProcessor = R.assocPath(
["definition", "predicates"],
R.reject(
p => p.id === predicate.id,
this.props.processor.definition.predicates
),
this.props.processor
);
this.select.getFeatures().clear();
this.props.actions.updateProcessor(newProcessor);
}
onCancelPredicate(predicate) {
const layer = this.predicateLayers[predicate.id];
this.map.removeInteraction(this.modify);
this.newPredicateSource.clear();
this.map.addLayer(layer);
this.setState({
editingPredicate: false
});
}
createMap() {
while (this.mapRef.firstChild) {
this.mapRef.removeChild(this.mapRef.firstChild);
}
this.allPredicateSource = new ol.source.Vector();
this.newPredicateSource = new ol.source.Vector();
const newPredicateLayer = new ol.layer.Vector({
source: this.newPredicateSource,
style: newPredicateStyle
});
this.select = new ol.interaction.Select({
wrapX: false,
style: newPredicateStyle
});
this.modify = new ol.interaction.Modify({
features: new ol.Collection(this.newPredicateSource.getFeatures())
});
this.create = new ol.interaction.Draw({
source: this.newPredicateSource,
type: "Polygon"
});
this.map = new ol.Map({
target: this.mapRef,
interactions: ol.interaction.defaults().extend([this.select]),
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
newPredicateLayer
],
view: new ol.View({
center: ol.proj.fromLonLat([-100, 30]),
zoom: 3
})
});
this.addPredicates(this.props.processor);
}
addPredicates(processor) {
const { predicates } = this.props.processor.definition;
Object.keys(this.predicateLayers).forEach(layerid =>
this.map.removeLayer(this.predicateLayers[layerid])
);
this.predicateLayers = {};
if (predicates && predicates.length) {
predicates.forEach(predicate => {
if (predicate.type === "geowithin") {
this.addPredicate(predicate);
}
});
this.map
.getView()
.fit(this.allPredicateSource.getExtent(), this.map.getSize());
}
}
addPredicate(predicate) {
if (isEmpty(predicate)) return;
const predicateSource = new ol.source.Vector();
const features = format.readFeatures(predicate.definition);
features.forEach(feature => {
feature.getGeometry().transform("EPSG:4326", "EPSG:3857");
predicateSource.addFeature(feature);
this.allPredicateSource.addFeature(feature);
});
const layer = new ol.layer.Vector({
source: predicateSource,
style: processorStyle
});
this.predicateLayers[predicate.id] = layer;
this.map.addLayer(layer);
this.setState(prevState => ({
activePredicates: {
...prevState.activePredicates,
[predicate.id]: true
}
}));
}
togglePredicate(predicate) {
if (this.predicateLayers[predicate.id]) {
const layer = this.predicateLayers[predicate.id];
const active = this.state.activePredicates[predicate.id];
if (active) {
this.map.removeLayer(layer);
} else {
this.map.addLayer(layer);
}
this.setState(prevState => ({
activePredicates: {
...prevState.activePredicates,
[predicate.id]: !active
}
}));
}
}
viewPredicate(predicate) {
if (this.predicateLayers[predicate.id]) {
const layer = this.predicateLayers[predicate.id];
const fs = layer.getSource().getFeatures();
this.map.getView().fit(layer.getSource().getExtent(), this.map.getSize());
this.select.getFeatures().clear();
fs.forEach(f => this.select.getFeatures().push(f));
}
}
renderPredicates() {
const predicateList =
this.props.processor.definition.predicates === undefined ||
this.props.processor.definition.predicates.length === 0 ? (
<span className="note">
No predicates have been added to this processor.
</span>
) : (
this.props.processor.definition.predicates.map(p => (
<div className="form-item mini" key={p.id}>
<div className="properties">
<PropertyListItem name={"Type"} value={p.type} />
</div>
{this.state.editingPredicate === p.id ? (
<div className="btn-toolbar plain">
<span
className="btn-plain"
onClick={() => this.onSavePredicate(p)}
>
Save
</span>
<span
className="btn-plain"
onClick={() => this.onCancelPredicate(p)}
>
Cancel
</span>
</div>
) : (
<div className="btn-toolbar plain">
<span
className="btn-plain"
onClick={() => this.viewPredicate(p)}
>
View
</span>
<span
className="btn-plain"
onClick={() => this.onEditPredicate(p)}
>
Edit
</span>
<span
className="btn-plain"
onClick={() => this.onDeletePredicate(p)}
>
Delete
</span>
</div>
)}
</div>
))
);
return (
<div>
<h4>Predicates</h4>
<div className="predicate-list">{predicateList}</div>
{!this.state.creating && (
<div className="btn-toolbar">
<button className="btn btn-sc" onClick={this.onAddPredicate}>
Add Predicate
</button>
</div>
)}
</div>
);
}
renderEditing() {
return (
<div>
<div className="btn-toolbar">
<button className="btn btn-sc" onClick={this.onEditProcessor}>
Edit Processor
</button>
<button className="btn btn-danger" onClick={this.onDelete}>
Delete
</button>
</div>
</div>
);
}
renderCreating() {
const uploading = this.state.uploadedFile ? (
<span>{this.state.uploadedFile}</span>
) : (
<div>
<Dropzone
onDrop={this.onDrop}
multiple={false}
className="drop-zone"
activeClassName="drop-zone-active"
>
<div>
<span>Drop file here, or click to select file to upload.</span>
<br />
<br />
<span>GeoJSON files accepted.</span>
</div>
</Dropzone>
{!!this.state.uploadErr && <p>{this.state.uploadErr}</p>}
</div>
);
const done = (
<div className="btn-toolbar">
<button className="btn btn-sc" onClick={this.onSave}>
Save
</button>
<button className="btn btn-sc" onClick={this.onCancel}>
Cancel
</button>
</div>
);
if (this.state.creating) {
return (
<div className="add-predicate">
<h4>Add Predicate</h4>
<div className="form-group">
<label htmlFor="comparator">Predicate Type:</label>
<select
id="comparator"
className="form-control"
value={this.state.predicate_comparator}
onChange={this.onPredicateComparatorChange}
>
<option value="$geowithin">geowithin</option>
</select>
</div>
{this.state.drawing && done}
{this.state.uploading && (
<div>
{uploading}
{done}
</div>
)}
{!this.state.drawing &&
!this.state.uploading && (
<div>
<div className="btn-toolbar">
<button className="btn btn-sc" onClick={this.onDraw}>
Draw
</button>
<button className="btn btn-sc" onClick={this.onUpload}>
Upload
</button>
</div>
<div className="btn-toolbar">
<button className="btn btn-default" onClick={this.onCancel}>
Cancel
</button>
</div>
</div>
)}
</div>
);
}
return null;
}
render() {
const { processor } = this.props;
if (this.state.editingProcessor) {
return (
<div className="wrapper">
<section className="main">
<ProcessorEdit
processor={processor}
cancel={this.onCancelEditProcessor}
onSave={this.onEditProcessorSave}
errors={this.props.errors}
actions={this.props.actions}
capabilities={this.props.capabilities}
/>
</section>
</div>
);
}
return (
<div className="processor-details">
<div className="processor-props">
<ProcessorListLabel processor={processor} />
{this.renderEditing()}
{this.renderPredicates()}
{this.renderCreating()}
</div>
<div
className="processor-map"
ref={c => {
this.mapRef = c;
}}
/>
</div>
);
}
}
ProcessorView.propTypes = {
processor: PropTypes.object.isRequired,
menu: PropTypes.object.isRequired,
actions: PropTypes.object.isRequired,
errors: PropTypes.object.isRequired
};
export default ProcessorView;
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.