package org.linkality.xacmlanalysr; import java.io.File; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Applies transformation to the Prolog file that has been generated by the * OWL2Prolog converter. * <p> * The post-processor takes care of re-formatting terms in correct Prolog syntax * and adds required functions. * * @author Julian Schuette * */ public class PrologPostProcessor { private static PrologPostProcessor instance; private PrologPostProcessor() { super(); } /** * Gets a singleton instance. * * @return */ public static PrologPostProcessor getInstance() { if (instance == null) { instance = new PrologPostProcessor(); } return instance; } /** * Applies post-processing to a Prolog file. * * @param xacmlPrologFile The prolog file. */ public void execute(File xacmlPrologFile) { try { String content = Utils.readFileAsString(xacmlPrologFile); // content = content.replace("'hydra#ANY'", "_"); content = content.replace("http://www.hydramiddleware.eu/xacml#", "hydra#"); Pattern pattern = Pattern.compile("(literal\\(type\\([^,]+,[\\s]+)(.+[^\\)])(\\)\\))"); Matcher matcher = pattern.matcher(content); content = matcher.replaceAll("$2"); content = content + "\n" + "getExplanation(Effect, ResourceValue, ActionValue, SubjectValue, EnvironmentValue, Result):-setof([X, R, Effect, ResourceValue, ActionValue, SubjectValue, EnvironmentValue], " + "(('hydra#hasEffect'(X,Effect) ; ('hydra#hasRule'(X,R),'hydra#hasEffect'(R,Effect))), 'hydra#hasTarget'(X, T), " + "('hydra#hasResource'(T, 'hydra#ANY')->(ResourceValue='hydra#ANY') ;('hydra#hasResource'(T, Resource), 'hydra#hasMatch'(Resource, ResourceMatch), 'hydra#hasAttributeValue'(ResourceMatch, ResourceValue))), " + "('hydra#hasAction'(T, 'hydra#ANY')->(ActionValue='hydra#ANY') ;('hydra#hasAction'(T, Action), 'hydra#hasMatch'(Action, ActionMatch), 'hydra#hasAttributeValue'(ActionMatch, ActionValue))), " + "('hydra#hasSubject'(T, 'hydra#ANY')->(SubjectValue='hydra#ANY') ;('hydra#hasSubject'(T, Subject), 'hydra#hasMatch'(Subject, SubjectMatch), 'hydra#hasAttributeValue'(SubjectMatch, SubjectValue))), " + "('hydra#hasEnvironment'(T, 'hydra#ANY')->(EnvironmentValue='hydra#ANY');('hydra#hasEnvironment'(T, Environment), 'hydra#hasMatch'(Environment, EnvironmentMatch), 'hydra#hasAttributeValue'(EnvironmentMatch, EnvironmentValue)))), Result). "; content = content + "\n" + "getRequiredAttributes(Effect, ResourceValue, ActionValue, SubjectValue, EnvironmentValue, Result):-setof([ResourceValue, SubjectValue, EnvironmentValue], " + "(('hydra#hasEffect'(X,Effect) ; ('hydra#hasRule'(X,R),'hydra#hasEffect'(R,Effect))), 'hydra#hasTarget'(X, T), " + "('hydra#hasResource'(T, 'hydra#ANY')->(ResourceValue='hydra#ANY') ;('hydra#hasResource'(T, Resource), 'hydra#hasMatch'(Resource, ResourceMatch), 'hydra#hasAttributeValue'(ResourceMatch, ResourceValue))), " + "('hydra#hasAction'(T, 'hydra#ANY')->(ActionValue='hydra#ANY') ;('hydra#hasAction'(T, Action), 'hydra#hasMatch'(Action, ActionMatch), 'hydra#hasAttributeValue'(ActionMatch, ActionValue))), " + "('hydra#hasSubject'(T, 'hydra#ANY')->(SubjectValue='hydra#ANY') ;('hydra#hasSubject'(T, Subject), 'hydra#hasMatch'(Subject, SubjectMatch), 'hydra#hasAttributeValue'(SubjectMatch, SubjectValue))), " + "('hydra#hasEnvironment'(T, 'hydra#ANY')->(EnvironmentValue='hydra#ANY');('hydra#hasEnvironment'(T, Environment), 'hydra#hasMatch'(Environment, EnvironmentMatch), 'hydra#hasAttributeValue'(EnvironmentMatch, EnvironmentValue)))), Result). "; Utils.writeStringToFile(content, xacmlPrologFile); } catch (Exception e) { e.printStackTrace(); } } }