What is a xacml condition and how can I convert it to an odrl constraint in java language; - xacml

I am trying to write an algorithm in java which will convert a xacml policy into an odrl policy. So I try piece by piece to match the elements that make up the policies. Now I'm trying to find what the xacml condition consists of so I can convert it to the corresponding odrl constraint?
I have written this but I can't continue the correspondence of condition and constraint elements and how to convert them.
private static Constraint Transform_XACML_condition_To_ODRL_constraint(Condition xacmlCondition , String effect1)
{
// check if condition is a) simple or b) logical
// if a) --> transform if(condition.getLogicalOperator == null)
// if b) -->
// get sub-conditions
// repeat the above
Condition[] conditions = Condition.getSubConditions();
for (Condition condition : conditions)
{
if (xacmlCondition.getLogicalOperator() == null)
{
Operetor ODRLOperetor=Condition.getOperator();
}
}
return null;
}

Related

password validation with Kotlin [duplicate]

This question already has answers here:
Regular Expression In Android for Password Field
(13 answers)
Closed 1 year ago.
I'm new in Kotlin and trying to find the most elegant solution of password validation with conditions:
Password must be at least 8 characters.
It must have at least 1 lowercase and at least 1 uppercase letter.
It must have one special character like ! or + or - or similar
It must have at least 1 digit
"Elegant" is subjective!
Here's a functional approach to it:
// you can define each rule as a separate checking function,
// adding more doesn't change the complexity
fun String.isLongEnough() = length >= 8
fun String.hasEnoughDigits() = count(Char::isDigit) > 0
fun String.isMixedCase() = any(Char::isLowerCase) && any(Char::isUpperCase)
fun String.hasSpecialChar() = any { it in "!,+^" }
// you can decide which requirements need to be included (or make separate lists
// of different priority requirements, and check that enough of each have been met)
val requirements = listOf(String::isLongEnough, String::hasEnoughDigits)
val String.meetsRequirements get() = requirements.all { check -> check(this) }
fun main() {
val password = "hiThere2!+"
println(password.meetsRequirements)
}
I think the benefit is it's easy to add new rules and they can be very simple and readable, and you can handle the validation logic in a separate step (e.g. if you're implementing a "password strength" metric, where meeting some requirements counts more than others).
I've used some fancier language features in there, but it's to keep it concise really. The String.whatever() extension functions just mean you don't need to reference the string parameter in the function (it's this), and the function references (String::hasEnoughDigits) let you do that requirements.all call instead of going if (isLongEnough(password) && hasEnoughDigits(password) && ...) and so on. You could do it that way if you wanted!
Lots of options and ways to approach it. Regexes can definitely be elegant, but they can also be hard to work with
You can do like this ...
internal fun isValidPassword(password: String): Boolean {
if (password.length < 8) return false
if (password.filter { it.isDigit() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isUpperCase() }.firstOrNull() == null) return false
if (password.filter { it.isLetter() }.filter { it.isLowerCase() }.firstOrNull() == null) return false
if (password.filter { !it.isLetterOrDigit() }.firstOrNull() == null) return false
return true
}

Specman/e constraint (for each in) iteration

Can I iterate through only a part of a list in e, in a constraint.
For example, this code will go through the whole layer_l list:
<'
struct layer_s {
a : int;
keep soft a == 3;
};
struct layer_gen_s {
n_layers : int;
keep soft n_layers == 8;
layer_l : list of layer_s;
keep layer_l.size() == read_only(n_layers);
};
extend sys {
layer_gen : layer_gen_s;
run() is also {
messagef(LOW, "n_layers = %0d", layer_gen.n_layers);
for each in layer_gen.layer_l{
messagef(LOW, "layer[%2d]: a = %0d", index, it.a);
};
};
};
-- this will go through all layer_l
extend layer_gen_s {
keep for each (layer) using index (i) in layer_l {
layer.a == 7;
};
};
But, I would like to only iterate the for each in through, for example, 2 items. I tried the code below, but it doesn't work:
-- this produces an error
extend layer_gen_s {
keep for each (layer) using index (i) in [layer_l.all(index < 2)] {
layer.a == 7;
};
};
Also I don't want to use implication, so this is not what I want:
-- not what I want, I want to specify directly in iterated list
extend layer_gen_s {
keep for each (layer) using index (i) in layer_l {
(i < 2) => {
layer.a == 7;
};
};
};
Using the list slicing operator doesn't work either, because the path in a for..each constraint is limited to a simple path (e.g. a list variable). The following doesn't work either:
keep for each (layer) using index (i) in layer_l[0..2] {
//...
};
This is a Specman limitation.
To force looping over a sub-list your only bet is to create that sub-list as a separate variable:
layer_subl: list of layer_s;
keep layer_subl.size() == 3;
keep for each (layer) using index (i) in layer_subl {
layer == layer_l[i];
};
Now you can loop on only the first 3 elements within your for..each constraint:
keep for each (layer) in layer_subl {
layer.a == 7;
};
This avoids using implication inside the constraint. Whether this is worth is for you to decide. Also note that the lists will contain the same objects (this is good). No extra struct objects get created.
Creation of the sub-list like this is boilerplate code that could be handled by the tool itself. This would make the code much more concise and readable. You could contact your vendor and request this feature.

How to list all keys of a generic yaml by yaml-cpp

If a yaml document contains a mix of sequences and maps and scalars, and those collection types are themselves multi-level deep, is there a built-in function or an easy way to list all the keys, but not the final value at the leaf? Assuming the keys are strings.
You'll have to recurse on the nodes in your document, checking the type of each:
switch (node.Type()) {
case Null: // ...
case Scalar: // ...
case Sequence:
for (auto it = node.begin(); it != node.end(); ++it) {
auto element = *it;
// recurse on "element"
}
break;
case Map:
for (auto it = node.begin(); it != node.end(); ++it) {
auto key = it->first;
auto value = it->second;
// recurse on "key" and "value"
// if you're sure that "key" is a string, just grab it here
}
break;
case Undefined: // ...
}

What could be a reason for `_localctx` being null in an antlr4 semantic predicate?

I'm using list labels to gather tokens and semantic predicates to validate sequences in my parser grammar. E.g.
line
:
(text+=WORD | text+=NUMBER)+ ((BLANK | SKIP)+ (text+=WORD | text+=NUMBER)+)+
{Parser.validateContext(_localctx)}?
(BLANK | SKIP)*
;
where
WORD: [\u0021-\u002F\u003A-\u007E]+; // printable ASCII characters (excluding SP and numbers)
NUMBER: [\u0030-\u0039]+; // printable ASCII number characters
BLANK: '\u0020';
SKIP: '\u0020\u0020' | '\t'; // two SPs or a HT symbol
The part of Parser.validateContext used to validate the line rule would be implemented like this
private static final boolean validateContext(ParserRuleContext context) {
//.. other contexts
if(context instanceof LineContext)
return "<reference-sequence>".equals(Parser.joinTokens(((LineContext) context).text, " "));
return false;}
where Parser.joinTokens is defined as
private static String joinTokens(java.util.List<org.antlr.v4.runtime.Token> tokens, String delimiter) {
StringBuilder builder = new StringBuilder();
int i = 0, n;
if((n = tokens.size()) == 0) return "";
builder.append(tokens.get(0).getText());
while(++i < n) builder.append(delimiter + tokens.get(i).getText());
return builder.toString();}
Both are put in a #parser::members clause a the beginning of the grammar file.
My problem is this: sometimes the _localctx reference is null and I receive "no viable alternative" errors. These are probably caused because the failing predicate guards the respective rule and no alternative is given.
Is there a reason–potentially an error on my part–why _localctx would be null?
UPDATE: The answer to this question seems to suggest that semantic predicates are also called during prediction. Maybe during prediction no context is created and _localctx is set to null.
The semantics of _localctx in a predicate are not defined. Allowable behavior includes, but is not limited to the following (and may change during any release):
Failing to compile (no identifier with that name)
Using the wrong context object
Not having a context object (null)
To reference the context of the current rule from within a predicate, you need to use $ctx instead.
Note that the same applies for rule parameters, locals, and/or return values which are used in a predicate. For example, the parameter a cannot be referenced as a, but must instead be $a.

How to filter records with a null value in PIG?

I am trying to drop records that contain at least one null in any of the fields. For example, if the data has 3 fields, then:
filtered = FILTER data by ($0 is not null) AND ($1 is not null) AND ($2 is not null)
Is there any cleaner way to do this, without having to write out 3 boolean expressions?
If all of the fields are of numeric types, you could simply do something like
filtered = FILTER data BY $0*$1*$2 is not null;
In Pig, if any terms in an arithmetic expression are null, the result is null.
You could also write a UDF to take an arbitrary number of arguments and return null (or 0, or false, whatever you find most convenient) if any of the arguments are null.
filtered = FILTER data BY NUMBER_OF_NULLS($0, $1, $2) == 0;
where NUMBER_OF_NULLS is defined elsewhere, e.g.
public class NUMBER_OF_NULLS extends EvalFunc {
public Integer exec(Tuple input) {
if (input == null) { return 0; }
int c = 0;
for (int i = 0; i < input.size(); i++) {
if (input.get(i) == null) c++;
}
return c;
}
}
Note: I have not tested the above UDF, and I don't claim it adheres to any best practices for writing clear, robust UDFs. You should add exception-handling code, for example.
I was thinking there is a better way of doing this without using the UDF, i.e, using SPLIT in Pig.
emp = load '/Batch1/pig/emp' using PigStorage(',') as (id:chararray, name:chararray, salary:int, dept:chararray);
SPLIT emp INTO emptyDept IF depart == '', nonemptyDept IF depart != '';
DUMP nonemptyDept;
The resulting relation nonemptyDept would display all the non-empty Department values of the emp relation.