Skip to content

Commit

Permalink
Version 0.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
lcbarcellos committed Nov 23, 2020
2 parents 68daeff + 77ac9b0 commit 6119d77
Show file tree
Hide file tree
Showing 25 changed files with 599 additions and 186 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>br.ufes.inf.nemo</groupId>
<artifactId>ufo-protege-plugin</artifactId>
<version>0.0.7</version>
<version>0.0.8</version>
<packaging>bundle</packaging>

<name>UFO Protégé Plugin</name>
Expand Down
4 changes: 2 additions & 2 deletions scripts/ufo-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
+ gufo:Relator
+ gufo:ExtrinsicMode

- gufo:AbstractIndividual
+ gufo:AbstractIndividual
+ gufo:QualityValue
+ time:Instant

+ gufo:Event
+ gufo:Participation

- gufo:Situation
+ gufo:Situation
+ gufo:QualityValueAttributionSituation
+ gufo:TemporaryConstitutionSituation
+ gufo:TemporaryInstantiationSituation
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/br/ufes/inf/nemo/ufo/protege/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.io.LineNumberReader;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -112,4 +115,80 @@ public static String readAsString(Class<?> clazz, String resourceName) {
return scanner.useDelimiter("\\A").next();
}
}

/**
* Create a new string scanner for a regular expression.
*
* @param regexString The regular expression to be used by scanner
* @return The scanner
*/
public static StringScanner stringScanner(String regexString) {
return new StringScanner(regexString);
}

/**
* Utility class to scan a string, spliting it according to a regular
* expression.
* <br>
*/
public static class StringScanner {

private final Pattern pattern;

StringScanner(String regexString) {
pattern = Pattern.compile(regexString);
}

/**
* Scans given string and reporting matched and unmatched blocks.
* <br>
* A matcher is created based on regular expression of this scanner for
* the given string. The string is then scanned by occurrence of the
* regular expression, and the consumers are invoked to accept
* sequentially the substrings matching and not matching the regular
* expression.
*
* @param str The string to be scanned
* @param unmatchConsumer The consumer of unmatched blocks
* @param matcherConsumer The consumer of matched blocks
*/
public void scan(String str,
Consumer<String> unmatchConsumer,
Consumer<Matcher> matcherConsumer) {

Matcher matcher = pattern.matcher(str);
int startIndex = 0;
while (matcher.find()) {
int matcherStart = matcher.start();
if (startIndex < matcherStart) {
unmatchConsumer.accept(
str.substring(startIndex, matcherStart));
}
matcherConsumer.accept(matcher);
startIndex = matcher.end();
}
if (startIndex < str.length()) {
unmatchConsumer.accept(str.substring(startIndex));
}
}

/**
* Replace expression matches by values returned by given function.
* <br>
* The string is scanned by occurrence of the regular expression and
* the given function is applied for every match. The result string has
* all matches replaced by the result of applying the function.
*
* @param str The string to be scanned
* @param doReplace Function replacing the matches
* @return Text with occurrences of regular expression replaced
*/
public String replace(String str, Function<Matcher, String> doReplace) {
StringBuilder result = new StringBuilder();
scan(str, result::append, matcher -> {
result.append(doReplace.apply(matcher));
});
return result.toString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

import br.ufes.inf.nemo.ufo.protege.validation.helpers.ObjectGraph;
import br.ufes.inf.nemo.ufo.protege.validation.helpers.ObjectGraphNode;
import java.util.Set;
import java.util.stream.Collectors;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;

/**
Expand All @@ -18,4 +21,33 @@ public abstract class ClassRule extends Rule<OWLClass> {
protected ObjectGraphNode classNode() {
return get(ObjectGraph.class).getNode(getTarget());
}

/**
* Register a violation when the subject subclasses an instance of an
* specific type.
* <br>
* This method checks ancestor list of the subject and registers a violation
* when given any class is in that list instantiates given type. The
* ancestors which instantiate the type are put in a {@link Collection} and
* that collection is stored in the field map under the given name.
*
* @param forbiddenType IRI of the type which cannot be instantiated by any
* ancestor
* @param fieldName Name of the field to store the list of violating
* ancestors
*/
protected void forbidAncestors(IRI forbiddenType, String fieldName) {

Set<ObjectGraphNode> forbiddenAncestors = classNode()
.properAncestors()
.filter(node -> node.isInstanceOf(forbiddenType))
.collect(Collectors.toSet())
;

if (!forbiddenAncestors.isEmpty()) {
setField(fieldName, ObjectGraphNode.closestAncestors(
forbiddenAncestors));
newViolation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public String toString() {
.map(violation ->
Stream.concat(
Stream.of(violation.getRule().getClass().getSimpleName()),
violation.getArguments()[0]
violation.getSubject()
.getSignature().stream()
.map(entity -> entity.getIRI().getShortForm())
)
Expand Down

This file was deleted.

21 changes: 13 additions & 8 deletions src/main/java/br/ufes/inf/nemo/ufo/protege/validation/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

import br.ufes.inf.nemo.ufo.protege.GufoIris;
import java.lang.reflect.ParameterizedType;
import java.util.function.Supplier;
import org.semanticweb.owlapi.model.HasIRI;
import java.util.Map;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLObject;

Expand Down Expand Up @@ -125,6 +124,16 @@ void validate(OWLObject subject) {
*/
public abstract void validate();

/**
* Set field value for validation reporting.
*
* @param name Name of the field used in message
* @param value Object referenced by the name
*/
protected void setField(String name, Object value) {
this.validation.setField(name, value);
}

/**
* @return Label of this rule
*/
Expand All @@ -150,11 +159,7 @@ public <T> T get(Class<T> helperClass) {
return validation.get(helperClass);
}

protected Violation newViolation(OWLObject... arguments) {
return validation.newViolation(arguments);
}

protected ResultBuilder when(boolean b) {
return new ResultBuilder(b, this);
protected Violation newViolation() {
return validation.registerViolation();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static Result on(ModelManager modelManager,
private OWLObject currentTarget;
private Rule currentRule;
private Violation currentViolation;
private HashMap<String, Object> currentArguments;

public Validation(OWLModelManager modelManager) {
this.modelManager = modelManager;
Expand Down Expand Up @@ -151,18 +152,18 @@ private Result validate(Class<? extends Rule> ruleClass) {
return new Result(targetOntology, allOntologies, violations);
}

public Violation newViolation(OWLObject... arguments) {
public Violation registerViolation() {
if (currentViolation != null) {
throw new RuntimeException(String.format(
"Unexpected error. A violation has already been created by this rule for this target. Rule class: %s; Target: %s",
currentTarget.getClass().getName(),
currentRule.getClass().getName(),
(currentTarget instanceof HasIRI) ?
((HasIRI)currentTarget).getIRI().toString() :
currentTarget.toString()
));

}
currentViolation = new Violation(currentRule, arguments);
currentViolation = new Violation(currentRule, currentArguments);
violations.add(currentViolation);
return currentViolation;
}
Expand All @@ -174,6 +175,8 @@ private void validateRulesOn(OWLObject subject) {
.forEach(rule -> {
currentRule = rule;
currentViolation = null;
currentArguments = new HashMap<String, Object>();
currentArguments.put("", currentTarget);
rule.validate(subject);
})
;
Expand All @@ -183,6 +186,21 @@ public OWLObject getCurrentTarget() {
return currentTarget;
}

void setField(String name, Object value) {
Object old = currentArguments.put(name, value);
if (old != null) {
throw new RuntimeException(String.format(
"Unexpected error. A value has already been registered by this "
+ "rule for field '%s'. Rule class: '%s'; Target: '%s'",
name,
currentRule.getClass().getName(),
(currentTarget instanceof HasIRI) ?
((HasIRI)currentTarget).getIRI().toString() :
currentTarget.toString()
));
}
}

public interface Initializable {
public void initialize(Validation validation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
package br.ufes.inf.nemo.ufo.protege.validation;

import java.util.Map;
import org.semanticweb.owlapi.model.OWLObject;

/**
Expand All @@ -14,24 +15,24 @@
*/
public class Violation<T extends OWLObject> {

private final OWLObject[] arguments;
private final Map<String, Object> arguments;
private final Rule<T> rule;

protected Violation(final Rule<T> rule, OWLObject... arguments) {
protected Violation(final Rule<T> rule, Map<String, Object> arguments) {
this.rule = rule;
assert arguments[0] == rule.getTarget();
assert arguments.get("") == rule.getTarget();
this.arguments = arguments;
}

public Rule<T> getRule() {
return (Rule<T>) rule;
}

public OWLObject[] getArguments() {
public Map<String, Object> getArguments() {
return arguments;
}

public T getSubject() {
return (T) arguments[0];
return (T) arguments.get("");
}
}
Loading

0 comments on commit 6119d77

Please sign in to comment.